]> source.dussan.org Git - archiva.git/commitdiff
[MRM-462] tests for editing repositories. Also removed annoying logging, and erroneou...
authorBrett Porter <brett@apache.org>
Wed, 12 Sep 2007 01:11:02 +0000 (01:11 +0000)
committerBrett Porter <brett@apache.org>
Wed, 12 Sep 2007 01:11:02 +0000 (01:11 +0000)
git-svn-id: https://svn.apache.org/repos/asf/maven/archiva/branches@574742 13f79535-47bb-0310-9956-ffa450edef68

MRM-462/archiva-web/archiva-webapp/src/main/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryAction.java
MRM-462/archiva-web/archiva-webapp/src/test/java/org/apache/maven/archiva/web/action/admin/repositories/ConfigureRepositoryActionTest.java

index 0297a3778b87cb3f2123147ddd3ba653b2560516..166346f48fc516deb30b5873ac8ea6ab72cf8861 100644 (file)
@@ -19,7 +19,6 @@ package org.apache.maven.archiva.web.action.admin.repositories;
  * under the License.
  */
 
-import com.opensymphony.xwork.ActionContext;
 import com.opensymphony.xwork.Preparable;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringUtils;
@@ -29,13 +28,9 @@ import org.apache.maven.archiva.configuration.IndeterminateConfigurationExceptio
 import org.apache.maven.archiva.configuration.InvalidConfigurationException;
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 import org.apache.maven.archiva.security.ArchivaRoleConstants;
-import org.codehaus.plexus.redback.authorization.AuthorizationException;
-import org.codehaus.plexus.redback.authorization.AuthorizationResult;
 import org.codehaus.plexus.redback.rbac.Resource;
 import org.codehaus.plexus.redback.role.RoleManager;
 import org.codehaus.plexus.redback.role.RoleManagerException;
-import org.codehaus.plexus.redback.system.SecuritySession;
-import org.codehaus.plexus.redback.system.SecuritySystem;
 import org.codehaus.plexus.redback.xwork.interceptor.SecureAction;
 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
@@ -60,11 +55,6 @@ public class ConfigureRepositoryAction
      */
     private RoleManager roleManager;
 
-    /**
-     * @plexus.requirement
-     */
-    private SecuritySystem securitySystem;
-
     /**
      * @plexus.requirement
      */
@@ -82,7 +72,6 @@ public class ConfigureRepositoryAction
 
     public String add()
     {
-        getLogger().info( ".add()" );
         this.mode = "add";
 
         this.repository.setReleases( true );
@@ -93,27 +82,11 @@ public class ConfigureRepositoryAction
 
     public String confirm()
     {
-        getLogger().info( ".confirm()" );
-
-        if ( operationAllowed( ArchivaRoleConstants.OPERATION_DELETE_REPOSITORY, repoid ) )
-        {
-            addActionError( "You do not have the appropriate permissions to delete the " + repoid + " repository." );
-            return ERROR;
-        }
-
         return INPUT;
     }
 
     public String delete()
     {
-        getLogger().info( ".delete()" );
-
-        if ( operationAllowed( ArchivaRoleConstants.OPERATION_DELETE_REPOSITORY, repoid ) )
-        {
-            addActionError( "You do not have the appropriate permissions to delete the " + repoid + " repository." );
-            return ERROR;
-        }
-
         String result = SUCCESS;
         if ( StringUtils.equals( mode, "delete-entry" ) || StringUtils.equals( mode, "delete-contents" ) )
         {
@@ -128,7 +101,7 @@ public class ConfigureRepositoryAction
 
             try
             {
-                removeRepository( repoid );
+                removeRepository( repoid, archivaConfiguration.getConfiguration() );
                 result = saveConfiguration( archivaConfiguration.getConfiguration() );
 
                 if ( result.equals( SUCCESS ) )
@@ -167,15 +140,8 @@ public class ConfigureRepositoryAction
 
     public String edit()
     {
-        getLogger().info( ".edit()" );
         this.mode = "edit";
 
-        if ( operationAllowed( ArchivaRoleConstants.OPERATION_EDIT_REPOSITORY, repoid ) )
-        {
-            addActionError( "You do not have the appropriate permissions to edit the " + repoid + " repository." );
-            return ERROR;
-        }
-
         return INPUT;
     }
 
@@ -228,8 +194,6 @@ public class ConfigureRepositoryAction
     {
         String repoId = repository.getId();
 
-        getLogger().info( ".save(" + mode + ":" + repoId + ")" );
-
         Configuration configuration = archivaConfiguration.getConfiguration();
         boolean containsError = validateFields( configuration );
 
@@ -244,7 +208,7 @@ public class ConfigureRepositoryAction
 
         if ( StringUtils.equalsIgnoreCase( "edit", this.mode ) )
         {
-            removeRepository( repoId );
+            removeRepository( repoId, configuration );
         }
 
         String result;
@@ -336,8 +300,6 @@ public class ConfigureRepositoryAction
     private void addRepository( AdminRepositoryConfiguration repository, Configuration configuration )
         throws IOException, RoleManagerException
     {
-        getLogger().info( ".addRepository(" + repository + ")" );
-
         // Fix the URL entry (could possibly be a filesystem path)
 /* TODO! reinstate
         String rawUrlEntry = repository.getUrl();
@@ -369,42 +331,19 @@ public class ConfigureRepositoryAction
         roleManager.createTemplatedRole( "archiva-repository-observer", repository.getId() );
     }
 
-    private boolean operationAllowed( String permission, String repoid )
-    {
-        ActionContext context = ActionContext.getContext();
-        SecuritySession securitySession = (SecuritySession) context.get( SecuritySession.ROLE );
-
-        try
-        {
-            AuthorizationResult authzResult = securitySystem.authorize( securitySession, permission, repoid );
-
-            return authzResult.isAuthorized();
-        }
-        catch ( AuthorizationException e )
-        {
-            getLogger().info( "Unable to authorize permission: " + permission + " against repo: " + repoid +
-                " due to: " + e.getMessage() );
-            return false;
-        }
-    }
-
     private void removeContents( AdminRepositoryConfiguration existingRepository )
         throws IOException
     {
-        getLogger().info( "Removing " + existingRepository.getLocation() );
         FileUtils.deleteDirectory( new File( existingRepository.getLocation() ) );
     }
 
-    private void removeRepository( String repoId )
+    private void removeRepository( String repoId, Configuration configuration )
     {
-        getLogger().info( ".removeRepository()" );
-
         // TODO! what about others?
-        ManagedRepositoryConfiguration toremove =
-            archivaConfiguration.getConfiguration().findManagedRepositoryById( repoId );
+        ManagedRepositoryConfiguration toremove = configuration.findManagedRepositoryById( repoId );
         if ( toremove != null )
         {
-            archivaConfiguration.getConfiguration().removeManagedRepository( toremove );
+            configuration.removeManagedRepository( toremove );
         }
     }
 
@@ -414,14 +353,12 @@ public class ConfigureRepositoryAction
         roleManager.removeTemplatedRole( "archiva-repository-manager", existingRepository.getId() );
         roleManager.removeTemplatedRole( "archiva-repository-observer", existingRepository.getId() );
 
-        getLogger().info( "removed user roles associated with repository " + existingRepository.getId() );
+        getLogger().debug( "removed user roles associated with repository " + existingRepository.getId() );
     }
 
     private String saveConfiguration( Configuration configuration )
         throws IOException, InvalidConfigurationException, RegistryException
     {
-        getLogger().info( ".saveConfiguration()" );
-
         try
         {
             archivaConfiguration.save( configuration );
@@ -445,4 +382,5 @@ public class ConfigureRepositoryAction
     {
         this.archivaConfiguration = archivaConfiguration;
     }
+
 }
index ccdbe54d1ced9d85e1b8583978c74dc8182b53db..6cd171ff6a442723ebccd906b25ec21e23d29a68 100644 (file)
@@ -22,6 +22,7 @@ package org.apache.maven.archiva.web.action.admin.repositories;
 import com.opensymphony.xwork.Action;
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
 import org.apache.maven.archiva.configuration.Configuration;
+import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 import org.codehaus.plexus.PlexusTestCase;
 import org.codehaus.plexus.redback.role.RoleManager;
 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
@@ -46,6 +47,8 @@ public class ConfigureRepositoryActionTest
 
     private ArchivaConfiguration archivaConfiguration;
 
+    private static final String REPO_ID = "repo-ident";
+
     protected void setUp()
         throws Exception
     {
@@ -97,10 +100,9 @@ public class ConfigureRepositoryActionTest
     public void testAddRepository()
         throws Exception
     {
-        String repoId = "repo-ident";
         // TODO: should be in the business model
-        roleManager.createTemplatedRole( "archiva-repository-manager", repoId );
-        roleManager.createTemplatedRole( "archiva-repository-observer", repoId );
+        roleManager.createTemplatedRole( "archiva-repository-manager", REPO_ID );
+        roleManager.createTemplatedRole( "archiva-repository-observer", REPO_ID );
 
         roleManagerControl.replay();
 
@@ -117,7 +119,114 @@ public class ConfigureRepositoryActionTest
         action.prepare();
         action.setMode( "add" );
         AdminRepositoryConfiguration repository = action.getRepository();
-        repository.setId( repoId );
+        populateRepository( repository );
+
+        String status = action.save();
+        assertEquals( Action.SUCCESS, status );
+
+        assertEquals( configuration.getManagedRepositories(), Collections.singletonList( repository ) );
+
+        roleManagerControl.verify();
+        archivaConfigurationControl.verify();
+    }
+
+    public void testEditRepositoryInitialPage()
+        throws Exception
+    {
+        Configuration configuration = createConfigurationForEditing();
+
+        archivaConfiguration.getConfiguration();
+        archivaConfigurationControl.setReturnValue( configuration );
+        archivaConfigurationControl.replay();
+
+        action.setRepoid( REPO_ID );
+
+        action.prepare();
+        assertEquals( REPO_ID, action.getRepoid() );
+        assertNull( action.getMode() );
+        AdminRepositoryConfiguration repository = action.getRepository();
+        assertNotNull( repository );
+        assertRepositoryEquals( repository, createRepository() );
+
+        String status = action.edit();
+        assertEquals( Action.INPUT, status );
+        repository = action.getRepository();
+        assertRepositoryEquals( repository, createRepository() );
+    }
+
+    private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
+                                         ManagedRepositoryConfiguration actualRepository )
+    {
+        assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
+        assertEquals( expectedRepository.getId(), actualRepository.getId() );
+        assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
+        assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
+        assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
+        assertEquals( expectedRepository.getName(), actualRepository.getName() );
+        assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
+        assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
+        assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
+        assertEquals( expectedRepository.isIndexed(), actualRepository.isIndexed() );
+        assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
+        assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
+    }
+
+    private static Configuration createConfigurationForEditing()
+    {
+        Configuration configuration = new Configuration();
+        ManagedRepositoryConfiguration r = createRepository();
+        configuration.addManagedRepository( r );
+        return configuration;
+    }
+
+    private static ManagedRepositoryConfiguration createRepository()
+    {
+        ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
+        r.setId( REPO_ID );
+        populateRepository( r );
+        return r;
+    }
+
+    public void testEditRepository()
+        throws Exception
+    {
+        // TODO: should be in the business model
+        roleManager.createTemplatedRole( "archiva-repository-manager", REPO_ID );
+        roleManager.createTemplatedRole( "archiva-repository-observer", REPO_ID );
+
+        roleManagerControl.replay();
+
+        Configuration configuration = createConfigurationForEditing();
+        archivaConfiguration.getConfiguration();
+        archivaConfigurationControl.setReturnValue( configuration );
+        archivaConfiguration.getConfiguration();
+        archivaConfigurationControl.setReturnValue( configuration );
+
+        archivaConfiguration.save( configuration );
+
+        archivaConfigurationControl.replay();
+
+        action.prepare();
+        action.setMode( "edit" );
+        AdminRepositoryConfiguration repository = action.getRepository();
+        populateRepository( repository );
+        repository.setName( "new repo name" );
+
+        String status = action.save();
+        assertEquals( Action.SUCCESS, status );
+
+        ManagedRepositoryConfiguration newRepository = createRepository();
+        newRepository.setName( "new repo name" );
+        assertRepositoryEquals( repository, newRepository );
+        assertEquals( configuration.getManagedRepositories(), Collections.singletonList( repository ) );
+
+        roleManagerControl.verify();
+        archivaConfigurationControl.verify();
+    }
+
+    private static void populateRepository( ManagedRepositoryConfiguration repository )
+    {
+        repository.setId( REPO_ID );
         repository.setName( "repo name" );
         repository.setLocation( "location" );
         repository.setLayout( "default" );
@@ -128,14 +237,6 @@ public class ConfigureRepositoryActionTest
         repository.setSnapshots( true );
         repository.setIndexed( true );
         repository.setDeleteReleasedSnapshots( true );
-
-        String status = action.save();
-        assertEquals( Action.SUCCESS, status );
-
-        assertEquals( configuration.getManagedRepositories(), Collections.singletonList( repository ) );
-
-        roleManagerControl.verify();
-        archivaConfigurationControl.verify();
     }
 
     // TODO: test errors during add, other actions