From a5c6bf611950cb011f3d2bea044ccbd2b297d63d Mon Sep 17 00:00:00 2001 From: "Maria Odea B. Ching" Date: Fri, 10 Oct 2008 00:32:57 +0000 Subject: [PATCH] implemented getAllRepositoryConsumers() and configureRepositoryConsumer() in admin service git-svn-id: https://svn.apache.org/repos/asf/archiva/branches@703301 13f79535-47bb-0310-9956-ffa450edef68 --- .../services/AdministrationServiceImpl.java | 132 ++++++++++++-- .../AdministrationServiceImplTest.java | 168 ++++++++++++------ 2 files changed, 238 insertions(+), 62 deletions(-) diff --git a/MRM-124/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java b/MRM-124/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java index 17a4be093..ea3562310 100644 --- a/MRM-124/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java +++ b/MRM-124/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java @@ -19,6 +19,7 @@ package org.apache.archiva.web.xmlrpc.services; * under the License. */ +import java.util.ArrayList; import java.util.List; import org.apache.archiva.web.xmlrpc.api.AdministrationService; @@ -26,6 +27,13 @@ import org.apache.archiva.web.xmlrpc.api.beans.ManagedRepository; import org.apache.archiva.web.xmlrpc.api.beans.RemoteRepository; import org.apache.maven.archiva.configuration.ArchivaConfiguration; 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.InvalidRepositoryContentConsumer; +import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; +import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers; +import org.codehaus.plexus.registry.RegistryException; public class AdministrationServiceImpl implements AdministrationService @@ -35,21 +43,72 @@ public class AdministrationServiceImpl */ private ArchivaConfiguration archivaConfiguration; + private RepositoryContentConsumers consumerUtil; + public boolean configureDatabaseConsumer( String consumerId, boolean enable ) throws Exception { //Configuration config = archivaConfiguration.getConfiguration(); - - + // TODO Auto-generated method stub return false; } - public boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable ) throws Exception + /** + * @see AdministrationService#configureRepositoryConsumer(String, String, boolean) + */ + public boolean configureRepositoryConsumer( String repoId, String consumerId, boolean enable ) + throws Exception { - // TODO Auto-generated method stub - return false; + List knownConsumers = consumerUtil.getAvailableKnownConsumers(); + List invalidConsumers = consumerUtil.getAvailableInvalidConsumers(); + + boolean found = false; + boolean isKnownContentConsumer = false; + for( KnownRepositoryContentConsumer consumer : knownConsumers ) + { + if( consumer.getId().equals( consumerId ) ) + { + found = true; + isKnownContentConsumer = true; + break; + } + } + + if( !found ) + { + for( InvalidRepositoryContentConsumer consumer : invalidConsumers ) + { + if( consumer.getId().equals( consumerId ) ) + { + found = true; + break; + } + } + } + + if( !found ) + { + throw new Exception( "Invalid repository consumer." ); + } + + Configuration config = archivaConfiguration.getConfiguration(); + RepositoryScanningConfiguration repoScanningConfig = config.getRepositoryScanning(); + + if( isKnownContentConsumer ) + { + repoScanningConfig.addKnownContentConsumer( consumerId ); + } + else + { + repoScanningConfig.addInvalidContentConsumer( consumerId ); + } + + config.setRepositoryScanning( repoScanningConfig ); + saveConfiguration( config ); + + return true; } - + public boolean deleteArtifact( String repoId, String groupId, String artifactId, String version ) throws Exception { // TODO implement delete artifact in Archiva @@ -70,16 +129,37 @@ public class AdministrationServiceImpl return false; } + /** + * @see AdministrationService#getAllDatabaseConsumers() + */ public List getAllDatabaseConsumers() { - // TODO Auto-generated method stub - return null; + List consumers = new ArrayList(); + + return consumers; } + /** + * @see AdministrationService#getAllRepositoryConsumers() + */ public List getAllRepositoryConsumers() { - // TODO Auto-generated method stub - return null; + List consumers = new ArrayList(); + + List knownConsumers = consumerUtil.getAvailableKnownConsumers(); + List invalidConsumers = consumerUtil.getAvailableInvalidConsumers(); + + for( KnownRepositoryContentConsumer consumer : knownConsumers ) + { + consumers.add( consumer.getId() ); + } + + for( InvalidRepositoryContentConsumer consumer : invalidConsumers ) + { + consumers.add( consumer.getId() ); + } + + return consumers; } public List getAllManagedRepositories() @@ -91,4 +171,36 @@ public class AdministrationServiceImpl { return null; } + + private void saveConfiguration( Configuration config ) + throws Exception + { + try + { + archivaConfiguration.save( config ); + } + catch( RegistryException e ) + { + throw new Exception( "Error occurred in the registry." ); + } + catch ( IndeterminateConfigurationException e ) + { + throw new Exception( "Error occurred while saving the configuration." ); + } + } + + public void setArchivaConfiguration( ArchivaConfiguration archivaConfiguration ) + { + this.archivaConfiguration = archivaConfiguration; + } + + public RepositoryContentConsumers getConsumerUtil() + { + return consumerUtil; + } + + public void setConsumerUtil( RepositoryContentConsumers consumerUtil ) + { + this.consumerUtil = consumerUtil; + } } diff --git a/MRM-124/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java b/MRM-124/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java index ca1cf4fea..621fcf8f1 100644 --- a/MRM-124/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java +++ b/MRM-124/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.consumers.InvalidRepositoryContentConsumer; +import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer; +import org.apache.maven.archiva.repository.scanner.RepositoryContentConsumers; import org.apache.maven.archiva.scheduled.ArchivaTaskScheduler; import org.apache.maven.archiva.scheduled.tasks.DatabaseTask; import org.apache.maven.archiva.scheduled.tasks.RepositoryTask; @@ -53,11 +56,27 @@ public class AdministrationServiceImplTest private Configuration config; - private AdministrationService service; + private AdministrationServiceImpl service; private MockControl taskSchedulerControl; private ArchivaTaskScheduler taskScheduler; + + private MockControl consumerUtilControl; + + private RepositoryContentConsumers consumerUtil; + + private MockControl knownContentConsumerControl; + + private MockControl invalidContentConsumerControl; + + private KnownRepositoryContentConsumer indexArtifactConsumer; + + private KnownRepositoryContentConsumer indexPomConsumer; + + private InvalidRepositoryContentConsumer checkPomConsumer; + + private InvalidRepositoryContentConsumer checkMetadataConsumer; protected void setUp() throws Exception @@ -73,14 +92,27 @@ public class AdministrationServiceImplTest taskSchedulerControl = MockControl.createControl( ArchivaTaskScheduler.class ); taskScheduler = ( ArchivaTaskScheduler ) taskSchedulerControl.getMock(); + consumerUtilControl = MockClassControl.createControl( RepositoryContentConsumers.class ); + consumerUtil = ( RepositoryContentConsumers ) consumerUtilControl.getMock(); + + knownContentConsumerControl = MockControl.createControl( KnownRepositoryContentConsumer.class ); + indexArtifactConsumer = ( KnownRepositoryContentConsumer ) knownContentConsumerControl.getMock(); + indexPomConsumer = ( KnownRepositoryContentConsumer ) knownContentConsumerControl.getMock(); + + invalidContentConsumerControl = MockControl.createControl( InvalidRepositoryContentConsumer.class ); + checkPomConsumer = ( InvalidRepositoryContentConsumer ) invalidContentConsumerControl.getMock(); + checkMetadataConsumer = ( InvalidRepositoryContentConsumer ) invalidContentConsumerControl.getMock(); + service = new AdministrationServiceImpl(); + service.setArchivaConfiguration( archivaConfig ); + service.setConsumerUtil( consumerUtil ); } // DATABASE CONSUMERS - public void testGetAllDbConsumers() + /* public void testGetAllDbConsumers() throws Exception - { - /*DatabaseScanningConfiguration dbScanning = new DatabaseScanningConfiguration(); + { + DatabaseScanningConfiguration dbScanning = new DatabaseScanningConfiguration(); dbScanning.addCleanupConsumer( "cleanup-index" ); dbScanning.addCleanupConsumer( "cleanup-database" ); dbScanning.addUnprocessedConsumer( "unprocessed-artifacts" ); @@ -92,6 +124,8 @@ public class AdministrationServiceImplTest archivaConfigControl.replay(); configControl.replay(); + + List dbConsumers = service.getAllDatabaseConsumers(); archivaConfigControl.verify(); @@ -102,10 +136,10 @@ public class AdministrationServiceImplTest assertTrue( dbConsumers.contains( "cleanup-index" ) ); assertTrue( dbConsumers.contains( "cleanup-database" ) ); assertTrue( dbConsumers.contains( "unprocessed-artifacts" ) ); - assertTrue( dbConsumers.contains( "unprocessed-poms" ) );*/ + assertTrue( dbConsumers.contains( "unprocessed-poms" ) ); } -/* public void testConfigureValidDatabaseConsumer() + public void testConfigureValidDatabaseConsumer() throws Exception { DatabaseScanningConfiguration dbScanning = new DatabaseScanningConfiguration(); @@ -192,67 +226,62 @@ public class AdministrationServiceImplTest } } + */ + // 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(); + { + recordRepoConsumerValidation(); - List repoConsumers = service.getAllDatabaseConsumers(); - - archivaConfigControl.verify(); - configControl.verify(); + consumerUtilControl.replay(); + knownContentConsumerControl.replay(); + invalidContentConsumerControl.replay(); + + List repoConsumers = service.getAllRepositoryConsumers(); + consumerUtilControl.verify(); + knownContentConsumerControl.verify(); + invalidContentConsumerControl.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" ) ); + assertEquals( 4, repoConsumers.size() ); + assertTrue( repoConsumers.contains( "index-artifact" ) ); + assertTrue( repoConsumers.contains( "index-pom" ) ); + assertTrue( repoConsumers.contains( "check-pom" ) ); assertTrue( repoConsumers.contains( "check-metadata" ) ); } public void testConfigureValidRepositoryConsumer() throws Exception - { - //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" ); + repoScanning.addKnownContentConsumer( "index-artifact" ); + repoScanning.addKnownContentConsumer( "index-pom" ); + repoScanning.addInvalidContentConsumer( "check-pom" ); + + // test enable "check-metadata" consumer + recordRepoConsumerValidation(); - // test enable archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning ); - config.setRepositoryScanning( repoScanning ); - + config.setRepositoryScanning( repoScanning ); configControl.setMatcher( MockControl.ALWAYS_MATCHER ); configControl.setVoidCallable(); archivaConfig.save( config ); archivaConfigControl.setVoidCallable(); - + + consumerUtilControl.replay(); + knownContentConsumerControl.replay(); + invalidContentConsumerControl.replay(); archivaConfigControl.replay(); - configControl.replay(); + configControl.replay(); try { - boolean success = service.configureRepositoryConsumer( null, "new-repo-consumer", true ); + boolean success = service.configureRepositoryConsumer( null, "check-metadata", true ); assertTrue( success ); } catch ( Exception e ) @@ -260,14 +289,22 @@ public class AdministrationServiceImplTest fail( "An exception should not have been thrown." ); } + consumerUtilControl.verify(); + knownContentConsumerControl.verify(); + invalidContentConsumerControl.verify(); archivaConfigControl.verify(); configControl.verify(); - // test disable + // test disable "check-metadata" consumer + consumerUtilControl.reset(); + knownContentConsumerControl.reset(); + invalidContentConsumerControl.reset(); archivaConfigControl.reset(); configControl.reset(); - //TODO mock checking whether the repo consumer is valid or not + repoScanning.addInvalidContentConsumer( "check-metadata" ); + + recordRepoConsumerValidation(); archivaConfigControl.expectAndReturn( archivaConfig.getConfiguration(), config ); configControl.expectAndReturn( config.getRepositoryScanning(), repoScanning ); @@ -278,24 +315,32 @@ public class AdministrationServiceImplTest archivaConfig.save( config ); archivaConfigControl.setVoidCallable(); - + + consumerUtilControl.replay(); + knownContentConsumerControl.replay(); + invalidContentConsumerControl.replay(); archivaConfigControl.replay(); configControl.replay(); try { - boolean success = service.configureRepositoryConsumer( null, "new-repo-consumer", false ); + boolean success = service.configureRepositoryConsumer( null, "check-metadata", false ); + + consumerUtilControl.verify(); + knownContentConsumerControl.verify(); + invalidContentConsumerControl.verify(); + archivaConfigControl.verify(); + configControl.verify(); + assertTrue( success ); } catch ( Exception e ) { fail( "An excecption should not have been thrown." ); - } - - archivaConfigControl.verify(); - configControl.verify(); + } } - + + /* public void testConfigureInvalidRepositoryConsumer() throws Exception { @@ -308,7 +353,7 @@ public class AdministrationServiceImplTest } catch ( Exception e ) { - assertEquals( "Invalid database consumer.", e.getMessage() ); + assertEquals( "Invalid repository consumer.", e.getMessage() ); } } @@ -567,4 +612,23 @@ public class AdministrationServiceImplTest return repoConfig; } + private void recordRepoConsumerValidation() + { + List availableKnownConsumers = new ArrayList(); + availableKnownConsumers.add( indexArtifactConsumer ); + availableKnownConsumers.add( indexPomConsumer ); + + List availableInvalidConsumers = new ArrayList(); + availableInvalidConsumers.add( checkPomConsumer ); + availableInvalidConsumers.add( checkMetadataConsumer ); + + consumerUtilControl.expectAndReturn( consumerUtil.getAvailableKnownConsumers(), availableKnownConsumers ); + knownContentConsumerControl.expectAndReturn( indexArtifactConsumer.getId(), "index-artifact" ); + knownContentConsumerControl.expectAndReturn( indexPomConsumer.getId(), "index-pom" ); + + consumerUtilControl.expectAndReturn( consumerUtil.getAvailableInvalidConsumers(), availableInvalidConsumers ); + invalidContentConsumerControl.expectAndReturn( checkPomConsumer.getId(), "check-pom" ); + invalidContentConsumerControl.expectAndReturn( checkMetadataConsumer.getId(), "check-metadata" ); + } + } \ No newline at end of file -- 2.39.5