]> source.dussan.org Git - archiva.git/blob
6dd467648a0c4e28984d6a91df9a92c208a7d18e
[archiva.git] /
1 package org.apache.archiva.web.xmlrpc.services;
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 java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.apache.archiva.audit.AuditEvent;
32 import org.apache.archiva.audit.AuditListener;
33 import org.apache.archiva.metadata.model.ArtifactMetadata;
34 import org.apache.archiva.metadata.repository.MetadataRepository;
35 import org.apache.archiva.metadata.repository.filter.Filter;
36 import org.apache.archiva.metadata.repository.filter.IncludesFilter;
37 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
38 import org.apache.archiva.repository.events.RepositoryListener;
39 import org.apache.archiva.repository.scanner.RepositoryContentConsumers;
40 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
41 import org.apache.archiva.scheduler.repository.RepositoryTask;
42 import org.apache.archiva.stagerepository.merge.RepositoryMerger;
43 import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository;
44 import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository;
45 import org.apache.commons.io.FileUtils;
46 import org.apache.commons.io.filefilter.FileFilterUtils;
47 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
48 import org.apache.maven.archiva.configuration.Configuration;
49 import org.apache.maven.archiva.configuration.FileTypes;
50 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
51 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
52 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
53 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
54 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
55 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
56 import org.apache.maven.archiva.model.ArtifactReference;
57 import org.apache.maven.archiva.repository.RepositoryContentFactory;
58 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
59 import org.apache.maven.archiva.repository.content.ManagedLegacyRepositoryContent;
60 import org.apache.maven.archiva.repository.content.PathParser;
61 import org.apache.maven.archiva.repository.layout.LayoutException;
62 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
63 import org.easymock.MockControl;
64 import org.easymock.classextension.MockClassControl;
65
66 /**
67  * AdministrationServiceImplTest
68  * 
69  * @version $Id: AdministrationServiceImplTest.java
70  */
71 public class AdministrationServiceImplTest
72     extends PlexusInSpringTestCase
73 {
74     private MockControl archivaConfigControl;
75
76     private ArchivaConfiguration archivaConfig;
77
78     private MockControl configControl;
79
80     private Configuration config;
81
82     private AdministrationServiceImpl service;
83
84     private MockControl repositoryTaskSchedulerControl;
85
86     private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
87
88     // repository consumers
89     private MockControl repoConsumerUtilsControl;
90
91     private RepositoryContentConsumers repoConsumersUtil;
92
93     private MockControl knownContentConsumerControl;
94
95     private MockControl invalidContentConsumerControl;
96
97     private KnownRepositoryContentConsumer indexArtifactConsumer;
98
99     private KnownRepositoryContentConsumer indexPomConsumer;
100
101     private InvalidRepositoryContentConsumer checkPomConsumer;
102
103     private InvalidRepositoryContentConsumer checkMetadataConsumer;
104
105     // delete artifact
106     private MockControl repoFactoryControl;
107
108     private RepositoryContentFactory repositoryFactory;
109
110     private MockControl listenerControl;
111
112     private RepositoryListener listener;
113
114     private MockControl metadataRepositoryControl;
115
116     private MetadataRepository metadataRepository;
117
118     private MockControl repositoryStatisticsManagerControl;
119
120     private RepositoryStatisticsManager repositoryStatisticsManager;
121
122     private MockControl repositoryMergerControl;
123
124     private RepositoryMerger repositoryMerger;
125
126     private MockControl auditListenerControl;
127
128     private AuditListener auditListener;
129
130     private static final String STAGE = "-stage";
131
132     protected void setUp()
133         throws Exception
134     {
135         super.setUp();
136
137         archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
138         archivaConfig = (ArchivaConfiguration) archivaConfigControl.getMock();
139
140         configControl = MockClassControl.createControl( Configuration.class );
141         config = (Configuration) configControl.getMock();
142
143         repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
144         repositoryTaskScheduler = (RepositoryArchivaTaskScheduler) repositoryTaskSchedulerControl.getMock();
145
146         // repo consumers
147         repoConsumerUtilsControl = MockClassControl.createControl( RepositoryContentConsumers.class );
148         repoConsumersUtil = (RepositoryContentConsumers) repoConsumerUtilsControl.getMock();
149
150         knownContentConsumerControl = MockControl.createControl( KnownRepositoryContentConsumer.class );
151         indexArtifactConsumer = (KnownRepositoryContentConsumer) knownContentConsumerControl.getMock();
152         indexPomConsumer = (KnownRepositoryContentConsumer) knownContentConsumerControl.getMock();
153
154         invalidContentConsumerControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
155         checkPomConsumer = (InvalidRepositoryContentConsumer) invalidContentConsumerControl.getMock();
156         checkMetadataConsumer = (InvalidRepositoryContentConsumer) invalidContentConsumerControl.getMock();
157
158         // delete artifact
159         repoFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
160         repositoryFactory = (RepositoryContentFactory) repoFactoryControl.getMock();
161
162         metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
163         metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
164
165         listenerControl = MockControl.createControl( RepositoryListener.class );
166         listener = (RepositoryListener) listenerControl.getMock();
167
168         repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
169         repositoryStatisticsManager = (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
170
171         repositoryMergerControl = MockControl.createControl( RepositoryMerger.class );
172         repositoryMerger = (RepositoryMerger) repositoryMergerControl.getMock();
173
174         auditListenerControl = MockControl.createControl( AuditListener.class );
175         auditListener = (AuditListener) auditListenerControl.getMock();
176
177         service =
178             new AdministrationServiceImpl( archivaConfig, repoConsumersUtil, repositoryFactory, metadataRepository,
179                                            repositoryTaskScheduler, Collections.singletonList( listener ),
180                                            repositoryStatisticsManager, repositoryMerger, auditListener );
181     }
182
183     /* Tests for repository consumers */
184
185     public void testGetAllRepoConsumers()
186         throws Exception
187     {
188         recordRepoConsumers();
189
190         repoConsumerUtilsControl.replay();
191         knownContentConsumerControl.replay();
192         invalidContentConsumerControl.replay();
193
194         List<String> repoConsumers = service.getAllRepositoryConsumers();
195
196         repoConsumerUtilsControl.verify();
197         knownContentConsumerControl.verify();
198         invalidContentConsumerControl.verify();
199
200         assertNotNull( repoConsumers );
201         assertEquals( 4, repoConsumers.size() );
202         assertTrue( repoConsumers.contains( "index-artifact" ) );
203         assertTrue( repoConsumers.contains( "index-pom" ) );
204         assertTrue( repoConsumers.contains( "check-pom" ) );
205         assertTrue( repoConsumers.contains( "check-metadata" ) );
206     }
207
208     public void testConfigureValidRepositoryConsumer()
209         throws Exception
210     {
211         RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
212         repoScanning.addKnownContentConsumer( "index-artifact" );
213         repoScanning.addKnownContentConsumer( "index-pom" );
214         repoScanning.addInvalidContentConsumer( "check-pom" );
215
216         // test enable "check-metadata" consumer
217         recordRepoConsumers();
218
219         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
220         configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
221
222         config.setRepositoryScanning( repoScanning );
223         configControl.setMatcher( MockControl.ALWAYS_MATCHER );
224         configControl.setVoidCallable();
225
226         archivaConfig.save( config );
227         archivaConfigControl.setVoidCallable();
228
229         repoConsumerUtilsControl.replay();
230         knownContentConsumerControl.replay();
231         invalidContentConsumerControl.replay();
232         archivaConfigControl.replay();
233         configControl.replay();
234
235         try
236         {
237             boolean success = service.configureRepositoryConsumer( null, "check-metadata", true );
238             assertTrue( success );
239         }
240         catch ( Exception e )
241         {
242             fail( "An exception should not have been thrown." );
243         }
244
245         repoConsumerUtilsControl.verify();
246         knownContentConsumerControl.verify();
247         invalidContentConsumerControl.verify();
248         archivaConfigControl.verify();
249         configControl.verify();
250
251         // test disable "check-metadata" consumer
252         repoConsumerUtilsControl.reset();
253         knownContentConsumerControl.reset();
254         invalidContentConsumerControl.reset();
255         archivaConfigControl.reset();
256         configControl.reset();
257
258         repoScanning.addInvalidContentConsumer( "check-metadata" );
259
260         recordRepoConsumers();
261
262         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
263         configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
264
265         config.setRepositoryScanning( repoScanning );
266         configControl.setMatcher( MockControl.ALWAYS_MATCHER );
267         configControl.setVoidCallable();
268
269         archivaConfig.save( config );
270         archivaConfigControl.setVoidCallable();
271
272         repoConsumerUtilsControl.replay();
273         knownContentConsumerControl.replay();
274         invalidContentConsumerControl.replay();
275         archivaConfigControl.replay();
276         configControl.replay();
277
278         try
279         {
280             boolean success = service.configureRepositoryConsumer( null, "check-metadata", false );
281
282             repoConsumerUtilsControl.verify();
283             knownContentConsumerControl.verify();
284             invalidContentConsumerControl.verify();
285             archivaConfigControl.verify();
286             configControl.verify();
287
288             assertTrue( success );
289         }
290         catch ( Exception e )
291         {
292             fail( "An excecption should not have been thrown." );
293         }
294     }
295
296     public void testConfigureInvalidRepositoryConsumer()
297         throws Exception
298     {
299         recordRepoConsumers();
300
301         repoConsumerUtilsControl.replay();
302         knownContentConsumerControl.replay();
303         invalidContentConsumerControl.replay();
304
305         try
306         {
307             service.configureRepositoryConsumer( null, "invalid-consumer", true );
308             fail( "An exception should have been thrown." );
309         }
310         catch ( Exception e )
311         {
312             assertEquals( "Invalid repository consumer.", e.getMessage() );
313         }
314
315         repoConsumerUtilsControl.verify();
316         knownContentConsumerControl.verify();
317         invalidContentConsumerControl.verify();
318     }
319
320     /* Tests for delete artifact */
321
322     public void testDeleteM2ArtifactArtifactExists()
323         throws Exception
324     {
325         ManagedRepositoryConfiguration managedRepo = createManagedRepo( "default", "default-repo" );
326
327         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
328         configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
329
330         ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
331         repoContent.setRepository( managedRepo );
332
333         repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
334
335         List<ArtifactMetadata> artifacts = getArtifacts();
336         ArtifactMetadata artifact = artifacts.get( 0 );
337
338         metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( repoContent.getId(),
339                                                                                     artifact.getNamespace(),
340                                                                                     artifact.getProject(),
341                                                                                     artifact.getVersion() ), artifacts );
342         metadataRepository.deleteArtifact( repoContent.getId(), artifact.getNamespace(), artifact.getProject(),
343                                            artifact.getVersion(), artifact.getId() );
344
345         listener.deleteArtifact( repoContent.getId(), artifact.getNamespace(), artifact.getProject(),
346                                  artifact.getVersion(), artifact.getId() );
347         listenerControl.setVoidCallable( 1 );
348
349         archivaConfigControl.replay();
350         configControl.replay();
351         repoFactoryControl.replay();
352         metadataRepositoryControl.replay();
353         listenerControl.replay();
354
355         boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
356         assertTrue( success );
357
358         archivaConfigControl.verify();
359         configControl.verify();
360         repoFactoryControl.verify();
361         metadataRepositoryControl.verify();
362         listenerControl.verify();
363
364         assertFalse( new File( managedRepo.getLocation(), "org/apache/archiva/archiva-test/1.0" ).exists() );
365         assertTrue( new File( managedRepo.getLocation(), "org/apache/archiva/archiva-test/1.1" ).exists() );
366     }
367
368     public void testDeleteM1ArtifactArtifactExists()
369         throws Exception
370     {
371         MockControl fileTypesControl = MockClassControl.createControl( FileTypes.class );
372         FileTypes fileTypes = (FileTypes) fileTypesControl.getMock();
373
374         MockControl pathParserControl = MockClassControl.createControl( PathParser.class );
375         PathParser parser = (PathParser) pathParserControl.getMock();
376
377         ManagedRepositoryConfiguration managedRepo = createManagedRepo( "legacy", "legacy-repo" );
378
379         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
380         configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
381
382         ManagedLegacyRepositoryContent repoContent = new ManagedLegacyRepositoryContent();
383         repoContent.setRepository( managedRepo );
384         repoContent.setFileTypes( fileTypes );
385         repoContent.setLegacyPathParser( parser );
386
387         repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
388
389         recordInManagedLegacyRepoContent( fileTypesControl, fileTypes, pathParserControl, parser );
390
391         List<ArtifactMetadata> artifacts = getArtifacts();
392         ArtifactMetadata artifact = artifacts.get( 0 );
393
394         metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( repoContent.getId(),
395                                                                                     artifact.getNamespace(),
396                                                                                     artifact.getProject(),
397                                                                                     artifact.getVersion() ), artifacts );
398         metadataRepository.deleteArtifact( repoContent.getId(), artifact.getNamespace(), artifact.getProject(),
399                                            artifact.getVersion(), artifact.getId() );
400
401         listener.deleteArtifact( repoContent.getId(), artifact.getNamespace(), artifact.getProject(),
402                                  artifact.getVersion(), artifact.getId() );
403         listenerControl.setVoidCallable( 1 );
404
405         archivaConfigControl.replay();
406         configControl.replay();
407         repoFactoryControl.replay();
408         metadataRepositoryControl.replay();
409         listenerControl.replay();
410         fileTypesControl.replay();
411         pathParserControl.replay();
412
413         boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
414         assertTrue( success );
415
416         archivaConfigControl.verify();
417         configControl.verify();
418         repoFactoryControl.verify();
419         metadataRepositoryControl.verify();
420         listenerControl.verify();
421         fileTypesControl.verify();
422         pathParserControl.verify();
423
424         File repo = new File( managedRepo.getLocation() );
425         assertFalse( new File( repo, "org.apache.archiva/jars/archiva-test-1.0.jar" ).exists() );
426         assertFalse( new File( repo, "org.apache.archiva/poms/archiva-test-1.0.pom" ).exists() );
427
428         assertTrue( new File( repo, "org.apache.archiva/jars/archiva-test-1.1.jar" ).exists() );
429         assertTrue( new File( repo, "org.apache.archiva/jars/archiva-diff-1.0.jar" ).exists() );
430         assertTrue( new File( repo, "org.apache.archiva/poms/archiva-test-1.1.pom" ).exists() );
431         assertTrue( new File( repo, "org.apache.archiva/poms/archiva-diff-1.0.pom" ).exists() );
432     }
433
434     public void testDeleteArtifactArtifactDoesNotExist()
435         throws Exception
436     {
437         ManagedRepositoryConfiguration managedRepo = createManagedRepo( "default", "default-repo" );
438
439         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
440         configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), managedRepo );
441
442         ManagedDefaultRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
443         repoContent.setRepository( managedRepo );
444
445         repoFactoryControl.expectAndReturn( repositoryFactory.getManagedRepositoryContent( "internal" ), repoContent );
446
447         archivaConfigControl.replay();
448         configControl.replay();
449         repoFactoryControl.replay();
450
451         try
452         {
453             service.deleteArtifact( "internal", "org.apache.archiva", "archiva-non-existing", "1.0" );
454             fail( "An exception should have been thrown." );
455         }
456         catch ( Exception e )
457         {
458             assertEquals( "Artifact does not exist.", e.getMessage() );
459         }
460
461         archivaConfigControl.verify();
462         configControl.verify();
463         repoFactoryControl.verify();
464     }
465
466     private ManagedRepositoryConfiguration createManagedRepo( String layout, String directory )
467         throws IOException
468     {
469         File srcDir = new File( getBasedir(), "src/test/repositories/" + directory );
470
471         File repoDir = getTestFile( "target/test-repos/" + directory );
472
473         FileUtils.deleteDirectory( repoDir );
474
475         FileUtils.copyDirectory( srcDir, repoDir, FileFilterUtils.makeSVNAware( null ) );
476
477         ManagedRepositoryConfiguration managedRepo =
478             createManagedRepo( "internal", layout, "Internal Repository", true, false );
479         managedRepo.setLocation( repoDir.getAbsolutePath() );
480         return managedRepo;
481     }
482
483     public void testDeleteArtifacRepositoryDoesNotExist()
484         throws Exception
485     {
486         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
487         configControl.expectAndReturn( config.findManagedRepositoryById( "non-existing-repo" ), null );
488
489         archivaConfigControl.replay();
490         configControl.replay();
491
492         try
493         {
494             service.deleteArtifact( "non-existing-repo", "org.apache.archiva", "archiva-test", "1.0" );
495             fail( "An exception should have been thrown." );
496         }
497         catch ( Exception e )
498         {
499             assertEquals( "Repository does not exist.", e.getMessage() );
500         }
501
502         archivaConfigControl.verify();
503         configControl.verify();
504     }
505
506     /* Tests for repository scanning */
507
508     public void testExecuteRepoScannerRepoExistsAndNotBeingScanned()
509         throws Exception
510     {
511         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
512         configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
513                                        createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
514
515         RepositoryTask task = new RepositoryTask();
516
517         repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask( "internal" ),
518                                                         false );
519
520         repositoryTaskScheduler.queueTask( task );
521         repositoryTaskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
522         repositoryTaskSchedulerControl.setVoidCallable();
523
524         archivaConfigControl.replay();
525         configControl.replay();
526         repositoryTaskSchedulerControl.replay();
527
528         try
529         {
530             boolean success = service.executeRepositoryScanner( "internal" );
531             assertTrue( success );
532         }
533         catch ( Exception e )
534         {
535             fail( "An exception should not have been thrown." );
536         }
537
538         archivaConfigControl.verify();
539         configControl.verify();
540         repositoryTaskSchedulerControl.verify();
541     }
542
543     public void testExecuteRepoScannerRepoExistsButBeingScanned()
544         throws Exception
545     {
546         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
547         configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
548                                        createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
549
550         repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask( "internal" ),
551                                                         true );
552
553         archivaConfigControl.replay();
554         configControl.replay();
555         repositoryTaskSchedulerControl.replay();
556
557         try
558         {
559             boolean success = service.executeRepositoryScanner( "internal" );
560             assertFalse( success );
561         }
562         catch ( Exception e )
563         {
564             fail( "An exception should not have been thrown." );
565         }
566
567         archivaConfigControl.verify();
568         configControl.verify();
569         repositoryTaskSchedulerControl.verify();
570     }
571
572     public void testExecuteRepoScannerRepoDoesNotExist()
573         throws Exception
574     {
575         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
576         configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), null );
577
578         archivaConfigControl.replay();
579         configControl.replay();
580
581         try
582         {
583             service.executeRepositoryScanner( "internal" );
584             fail( "An exception should have been thrown." );
585         }
586         catch ( Exception e )
587         {
588             assertEquals( "Repository does not exist.", e.getMessage() );
589         }
590
591         archivaConfigControl.verify();
592         configControl.verify();
593     }
594
595     /* Tests for querying repositories */
596
597     public void testGetAllManagedRepositories()
598         throws Exception
599     {
600         List<ManagedRepositoryConfiguration> managedRepos = new ArrayList<ManagedRepositoryConfiguration>();
601         managedRepos.add( createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
602         managedRepos.add( createManagedRepo( "snapshots", "default", "Snapshots Repository", false, true ) );
603
604         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
605         configControl.expectAndReturn( config.getManagedRepositories(), managedRepos );
606
607         archivaConfigControl.replay();
608         configControl.replay();
609
610         List<ManagedRepository> repos = service.getAllManagedRepositories();
611
612         archivaConfigControl.verify();
613         configControl.verify();
614
615         assertNotNull( repos );
616         assertEquals( 2, repos.size() );
617
618         assertManagedRepo( repos.get( 0 ), managedRepos.get( 0 ) );
619         assertManagedRepo( repos.get( 1 ), managedRepos.get( 1 ) );
620     }
621
622     public void testGetAllRemoteRepositories()
623         throws Exception
624     {
625         List<RemoteRepositoryConfiguration> remoteRepos = new ArrayList<RemoteRepositoryConfiguration>();
626         remoteRepos.add( createRemoteRepository( "central", "Central Repository", "default",
627                                                  "http://repo1.maven.org/maven2" ) );
628         remoteRepos.add( createRemoteRepository( "dummy", "Dummy Remote Repository", "legacy", "http://dummy.com/dummy" ) );
629
630         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
631         configControl.expectAndReturn( config.getRemoteRepositories(), remoteRepos );
632
633         archivaConfigControl.replay();
634         configControl.replay();
635
636         List<RemoteRepository> repos = service.getAllRemoteRepositories();
637
638         archivaConfigControl.verify();
639         configControl.verify();
640
641         assertNotNull( repos );
642         assertEquals( 2, repos.size() );
643
644         assertRemoteRepo( repos.get( 0 ), remoteRepos.get( 0 ) );
645         assertRemoteRepo( repos.get( 1 ), remoteRepos.get( 1 ) );
646     }
647
648     /* Merge method */
649
650     public void testMergeRepositoryWithInvalidRepository()
651         throws Exception
652     {
653         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
654         configControl.expectAndReturn( config.findManagedRepositoryById( "invalid" ), null );
655
656         archivaConfigControl.replay();
657         configControl.replay();
658
659         try
660         {
661             service.merge( "invalid", true );
662         }
663         catch ( Exception e )
664         {
665             assertEquals( "Repository Id : invalid not found.", e.getMessage() );
666         }
667
668         archivaConfigControl.verify();
669         configControl.verify();
670     }
671
672     public void testMergeWithNoStagingRepository()
673         throws Exception
674     {
675         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
676         configControl.expectAndReturn( config.findManagedRepositoryById( "repo" ),
677                                        createManagedRepo( "repo", "default", "repo", true, false ) );
678         configControl.expectAndReturn( config.findManagedRepositoryById( "repo-stage" ), null );
679
680         archivaConfigControl.replay();
681         configControl.replay();
682
683         try
684         {
685             service.merge( "repo", true );
686         }
687         catch ( Exception e )
688         {
689             assertEquals( "Staging Id : repo-stage not found.", e.getMessage() );
690         }
691
692         archivaConfigControl.verify();
693         configControl.verify();
694     }
695
696     public void testMergeRepositoriesAndScan()
697         throws Exception
698     {
699         List<ArtifactMetadata> sources = new ArrayList<ArtifactMetadata>();
700
701         ArtifactMetadata artifact = new ArtifactMetadata();
702         artifact.setId( "artifact" );
703         artifact.setFileLastModified( System.currentTimeMillis() );
704
705         sources.add( artifact );
706
707         ManagedRepositoryConfiguration merge = createManagedRepo( "merge", "default", "merge", true, true );
708         merge.setLocation( "target/test-repository/merge" );
709         ManagedRepositoryConfiguration staging = createStagingRepo( merge );
710
711         RepositoryTask task = new RepositoryTask();
712         task.setScanAll( true );
713
714         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
715         configControl.expectAndReturn( config.findManagedRepositoryById( "merge" ), merge );
716         configControl.expectAndReturn( config.findManagedRepositoryById( "merge-stage" ), staging );
717
718         metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( staging.getId() ), sources );
719         repositoryMergerControl.expectAndDefaultReturn( repositoryMerger.getConflictingArtifacts( staging.getId(),
720                                                                                                   merge.getId() ),
721                                                         sources );
722         repositoryMerger.merge( staging.getId(), merge.getId() );
723         repositoryMergerControl.setMatcher( MockControl.ALWAYS_MATCHER );
724         repositoryMergerControl.setVoidCallable();
725         repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask( "merge" ),
726                                                         false );
727
728         // scanning after merge
729         repositoryTaskScheduler.queueTask( task );
730         repositoryTaskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
731         repositoryTaskSchedulerControl.setVoidCallable();
732
733         // audit logs
734         metadataRepository.addMetadataFacet( merge.getId(), createAuditEvent( merge ) );
735         metadataRepositoryControl.setMatcher( MockControl.ALWAYS_MATCHER );
736         metadataRepositoryControl.setVoidCallable();
737
738         archivaConfigControl.replay();
739         metadataRepositoryControl.replay();
740         configControl.replay();
741         repositoryMergerControl.replay();
742         repositoryTaskSchedulerControl.replay();
743
744         boolean a = service.merge( "merge", false );
745         assertTrue( a );
746
747         archivaConfigControl.verify();
748         configControl.verify();
749         configControl.verify();
750         metadataRepositoryControl.verify();
751         repositoryMergerControl.verify();
752         repositoryTaskSchedulerControl.verify();
753     }
754
755     public void testMergeRepositoriesWithConflictsAndScan()
756         throws Exception
757     {
758         List<ArtifactMetadata> sources = new ArrayList<ArtifactMetadata>();
759         ArtifactMetadata one = new ArtifactMetadata();
760         one.setId( "one" );
761         one.setVersion( "1.0" );
762
763         ArtifactMetadata two = new ArtifactMetadata();
764         two.setId( "two" );
765         two.setVersion( "1.0-SNAPSHOT" );
766
767         sources.add( one );
768         sources.add( two );
769
770         List<ArtifactMetadata> conflicts = new ArrayList<ArtifactMetadata>();
771         conflicts.add( one );
772
773         sources.removeAll( conflicts );
774
775         Filter<ArtifactMetadata> artifactsWithOutConflicts = new IncludesFilter<ArtifactMetadata>( sources );
776
777         RepositoryTask task = new RepositoryTask();
778         task.setScanAll( true );
779
780         ManagedRepositoryConfiguration repo = createManagedRepo( "repo", "default", "repo", true, true );
781         repo.setLocation( "target/test-repository/one" );
782         ManagedRepositoryConfiguration staging = createStagingRepo( repo );
783
784         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
785         configControl.expectAndReturn( config.findManagedRepositoryById( "repo" ), repo );
786         configControl.expectAndReturn( config.findManagedRepositoryById( "repo-stage" ), staging );
787
788         metadataRepositoryControl.expectAndReturn( metadataRepository.getArtifacts( staging.getId() ), sources );
789         repositoryMergerControl.expectAndDefaultReturn( repositoryMerger.getConflictingArtifacts( staging.getId(),
790                                                                                                   repo.getId() ),
791                                                         conflicts );
792         repositoryMerger.merge( staging.getId(), repo.getId(), artifactsWithOutConflicts );
793         repositoryMergerControl.setMatcher( MockControl.ALWAYS_MATCHER );
794         repositoryMergerControl.setVoidCallable();
795         repositoryTaskSchedulerControl.expectAndReturn( repositoryTaskScheduler.isProcessingRepositoryTask( "repo" ),
796                                                         false );
797         repositoryTaskScheduler.queueTask( task );
798         repositoryTaskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
799         repositoryTaskSchedulerControl.setVoidCallable();
800
801         // audit logs
802         metadataRepository.addMetadataFacet( repo.getId(), createAuditEvent( repo ) );
803         metadataRepositoryControl.setMatcher( MockControl.ALWAYS_MATCHER );
804         metadataRepositoryControl.setVoidCallable();
805
806         archivaConfigControl.replay();
807         metadataRepositoryControl.replay();
808         configControl.replay();
809         repositoryMergerControl.replay();
810         repositoryTaskSchedulerControl.replay();
811
812         boolean a = service.merge( "repo", true );
813         assertTrue( a );
814
815         archivaConfigControl.verify();
816         configControl.verify();
817         configControl.verify();
818         metadataRepositoryControl.verify();
819         repositoryMergerControl.verify();
820         repositoryTaskSchedulerControl.verify();
821     }
822
823     public void testAddManagedRepository()
824         throws Exception
825     {
826         String projId = "org.apache.archiva";
827         String repoId = projId + ".releases";
828         String layout = "default";
829         String name = projId + " Releases";
830         String releaseLocation = "target/test-repository/" + projId + ".releases";
831         String stageLocation = releaseLocation + "-stage";
832
833         ManagedRepositoryConfiguration managedRepo = createManagedRepo( "repo1", "default", "repo", true, false );
834         RemoteRepositoryConfiguration remoteRepo =
835             createRemoteRepository( "central", "Central Repository", "default", "http://repo1.maven.org/maven2" );
836         List<String> repositories = new ArrayList<String>();
837         repositories.add( managedRepo.getName() );
838         RepositoryGroupConfiguration repoGroup = createRepoGroupConfig( "repoGroup", repositories );
839         Map<String, ManagedRepositoryConfiguration> managedRepoMap =
840             new HashMap<String, ManagedRepositoryConfiguration>();
841         Map<String, RemoteRepositoryConfiguration> remoteRepoMap = new HashMap<String, RemoteRepositoryConfiguration>();
842         Map<String, RepositoryGroupConfiguration> repoGroupMap = new HashMap<String, RepositoryGroupConfiguration>();
843         managedRepoMap.put( "repo1", managedRepo );
844         remoteRepoMap.put( "repo1", remoteRepo );
845         repoGroupMap.put( "repo1", repoGroup );
846
847         archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
848
849         configControl.expectAndReturn( config.getManagedRepositoriesAsMap(), managedRepoMap );
850         configControl.expectAndReturn( config.getRemoteRepositoriesAsMap(), remoteRepoMap );
851         configControl.expectAndReturn( config.getRepositoryGroupsAsMap(), repoGroupMap );
852         config.addManagedRepository( managedRepo );
853         configControl.setMatcher( MockControl.ALWAYS_MATCHER );
854         configControl.setVoidCallable();
855         config.addManagedRepository( managedRepo );
856         configControl.setMatcher( MockControl.ALWAYS_MATCHER );
857         configControl.setVoidCallable();
858         archivaConfig.save( config );
859         archivaConfigControl.setVoidCallable();
860
861         archivaConfigControl.replay();
862         configControl.replay();
863         assertFalse( new File( releaseLocation ).isDirectory() );
864         assertFalse( new File( stageLocation ).isDirectory() );
865         boolean success =
866             service.addManagedRepository( repoId, layout, name, releaseLocation, true, true, false, true,
867                                           "0 15 3 * * ? *" );
868         assertTrue( success );
869         assertTrue( new File( releaseLocation ).isDirectory() );
870         assertTrue( new File( stageLocation ).isDirectory() );
871         new File( releaseLocation ).delete();
872         new File( stageLocation ).delete();
873     }
874
875     /* private methods */
876
877     private void assertRemoteRepo( RemoteRepository remoteRepo, RemoteRepositoryConfiguration expectedRepoConfig )
878     {
879         assertEquals( expectedRepoConfig.getId(), remoteRepo.getId() );
880         assertEquals( expectedRepoConfig.getLayout(), remoteRepo.getLayout() );
881         assertEquals( expectedRepoConfig.getName(), remoteRepo.getName() );
882         assertEquals( expectedRepoConfig.getUrl(), remoteRepo.getUrl() );
883     }
884
885     private RemoteRepositoryConfiguration createRemoteRepository( String id, String name, String layout, String url )
886     {
887         RemoteRepositoryConfiguration remoteConfig = new RemoteRepositoryConfiguration();
888         remoteConfig.setId( id );
889         remoteConfig.setName( name );
890         remoteConfig.setLayout( layout );
891         remoteConfig.setUrl( url );
892
893         return remoteConfig;
894     }
895
896     private void assertManagedRepo( ManagedRepository managedRepo, ManagedRepositoryConfiguration expectedRepoConfig )
897     {
898         assertEquals( expectedRepoConfig.getId(), managedRepo.getId() );
899         assertEquals( expectedRepoConfig.getLayout(), managedRepo.getLayout() );
900         assertEquals( expectedRepoConfig.getName(), managedRepo.getName() );
901
902         // TODO enable assert once fixed in AdministrationServiceImpl!
903         // assertEquals( "http://localhost:8080/archiva/repository/" + expectedRepoConfig.getId(), managedRepo.getUrl()
904         // );
905         assertEquals( expectedRepoConfig.isReleases(), managedRepo.isReleases() );
906         assertEquals( expectedRepoConfig.isSnapshots(), managedRepo.isSnapshots() );
907     }
908
909     private ManagedRepositoryConfiguration createManagedRepo( String id, String layout, String name,
910                                                               boolean hasReleases, boolean hasSnapshots )
911     {
912         ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
913         repoConfig.setId( id );
914         repoConfig.setLayout( layout );
915         repoConfig.setName( name );
916         repoConfig.setReleases( hasReleases );
917         repoConfig.setSnapshots( hasSnapshots );
918
919         return repoConfig;
920     }
921
922     private ManagedRepositoryConfiguration createStagingRepo( ManagedRepositoryConfiguration repoConfig )
923     {
924         ManagedRepositoryConfiguration stagingRepo = new ManagedRepositoryConfiguration();
925         stagingRepo.setId( repoConfig.getId() + STAGE );
926         stagingRepo.setLayout( repoConfig.getLayout() );
927         stagingRepo.setName( repoConfig + STAGE );
928         stagingRepo.setReleases( repoConfig.isReleases() );
929         stagingRepo.setSnapshots( repoConfig.isSnapshots() );
930         stagingRepo.setLocation( repoConfig.getLocation() );
931
932         return stagingRepo;
933     }
934
935     private AuditEvent createAuditEvent( ManagedRepositoryConfiguration repoConfig )
936     {
937         AuditEvent auditEvent = new AuditEvent();
938
939         auditEvent.setAction( AuditEvent.MERGE_REPO_REMOTE );
940         auditEvent.setRepositoryId( repoConfig.getId() );
941         auditEvent.setResource( repoConfig.getLocation() );
942         auditEvent.setTimestamp( new Date() );
943
944         return auditEvent;
945     }
946
947     private void recordRepoConsumers()
948     {
949         List<KnownRepositoryContentConsumer> availableKnownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
950         availableKnownConsumers.add( indexArtifactConsumer );
951         availableKnownConsumers.add( indexPomConsumer );
952
953         List<InvalidRepositoryContentConsumer> availableInvalidConsumers =
954             new ArrayList<InvalidRepositoryContentConsumer>();
955         availableInvalidConsumers.add( checkPomConsumer );
956         availableInvalidConsumers.add( checkMetadataConsumer );
957
958         repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableKnownConsumers(),
959                                                   availableKnownConsumers );
960         knownContentConsumerControl.expectAndReturn( indexArtifactConsumer.getId(), "index-artifact" );
961         knownContentConsumerControl.expectAndReturn( indexPomConsumer.getId(), "index-pom" );
962
963         repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableInvalidConsumers(),
964                                                   availableInvalidConsumers );
965         invalidContentConsumerControl.expectAndReturn( checkPomConsumer.getId(), "check-pom" );
966         invalidContentConsumerControl.expectAndReturn( checkMetadataConsumer.getId(), "check-metadata" );
967     }
968
969     private void recordInManagedLegacyRepoContent( MockControl fileTypesControl, FileTypes fileTypes,
970                                                    MockControl pathParserControl, PathParser parser )
971         throws LayoutException
972     {
973         String sep = File.separator;
974         String ad10p = "org.apache.archiva" + sep + "poms" + sep + "archiva-diff-1.0.pom";
975         String at10p = "org.apache.archiva" + sep + "poms" + sep + "archiva-test-1.0.pom";
976         String at11p = "org.apache.archiva" + sep + "poms" + sep + "archiva-test-1.1.pom";
977         String ad10j = "org.apache.archiva" + sep + "jars" + sep + "archiva-diff-1.0.jar";
978         String at10j = "org.apache.archiva" + sep + "jars" + sep + "archiva-test-1.0.jar";
979         String at11j = "org.apache.archiva" + sep + "jars" + sep + "archiva-test-1.1.jar";
980
981         fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at10p ), true );
982         fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at11p ), true );
983         fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( ad10p ), true );
984         fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( ad10j ), true );
985         fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at10j ), true );
986         fileTypesControl.expectAndReturn( fileTypes.matchesArtifactPattern( at11j ), true );
987
988         ArtifactReference aRef = createArtifactReference( "archiva-test", "org.apache.archiva", "1.1", "pom" );
989         pathParserControl.expectAndReturn( parser.toArtifactReference( at11p ), aRef );
990
991         aRef = createArtifactReference( "archiva-test", "org.apache.archiva", "1.0", "pom" );
992         pathParserControl.expectAndReturn( parser.toArtifactReference( at10p ), aRef );
993
994         aRef = createArtifactReference( "archiva-diff", "org.apache.archiva", "1.0", "pom" );
995         pathParserControl.expectAndReturn( parser.toArtifactReference( ad10p ), aRef );
996
997         aRef = createArtifactReference( "archiva-diff", "org.apache.archiva", "1.0", "jar" );
998         pathParserControl.expectAndReturn( parser.toArtifactReference( ad10j ), aRef );
999
1000         aRef = createArtifactReference( "archiva-test", "org.apache.archiva", "1.0", "jar" );
1001         pathParserControl.expectAndReturn( parser.toArtifactReference( at10j ), aRef );
1002
1003         aRef = createArtifactReference( "archiva-test", "org.apache.archiva", "1.1", "jar" );
1004         pathParserControl.expectAndReturn( parser.toArtifactReference( at11j ), aRef );
1005     }
1006
1007     private List<ArtifactMetadata> getArtifacts()
1008     {
1009         List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>();
1010
1011         ArtifactMetadata artifact = new ArtifactMetadata();
1012         artifact.setId( "archiva-test-1.0.jar" );
1013         artifact.setProject( "archiva-test" );
1014         artifact.setVersion( "1.0" );
1015         artifact.setProjectVersion( "1.0" );
1016         artifact.setNamespace( "org.apache.archiva" );
1017         artifact.setRepositoryId( "internal" );
1018         artifacts.add( artifact );
1019         return artifacts;
1020     }
1021
1022     private ArtifactReference createArtifactReference( String artifactId, String groupId, String version, String type )
1023     {
1024         ArtifactReference aRef = new ArtifactReference();
1025         aRef.setArtifactId( artifactId );
1026         aRef.setGroupId( groupId );
1027         aRef.setType( type );
1028         aRef.setVersion( version );
1029
1030         return aRef;
1031     }
1032
1033     private RepositoryGroupConfiguration createRepoGroupConfig( String id, List<String> repositories )
1034     {
1035         RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
1036         repoGroup.setId( id );
1037         repoGroup.setRepositories( repositories );
1038         return repoGroup;
1039     }
1040 }