From 42e8d6896b14b52d65c02b30b9659fd73763d37b Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Thu, 9 Oct 2008 05:33:44 +0000 Subject: [PATCH] added test cases to AdministrationServiceImplTest git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-124@703062 13f79535-47bb-0310-9956-ffa450edef68 --- .../web/xmlrpc/api/AdministrationService.java | 74 +++- .../archiva-xmlrpc-services/pom.xml | 4 + .../services/AdministrationServiceImpl.java | 12 +- .../AdministrationServiceImplTest.java | 384 ++++++++++++++---- 4 files changed, 389 insertions(+), 85 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-api/src/main/java/org/apache/archiva/web/xmlrpc/api/AdministrationService.java b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-api/src/main/java/org/apache/archiva/web/xmlrpc/api/AdministrationService.java index 1086d64fd..260bef8fd 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-api/src/main/java/org/apache/archiva/web/xmlrpc/api/AdministrationService.java +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-api/src/main/java/org/apache/archiva/web/xmlrpc/api/AdministrationService.java @@ -29,22 +29,78 @@ import com.atlassian.xmlrpc.ServiceObject; @ServiceObject( "Administration" ) public interface AdministrationService { - public boolean executeRepositoryScanner( String repoId ); - - public boolean executeDatabaseScanner(); + /** + * Executes repository scanner on the given repository. + * + * @param repoId id of the repository to be scanned + * @return + * @throws Exception + */ + public boolean executeRepositoryScanner( String repoId ) throws Exception; + + /** + * Executes the database scanner. + * + * @return + * @throws Exception + */ + public boolean executeDatabaseScanner() throws Exception; + /** + * Gets all available database consumers. + * @return + */ public List getAllDatabaseConsumers(); + + /** + * Configures (enable or disable) database consumer. + * + * @param consumerId id of the database consumer + * @param enable flag whether to enable or disable the specified consumer + * @return + * @throws Exception + */ + public boolean configureDatabaseConsumer( String consumerId, boolean enable ) throws Exception; - public boolean configureDatabaseConsumer( String consumerId, boolean enable ); - - // TODO should we already implement config of consumers per repository? - public boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable ); - + /** + * Gets all available repository consumers. + * + * @return + */ public List getAllRepositoryConsumers(); + + // TODO should we already implement config of consumers per repository? + public boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable ) throws Exception; + /** + * Gets all managed repositories. + * + * @return + */ public List getAllManagedRepositories(); + /** + * Gets all remote repositories. + * + * @return + */ public List getAllRemoteRepositories(); - public boolean deleteArtifact( String repoId, String groupId, String artifactId, String version ); + /** + * Deletes given artifact from the specified repository. + * + * @param repoId id of the repository where the artifact to be deleted resides + * @param groupId groupId of the artifact to be deleted + * @param artifactId artifactId of the artifact to be deleted + * @param version version of the artifact to be deleted + * @return + * @throws Exception + */ + public boolean deleteArtifact( String repoId, String groupId, String artifactId, String version ) + throws Exception; + + //TODO + // consider the following as additional services: + // - getAllConfiguredRepositoryConsumers( String repoId ) - list all enabled consumers for the repo + // - getAllConfiguredDatabaseConsumers() - list all enabled db consumers } diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/pom.xml b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/pom.xml index b657132b0..8d68c8df3 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/pom.xml +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/pom.xml @@ -40,6 +40,10 @@ org.apache.archiva archiva-configuration + + org.apache.archiva + archiva-scheduled + javax.servlet servlet-api diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java index 7400f48f1..17a4be093 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java @@ -35,7 +35,7 @@ public class AdministrationServiceImpl */ private ArchivaConfiguration archivaConfiguration; - public boolean configureDatabaseConsumer( String consumerId, boolean enable ) + public boolean configureDatabaseConsumer( String consumerId, boolean enable ) throws Exception { //Configuration config = archivaConfiguration.getConfiguration(); @@ -44,25 +44,27 @@ public class AdministrationServiceImpl return false; } - public boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable ) + public boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable ) throws Exception { // TODO Auto-generated method stub return false; } - public boolean deleteArtifact( String repoId, String groupId, String artifactId, String version ) + public boolean deleteArtifact( String repoId, String groupId, String artifactId, String version ) throws Exception { + // TODO implement delete artifact in Archiva + // TODO Auto-generated method stub return false; } - public boolean executeDatabaseScanner() + public boolean executeDatabaseScanner() throws Exception { // TODO Auto-generated method stub return false; } - public boolean executeRepositoryScanner( String repoId ) + public boolean executeRepositoryScanner( String repoId ) throws Exception { // TODO Auto-generated method stub return false; diff --git a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java index 17f15b08e..ca1cf4fea 100644 --- a/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java +++ b/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java @@ -31,6 +31,9 @@ import org.apache.maven.archiva.configuration.DatabaseScanningConfiguration; import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration; import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration; import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration; +import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler; +import org.apache.maven.archiva.scheduled.tasks.DatabaseTask; +import org.apache.maven.archiva.scheduled.tasks.RepositoryTask; import org.codehaus.plexus.spring.PlexusInSpringTestCase; import org.easymock.MockControl; import org.easymock.classextension.MockClassControl; @@ -52,6 +55,10 @@ public class AdministrationServiceImplTest private AdministrationService service; + private MockControl taskSchedulerControl; + + private ArchivaTaskScheduler taskScheduler; + protected void setUp() throws Exception { @@ -63,10 +70,14 @@ public class AdministrationServiceImplTest configControl = MockClassControl.createControl( Configuration.class ); config = ( Configuration ) configControl.getMock(); + taskSchedulerControl = MockControl.createControl( ArchivaTaskScheduler.class ); + taskScheduler = ( ArchivaTaskScheduler ) taskSchedulerControl.getMock(); + service = new AdministrationServiceImpl(); } - - public void testConfigureValidDatabaseConsumer() + + // DATABASE CONSUMERS + public void testGetAllDbConsumers() throws Exception { /*DatabaseScanningConfiguration dbScanning = new DatabaseScanningConfiguration(); @@ -75,6 +86,36 @@ public class AdministrationServiceImplTest dbScanning.addUnprocessedConsumer( "unprocessed-artifacts" ); dbScanning.addUnprocessedConsumer( "unprocessed-poms" ); + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning ); + + archivaConfigControl.replay(); + configControl.replay(); + + List dbConsumers = service.getAllDatabaseConsumers(); + + archivaConfigControl.verify(); + configControl.verify(); + + assertNotNull( dbConsumers ); + assertEquals( 4, dbConsumers.size() ); + assertTrue( dbConsumers.contains( "cleanup-index" ) ); + assertTrue( dbConsumers.contains( "cleanup-database" ) ); + assertTrue( dbConsumers.contains( "unprocessed-artifacts" ) ); + assertTrue( dbConsumers.contains( "unprocessed-poms" ) );*/ + } + +/* public void testConfigureValidDatabaseConsumer() + throws Exception + { + DatabaseScanningConfiguration dbScanning = new DatabaseScanningConfiguration(); + dbScanning.addCleanupConsumer( "cleanup-index" ); + dbScanning.addCleanupConsumer( "cleanup-database" ); + dbScanning.addUnprocessedConsumer( "unprocessed-artifacts" ); + dbScanning.addUnprocessedConsumer( "unprocessed-poms" ); + + //TODO mock checking whether the db consumer is valid or not + // test enable archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning ); @@ -89,17 +130,25 @@ public class AdministrationServiceImplTest archivaConfigControl.replay(); configControl.replay(); - boolean success = service.configureDatabaseConsumer( "new-cleanup-consumer", true ); + try + { + boolean success = service.configureDatabaseConsumer( "new-cleanup-consumer", true ); + assertTrue( success ); + } + catch ( Exception e ) + { + fail( "An exception should not have been thrown." ); + } archivaConfigControl.verify(); configControl.verify(); - - assertTrue( success ); - + // test disable archivaConfigControl.reset(); configControl.reset(); + //TODO mock checking whether the db consumer is valid or not + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning ); @@ -113,130 +162,323 @@ public class AdministrationServiceImplTest archivaConfigControl.replay(); configControl.replay(); - success = service.configureDatabaseConsumer( "new-cleanup-consumer", false ); + try + { + boolean success = service.configureDatabaseConsumer( "new-cleanup-consumer", false ); + assertTrue( success ); + } + catch ( Exception e ) + { + fail( "An exception should not have been thrown." ); + } archivaConfigControl.verify(); configControl.verify(); - - assertTrue( success );*/ } public void testConfigureInvalidDatabaseConsumer() throws Exception { + //TODO mock checking whether the db consumer is valid or not + + try + { + service.configureDatabaseConsumer( "invalid-consumer", true ); + fail( "An exception should have been thrown." ); + } + catch ( Exception e ) + { + assertEquals( "Invalid database consumer.", e.getMessage() ); + } + } + + // REPOSITORY CONSUMERS + public void testGetAllRepoConsumers() + throws Exception + { + RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration(); + repoScanning.addKnownContentConsumer( "index-artifacts" ); + repoScanning.addKnownContentConsumer( "index-poms" ); + repoScanning.addKnownContentConsumer( "fix-checksums" ); + repoScanning.addInvalidContentConsumer( "check-poms" ); + repoScanning.addInvalidContentConsumer( "check-metadata" ); + + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning ); + archivaConfigControl.replay(); + configControl.replay(); + + List repoConsumers = service.getAllDatabaseConsumers(); + + archivaConfigControl.verify(); + configControl.verify(); + + assertNotNull( repoConsumers ); + assertEquals( 5, repoConsumers.size() ); + assertTrue( repoConsumers.contains( "index-artifacts" ) ); + assertTrue( repoConsumers.contains( "index-poms" ) ); + assertTrue( repoConsumers.contains( "fix-checksums" ) ); + assertTrue( repoConsumers.contains( "check-poms" ) ); + assertTrue( repoConsumers.contains( "check-metadata" ) ); } public void testConfigureValidRepositoryConsumer() throws Exception { - // test enable & disable + //TODO mock checking whether the repo consumer is valid or not + + RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration(); + repoScanning.addKnownContentConsumer( "index-artifacts" ); + repoScanning.addKnownContentConsumer( "index-poms" ); + repoScanning.addKnownContentConsumer( "fix-checksums" ); + repoScanning.addInvalidContentConsumer( "check-poms" ); + repoScanning.addInvalidContentConsumer( "check-metadata" ); + + // test enable + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning ); + + config.setRepositoryScanning( repoScanning ); + + configControl.setMatcher( MockControl.ALWAYS_MATCHER ); + configControl.setVoidCallable(); + + archivaConfig.save( config ); + archivaConfigControl.setVoidCallable(); + + archivaConfigControl.replay(); + configControl.replay(); + + try + { + boolean success = service.configureRepositoryConsumer( null, "new-repo-consumer", true ); + assertTrue( success ); + } + catch ( Exception e ) + { + fail( "An exception should not have been thrown." ); + } + + archivaConfigControl.verify(); + configControl.verify(); + + // test disable + archivaConfigControl.reset(); + configControl.reset(); + + //TODO mock checking whether the repo consumer is valid or not + + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning ); + + config.setRepositoryScanning( repoScanning ); + configControl.setMatcher( MockControl.ALWAYS_MATCHER ); + configControl.setVoidCallable(); + + archivaConfig.save( config ); + archivaConfigControl.setVoidCallable(); + + archivaConfigControl.replay(); + configControl.replay(); + + try + { + boolean success = service.configureRepositoryConsumer( null, "new-repo-consumer", false ); + assertTrue( success ); + } + catch ( Exception e ) + { + fail( "An excecption should not have been thrown." ); + } + + archivaConfigControl.verify(); + configControl.verify(); } public void testConfigureInvalidRepositoryConsumer() throws Exception { - + //TODO mock checking whether the repo consumer is valid or not + + try + { + service.configureRepositoryConsumer( null, "invalid-consumer", true ); + fail( "An exception should have been thrown." ); + } + catch ( Exception e ) + { + assertEquals( "Invalid database consumer.", e.getMessage() ); + } } + +// DELETE ARTIFACT public void testDeleteArtifactArtifactExists() throws Exception { - + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), + createManagedRepo( "internal", "default", "Internal Repository", true, false ) ); + + // TODO + // - mock checking of artifact existence in the repo + // - mock artifact delete + + archivaConfigControl.replay(); + configControl.replay(); + + try + { + boolean success = service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" ); + assertTrue( success ); + } + catch ( Exception e ) + { + fail( "An exception should not have been thrown." ); + } + + archivaConfigControl.verify(); + configControl.verify(); } public void testDeleteArtifactArtifactDoesNotExist() throws Exception { - + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), + createManagedRepo( "internal", "default", "Internal Repository", true, false ) ); + + // TODO mock checking of artifact existence in the repo + + archivaConfigControl.replay(); + configControl.replay(); + + try + { + service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" ); + fail( "An exception should have been thrown." ); + } + catch ( Exception e ) + { + assertEquals( "Artifact does not exist.", e.getMessage() ); + } + + archivaConfigControl.verify(); + configControl.verify(); } public void testDeleteArtifacRepositoryDoesNotExist() throws Exception { - - } - - public void testExecuteRepoScannerRepoExists() - throws Exception - { - + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), null ); + + archivaConfigControl.replay(); + configControl.replay(); + + try + { + service.deleteArtifact( "internal", "org.apache.archiva", "archiva-test", "1.0" ); + fail( "An exception should have been thrown." ); + } + catch ( Exception e ) + { + assertEquals( "Repository does not exist.", e.getMessage() ); + } + + archivaConfigControl.verify(); + configControl.verify(); } - public void testExecuteRepoScannerRepoDoesNotExist() - throws Exception - { - - } +// REPO SCANNING - public void testExecuteDbScanner() + public void testExecuteRepoScannerRepoExists() throws Exception - { + { + archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); + configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), + createManagedRepo( "internal", "default", "Internal Repository", true, false ) ); - } - - public void testGetAllDbConsumers() - throws Exception - { - /*DatabaseScanningConfiguration dbScanning = new DatabaseScanningConfiguration(); - dbScanning.addCleanupConsumer( "cleanup-index" ); - dbScanning.addCleanupConsumer( "cleanup-database" ); - dbScanning.addUnprocessedConsumer( "unprocessed-artifacts" ); - dbScanning.addUnprocessedConsumer( "unprocessed-poms" ); + RepositoryTask task = new RepositoryTask(); - archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); - configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning ); + taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingAnyRepositoryTask(), false ); + taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingRepositoryTask( "internal" ), false ); + + taskScheduler.queueRepositoryTask( task ); + taskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER ); + taskSchedulerControl.setVoidCallable(); archivaConfigControl.replay(); configControl.replay(); - - List dbConsumers = service.getAllDatabaseConsumers(); + taskSchedulerControl.replay(); + + try + { + boolean success = service.executeRepositoryScanner( "internal" ); + assertTrue( success ); + } + catch ( Exception e ) + { + fail( "An exception should not have been thrown." ); + } archivaConfigControl.verify(); configControl.verify(); - - assertNotNull( dbConsumers ); - assertEquals( 4, dbConsumers.size() ); - assertTrue( dbConsumers.contains( "cleanup-index" ) ); - assertTrue( dbConsumers.contains( "cleanup-database" ) ); - assertTrue( dbConsumers.contains( "unprocessed-artifacts" ) ); - assertTrue( dbConsumers.contains( "unprocessed-poms" ) );*/ + taskSchedulerControl.verify(); } - public void testGetAllRepoConsumers() + public void testExecuteRepoScannerRepoDoesNotExist() throws Exception { - /*RepositoryScanningConfiguration repoScanning = new RepositoryScanningConfiguration(); - repoScanning.addKnownContentConsumer( "index-artifacts" ); - repoScanning.addKnownContentConsumer( "index-poms" ); - repoScanning.addKnownContentConsumer( "fix-checksums" ); - repoScanning.addInvalidContentConsumer( "check-poms" ); - repoScanning.addInvalidContentConsumer( "check-metadata" ); - archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); - configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning ); + configControl.expectAndReturn( config.findManagedRepositoryById( "internal" ), null ); archivaConfigControl.replay(); configControl.replay(); - - List repoConsumers = service.getAllDatabaseConsumers(); + + try + { + service.executeRepositoryScanner( "internal" ); + fail( "An exception should have been thrown." ); + } + catch ( Exception e ) + { + assertEquals( "Repository does not exist.", e.getMessage() ); + } archivaConfigControl.verify(); configControl.verify(); + } + + // DATABASE SCANNING + + public void testExecuteDbScanner() + throws Exception + { + DatabaseTask task = new DatabaseTask(); - assertNotNull( repoConsumers ); - assertEquals( 5, repoConsumers.size() ); - assertTrue( repoConsumers.contains( "index-artifacts" ) ); - assertTrue( repoConsumers.contains( "index-poms" ) ); - assertTrue( repoConsumers.contains( "fix-checksums" ) ); - assertTrue( repoConsumers.contains( "check-poms" ) ); - assertTrue( repoConsumers.contains( "check-metadata" ) );*/ + taskSchedulerControl.expectAndReturn( taskScheduler.isProcessingDatabaseTask(), false ); + + taskScheduler.queueDatabaseTask( task ); + taskSchedulerControl.setMatcher( MockControl.ALWAYS_MATCHER ); + taskSchedulerControl.setVoidCallable(); + + taskSchedulerControl.replay(); + + boolean success = service.executeDatabaseScanner(); + + taskSchedulerControl.verify(); + + assertTrue( success ); } + + // REPOSITORIES public void testGetAllManagedRepositories() throws Exception { - /*List managedRepos = new ArrayList(); + List managedRepos = new ArrayList(); managedRepos.add( createManagedRepo( "internal", "default", "Internal Repository", true, false ) ); managedRepos.add( createManagedRepo( "snapshots", "default", "Snapshots Repository", false, true ) ); @@ -255,13 +497,13 @@ public class AdministrationServiceImplTest assertEquals( 2, repos.size() ); assertManagedRepo( ( ManagedRepository ) repos.get( 0 ), managedRepos.get( 0 ) ); - assertManagedRepo( ( ManagedRepository ) repos.get( 1 ), managedRepos.get( 1 ) );*/ + assertManagedRepo( ( ManagedRepository ) repos.get( 1 ), managedRepos.get( 1 ) ); } public void testGetAllRemoteRepositories() throws Exception { - /*List remoteRepos = new ArrayList(); + List remoteRepos = new ArrayList(); remoteRepos.add( createRemoteRepository( "central", "Central Repository", "default", "http://repo1.maven.org/maven2") ); remoteRepos.add( createRemoteRepository( "dummy", "Dummy Remote Repository", "legacy", "http://dummy.com/dummy") ); @@ -280,9 +522,9 @@ public class AdministrationServiceImplTest assertEquals( 2, repos.size() ); assertRemoteRepo( (RemoteRepository) repos.get( 0 ), remoteRepos.get( 0 ) ); - assertRemoteRepo( (RemoteRepository) repos.get( 1 ), remoteRepos.get( 1 ) ); */ + assertRemoteRepo( (RemoteRepository) repos.get( 1 ), remoteRepos.get( 1 ) ); } - +*/ private void assertRemoteRepo( RemoteRepository remoteRepo, RemoteRepositoryConfiguration expectedRepoConfig ) { assertEquals( expectedRepoConfig.getId(), remoteRepo.getId() ); -- 2.39.5