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