]> source.dussan.org Git - archiva.git/blob
d19c24c9db83515e21092c6f5a4d5c0976fab9d3
[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.RepositorySession;
26 import org.apache.archiva.metadata.repository.RepositorySessionFactory;
27 import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
28 import org.apache.archiva.metadata.repository.stats.RepositoryStatistics;
29 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
30 import org.apache.archiva.reports.RepositoryProblemFacet;
31 import org.apache.commons.io.IOUtils;
32 import org.apache.maven.archiva.web.action.AbstractActionTestCase;
33 import org.easymock.MockControl;
34
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.Date;
40 import java.util.List;
41
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44
45 /**
46  * Test the GenerationReportAction. Note that we are testing for <i>current</i> behaviour, however there are several
47  * instances below where other behaviour may actually be more appropriate (eg the error handling, download stats should
48  * never forward to HTML page, etc). This is also missing tests for various combinations of paging at this point.
49  */
50 public class GenerateReportActionTest
51     extends AbstractActionTestCase
52 {
53     private GenerateReportAction action;
54
55     private static final String SNAPSHOTS = "snapshots";
56
57     private static final String INTERNAL = "internal";
58
59     private static final String GROUP_ID = "groupId";
60
61     private RepositoryStatisticsManager repositoryStatisticsManager;
62
63     private MockControl repositoryStatisticsManagerControl;
64
65     private MockControl metadataRepositoryControl;
66
67     private MetadataRepository metadataRepository;
68
69     private static final String PROBLEM = "problem";
70
71     @Override
72     protected void setUp()
73         throws Exception
74     {
75         super.setUp();
76
77         try
78         {
79             action = (GenerateReportAction) lookup( Action.class, "generateReport" );
80         }
81         catch ( Exception e )
82         {
83             // clean up cache - TODO: move handling to plexus-spring
84             applicationContext.close();
85             throw e;
86         }
87
88         repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
89         repositoryStatisticsManager = (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
90         action.setRepositoryStatisticsManager( repositoryStatisticsManager );
91
92         metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
93         metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
94
95         RepositorySession repositorySession = mock( RepositorySession.class );
96         when( repositorySession.getRepository() ).thenReturn( metadataRepository );
97
98         TestRepositorySessionFactory factory = (TestRepositorySessionFactory) lookup( RepositorySessionFactory.class );
99         factory.setRepositorySession( repositorySession );
100     }
101
102     private void prepareAction( List<String> selectedRepositories, List<String> availableRepositories )
103     {
104         action.setSelectedRepositories( selectedRepositories );
105         action.prepare();
106
107         assertEquals( Arrays.asList( GenerateReportAction.ALL_REPOSITORIES, INTERNAL, SNAPSHOTS ),
108                       action.getRepositoryIds() );
109         assertEquals( availableRepositories, action.getAvailableRepositories() );
110     }
111
112     public void testGenerateStatisticsInvalidRowCount()
113     {
114         repositoryStatisticsManagerControl.replay();
115         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
116
117         action.setRowCount( 0 );
118         String result = action.generateStatistics();
119         assertEquals( Action.INPUT, result );
120         assertTrue( action.hasFieldErrors() );
121         repositoryStatisticsManagerControl.verify();
122     }
123
124     public void testGenerateStatisticsInvalidEndDate()
125     {
126         repositoryStatisticsManagerControl.replay();
127         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
128
129         action.setStartDate( "2009/12/12" );
130         action.setEndDate( "2008/11/11" );
131         String result = action.generateStatistics();
132         assertEquals( Action.INPUT, result );
133         assertTrue( action.hasFieldErrors() );
134         repositoryStatisticsManagerControl.verify();
135     }
136
137     public void testGenerateStatisticsMalformedEndDate()
138     {
139         repositoryStatisticsManagerControl.replay();
140         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
141
142         action.setEndDate( "This is not a date" );
143         String result = action.generateStatistics();
144
145         // TODO: should be an input error
146         assertEquals( Action.ERROR, result );
147         assertTrue( action.hasActionErrors() );
148         repositoryStatisticsManagerControl.verify();
149     }
150
151     public void testGenerateStatisticsInvalidEndDateMultiRepo()
152     {
153         repositoryStatisticsManagerControl.replay();
154         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
155
156         action.setStartDate( "2009/12/12" );
157         action.setEndDate( "2008/11/11" );
158         String result = action.generateStatistics();
159         assertEquals( Action.INPUT, result );
160         assertTrue( action.hasFieldErrors() );
161         repositoryStatisticsManagerControl.verify();
162     }
163
164     public void testGenerateStatisticsMalformedEndDateMultiRepo()
165     {
166         repositoryStatisticsManagerControl.replay();
167         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
168
169         action.setEndDate( "This is not a date" );
170         String result = action.generateStatistics();
171
172         // TODO: should be an input error
173         assertEquals( Action.ERROR, result );
174         assertTrue( action.hasActionErrors() );
175         repositoryStatisticsManagerControl.verify();
176     }
177
178     public void testGenerateStatisticsNoRepos()
179     {
180         repositoryStatisticsManagerControl.replay();
181         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
182
183         String result = action.generateStatistics();
184         assertEquals( Action.INPUT, result );
185         assertTrue( action.hasFieldErrors() );
186         repositoryStatisticsManagerControl.verify();
187     }
188
189     public void testGenerateStatisticsSingleRepo()
190         throws Exception
191     {
192         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
193             metadataRepository, INTERNAL, null, null ), Collections.singletonList( createDefaultStats() ) );
194
195         repositoryStatisticsManagerControl.replay();
196         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
197
198         String result = action.generateStatistics();
199         assertSuccessResult( result );
200         repositoryStatisticsManagerControl.verify();
201     }
202
203     public void testGenerateStatisticsSingleRepoNoStats()
204         throws Exception
205
206     {
207         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
208             metadataRepository, INTERNAL, null, null ), Collections.<Object>emptyList() );
209         repositoryStatisticsManagerControl.replay();
210         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
211
212         String result = action.generateStatistics();
213         assertEquals( Action.ERROR, result );
214         assertTrue( action.hasActionErrors() );
215
216         repositoryStatisticsManagerControl.verify();
217     }
218
219     public void testGenerateStatisticsOvershotPages()
220         throws Exception
221
222     {
223         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
224             metadataRepository, INTERNAL, null, null ), Collections.singletonList( createDefaultStats() ) );
225         repositoryStatisticsManagerControl.replay();
226         action.setPage( 2 );
227         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
228
229         String result = action.generateStatistics();
230         assertEquals( Action.ERROR, result );
231         assertTrue( action.hasActionErrors() );
232         repositoryStatisticsManagerControl.verify();
233     }
234
235     public void testGenerateStatisticsMultipleRepoNoResults()
236         throws Exception
237
238     {
239         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
240             metadataRepository, SNAPSHOTS, null, null ), Collections.<Object>emptyList() );
241         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
242             metadataRepository, INTERNAL, null, null ), Collections.<Object>emptyList() );
243         repositoryStatisticsManagerControl.replay();
244         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
245
246         String result = action.generateStatistics();
247         assertEquals( GenerateReportAction.BLANK, result );
248         assertFalse( action.hasActionErrors() );
249         assertFalse( action.hasActionMessages() );
250         assertFalse( action.hasFieldErrors() );
251
252         repositoryStatisticsManagerControl.verify();
253     }
254
255     public void testGenerateStatisticsMultipleRepo()
256         throws Exception
257
258     {
259         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
260             metadataRepository, SNAPSHOTS, null, null ), Collections.singletonList( createDefaultStats() ) );
261         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
262             metadataRepository, INTERNAL, null, null ), Collections.singletonList( createDefaultStats() ) );
263
264         repositoryStatisticsManagerControl.replay();
265         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
266
267         String result = action.generateStatistics();
268         assertSuccessResult( result );
269         repositoryStatisticsManagerControl.verify();
270     }
271
272     public void testDownloadStatisticsSingleRepo()
273         throws Exception
274     {
275         Date date = new Date();
276         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
277             metadataRepository, SNAPSHOTS, null, null ), Collections.singletonList( createStats( date ) ) );
278         repositoryStatisticsManagerControl.replay();
279
280         prepareAction( Arrays.asList( SNAPSHOTS ), Arrays.asList( INTERNAL ) );
281
282         String result = action.downloadStatisticsReport();
283         assertEquals( GenerateReportAction.SEND_FILE, result );
284         assertFalse( action.hasActionErrors() );
285         assertFalse( action.hasFieldErrors() );
286
287         assertEquals(
288             "Date of Scan,Total File Count,Total Size,Artifact Count,Group Count,Project Count,Plugins,Archetypes,Jars,Wars\n" +
289                 date + ",0,0,0,0,0,0,0,0,0\n", IOUtils.toString( action.getInputStream() ) );
290         repositoryStatisticsManagerControl.verify();
291     }
292
293     public void testDownloadStatisticsMultipleRepos()
294         throws Exception
295     {
296         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
297             metadataRepository, SNAPSHOTS, null, null ), Collections.singletonList( createDefaultStats() ) );
298         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
299             metadataRepository, INTERNAL, null, null ), 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(
371             metadataRepository, INTERNAL, null, null ), Collections.<Object>emptyList() );
372         repositoryStatisticsManagerControl.replay();
373         prepareAction( Collections.singletonList( INTERNAL ), Collections.singletonList( SNAPSHOTS ) );
374
375         String result = action.downloadStatisticsReport();
376         assertEquals( Action.ERROR, result );
377         assertTrue( action.hasActionErrors() );
378         repositoryStatisticsManagerControl.verify();
379     }
380
381     public void testDownloadStatisticsNoRepos()
382     {
383         repositoryStatisticsManagerControl.replay();
384         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
385
386         String result = action.downloadStatisticsReport();
387         assertEquals( Action.INPUT, result );
388         assertTrue( action.hasFieldErrors() );
389         repositoryStatisticsManagerControl.verify();
390     }
391
392     public void testDownloadStatisticsMultipleRepoNoResults()
393         throws Exception
394
395     {
396         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
397             metadataRepository, SNAPSHOTS, null, null ), Collections.<Object>emptyList() );
398         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
399             metadataRepository, INTERNAL, null, null ), Collections.<Object>emptyList() );
400         repositoryStatisticsManagerControl.replay();
401         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
402
403         String result = action.downloadStatisticsReport();
404         assertEquals( GenerateReportAction.BLANK, result );
405         assertFalse( action.hasActionErrors() );
406         assertFalse( action.hasActionMessages() );
407         assertFalse( action.hasFieldErrors() );
408         repositoryStatisticsManagerControl.verify();
409     }
410
411     public void testDownloadStatisticsMultipleRepoInStrutsFormat()
412         throws Exception
413     {
414         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
415             metadataRepository, SNAPSHOTS, null, null ), Collections.singletonList( createDefaultStats() ) );
416         repositoryStatisticsManagerControl.expectAndReturn( repositoryStatisticsManager.getStatisticsInRange(
417             metadataRepository, INTERNAL, null, null ), Collections.singletonList( createDefaultStats() ) );
418         repositoryStatisticsManagerControl.replay();
419         prepareAction( Arrays.asList( SNAPSHOTS, INTERNAL ), Collections.<String>emptyList() );
420
421         action.setSelectedRepositories( Collections.singletonList( "[" + SNAPSHOTS + "],[" + INTERNAL + "]" ) );
422         String result = action.downloadStatisticsReport();
423         assertEquals( GenerateReportAction.SEND_FILE, result );
424         assertFalse( action.hasActionErrors() );
425         assertFalse( action.hasFieldErrors() );
426
427         assertMultiRepoCsvResult();
428         repositoryStatisticsManagerControl.verify();
429     }
430
431     public void testHealthReportSingleRepo()
432         throws Exception
433     {
434         RepositoryProblemFacet problem1 = createProblem( GROUP_ID, "artifactId", INTERNAL );
435         RepositoryProblemFacet problem2 = createProblem( GROUP_ID, "artifactId-2", INTERNAL );
436
437         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( INTERNAL,
438                                                                                          RepositoryProblemFacet.FACET_ID ),
439                                                    Arrays.asList( problem1.getName(), problem2.getName() ) );
440         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( INTERNAL,
441                                                                                         RepositoryProblemFacet.FACET_ID,
442                                                                                         problem1.getName() ),
443                                                    problem1 );
444         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( INTERNAL,
445                                                                                         RepositoryProblemFacet.FACET_ID,
446                                                                                         problem2.getName() ),
447                                                    problem2 );
448         metadataRepositoryControl.replay();
449
450         action.setRepositoryId( INTERNAL );
451
452         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
453
454         String result = action.execute();
455         assertSuccessResult( result );
456
457         assertEquals( Collections.singleton( INTERNAL ), action.getRepositoriesMap().keySet() );
458         assertEquals( Arrays.asList( problem1, problem2 ), action.getRepositoriesMap().get( INTERNAL ) );
459
460         metadataRepositoryControl.verify();
461     }
462
463     public void testHealthReportInvalidRowCount()
464         throws Exception
465     {
466         metadataRepositoryControl.replay();
467
468         action.setRowCount( 0 );
469         action.setRepositoryId( INTERNAL );
470
471         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
472
473         String result = action.execute();
474         assertEquals( Action.INPUT, result );
475         assertFalse( action.hasActionErrors() );
476         assertTrue( action.hasFieldErrors() );
477
478         metadataRepositoryControl.verify();
479     }
480
481     public void testHealthReportAllRepos()
482         throws Exception
483     {
484         RepositoryProblemFacet problem1 = createProblem( GROUP_ID, "artifactId", INTERNAL );
485         RepositoryProblemFacet problem2 = createProblem( GROUP_ID, "artifactId-2", SNAPSHOTS );
486         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( INTERNAL,
487                                                                                          RepositoryProblemFacet.FACET_ID ),
488                                                    Arrays.asList( problem1.getName() ) );
489         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( SNAPSHOTS,
490                                                                                          RepositoryProblemFacet.FACET_ID ),
491                                                    Arrays.asList( problem2.getName() ) );
492         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( INTERNAL,
493                                                                                         RepositoryProblemFacet.FACET_ID,
494                                                                                         problem1.getName() ),
495                                                    problem1 );
496         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( SNAPSHOTS,
497                                                                                         RepositoryProblemFacet.FACET_ID,
498                                                                                         problem2.getName() ),
499                                                    problem2 );
500         metadataRepositoryControl.replay();
501
502         action.setRepositoryId( GenerateReportAction.ALL_REPOSITORIES );
503
504         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
505
506         String result = action.execute();
507         assertSuccessResult( result );
508
509         assertEquals( Arrays.asList( INTERNAL, SNAPSHOTS ), new ArrayList<String>(
510             action.getRepositoriesMap().keySet() ) );
511         assertEquals( Arrays.asList( problem1 ), action.getRepositoriesMap().get( INTERNAL ) );
512         assertEquals( Arrays.asList( problem2 ), action.getRepositoriesMap().get( SNAPSHOTS ) );
513
514         metadataRepositoryControl.verify();
515     }
516
517     public void testHealthReportSingleRepoByCorrectGroupId()
518         throws Exception
519     {
520         RepositoryProblemFacet problem1 = createProblem( GROUP_ID, "artifactId", INTERNAL );
521         RepositoryProblemFacet problem2 = createProblem( GROUP_ID, "artifactId-2", INTERNAL );
522         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( INTERNAL,
523                                                                                          RepositoryProblemFacet.FACET_ID ),
524                                                    Arrays.asList( problem1.getName(), problem2.getName() ) );
525         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( INTERNAL,
526                                                                                         RepositoryProblemFacet.FACET_ID,
527                                                                                         problem1.getName() ),
528                                                    problem1 );
529         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( INTERNAL,
530                                                                                         RepositoryProblemFacet.FACET_ID,
531                                                                                         problem2.getName() ),
532                                                    problem2 );
533         metadataRepositoryControl.replay();
534
535         action.setGroupId( GROUP_ID );
536         action.setRepositoryId( INTERNAL );
537
538         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
539
540         String result = action.execute();
541         assertSuccessResult( result );
542
543         assertEquals( Collections.singleton( INTERNAL ), action.getRepositoriesMap().keySet() );
544         assertEquals( Arrays.asList( problem1, problem2 ), action.getRepositoriesMap().get( INTERNAL ) );
545
546         metadataRepositoryControl.verify();
547     }
548
549     public void testHealthReportSingleRepoByCorrectGroupIdAllRepositories()
550         throws Exception
551     {
552         RepositoryProblemFacet problem1 = createProblem( GROUP_ID, "artifactId", INTERNAL );
553         RepositoryProblemFacet problem2 = createProblem( GROUP_ID, "artifactId-2", SNAPSHOTS );
554         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( INTERNAL,
555                                                                                          RepositoryProblemFacet.FACET_ID ),
556                                                    Arrays.asList( problem1.getName() ) );
557         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( SNAPSHOTS,
558                                                                                          RepositoryProblemFacet.FACET_ID ),
559                                                    Arrays.asList( problem2.getName() ) );
560         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( INTERNAL,
561                                                                                         RepositoryProblemFacet.FACET_ID,
562                                                                                         problem1.getName() ),
563                                                    problem1 );
564         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacet( SNAPSHOTS,
565                                                                                         RepositoryProblemFacet.FACET_ID,
566                                                                                         problem2.getName() ),
567                                                    problem2 );
568         metadataRepositoryControl.replay();
569
570         action.setGroupId( GROUP_ID );
571         action.setRepositoryId( GenerateReportAction.ALL_REPOSITORIES );
572
573         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
574
575         String result = action.execute();
576         assertSuccessResult( result );
577
578         assertEquals( Arrays.asList( INTERNAL, SNAPSHOTS ), new ArrayList<String>(
579             action.getRepositoriesMap().keySet() ) );
580         assertEquals( Arrays.asList( problem1 ), action.getRepositoriesMap().get( INTERNAL ) );
581         assertEquals( Arrays.asList( problem2 ), action.getRepositoriesMap().get( SNAPSHOTS ) );
582
583         metadataRepositoryControl.verify();
584     }
585
586     public void testHealthReportSingleRepoByIncorrectGroupId()
587         throws Exception
588     {
589         metadataRepositoryControl.expectAndReturn( metadataRepository.getMetadataFacets( INTERNAL,
590                                                                                          RepositoryProblemFacet.FACET_ID ),
591                                                    Collections.<MetadataFacet>emptyList() );
592         metadataRepositoryControl.replay();
593
594         action.setGroupId( "not.it" );
595         action.setRepositoryId( INTERNAL );
596
597         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
598
599         String result = action.execute();
600         assertEquals( GenerateReportAction.BLANK, result );
601         assertFalse( action.hasActionErrors() );
602         assertFalse( action.hasFieldErrors() );
603
604         metadataRepositoryControl.verify();
605     }
606
607     private void assertMultiRepoCsvResult()
608         throws IOException
609     {
610         assertEquals(
611             "Repository,Total File Count,Total Size,Artifact Count,Group Count,Project Count,Plugins,Archetypes,Jars,Wars\n" +
612                 "snapshots,0,0,0,0,0,0,0,0,0\n" + "internal,0,0,0,0,0,0,0,0,0\n", IOUtils.toString(
613             action.getInputStream() ) );
614     }
615
616     private RepositoryProblemFacet createProblem( String groupId, String artifactId, String repoId )
617     {
618         RepositoryProblemFacet problem = new RepositoryProblemFacet();
619         problem.setRepositoryId( repoId );
620         problem.setNamespace( groupId );
621         problem.setProject( artifactId );
622         problem.setProblem( PROBLEM );
623         return problem;
624     }
625
626     public void testHealthReportNoRepositoryId()
627         throws Exception
628     {
629         prepareAction( Collections.<String>emptyList(), Arrays.asList( SNAPSHOTS, INTERNAL ) );
630
631         String result = action.execute();
632         assertEquals( Action.INPUT, result );
633         assertTrue( action.hasFieldErrors() );
634     }
635
636     private void assertSuccessResult( String result )
637     {
638         assertEquals( Action.SUCCESS, result );
639         assertFalse( action.hasActionErrors() );
640         assertFalse( action.hasFieldErrors() );
641     }
642
643     private RepositoryStatistics createDefaultStats()
644     {
645         return createStats( new Date() );
646     }
647
648     private RepositoryStatistics createStats( Date date )
649     {
650         RepositoryStatistics stats = new RepositoryStatistics();
651         stats.setScanStartTime( date );
652         return stats;
653     }
654 }