From 6fbd28176b912c4815b845184cf4e7d949e033b1 Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Fri, 10 Oct 2008 01:31:13 +0000 Subject: [PATCH] -implemented getAllDatabaseConsumers() and configureDatabaseConsumer() in admin service -activated tests for these 2 methods git-svn-id: https://svn.apache.org/repos/asf/archiva/branches/MRM-124@703311 13f79535-47bb-0310-9956-ffa450edef68 --- .../services/AdministrationServiceImpl.java | 120 +++++++++-- .../AdministrationServiceImplTest.java | 194 +++++++++++++----- 2 files changed, 241 insertions(+), 73 deletions(-) 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 ea3562310..2ccf33d21 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 @@ -30,11 +30,19 @@ import org.apache.maven.archiva.configuration.Configuration; import org.apache.maven.archiva.configuration.DatabaseScanningConfiguration; import org.apache.maven.archiva.configuration.IndeterminateConfigurationException; import org.apache.maven.archiva.configuration.RepositoryScanningConfiguration; +import org.apache.maven.archiva.consumers.DatabaseCleanupConsumer; +import org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer; import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer; import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; +import org.apache.maven.archiva.database.updater.DatabaseConsumers; import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers; import org.codehaus.plexus.registry.RegistryException; +/** + * AdministrationServiceImpl + * + * @version $Id: AdministrationServiceImpl.java + */ public class AdministrationServiceImpl implements AdministrationService { @@ -43,14 +51,64 @@ public class AdministrationServiceImpl */ private ArchivaConfiguration archivaConfiguration; - private RepositoryContentConsumers consumerUtil; + private RepositoryContentConsumers repoConsumersUtil; + + private DatabaseConsumers dbConsumersUtil; + /** + * @see AdministrationService#configureDatabaseConsumer(String, boolean) + */ public boolean configureDatabaseConsumer( String consumerId, boolean enable ) throws Exception { - //Configuration config = archivaConfiguration.getConfiguration(); - - // TODO Auto-generated method stub - return false; + List cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers(); + List unprocessedConsumers = + dbConsumersUtil.getAvailableUnprocessedConsumers(); + + boolean found = false; + boolean isCleanupConsumer = false; + for( DatabaseCleanupConsumer consumer : cleanupConsumers ) + { + if( consumer.getId().equals( consumerId ) ) + { + found = true; + isCleanupConsumer = true; + break; + } + } + + if( !found ) + { + for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers ) + { + if( consumer.getId().equals( consumerId ) ) + { + found = true; + break; + } + } + } + + if( !found ) + { + throw new Exception( "Invalid database consumer." ); + } + + Configuration config = archivaConfiguration.getConfiguration(); + DatabaseScanningConfiguration dbScanningConfig = config.getDatabaseScanning(); + + if( isCleanupConsumer ) + { + dbScanningConfig.addCleanupConsumer( consumerId ); + } + else + { + dbScanningConfig.addUnprocessedConsumer( consumerId ); + } + + config.setDatabaseScanning( dbScanningConfig ); + saveConfiguration( config ); + + return true; } /** @@ -59,8 +117,10 @@ public class AdministrationServiceImpl public boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable ) throws Exception { - List knownConsumers = consumerUtil.getAvailableKnownConsumers(); - List invalidConsumers = consumerUtil.getAvailableInvalidConsumers(); + // TODO use repoId once consumers are configured per repository! (MRM-930) + + List knownConsumers = repoConsumersUtil.getAvailableKnownConsumers(); + List invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers(); boolean found = false; boolean isKnownContentConsumer = false; @@ -109,6 +169,9 @@ public class AdministrationServiceImpl return true; } + /** + * @see AdministrationService#deleteArtifact(String, String, String, String) + */ public boolean deleteArtifact( String repoId, String groupId, String artifactId, String version ) throws Exception { // TODO implement delete artifact in Archiva @@ -117,12 +180,18 @@ public class AdministrationServiceImpl return false; } + /** + * @see AdministrationService#executeDatabaseScanner() + */ public boolean executeDatabaseScanner() throws Exception { // TODO Auto-generated method stub return false; } + /** + * @see AdministrationService#executeRepositoryScanner(String) + */ public boolean executeRepositoryScanner( String repoId ) throws Exception { // TODO Auto-generated method stub @@ -136,6 +205,19 @@ public class AdministrationServiceImpl { List consumers = new ArrayList(); + List cleanupConsumers = dbConsumersUtil.getAvailableCleanupConsumers(); + List unprocessedConsumers = dbConsumersUtil.getAvailableUnprocessedConsumers(); + + for( DatabaseCleanupConsumer consumer : cleanupConsumers ) + { + consumers.add( consumer.getId() ); + } + + for( DatabaseUnprocessedArtifactConsumer consumer : unprocessedConsumers ) + { + consumers.add( consumer.getId() ); + } + return consumers; } @@ -146,8 +228,8 @@ public class AdministrationServiceImpl { List consumers = new ArrayList(); - List knownConsumers = consumerUtil.getAvailableKnownConsumers(); - List invalidConsumers = consumerUtil.getAvailableInvalidConsumers(); + List knownConsumers = repoConsumersUtil.getAvailableKnownConsumers(); + List invalidConsumers = repoConsumersUtil.getAvailableInvalidConsumers(); for( KnownRepositoryContentConsumer consumer : knownConsumers ) { @@ -162,11 +244,17 @@ public class AdministrationServiceImpl return consumers; } + /** + * @see AdministrationService#getAllManagedRepositories() + */ public List getAllManagedRepositories() { return null; } + /** + * @see AdministrationService#getAllRemoteRepositories() + */ public List getAllRemoteRepositories() { return null; @@ -194,13 +282,13 @@ public class AdministrationServiceImpl this.archivaConfiguration = archivaConfiguration; } - public RepositoryContentConsumers getConsumerUtil() - { - return consumerUtil; - } - - public void setConsumerUtil( RepositoryContentConsumers consumerUtil ) + public void setRepoConsumersUtil( RepositoryContentConsumers consumerUtil ) { - this.consumerUtil = consumerUtil; + this.repoConsumersUtil = consumerUtil; } + + public void setDbConsumersUtil( DatabaseConsumers consumerUtil ) + { + this.dbConsumersUtil = consumerUtil; + } } 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 621fcf8f1..aa4acc2de 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,8 +31,11 @@ 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.consumers.DatabaseCleanupConsumer; +import org.apache.maven.archiva.consumers.DatabaseUnprocessedArtifactConsumer; import org.apache.maven.archiva.consumers.InvalidRepositoryContentConsumer; import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; +import org.apache.maven.archiva.database.updater.DatabaseConsumers; import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers; import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler; import org.apache.maven.archiva.scheduled.tasks.DatabaseTask; @@ -42,8 +45,9 @@ import org.easymock.MockControl; import org.easymock.classextension.MockClassControl; /** + * AdministrationServiceImplTest * - * @version $Id$ + * @version $Id: AdministrationServiceImplTest.java */ public class AdministrationServiceImplTest extends PlexusInSpringTestCase @@ -62,10 +66,11 @@ public class AdministrationServiceImplTest private ArchivaTaskScheduler taskScheduler; - private MockControl consumerUtilControl; + // repository consumers + private MockControl repoConsumerUtilsControl; + + private RepositoryContentConsumers repoConsumersUtil; - private RepositoryContentConsumers consumerUtil; - private MockControl knownContentConsumerControl; private MockControl invalidContentConsumerControl; @@ -77,6 +82,23 @@ public class AdministrationServiceImplTest private InvalidRepositoryContentConsumer checkPomConsumer; private InvalidRepositoryContentConsumer checkMetadataConsumer; + + // database consumers + private MockControl dbConsumersUtilControl; + + private DatabaseConsumers dbConsumersUtil; + + private MockControl unprocessedConsumersControl; + + private MockControl cleanupConsumersControl; + + private DatabaseUnprocessedArtifactConsumer processArtifactConsumer; + + private DatabaseUnprocessedArtifactConsumer processPomConsumer; + + private DatabaseCleanupConsumer cleanupIndexConsumer; + + private DatabaseCleanupConsumer cleanupDbConsumer; protected void setUp() throws Exception @@ -92,8 +114,9 @@ public class AdministrationServiceImplTest taskSchedulerControl = MockControl.createControl( ArchivaTaskScheduler.class ); taskScheduler = ( ArchivaTaskScheduler ) taskSchedulerControl.getMock(); - consumerUtilControl = MockClassControl.createControl( RepositoryContentConsumers.class ); - consumerUtil = ( RepositoryContentConsumers ) consumerUtilControl.getMock(); + // repo consumers + repoConsumerUtilsControl = MockClassControl.createControl( RepositoryContentConsumers.class ); + repoConsumersUtil = ( RepositoryContentConsumers ) repoConsumerUtilsControl.getMock(); knownContentConsumerControl = MockControl.createControl( KnownRepositoryContentConsumer.class ); indexArtifactConsumer = ( KnownRepositoryContentConsumer ) knownContentConsumerControl.getMock(); @@ -103,40 +126,47 @@ public class AdministrationServiceImplTest checkPomConsumer = ( InvalidRepositoryContentConsumer ) invalidContentConsumerControl.getMock(); checkMetadataConsumer = ( InvalidRepositoryContentConsumer ) invalidContentConsumerControl.getMock(); + // db consumers + dbConsumersUtilControl = MockClassControl.createControl( DatabaseConsumers.class ); + dbConsumersUtil = ( DatabaseConsumers ) dbConsumersUtilControl.getMock(); + + cleanupConsumersControl = MockControl.createControl( DatabaseCleanupConsumer.class ); + cleanupIndexConsumer = ( DatabaseCleanupConsumer ) cleanupConsumersControl.getMock(); + cleanupDbConsumer = ( DatabaseCleanupConsumer ) cleanupConsumersControl.getMock(); + + unprocessedConsumersControl = MockControl.createControl( DatabaseUnprocessedArtifactConsumer.class ); + processArtifactConsumer = ( DatabaseUnprocessedArtifactConsumer ) unprocessedConsumersControl.getMock(); + processPomConsumer = ( DatabaseUnprocessedArtifactConsumer ) unprocessedConsumersControl.getMock(); + service = new AdministrationServiceImpl(); service.setArchivaConfiguration( archivaConfig ); - service.setConsumerUtil( consumerUtil ); + service.setRepoConsumersUtil( repoConsumersUtil ); + service.setDbConsumersUtil( dbConsumersUtil ); } - - // DATABASE CONSUMERS - /* public void testGetAllDbConsumers() + +/* Tests for database consumers */ + + 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" ); - - archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); - configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning ); - - archivaConfigControl.replay(); - configControl.replay(); + recordDbConsumers(); + dbConsumersUtilControl.replay(); + cleanupConsumersControl.replay(); + unprocessedConsumersControl.replay(); + List dbConsumers = service.getAllDatabaseConsumers(); - List dbConsumers = service.getAllDatabaseConsumers(); - - archivaConfigControl.verify(); - configControl.verify(); + dbConsumersUtilControl.verify(); + cleanupConsumersControl.verify(); + unprocessedConsumersControl.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" ) ); + assertTrue( dbConsumers.contains( "process-artifact" ) ); + assertTrue( dbConsumers.contains( "process-pom" ) ); } public void testConfigureValidDatabaseConsumer() @@ -145,12 +175,11 @@ public class AdministrationServiceImplTest 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 + dbScanning.addUnprocessedConsumer( "process-artifact" ); + + recordDbConsumers(); - // test enable + // test enable "process-pom" db consumer archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning ); @@ -161,12 +190,15 @@ public class AdministrationServiceImplTest archivaConfig.save( config ); archivaConfigControl.setVoidCallable(); + dbConsumersUtilControl.replay(); + cleanupConsumersControl.replay(); + unprocessedConsumersControl.replay(); archivaConfigControl.replay(); configControl.replay(); try { - boolean success = service.configureDatabaseConsumer( "new-cleanup-consumer", true ); + boolean success = service.configureDatabaseConsumer( "process-pom", true ); assertTrue( success ); } catch ( Exception e ) @@ -174,14 +206,22 @@ public class AdministrationServiceImplTest fail( "An exception should not have been thrown." ); } + dbConsumersUtilControl.verify(); + cleanupConsumersControl.verify(); + unprocessedConsumersControl.verify(); archivaConfigControl.verify(); configControl.verify(); - // test disable + // test disable "process-pom" db consumer + dbConsumersUtilControl.reset(); + cleanupConsumersControl.reset(); + unprocessedConsumersControl.reset(); archivaConfigControl.reset(); configControl.reset(); + + dbScanning.addUnprocessedConsumer( "process-pom" ); - //TODO mock checking whether the db consumer is valid or not + recordDbConsumers(); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); configControl.expectAndReturn( config.getDatabaseScanning(), dbScanning ); @@ -193,12 +233,15 @@ public class AdministrationServiceImplTest archivaConfig.save( config ); archivaConfigControl.setVoidCallable(); + dbConsumersUtilControl.replay(); + cleanupConsumersControl.replay(); + unprocessedConsumersControl.replay(); archivaConfigControl.replay(); configControl.replay(); try { - boolean success = service.configureDatabaseConsumer( "new-cleanup-consumer", false ); + boolean success = service.configureDatabaseConsumer( "process-pom", false ); assertTrue( success ); } catch ( Exception e ) @@ -206,6 +249,9 @@ public class AdministrationServiceImplTest fail( "An exception should not have been thrown." ); } + dbConsumersUtilControl.verify(); + cleanupConsumersControl.verify(); + unprocessedConsumersControl.verify(); archivaConfigControl.verify(); configControl.verify(); } @@ -213,7 +259,11 @@ public class AdministrationServiceImplTest public void testConfigureInvalidDatabaseConsumer() throws Exception { - //TODO mock checking whether the db consumer is valid or not + recordDbConsumers(); + + dbConsumersUtilControl.replay(); + cleanupConsumersControl.replay(); + unprocessedConsumersControl.replay(); try { @@ -224,23 +274,26 @@ public class AdministrationServiceImplTest { assertEquals( "Invalid database consumer.", e.getMessage() ); } + + dbConsumersUtilControl.verify(); + cleanupConsumersControl.verify(); + unprocessedConsumersControl.verify(); } + +/* Tests for repository consumers */ - */ - - // REPOSITORY CONSUMERS public void testGetAllRepoConsumers() throws Exception { - recordRepoConsumerValidation(); + recordRepoConsumers(); - consumerUtilControl.replay(); + repoConsumerUtilsControl.replay(); knownContentConsumerControl.replay(); invalidContentConsumerControl.replay(); List repoConsumers = service.getAllRepositoryConsumers(); - consumerUtilControl.verify(); + repoConsumerUtilsControl.verify(); knownContentConsumerControl.verify(); invalidContentConsumerControl.verify(); @@ -261,7 +314,7 @@ public class AdministrationServiceImplTest repoScanning.addInvalidContentConsumer( "check-pom" ); // test enable "check-metadata" consumer - recordRepoConsumerValidation(); + recordRepoConsumers(); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning ); @@ -273,7 +326,7 @@ public class AdministrationServiceImplTest archivaConfig.save( config ); archivaConfigControl.setVoidCallable(); - consumerUtilControl.replay(); + repoConsumerUtilsControl.replay(); knownContentConsumerControl.replay(); invalidContentConsumerControl.replay(); archivaConfigControl.replay(); @@ -289,14 +342,14 @@ public class AdministrationServiceImplTest fail( "An exception should not have been thrown." ); } - consumerUtilControl.verify(); + repoConsumerUtilsControl.verify(); knownContentConsumerControl.verify(); invalidContentConsumerControl.verify(); archivaConfigControl.verify(); configControl.verify(); // test disable "check-metadata" consumer - consumerUtilControl.reset(); + repoConsumerUtilsControl.reset(); knownContentConsumerControl.reset(); invalidContentConsumerControl.reset(); archivaConfigControl.reset(); @@ -304,7 +357,7 @@ public class AdministrationServiceImplTest repoScanning.addInvalidContentConsumer( "check-metadata" ); - recordRepoConsumerValidation(); + recordRepoConsumers(); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning ); @@ -316,7 +369,7 @@ public class AdministrationServiceImplTest archivaConfig.save( config ); archivaConfigControl.setVoidCallable(); - consumerUtilControl.replay(); + repoConsumerUtilsControl.replay(); knownContentConsumerControl.replay(); invalidContentConsumerControl.replay(); archivaConfigControl.replay(); @@ -326,7 +379,7 @@ public class AdministrationServiceImplTest { boolean success = service.configureRepositoryConsumer( null, "check-metadata", false ); - consumerUtilControl.verify(); + repoConsumerUtilsControl.verify(); knownContentConsumerControl.verify(); invalidContentConsumerControl.verify(); archivaConfigControl.verify(); @@ -340,11 +393,15 @@ public class AdministrationServiceImplTest } } - /* + public void testConfigureInvalidRepositoryConsumer() throws Exception { - //TODO mock checking whether the repo consumer is valid or not + recordRepoConsumers(); + + repoConsumerUtilsControl.replay(); + knownContentConsumerControl.replay(); + invalidContentConsumerControl.replay(); try { @@ -355,11 +412,15 @@ public class AdministrationServiceImplTest { assertEquals( "Invalid repository consumer.", e.getMessage() ); } + + repoConsumerUtilsControl.verify(); + knownContentConsumerControl.verify(); + invalidContentConsumerControl.verify(); } -// DELETE ARTIFACT +/* Tests for delete artifact */ - public void testDeleteArtifactArtifactExists() + /* public void testDeleteArtifactArtifactExists() throws Exception { archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); @@ -612,7 +673,7 @@ public class AdministrationServiceImplTest return repoConfig; } - private void recordRepoConsumerValidation() + private void recordRepoConsumers() { List availableKnownConsumers = new ArrayList(); availableKnownConsumers.add( indexArtifactConsumer ); @@ -622,13 +683,32 @@ public class AdministrationServiceImplTest availableInvalidConsumers.add( checkPomConsumer ); availableInvalidConsumers.add( checkMetadataConsumer ); - consumerUtilControl.expectAndReturn( consumerUtil.getAvailableKnownConsumers(), availableKnownConsumers ); + repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableKnownConsumers(), availableKnownConsumers ); knownContentConsumerControl.expectAndReturn( indexArtifactConsumer.getId(), "index-artifact" ); knownContentConsumerControl.expectAndReturn( indexPomConsumer.getId(), "index-pom" ); - consumerUtilControl.expectAndReturn( consumerUtil.getAvailableInvalidConsumers(), availableInvalidConsumers ); + repoConsumerUtilsControl.expectAndReturn( repoConsumersUtil.getAvailableInvalidConsumers(), availableInvalidConsumers ); invalidContentConsumerControl.expectAndReturn( checkPomConsumer.getId(), "check-pom" ); invalidContentConsumerControl.expectAndReturn( checkMetadataConsumer.getId(), "check-metadata" ); } + private void recordDbConsumers() + { + List cleanupConsumers = new ArrayList(); + cleanupConsumers.add( cleanupIndexConsumer ); + cleanupConsumers.add( cleanupDbConsumer ); + + List unprocessedConsumers = + new ArrayList(); + unprocessedConsumers.add( processArtifactConsumer ); + unprocessedConsumers.add( processPomConsumer ); + + dbConsumersUtilControl.expectAndReturn( dbConsumersUtil.getAvailableCleanupConsumers(), cleanupConsumers ); + cleanupConsumersControl.expectAndReturn( cleanupIndexConsumer.getId(), "cleanup-index" ); + cleanupConsumersControl.expectAndReturn( cleanupDbConsumer.getId(), "cleanup-database" ); + + dbConsumersUtilControl.expectAndReturn( dbConsumersUtil.getAvailableUnprocessedConsumers(), unprocessedConsumers ); + unprocessedConsumersControl.expectAndReturn( processArtifactConsumer.getId(), "process-artifact" ); + unprocessedConsumersControl.expectAndReturn( processPomConsumer.getId(), "process-pom" ); + } } \ No newline at end of file -- 2.39.5