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;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.Configuration;
25 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
28 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
29 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
30 import org.apache.maven.archiva.model.ArchivaProjectModel;
31 import org.apache.maven.archiva.security.ArchivaRoleConstants;
32 import org.codehaus.plexus.redback.role.RoleManager;
33 import org.codehaus.plexus.redback.role.RoleManagerException;
34 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
35 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
36 import org.codehaus.plexus.registry.RegistryException;
37 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
38 import org.easymock.MockControl;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.List;
46 * DeleteManagedRepositoryActionTest
48 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
51 public class DeleteManagedRepositoryActionTest
52 extends PlexusInSpringTestCase
54 private DeleteManagedRepositoryAction action;
56 private RoleManager roleManager;
58 private MockControl roleManagerControl;
60 private MockControl archivaConfigurationControl;
62 private ArchivaConfiguration archivaConfiguration;
64 private static final String REPO_ID = "repo-ident";
66 private File location;
69 protected String getPlexusConfigLocation()
71 return AbstractManagedRepositoriesAction.class.getName().replace( '.', '/' ) + "Test.xml";
74 protected void setUp()
79 action = (DeleteManagedRepositoryAction) lookup( Action.class.getName(), "deleteManagedRepositoryAction" );
81 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
82 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
83 action.setArchivaConfiguration( archivaConfiguration );
85 roleManagerControl = MockControl.createControl( RoleManager.class );
86 roleManager = (RoleManager) roleManagerControl.getMock();
87 action.setRoleManager( roleManager );
88 location = getTestFile( "target/test/location" );
91 public void testSecureActionBundle()
92 throws SecureActionException
94 archivaConfiguration.getConfiguration();
95 archivaConfigurationControl.setReturnValue( new Configuration() );
96 archivaConfigurationControl.replay();
99 SecureActionBundle bundle = action.getSecureActionBundle();
100 assertTrue( bundle.requiresAuthentication() );
101 assertEquals( 1, bundle.getAuthorizationTuples().size() );
104 public void testDeleteRepositoryConfirmation()
107 ManagedRepositoryConfiguration originalRepository = createRepository();
108 Configuration configuration = createConfigurationForEditing( originalRepository );
110 archivaConfiguration.getConfiguration();
111 archivaConfigurationControl.setReturnValue( configuration );
112 archivaConfigurationControl.replay();
114 action.setRepoid( REPO_ID );
117 assertEquals( REPO_ID, action.getRepoid() );
118 ManagedRepositoryConfiguration repository = action.getRepository();
119 assertNotNull( repository );
120 assertRepositoryEquals( repository, createRepository() );
122 String status = action.execute();
123 assertEquals( Action.SUCCESS, status );
124 repository = action.getRepository();
125 assertRepositoryEquals( repository, createRepository() );
126 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
129 public void testDeleteRepositoryKeepContent()
132 prepareRoleManagerMock();
134 Configuration configuration = prepDeletionTest( createRepository(), 4 );
136 String status = action.deleteEntry();
138 assertEquals( Action.SUCCESS, status );
140 assertTrue( configuration.getManagedRepositories().isEmpty() );
142 assertTrue( location.exists() );
145 public void testDeleteRepositoryDeleteContent()
148 prepareRoleManagerMock();
150 Configuration configuration = prepDeletionTest( createRepository(), 4 );
152 String status = action.deleteContents();
154 assertEquals( Action.SUCCESS, status );
156 assertTrue( configuration.getManagedRepositories().isEmpty() );
158 assertFalse( location.exists() );
161 public void testDeleteRepositoryAndAssociatedProxyConnectors()
164 Configuration configuration = prepDeletionTest( createRepository(), 5 );
165 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
166 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
167 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
169 prepareRoleManagerMock();
171 assertEquals( 1, configuration.getProxyConnectors().size() );
173 String status = action.deleteContents();
174 assertEquals( Action.SUCCESS, status );
176 assertTrue( configuration.getManagedRepositories().isEmpty() );
177 assertEquals( 0, configuration.getProxyConnectors().size() );
179 assertFalse( location.exists() );
182 public void testDeleteRepositoryCancelled()
185 ManagedRepositoryConfiguration originalRepository = createRepository();
186 Configuration configuration = prepDeletionTest( originalRepository, 3 );
187 String status = action.execute();
188 assertEquals( Action.SUCCESS, status );
190 ManagedRepositoryConfiguration repository = action.getRepository();
191 assertRepositoryEquals( repository, createRepository() );
192 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
194 assertTrue( location.exists() );
197 public void testDeleteRepositoryAndReposUnderRepoGroup()
200 Configuration configuration = prepDeletionTest( createRepository(), 5 );
201 List<String> repoIds = new ArrayList<String>();
202 repoIds.add( REPO_ID );
203 configuration.addRepositoryGroup( createRepoGroup( repoIds, "repo.group" ) );
205 prepareRoleManagerMock();
207 assertEquals( 1, configuration.getRepositoryGroups().size() );
209 String status = action.deleteContents();
210 assertEquals( Action.SUCCESS, status );
212 assertTrue( configuration.getManagedRepositories().isEmpty() );
213 assertEquals( 0, ( ( RepositoryGroupConfiguration ) configuration.getRepositoryGroups().get( 0 ) ).getRepositories().size() );
215 assertFalse( location.exists() );
218 private Configuration prepDeletionTest( ManagedRepositoryConfiguration originalRepository, int expectCountGetConfig )
219 throws RegistryException, IndeterminateConfigurationException
223 Configuration configuration = createConfigurationForEditing( originalRepository );
225 archivaConfiguration.getConfiguration();
226 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
228 archivaConfiguration.save( configuration );
229 archivaConfigurationControl.replay();
231 action.setRepoid( REPO_ID );
234 assertEquals( REPO_ID, action.getRepoid() );
235 ManagedRepositoryConfiguration repository = action.getRepository();
236 assertNotNull( repository );
237 assertRepositoryEquals( repository, createRepository() );
239 assertTrue( location.exists() );
240 return configuration;
243 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
244 ManagedRepositoryConfiguration actualRepository )
246 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
247 assertEquals( expectedRepository.getId(), actualRepository.getId() );
248 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
249 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
250 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
251 assertEquals( expectedRepository.getName(), actualRepository.getName() );
252 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
253 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
254 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
255 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
256 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
257 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
260 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
262 Configuration configuration = new Configuration();
263 configuration.addManagedRepository( repositoryConfiguration );
264 return configuration;
267 private ManagedRepositoryConfiguration createRepository()
269 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
271 r.setName( "repo name" );
272 r.setLocation( location.getAbsolutePath() );
273 r.setLayout( "default" );
274 r.setRefreshCronExpression( "* 0/5 * * * ?" );
276 r.setRetentionCount( 0 );
277 r.setReleases( true );
278 r.setSnapshots( true );
279 r.setScanned( false );
280 r.setDeleteReleasedSnapshots( false );
284 private RemoteRepositoryConfiguration createRemoteRepository(String id, String url)
286 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
289 r.setLayout( "default" );
294 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
296 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
297 connector.setSourceRepoId( managedRepoId );
298 connector.setTargetRepoId( remoteRepoId );
303 private RepositoryGroupConfiguration createRepoGroup( List<String> repoIds, String repoGroupId )
305 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
306 repoGroup.setId( repoGroupId );
307 repoGroup.setRepositories( repoIds );
312 private void prepareRoleManagerMock()
313 throws RoleManagerException
315 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
316 roleManagerControl.setReturnValue( true );
317 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
318 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
319 roleManagerControl.setReturnValue( true );
320 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
321 roleManagerControl.replay();
324 protected ArchivaProjectModel createProjectModel( String groupId, String artifactId, String version )
326 ArchivaProjectModel projectModel = new ArchivaProjectModel();
327 projectModel.setGroupId( groupId );
328 projectModel.setArtifactId( artifactId );
329 projectModel.setVersion( version );