1 package org.apache.maven.archiva.web.action.admin.repositories;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.xwork.Action;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.security.ArchivaRoleConstants;
29 import org.codehaus.plexus.PlexusTestCase;
30 import org.codehaus.plexus.redback.role.RoleManager;
31 import org.codehaus.plexus.redback.role.RoleManagerException;
32 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
33 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
34 import org.codehaus.plexus.registry.RegistryException;
35 import org.easymock.MockControl;
38 import java.util.Collections;
41 * DeleteManagedRepositoryActionTest
43 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
46 public class DeleteManagedRepositoryActionTest
47 extends PlexusTestCase
49 private DeleteManagedRepositoryAction action;
51 private RoleManager roleManager;
53 private MockControl roleManagerControl;
55 private MockControl archivaConfigurationControl;
57 private ArchivaConfiguration archivaConfiguration;
59 private static final String REPO_ID = "repo-ident";
61 private File location;
63 protected void setUp()
68 action = (DeleteManagedRepositoryAction) lookup( Action.class.getName(), "deleteManagedRepositoryAction" );
70 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
71 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
72 action.setArchivaConfiguration( archivaConfiguration );
74 roleManagerControl = MockControl.createControl( RoleManager.class );
75 roleManager = (RoleManager) roleManagerControl.getMock();
76 action.setRoleManager( roleManager );
77 location = getTestFile( "target/test/location" );
80 public void testSecureActionBundle()
81 throws SecureActionException
83 archivaConfiguration.getConfiguration();
84 archivaConfigurationControl.setReturnValue( new Configuration() );
85 archivaConfigurationControl.replay();
88 SecureActionBundle bundle = action.getSecureActionBundle();
89 assertTrue( bundle.requiresAuthentication() );
90 assertEquals( 1, bundle.getAuthorizationTuples().size() );
93 public void testDeleteRepositoryConfirmation()
96 ManagedRepositoryConfiguration originalRepository = createRepository();
97 Configuration configuration = createConfigurationForEditing( originalRepository );
99 archivaConfiguration.getConfiguration();
100 archivaConfigurationControl.setReturnValue( configuration );
101 archivaConfigurationControl.replay();
103 action.setRepoid( REPO_ID );
106 assertEquals( REPO_ID, action.getRepoid() );
107 ManagedRepositoryConfiguration repository = action.getRepository();
108 assertNotNull( repository );
109 assertRepositoryEquals( repository, createRepository() );
111 String status = action.execute();
112 assertEquals( Action.SUCCESS, status );
113 repository = action.getRepository();
114 assertRepositoryEquals( repository, createRepository() );
115 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
118 public void testDeleteRepositoryKeepContent()
121 prepareRoleManagerMock();
123 Configuration configuration = prepDeletionTest( createRepository(), "delete-entry" );
124 String status = action.deleteEntry();
125 assertEquals( Action.SUCCESS, status );
127 assertTrue( configuration.getManagedRepositories().isEmpty() );
129 assertTrue( location.exists() );
132 public void testDeleteRepositoryDeleteContent()
135 prepareRoleManagerMock();
137 Configuration configuration = prepDeletionTest( createRepository(), "delete-contents" );
138 String status = action.deleteContents();
139 assertEquals( Action.SUCCESS, status );
141 assertTrue( configuration.getManagedRepositories().isEmpty() );
143 assertFalse( location.exists() );
146 public void testDeleteRepositoryCancelled()
149 ManagedRepositoryConfiguration originalRepository = createRepository();
150 Configuration configuration = prepDeletionTest( originalRepository, null );
151 String status = action.execute();
152 assertEquals( Action.SUCCESS, status );
154 ManagedRepositoryConfiguration repository = action.getRepository();
155 assertRepositoryEquals( repository, createRepository() );
156 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
158 assertTrue( location.exists() );
161 private Configuration prepDeletionTest( ManagedRepositoryConfiguration originalRepository, String mode )
162 throws RegistryException, IndeterminateConfigurationException
166 Configuration configuration = createConfigurationForEditing( originalRepository );
168 archivaConfiguration.getConfiguration();
169 archivaConfigurationControl.setReturnValue( configuration );
170 archivaConfiguration.getConfiguration();
171 archivaConfigurationControl.setReturnValue( configuration );
173 archivaConfiguration.save( configuration );
174 archivaConfigurationControl.replay();
176 action.setRepoid( REPO_ID );
179 assertEquals( REPO_ID, action.getRepoid() );
180 ManagedRepositoryConfiguration repository = action.getRepository();
181 assertNotNull( repository );
182 assertRepositoryEquals( repository, createRepository() );
184 assertTrue( location.exists() );
185 return configuration;
188 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
189 ManagedRepositoryConfiguration actualRepository )
191 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
192 assertEquals( expectedRepository.getId(), actualRepository.getId() );
193 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
194 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
195 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
196 assertEquals( expectedRepository.getName(), actualRepository.getName() );
197 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
198 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
199 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
200 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
201 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
202 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
205 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
207 Configuration configuration = new Configuration();
208 configuration.addManagedRepository( repositoryConfiguration );
209 return configuration;
212 private ManagedRepositoryConfiguration createRepository()
214 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
216 populateRepository( r );
220 private void populateRepository( ManagedRepositoryConfiguration repository )
222 repository.setId( REPO_ID );
223 repository.setName( "repo name" );
224 repository.setLocation( location.getAbsolutePath() );
225 repository.setLayout( "default" );
226 repository.setRefreshCronExpression( "* 0/5 * * * ?" );
227 repository.setDaysOlder( 31 );
228 repository.setRetentionCount( 20 );
229 repository.setReleases( true );
230 repository.setSnapshots( true );
231 repository.setScanned( false );
232 repository.setDeleteReleasedSnapshots( true );
235 private void prepareRoleManagerMock()
236 throws RoleManagerException
238 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
239 roleManagerControl.setReturnValue( true );
240 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
241 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
242 roleManagerControl.setReturnValue( true );
243 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
244 roleManagerControl.replay();