1 package org.apache.archiva.web.xmlrpc.services;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.util.ArrayList;
23 import java.util.List;
25 import org.apache.archiva.web.xmlrpc.api.AdministrationService;
26 import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository;
27 import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository;
28 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.configuration.Configuration;
30 import org.apache.maven.archiva.configuration.DatabaseScanningConfiguration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
33 import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration;
34 import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer;
35 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
36 import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers;
37 import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler;
38 import org.apache.maven.archiva.scheduled.tasks.DatabaseTask;
39 import org.apache.maven.archiva.scheduled.tasks.RepositoryTask;
40 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
41 import org.easymock.MockControl;
42 import org.easymock.classextension.MockClassControl;
48 public class AdministrationServiceImplTest
49 extends PlexusInSpringTestCase
51 private MockControl archivaConfigControl;
53 private ArchivaConfiguration archivaConfig;
55 private MockControl configControl;
57 private Configuration config;
59 private AdministrationServiceImpl service;
61 private MockControl taskSchedulerControl;
63 private ArchivaTaskScheduler taskScheduler;
65 private MockControl consumerUtilControl;
67 private RepositoryContentConsumers consumerUtil;
69 private MockControl knownContentConsumerControl;
71 private MockControl invalidContentConsumerControl;
73 private KnownRepositoryContentConsumer indexArtifactConsumer;
75 private KnownRepositoryContentConsumer indexPomConsumer;
77 private InvalidRepositoryContentConsumer checkPomConsumer;
79 private InvalidRepositoryContentConsumer checkMetadataConsumer;
81 protected void setUp()
86 archivaConfigControl = MockControl.createControl( ArchivaConfiguration.class );
87 archivaConfig = ( ArchivaConfiguration ) archivaConfigControl.getMock();
89 configControl = MockClassControl.createControl( Configuration.class );
90 config = ( Configuration ) configControl.getMock();
92 taskSchedulerControl = MockControl.createControl( ArchivaTaskScheduler.class );
93 taskScheduler = ( ArchivaTaskScheduler ) taskSchedulerControl.getMock();
95 consumerUtilControl = MockClassControl.createControl( RepositoryContentConsumers.class );
96 consumerUtil = ( RepositoryContentConsumers ) consumerUtilControl.getMock();
98 knownContentConsumerControl = MockControl.createControl( KnownRepositoryContentConsumer.class );
99 indexArtifactConsumer = ( KnownRepositoryContentConsumer ) knownContentConsumerControl.getMock();
100 indexPomConsumer = ( KnownRepositoryContentConsumer ) knownContentConsumerControl.getMock();
102 invalidContentConsumerControl = MockControl.createControl( InvalidRepositoryContentConsumer.class );
103 checkPomConsumer = ( InvalidRepositoryContentConsumer ) invalidContentConsumerControl.getMock();
104 checkMetadataConsumer = ( InvalidRepositoryContentConsumer ) invalidContentConsumerControl.getMock();
106 service = new AdministrationServiceImpl();
107 service.setArchivaConfiguration( archivaConfig );
108 service.setConsumerUtil( consumerUtil );
111 // DATABASE CONSUMERS
112 /* public void testGetAllDbConsumers()
115 DatabaseScanningConfiguration dbScanning = new DatabaseScanningConfiguration();
116 dbScanning.addCleanupConsumer( "cleanup-index" );
117 dbScanning.addCleanupConsumer( "cleanup-database" );
118 dbScanning.addUnprocessedConsumer( "unprocessed-artifacts" );
119 dbScanning.addUnprocessedConsumer( "unprocessed-poms" );
121 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
122 configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning );
124 archivaConfigControl.replay();
125 configControl.replay();
129 List<String> dbConsumers = service.getAllDatabaseConsumers();
131 archivaConfigControl.verify();
132 configControl.verify();
134 assertNotNull( dbConsumers );
135 assertEquals( 4, dbConsumers.size() );
136 assertTrue( dbConsumers.contains( "cleanup-index" ) );
137 assertTrue( dbConsumers.contains( "cleanup-database" ) );
138 assertTrue( dbConsumers.contains( "unprocessed-artifacts" ) );
139 assertTrue( dbConsumers.contains( "unprocessed-poms" ) );
142 public void testConfigureValidDatabaseConsumer()
145 DatabaseScanningConfiguration dbScanning = new DatabaseScanningConfiguration();
146 dbScanning.addCleanupConsumer( "cleanup-index" );
147 dbScanning.addCleanupConsumer( "cleanup-database" );
148 dbScanning.addUnprocessedConsumer( "unprocessed-artifacts" );
149 dbScanning.addUnprocessedConsumer( "unprocessed-poms" );
151 //TODO mock checking whether the db consumer is valid or not
154 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
155 configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning );
157 config.setDatabaseScanning( dbScanning );
158 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
159 configControl.setVoidCallable();
161 archivaConfig.save( config );
162 archivaConfigControl.setVoidCallable();
164 archivaConfigControl.replay();
165 configControl.replay();
169 boolean success = service.configureDatabaseConsumer( "new-cleanup-consumer", true );
170 assertTrue( success );
172 catch ( Exception e )
174 fail( "An exception should not have been thrown." );
177 archivaConfigControl.verify();
178 configControl.verify();
181 archivaConfigControl.reset();
182 configControl.reset();
184 //TODO mock checking whether the db consumer is valid or not
186 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
187 configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning );
189 config.setDatabaseScanning( dbScanning );
190 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
191 configControl.setVoidCallable();
193 archivaConfig.save( config );
194 archivaConfigControl.setVoidCallable();
196 archivaConfigControl.replay();
197 configControl.replay();
201 boolean success = service.configureDatabaseConsumer( "new-cleanup-consumer", false );
202 assertTrue( success );
204 catch ( Exception e )
206 fail( "An exception should not have been thrown." );
209 archivaConfigControl.verify();
210 configControl.verify();
213 public void testConfigureInvalidDatabaseConsumer()
216 //TODO mock checking whether the db consumer is valid or not
220 service.configureDatabaseConsumer( "invalid-consumer", true );
221 fail( "An exception should have been thrown." );
223 catch ( Exception e )
225 assertEquals( "Invalid database consumer.", e.getMessage() );
231 // REPOSITORY CONSUMERS
232 public void testGetAllRepoConsumers()
235 recordRepoConsumerValidation();
237 consumerUtilControl.replay();
238 knownContentConsumerControl.replay();
239 invalidContentConsumerControl.replay();
241 List<String> repoConsumers = service.getAllRepositoryConsumers();
243 consumerUtilControl.verify();
244 knownContentConsumerControl.verify();
245 invalidContentConsumerControl.verify();
247 assertNotNull( repoConsumers );
248 assertEquals( 4, repoConsumers.size() );
249 assertTrue( repoConsumers.contains( "index-artifact" ) );
250 assertTrue( repoConsumers.contains( "index-pom" ) );
251 assertTrue( repoConsumers.contains( "check-pom" ) );
252 assertTrue( repoConsumers.contains( "check-metadata" ) );
255 public void testConfigureValidRepositoryConsumer()
258 RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration();
259 repoScanning.addKnownContentConsumer( "index-artifact" );
260 repoScanning.addKnownContentConsumer( "index-pom" );
261 repoScanning.addInvalidContentConsumer( "check-pom" );
263 // test enable "check-metadata" consumer
264 recordRepoConsumerValidation();
266 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
267 configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
269 config.setRepositoryScanning( repoScanning );
270 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
271 configControl.setVoidCallable();
273 archivaConfig.save( config );
274 archivaConfigControl.setVoidCallable();
276 consumerUtilControl.replay();
277 knownContentConsumerControl.replay();
278 invalidContentConsumerControl.replay();
279 archivaConfigControl.replay();
280 configControl.replay();
284 boolean success = service.configureRepositoryConsumer( null, "check-metadata", true );
285 assertTrue( success );
287 catch ( Exception e )
289 fail( "An exception should not have been thrown." );
292 consumerUtilControl.verify();
293 knownContentConsumerControl.verify();
294 invalidContentConsumerControl.verify();
295 archivaConfigControl.verify();
296 configControl.verify();
298 // test disable "check-metadata" consumer
299 consumerUtilControl.reset();
300 knownContentConsumerControl.reset();
301 invalidContentConsumerControl.reset();
302 archivaConfigControl.reset();
303 configControl.reset();
305 repoScanning.addInvalidContentConsumer( "check-metadata" );
307 recordRepoConsumerValidation();
309 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
310 configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning );
312 config.setRepositoryScanning( repoScanning );
313 configControl.setMatcher( MockControl.ALWAYS_MATCHER );
314 configControl.setVoidCallable();
316 archivaConfig.save( config );
317 archivaConfigControl.setVoidCallable();
319 consumerUtilControl.replay();
320 knownContentConsumerControl.replay();
321 invalidContentConsumerControl.replay();
322 archivaConfigControl.replay();
323 configControl.replay();
327 boolean success = service.configureRepositoryConsumer( null, "check-metadata", false );
329 consumerUtilControl.verify();
330 knownContentConsumerControl.verify();
331 invalidContentConsumerControl.verify();
332 archivaConfigControl.verify();
333 configControl.verify();
335 assertTrue( success );
337 catch ( Exception e )
339 fail( "An excecption should not have been thrown." );
344 public void testConfigureInvalidRepositoryConsumer()
347 //TODO mock checking whether the repo consumer is valid or not
351 service.configureRepositoryConsumer( null, "invalid-consumer", true );
352 fail( "An exception should have been thrown." );
354 catch ( Exception e )
356 assertEquals( "Invalid repository consumer.", e.getMessage() );
362 public void testDeleteArtifactArtifactExists()
365 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
366 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
367 createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
370 // - mock checking of artifact existence in the repo
371 // - mock artifact delete
373 archivaConfigControl.replay();
374 configControl.replay();
378 boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
379 assertTrue( success );
381 catch ( Exception e )
383 fail( "An exception should not have been thrown." );
386 archivaConfigControl.verify();
387 configControl.verify();
390 public void testDeleteArtifactArtifactDoesNotExist()
393 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
394 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
395 createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
397 // TODO mock checking of artifact existence in the repo
399 archivaConfigControl.replay();
400 configControl.replay();
404 service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
405 fail( "An exception should have been thrown." );
407 catch ( Exception e )
409 assertEquals( "Artifact does not exist.", e.getMessage() );
412 archivaConfigControl.verify();
413 configControl.verify();
416 public void testDeleteArtifacRepositoryDoesNotExist()
419 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
420 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), null );
422 archivaConfigControl.replay();
423 configControl.replay();
427 service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" );
428 fail( "An exception should have been thrown." );
430 catch ( Exception e )
432 assertEquals( "Repository does not exist.", e.getMessage() );
435 archivaConfigControl.verify();
436 configControl.verify();
441 public void testExecuteRepoScannerRepoExists()
444 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
445 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ),
446 createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
448 RepositoryTask task = new RepositoryTask();
450 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingAnyRepositoryTask(), false );
451 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingRepositoryTask( "internal" ), false );
453 taskScheduler.queueRepositoryTask( task );
454 taskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
455 taskSchedulerControl.setVoidCallable();
457 archivaConfigControl.replay();
458 configControl.replay();
459 taskSchedulerControl.replay();
463 boolean success = service.executeRepositoryScanner( "internal" );
464 assertTrue( success );
466 catch ( Exception e )
468 fail( "An exception should not have been thrown." );
471 archivaConfigControl.verify();
472 configControl.verify();
473 taskSchedulerControl.verify();
476 public void testExecuteRepoScannerRepoDoesNotExist()
479 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
480 configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), null );
482 archivaConfigControl.replay();
483 configControl.replay();
487 service.executeRepositoryScanner( "internal" );
488 fail( "An exception should have been thrown." );
490 catch ( Exception e )
492 assertEquals( "Repository does not exist.", e.getMessage() );
495 archivaConfigControl.verify();
496 configControl.verify();
501 public void testExecuteDbScanner()
504 DatabaseTask task = new DatabaseTask();
506 taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingDatabaseTask(), false );
508 taskScheduler.queueDatabaseTask( task );
509 taskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER );
510 taskSchedulerControl.setVoidCallable();
512 taskSchedulerControl.replay();
514 boolean success = service.executeDatabaseScanner();
516 taskSchedulerControl.verify();
518 assertTrue( success );
523 public void testGetAllManagedRepositories()
526 List<ManagedRepositoryConfiguration> managedRepos = new ArrayList<ManagedRepositoryConfiguration>();
527 managedRepos.add( createManagedRepo( "internal", "default", "Internal Repository", true, false ) );
528 managedRepos.add( createManagedRepo( "snapshots", "default", "Snapshots Repository", false, true ) );
530 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
531 configControl.expectAndReturn( config.getManagedRepositories(), managedRepos );
533 archivaConfigControl.replay();
534 configControl.replay();
536 List<ManagedRepository> repos = service.getAllManagedRepositories();
538 archivaConfigControl.verify();
539 configControl.verify();
541 assertNotNull( repos );
542 assertEquals( 2, repos.size() );
544 assertManagedRepo( ( ManagedRepository ) repos.get( 0 ), managedRepos.get( 0 ) );
545 assertManagedRepo( ( ManagedRepository ) repos.get( 1 ), managedRepos.get( 1 ) );
548 public void testGetAllRemoteRepositories()
551 List<RemoteRepositoryConfiguration> remoteRepos = new ArrayList<RemoteRepositoryConfiguration>();
552 remoteRepos.add( createRemoteRepository( "central", "Central Repository", "default", "http://repo1.maven.org/maven2") );
553 remoteRepos.add( createRemoteRepository( "dummy", "Dummy Remote Repository", "legacy", "http://dummy.com/dummy") );
555 archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config );
556 configControl.expectAndReturn( config.getRemoteRepositories(), remoteRepos );
558 archivaConfigControl.replay();
559 configControl.replay();
561 List<RemoteRepository> repos = service.getAllRemoteRepositories();
563 archivaConfigControl.verify();
564 configControl.verify();
566 assertNotNull( repos );
567 assertEquals( 2, repos.size() );
569 assertRemoteRepo( (RemoteRepository) repos.get( 0 ), remoteRepos.get( 0 ) );
570 assertRemoteRepo( (RemoteRepository) repos.get( 1 ), remoteRepos.get( 1 ) );
573 private void assertRemoteRepo( RemoteRepository remoteRepo, RemoteRepositoryConfiguration expectedRepoConfig )
575 assertEquals( expectedRepoConfig.getId(), remoteRepo.getId() );
576 assertEquals( expectedRepoConfig.getLayout(), remoteRepo.getLayout() );
577 assertEquals( expectedRepoConfig.getName(), remoteRepo.getName() );
578 assertEquals( expectedRepoConfig.getUrl(), remoteRepo.getUrl() );
581 private RemoteRepositoryConfiguration createRemoteRepository(String id, String name, String layout, String url)
583 RemoteRepositoryConfiguration remoteConfig = new RemoteRepositoryConfiguration();
584 remoteConfig.setId( id );
585 remoteConfig.setName( name );
586 remoteConfig.setLayout( layout );
587 remoteConfig.setUrl( url );
592 private void assertManagedRepo( ManagedRepository managedRepo, ManagedRepositoryConfiguration expectedRepoConfig )
594 assertEquals( expectedRepoConfig.getId(), managedRepo.getId() );
595 assertEquals( expectedRepoConfig.getLayout(), managedRepo.getLayout() );
596 assertEquals( expectedRepoConfig.getName(), managedRepo.getName() );
597 assertEquals( "http://localhost:8080/archiva/repository/" + expectedRepoConfig.getId(), managedRepo.getUrl() );
598 assertEquals( expectedRepoConfig.isReleases(), managedRepo.isReleases() );
599 assertEquals( expectedRepoConfig.isSnapshots(), managedRepo.isSnapshots() );
602 private ManagedRepositoryConfiguration createManagedRepo( String id, String layout, String name,
603 boolean hasReleases, boolean hasSnapshots )
605 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
606 repoConfig.setId( id );
607 repoConfig.setLayout( layout );
608 repoConfig.setName( name );
609 repoConfig.setReleases( hasReleases );
610 repoConfig.setSnapshots( hasSnapshots );
615 private void recordRepoConsumerValidation()
617 List<KnownRepositoryContentConsumer> availableKnownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
618 availableKnownConsumers.add( indexArtifactConsumer );
619 availableKnownConsumers.add( indexPomConsumer );
621 List<InvalidRepositoryContentConsumer> availableInvalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
622 availableInvalidConsumers.add( checkPomConsumer );
623 availableInvalidConsumers.add( checkMetadataConsumer );
625 consumerUtilControl.expectAndReturn( consumerUtil.getAvailableKnownConsumers(), availableKnownConsumers );
626 knownContentConsumerControl.expectAndReturn( indexArtifactConsumer.getId(), "index-artifact" );
627 knownContentConsumerControl.expectAndReturn( indexPomConsumer.getId(), "index-pom" );
629 consumerUtilControl.expectAndReturn( consumerUtil.getAvailableInvalidConsumers(), availableInvalidConsumers );
630 invalidContentConsumerControl.expectAndReturn( checkPomConsumer.getId(), "check-pom" );
631 invalidContentConsumerControl.expectAndReturn( checkMetadataConsumer.getId(), "check-metadata" );