]> source.dussan.org Git - archiva.git/commitdiff
implemented getAllRepositoryConsumers() and configureRepositoryConsumer() in admin...
authorMaria Odea B. Ching <oching@apache.org>
Fri, 10 Oct 2008 00:32:57 +0000 (00:32 +0000)
committerMaria Odea B. Ching <oching@apache.org>
Fri, 10 Oct 2008 00:32:57 +0000 (00:32 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/branches@703301 13f79535-47bb-0310-9956-ffa450edef68

MRM-124/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/main/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImpl.java
MRM-124/archiva-modules/archiva-web/archiva-xmlrpc/archiva-xmlrpc-services/src/test/java/org/apache/archiva/web/xmlrpc/services/AdministrationServiceImplTest.java

index 17a4be09364606d6f1ab32edb119bb6e88f93888..ea35623103f32ddd1a998149b78a9e12908add6d 100644 (file)
@@ -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<KnownRepositoryContentConsumer> knownConsumers = consumerUtil.getAvailableKnownConsumers();
+        List<InvalidRepositoryContentConsumer> 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<String> getAllDatabaseConsumers()
     {
-        // TODO Auto-generated method stub
-        return null;
+        List<String> consumers = new ArrayList<String>();
+        
+        return consumers;
     }
 
+    /**
+     * @see AdministrationService#getAllRepositoryConsumers()
+     */
     public List<String> getAllRepositoryConsumers()
     {
-        // TODO Auto-generated method stub
-        return null;
+        List<String> consumers = new ArrayList<String>();
+                
+        List<KnownRepositoryContentConsumer> knownConsumers = consumerUtil.getAvailableKnownConsumers();
+        List<InvalidRepositoryContentConsumer> invalidConsumers = consumerUtil.getAvailableInvalidConsumers();
+        
+        for( KnownRepositoryContentConsumer consumer : knownConsumers )
+        {
+            consumers.add( consumer.getId() );
+        }
+        
+        for( InvalidRepositoryContentConsumer consumer : invalidConsumers )
+        {
+            consumers.add( consumer.getId() );
+        }
+
+        return consumers;
     }
 
     public List<ManagedRepository> 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;
+    }    
 }
index ca1cf4fea48c84824b411e9484e1375a0714cf24..621fcf8f19f12001e5ae8adc2bed09ffbea1bc7d 100644 (file)
@@ -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<String> 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<String> repoConsumers = service.getAllDatabaseConsumers(); 
-        
-        archivaConfigControl.verify();
-        configControl.verify();
+        consumerUtilControl.replay();
+        knownContentConsumerControl.replay();
+        invalidContentConsumerControl.replay();
+                
+        List<String> 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<KnownRepositoryContentConsumer> availableKnownConsumers = new ArrayList<KnownRepositoryContentConsumer>();
+        availableKnownConsumers.add( indexArtifactConsumer );
+        availableKnownConsumers.add( indexPomConsumer );
+        
+        List<InvalidRepositoryContentConsumer> availableInvalidConsumers = new ArrayList<InvalidRepositoryContentConsumer>();
+        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