]> source.dussan.org Git - archiva.git/blob
bf309a1706e04d93c0d40db87361ebab755ec4d2
[archiva.git] /
1 package org.apache.maven.archiva.web.action.reports;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21
22 import com.opensymphony.xwork2.Action;
23 import org.apache.archiva.metadata.model.MetadataFacet;
24 import org.apache.archiva.metadata.repository.MetadataRepository;
25 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
26 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
27 import org.apache.archiva.reports.RepositoryProblemFacet;
28 import org.apache.commons.io.IOUtils;
29 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
30 import org.easymock.MockControl;
31
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.Date;
37 import java.util.List;
38
39 /**
40  * Test the GenerationReportAction. Note that we are testing for <i>current</i> behaviour, however there are several
41  * instances below where other behaviour may actually be more appropriate (eg the error handling, download stats should
42  * never forward to HTML page, etc). This is also missing tests for various combinations of paging at this point.
43  */
44 public class GenerateReportActionTest
45     extends PlexusInSpringTestCase
46 {
47     private GenerateReportAction action;
48
49     private static final String SNAPSHOTS = "snapshots";
50
51     private static final String INTERNAL = "internal";
52
53     private static final String GROUP_ID = "groupId";
54
55     private RepositoryStatisticsManager repositoryStatisticsManager;
56
57     private MockControl repositoryStatisticsManagerControl;
58
59     private MockControl metadataRepositoryControl;
60
61     private MetadataRepository metadataRepository;
62
63     private static final String PROBLEM = "problem";
64
65     @Override
66     protected void setUp()
67         throws Exception
68     {
69         super.setUp();
70
71         action = (GenerateReportAction) lookup( Action.class, "generateReport" );
72
73         repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
74         repositoryStatisticsManager = (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
75         action.setRepositoryStatisticsManager( repositoryStatisticsManager );
76
77         metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
78         metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
79         action.setMetadataRepository( metadataRepository );
80     }
81
82     private void prepareAction( List<String> selectedRepositories, List<String> availableRepositories )
83     {
84         action.setSelectedRepositories( selectedRepositories );
85         action.prepare();
86
87         assertEquals( Arrays.asList( GenerateReportAction.ALL_REPOSITORIES, INTERNAL, SNAPSHOTS ),
88                       action.getRepositoryIds() );
89         assertEquals( availableRepositories, action.getAvailableRepositories() );
90     }
91
92     public void testGenerateStatisticsInvalidRowCount()
93     {
94         repositoryStatisticsManagerControl.replay();
95         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
96
97         action.setRowCount( 0 );
98         String result = action.generateStatistics();
99         assertEquals( Action.INPUT, result );
100         assertTrue( action.hasFieldErrors() );
101         repositoryStatisticsManagerControl.verify();
102     }
103
104     public void testGenerateStatisticsInvalidEndDate()
105     {
106         repositoryStatisticsManagerControl.replay();
107         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
108
109         action.setStartDate( "2009/12/12" );
110         action.setEndDate( "2008/11/11" );
111         String result = action.generateStatistics();
112         assertEquals( Action.INPUT, result );
113         assertTrue( action.hasFieldErrors() );
114         repositoryStatisticsManagerControl.verify();
115     }
116
117     public void testGenerateStatisticsMalformedEndDate()
118     {
119         repositoryStatisticsManagerControl.replay();
120         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
121
122         action.setEndDate( "This is not a date" );
123         String result = action.generateStatistics();
124
125         // TODO: should be an input error
126         assertEquals( Action.ERROR, result );
127         assertTrue( action.hasActionErrors() );
128         repositoryStatisticsManagerControl.verify();
129     }
130
131     public void testGenerateStatisticsInvalidEndDateMultiRepo()
132     {
133         repositoryStatisticsManagerControl.replay();
134         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
135
136         action.setStartDate( "2009/12/12" );
137         action.setEndDate( "2008/11/11" );
138         String result = action.generateStatistics();
139         assertEquals( Action.INPUT, result );
140         assertTrue( action.hasFieldErrors() );
141         repositoryStatisticsManagerControl.verify();
142     }
143
144     public void testGenerateStatisticsMalformedEndDateMultiRepo()
145     {
146         repositoryStatisticsManagerControl.replay();
147         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
148
149         action.setEndDate( "This is not a date" );
150         String result = action.generateStatistics();
151
152         // TODO: should be an input error
153         assertEquals( Action.ERROR, result );
154         assertTrue( action.hasActionErrors() );
155         repositoryStatisticsManagerControl.verify();
156     }
157
158     public void testGenerateStatisticsNoRepos()
159     {
160         repositoryStatisticsManagerControl.replay();
161         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
162
163         String result = action.generateStatistics();
164         assertEquals( Action.INPUT, result );
165         assertTrue( action.hasFieldErrors() );
166         repositoryStatisticsManagerControl.verify();
167     }
168
169     public void testGenerateStatisticsSingleRepo()
170         throws Exception
171     {
172         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( INTERNAL,
173                                                                                                               null,
174                                                                                                               null ),
175                                                             Collections.singletonList( createDefaultStats() ) );
176
177         repositoryStatisticsManagerControl.replay();
178         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
179
180         String result = action.generateStatistics();
181         assertSuccessResult( result );
182         repositoryStatisticsManagerControl.verify();
183     }
184
185     public void testGenerateStatisticsSingleRepoNoStats()
186         throws Exception
187
188     {
189         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( INTERNAL,
190                                                                                                               null,
191                                                                                                               null ),
192                                                             Collections.<Object>emptyList() );
193         repositoryStatisticsManagerControl.replay();
194         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
195
196         String result = action.generateStatistics();
197         assertEquals( Action.ERROR, result );
198         assertTrue( action.hasActionErrors() );
199
200         repositoryStatisticsManagerControl.verify();
201     }
202
203     public void testGenerateStatisticsOvershotPages()
204         throws Exception
205
206     {
207         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( INTERNAL,
208                                                                                                               null,
209                                                                                                               null ),
210                                                             Collections.singletonList( createDefaultStats() ) );
211         repositoryStatisticsManagerControl.replay();
212         action.setPage( 2 );
213         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
214
215         String result = action.generateStatistics();
216         assertEquals( Action.ERROR, result );
217         assertTrue( action.hasActionErrors() );
218         repositoryStatisticsManagerControl.verify();
219     }
220
221     public void testGenerateStatisticsMultipleRepoNoResults()
222         throws Exception
223
224     {
225         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( SNAPSHOTS,
226                                                                                                               null,
227                                                                                                               null ),
228                                                             Collections.<Object>emptyList() );
229         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( INTERNAL,
230                                                                                                               null,
231                                                                                                               null ),
232                                                             Collections.<Object>emptyList() );
233         repositoryStatisticsManagerControl.replay();
234         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
235
236         String result = action.generateStatistics();
237         assertEquals( GenerateReportAction.BLANK, result );
238         assertFalse( action.hasActionErrors() );
239         assertFalse( action.hasActionMessages() );
240         assertFalse( action.hasFieldErrors() );
241
242         repositoryStatisticsManagerControl.verify();
243     }
244
245     public void testGenerateStatisticsMultipleRepo()
246         throws Exception
247
248     {
249         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( SNAPSHOTS,
250                                                                                                               null,
251                                                                                                               null ),
252                                                             Collections.singletonList( createDefaultStats() ) );
253         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( INTERNAL,
254                                                                                                               null,
255                                                                                                               null ),
256                                                             Collections.singletonList( createDefaultStats() ) );
257
258         repositoryStatisticsManagerControl.replay();
259         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
260
261         String result = action.generateStatistics();
262         assertSuccessResult( result );
263         repositoryStatisticsManagerControl.verify();
264     }
265
266     public void testDownloadStatisticsSingleRepo()
267         throws Exception
268     {
269         Date date = new Date();
270         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( SNAPSHOTS,
271                                                                                                               null,
272                                                                                                               null ),
273                                                             Collections.singletonList( createStats( date ) ) );
274         repositoryStatisticsManagerControl.replay();
275
276         prepareAction( Arrays.asList( SNAPSHOTS ), Arrays.asList( INTERNAL ) );
277
278         String result = action.downloadStatisticsReport();
279         assertEquals( GenerateReportAction.SEND_FILE, result );
280         assertFalse( action.hasActionErrors() );
281         assertFalse( action.hasFieldErrors() );
282
283         assertEquals(
284             "Date of Scan,Total File Count,Total Size,Artifact Count,Group Count,Project Count,Plugins,Archetypes,Jars,Wars\n" +
285                 date + ",0,0,0,0,0,0,0,0,0\n", IOUtils.toString( action.getInputStream() ) );
286         repositoryStatisticsManagerControl.verify();
287     }
288
289     public void testDownloadStatisticsMultipleRepos()
290         throws Exception
291     {
292         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( SNAPSHOTS,
293                                                                                                               null,
294                                                                                                               null ),
295                                                             Collections.singletonList( createDefaultStats() ) );
296         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( INTERNAL,
297                                                                                                               null,
298                                                                                                               null ),
299                                                             Collections.singletonList( createDefaultStats() ) );
300         repositoryStatisticsManagerControl.replay();
301         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
302
303         String result = action.downloadStatisticsReport();
304         assertEquals( GenerateReportAction.SEND_FILE, result );
305         assertFalse( action.hasActionErrors() );
306         assertFalse( action.hasFieldErrors() );
307
308         assertMultiRepoCsvResult();
309         repositoryStatisticsManagerControl.verify();
310     }
311
312     public void testDownloadStatisticsMalformedEndDateMultiRepo()
313     {
314         repositoryStatisticsManagerControl.replay();
315         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
316
317         action.setEndDate( "This is not a date" );
318         String result = action.downloadStatisticsReport();
319
320         // TODO: should be an input error
321         assertEquals( Action.ERROR, result );
322         assertTrue( action.hasActionErrors() );
323         repositoryStatisticsManagerControl.verify();
324     }
325
326     public void testDownloadStatisticsMalformedEndDateSingleRepo()
327     {
328         repositoryStatisticsManagerControl.replay();
329         prepareAction( Arrays.asList( SNAPSHOTS ), Arrays.asList( INTERNAL ) );
330
331         action.setEndDate( "This is not a date" );
332         String result = action.downloadStatisticsReport();
333
334         // TODO: should be an input error
335         assertEquals( Action.ERROR, result );
336         assertTrue( action.hasActionErrors() );
337         repositoryStatisticsManagerControl.verify();
338     }
339
340     public void testDownloadStatisticsInvalidEndDateMultiRepo()
341     {
342         repositoryStatisticsManagerControl.replay();
343         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
344
345         action.setStartDate( "2009/12/12" );
346         action.setEndDate( "2008/11/11" );
347         String result = action.downloadStatisticsReport();
348         assertEquals( Action.INPUT, result );
349         assertTrue( action.hasFieldErrors() );
350         repositoryStatisticsManagerControl.verify();
351     }
352
353     public void testDownloadStatisticsInvalidEndDateSingleRepo()
354     {
355         repositoryStatisticsManagerControl.replay();
356         prepareAction( Arrays.asList( SNAPSHOTS ), Arrays.asList( INTERNAL ) );
357
358         action.setStartDate( "2009/12/12" );
359         action.setEndDate( "2008/11/11" );
360         String result = action.downloadStatisticsReport();
361         assertEquals( Action.INPUT, result );
362         assertTrue( action.hasFieldErrors() );
363         repositoryStatisticsManagerControl.verify();
364     }
365
366     public void testDownloadStatisticsSingleRepoNoStats()
367         throws Exception
368
369     {
370         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( INTERNAL,
371                                                                                                               null,
372                                                                                                               null ),
373                                                             Collections.<Object>emptyList() );
374         repositoryStatisticsManagerControl.replay();
375         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
376
377         String result = action.downloadStatisticsReport();
378         assertEquals( Action.ERROR, result );
379         assertTrue( action.hasActionErrors() );
380         repositoryStatisticsManagerControl.verify();
381     }
382
383     public void testDownloadStatisticsNoRepos()
384     {
385         repositoryStatisticsManagerControl.replay();
386         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
387
388         String result = action.downloadStatisticsReport();
389         assertEquals( Action.INPUT, result );
390         assertTrue( action.hasFieldErrors() );
391         repositoryStatisticsManagerControl.verify();
392     }
393
394     public void testDownloadStatisticsMultipleRepoNoResults()
395         throws Exception
396
397     {
398         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( SNAPSHOTS,
399                                                                                                               null,
400                                                                                                               null ),
401                                                             Collections.<Object>emptyList() );
402         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( INTERNAL,
403                                                                                                               null,
404                                                                                                               null ),
405                                                             Collections.<Object>emptyList() );
406         repositoryStatisticsManagerControl.replay();
407         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
408
409         String result = action.downloadStatisticsReport();
410         assertEquals( GenerateReportAction.BLANK, result );
411         assertFalse( action.hasActionErrors() );
412         assertFalse( action.hasActionMessages() );
413         assertFalse( action.hasFieldErrors() );
414         repositoryStatisticsManagerControl.verify();
415     }
416
417     public void testDownloadStatisticsMultipleRepoInStrutsFormat()
418         throws Exception
419     {
420         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( SNAPSHOTS,
421                                                                                                               null,
422                                                                                                               null ),
423                                                             Collections.singletonList( createDefaultStats() ) );
424         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange( INTERNAL,
425                                                                                                               null,
426                                                                                                               null ),
427                                                             Collections.singletonList( createDefaultStats() ) );
428         repositoryStatisticsManagerControl.replay();
429         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
430
431         action.setSelectedRepositories( Collections.singletonList( "[" + SNAPSHOTS + "],[" + INTERNAL + "]" ) );
432         String result = action.downloadStatisticsReport();
433         assertEquals( GenerateReportAction.SEND_FILE, result );
434         assertFalse( action.hasActionErrors() );
435         assertFalse( action.hasFieldErrors() );
436
437         assertMultiRepoCsvResult();
438         repositoryStatisticsManagerControl.verify();
439     }
440
441     public void testHealthReportSingleRepo()
442         throws Exception
443     {
444         RepositoryProblemFacet problem1 = createProblem( GROUP_ID, "artifactId", INTERNAL );
445         RepositoryProblemFacet problem2 = createProblem( GROUP_ID, "artifactId-2", INTERNAL );
446
447         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( INTERNAL,
448                                                                                          RepositoryProblemFacet.FACET_ID ),
449                                                    Arrays.asList( problem1.getName(), problem2.getName() ) );
450         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( INTERNAL,
451                                                                                         RepositoryProblemFacet.FACET_ID,
452                                                                                         problem1.getName() ),
453                                                    problem1 );
454         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( INTERNAL,
455                                                                                         RepositoryProblemFacet.FACET_ID,
456                                                                                         problem2.getName() ),
457                                                    problem2 );
458         metadataRepositoryControl.replay();
459
460         action.setRepositoryId( INTERNAL );
461
462         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
463
464         String result = action.execute();
465         assertSuccessResult( result );
466
467         assertEquals( Collections.singleton( INTERNAL ), action.getRepositoriesMap().keySet() );
468         assertEquals( Arrays.asList( problem1, problem2 ), action.getRepositoriesMap().get( INTERNAL ) );
469
470         metadataRepositoryControl.verify();
471     }
472
473     public void testHealthReportInvalidRowCount()
474         throws Exception
475     {
476         metadataRepositoryControl.replay();
477
478         action.setRowCount( 0 );
479         action.setRepositoryId( INTERNAL );
480
481         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
482
483         String result = action.execute();
484         assertEquals( Action.INPUT, result );
485         assertFalse( action.hasActionErrors() );
486         assertTrue( action.hasFieldErrors() );
487
488         metadataRepositoryControl.verify();
489     }
490
491     public void testHealthReportAllRepos()
492         throws Exception
493     {
494         RepositoryProblemFacet problem1 = createProblem( GROUP_ID, "artifactId", INTERNAL );
495         RepositoryProblemFacet problem2 = createProblem( GROUP_ID, "artifactId-2", SNAPSHOTS );
496         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( INTERNAL,
497                                                                                          RepositoryProblemFacet.FACET_ID ),
498                                                    Arrays.asList( problem1.getName() ) );
499         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( SNAPSHOTS,
500                                                                                          RepositoryProblemFacet.FACET_ID ),
501                                                    Arrays.asList( problem2.getName() ) );
502         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( INTERNAL,
503                                                                                         RepositoryProblemFacet.FACET_ID,
504                                                                                         problem1.getName() ),
505                                                    problem1 );
506         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( SNAPSHOTS,
507                                                                                         RepositoryProblemFacet.FACET_ID,
508                                                                                         problem2.getName() ),
509                                                    problem2 );
510         metadataRepositoryControl.replay();
511
512         action.setRepositoryId( GenerateReportAction.ALL_REPOSITORIES );
513
514         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
515
516         String result = action.execute();
517         assertSuccessResult( result );
518
519         assertEquals( Arrays.asList( INTERNAL, SNAPSHOTS ), new ArrayList<String>(
520             action.getRepositoriesMap().keySet() ) );
521         assertEquals( Arrays.asList( problem1 ), action.getRepositoriesMap().get( INTERNAL ) );
522         assertEquals( Arrays.asList( problem2 ), action.getRepositoriesMap().get( SNAPSHOTS ) );
523
524         metadataRepositoryControl.verify();
525     }
526
527     public void testHealthReportSingleRepoByCorrectGroupId()
528         throws Exception
529     {
530         RepositoryProblemFacet problem1 = createProblem( GROUP_ID, "artifactId", INTERNAL );
531         RepositoryProblemFacet problem2 = createProblem( GROUP_ID, "artifactId-2", INTERNAL );
532         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( INTERNAL,
533                                                                                          RepositoryProblemFacet.FACET_ID ),
534                                                    Arrays.asList( problem1.getName(), problem2.getName() ) );
535         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( INTERNAL,
536                                                                                         RepositoryProblemFacet.FACET_ID,
537                                                                                         problem1.getName() ),
538                                                    problem1 );
539         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( INTERNAL,
540                                                                                         RepositoryProblemFacet.FACET_ID,
541                                                                                         problem2.getName() ),
542                                                    problem2 );
543         metadataRepositoryControl.replay();
544
545         action.setGroupId( GROUP_ID );
546         action.setRepositoryId( INTERNAL );
547
548         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
549
550         String result = action.execute();
551         assertSuccessResult( result );
552
553         assertEquals( Collections.singleton( INTERNAL ), action.getRepositoriesMap().keySet() );
554         assertEquals( Arrays.asList( problem1, problem2 ), action.getRepositoriesMap().get( INTERNAL ) );
555
556         metadataRepositoryControl.verify();
557     }
558
559     public void testHealthReportSingleRepoByCorrectGroupIdAllRepositories()
560         throws Exception
561     {
562         RepositoryProblemFacet problem1 = createProblem( GROUP_ID, "artifactId", INTERNAL );
563         RepositoryProblemFacet problem2 = createProblem( GROUP_ID, "artifactId-2", SNAPSHOTS );
564         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( INTERNAL,
565                                                                                          RepositoryProblemFacet.FACET_ID ),
566                                                    Arrays.asList( problem1.getName() ) );
567         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( SNAPSHOTS,
568                                                                                          RepositoryProblemFacet.FACET_ID ),
569                                                    Arrays.asList( problem2.getName() ) );
570         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( INTERNAL,
571                                                                                         RepositoryProblemFacet.FACET_ID,
572                                                                                         problem1.getName() ),
573                                                    problem1 );
574         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( SNAPSHOTS,
575                                                                                         RepositoryProblemFacet.FACET_ID,
576                                                                                         problem2.getName() ),
577                                                    problem2 );
578         metadataRepositoryControl.replay();
579
580         action.setGroupId( GROUP_ID );
581         action.setRepositoryId( GenerateReportAction.ALL_REPOSITORIES );
582
583         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
584
585         String result = action.execute();
586         assertSuccessResult( result );
587
588         assertEquals( Arrays.asList( INTERNAL, SNAPSHOTS ), new ArrayList<String>(
589             action.getRepositoriesMap().keySet() ) );
590         assertEquals( Arrays.asList( problem1 ), action.getRepositoriesMap().get( INTERNAL ) );
591         assertEquals( Arrays.asList( problem2 ), action.getRepositoriesMap().get( SNAPSHOTS ) );
592
593         metadataRepositoryControl.verify();
594     }
595
596     public void testHealthReportSingleRepoByIncorrectGroupId()
597         throws Exception
598     {
599         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( INTERNAL,
600                                                                                          RepositoryProblemFacet.FACET_ID ),
601                                                    Collections.<MetadataFacet>emptyList() );
602         metadataRepositoryControl.replay();
603
604         action.setGroupId( "not.it" );
605         action.setRepositoryId( INTERNAL );
606
607         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
608
609         String result = action.execute();
610         assertEquals( GenerateReportAction.BLANK, result );
611         assertFalse( action.hasActionErrors() );
612         assertFalse( action.hasFieldErrors() );
613
614         metadataRepositoryControl.verify();
615     }
616
617     private void assertMultiRepoCsvResult()
618         throws IOException
619     {
620         assertEquals(
621             "Repository,Total File Count,Total Size,Artifact Count,Group Count,Project Count,Plugins,Archetypes,Jars,Wars\n" +
622                 "snapshots,0,0,0,0,0,0,0,0,0\n" + "internal,0,0,0,0,0,0,0,0,0\n", IOUtils.toString(
623             action.getInputStream() ) );
624     }
625
626     private RepositoryProblemFacet createProblem( String groupId, String artifactId, String repoId )
627     {
628         RepositoryProblemFacet problem = new RepositoryProblemFacet();
629         problem.setRepositoryId( repoId );
630         problem.setNamespace( groupId );
631         problem.setProject( artifactId );
632         problem.setProblem( PROBLEM );
633         return problem;
634     }
635
636     public void testHealthReportNoRepositoryId()
637         throws Exception
638     {
639         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
640
641         String result = action.execute();
642         assertEquals( Action.INPUT, result );
643         assertTrue( action.hasFieldErrors() );
644     }
645
646     private void assertSuccessResult( String result )
647     {
648         assertEquals( Action.SUCCESS, result );
649         assertFalse( action.hasActionErrors() );
650         assertFalse( action.hasFieldErrors() );
651     }
652
653     private RepositoryStatistics createDefaultStats()
654     {
655         return createStats( new Date() );
656     }
657
658     private RepositoryStatistics createStats( Date date )
659     {
660         RepositoryStatistics stats = new RepositoryStatistics();
661         stats.setScanStartTime( date );
662         return stats;
663     }
664 }