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
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
27 import com.opensymphony.xwork2.Action;
28 import org.apache.archiva.metadata.repository.MetadataRepository;
29 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.Configuration;
32 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
33 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
35 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
36 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
37 import org.apache.maven.archiva.security.ArchivaRoleConstants;
38 import org.codehaus.plexus.redback.role.RoleManager;
39 import org.codehaus.plexus.redback.role.RoleManagerException;
40 import org.codehaus.plexus.registry.RegistryException;
41 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
42 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
43 import org.codehaus.redback.integration.interceptor.SecureActionException;
44 import org.easymock.MockControl;
47 * DeleteManagedRepositoryActionTest
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;
68 private MockControl repositoryStatisticsManagerControl;
70 private RepositoryStatisticsManager repositoryStatisticsManager;
72 protected void setUp()
77 action = new DeleteManagedRepositoryAction();
79 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
80 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
81 action.setArchivaConfiguration( archivaConfiguration );
83 roleManagerControl = MockControl.createControl( RoleManager.class );
84 roleManager = (RoleManager) roleManagerControl.getMock();
85 action.setRoleManager( roleManager );
86 location = getTestFile( "target/test/location" );
88 repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
89 repositoryStatisticsManager = (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
90 action.setRepositoryStatisticsManager( repositoryStatisticsManager );
92 MockControl metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
93 MetadataRepository metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
94 metadataRepository.deleteRepository( REPO_ID );
95 action.setMetadataRepository( metadataRepository );
97 metadataRepositoryControl.replay();
100 public void testSecureActionBundle()
101 throws SecureActionException
103 archivaConfiguration.getConfiguration();
104 archivaConfigurationControl.setReturnValue( new Configuration() );
105 archivaConfigurationControl.replay();
108 SecureActionBundle bundle = action.getSecureActionBundle();
109 assertTrue( bundle.requiresAuthentication() );
110 assertEquals( 1, bundle.getAuthorizationTuples().size() );
113 public void testDeleteRepositoryConfirmation()
116 ManagedRepositoryConfiguration originalRepository = createRepository();
117 Configuration configuration = createConfigurationForEditing( originalRepository );
119 archivaConfiguration.getConfiguration();
120 archivaConfigurationControl.setReturnValue( configuration );
121 archivaConfigurationControl.replay();
123 action.setRepoid( REPO_ID );
126 assertEquals( REPO_ID, action.getRepoid() );
127 ManagedRepositoryConfiguration repository = action.getRepository();
128 assertNotNull( repository );
129 assertRepositoryEquals( repository, createRepository() );
131 String status = action.execute();
132 assertEquals( Action.SUCCESS, status );
134 repository = action.getRepository();
135 assertRepositoryEquals( repository, createRepository() );
136 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
139 public void testDeleteRepositoryKeepContent()
142 // even when we keep the content, we don't keep the metadata at this point
143 repositoryStatisticsManager.deleteStatistics( REPO_ID );
144 repositoryStatisticsManagerControl.replay();
146 prepareRoleManagerMock();
148 Configuration configuration = prepDeletionTest( createRepository(), 4 );
150 String status = action.deleteEntry();
152 assertEquals( Action.SUCCESS, status );
154 assertTrue( configuration.getManagedRepositories().isEmpty() );
156 assertTrue( location.exists() );
158 repositoryStatisticsManagerControl.verify();
161 public void testDeleteRepositoryDeleteContent()
164 repositoryStatisticsManager.deleteStatistics( REPO_ID );
165 repositoryStatisticsManagerControl.replay();
167 prepareRoleManagerMock();
169 Configuration configuration = prepDeletionTest( createRepository(), 4 );
171 String status = action.deleteContents();
173 assertEquals( Action.SUCCESS, status );
175 assertTrue( configuration.getManagedRepositories().isEmpty() );
177 assertFalse( location.exists() );
179 repositoryStatisticsManagerControl.verify();
182 public void testDeleteRepositoryAndAssociatedProxyConnectors()
185 repositoryStatisticsManager.deleteStatistics( REPO_ID );
186 repositoryStatisticsManagerControl.replay();
188 Configuration configuration = prepDeletionTest( createRepository(), 5 );
189 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
190 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
191 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
193 prepareRoleManagerMock();
195 assertEquals( 1, configuration.getProxyConnectors().size() );
197 String status = action.deleteContents();
199 assertEquals( Action.SUCCESS, status );
201 assertTrue( configuration.getManagedRepositories().isEmpty() );
202 assertEquals( 0, configuration.getProxyConnectors().size() );
204 assertFalse( location.exists() );
206 repositoryStatisticsManagerControl.verify();
209 public void testDeleteRepositoryCancelled()
212 repositoryStatisticsManagerControl.replay();
214 ManagedRepositoryConfiguration originalRepository = createRepository();
215 Configuration configuration = prepDeletionTest( originalRepository, 3 );
217 String status = action.execute();
218 assertEquals( Action.SUCCESS, status );
220 ManagedRepositoryConfiguration repository = action.getRepository();
221 assertRepositoryEquals( repository, createRepository() );
222 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
224 assertTrue( location.exists() );
226 repositoryStatisticsManagerControl.verify();
229 public void testDeleteRepositoryAndReposUnderRepoGroup()
232 repositoryStatisticsManager.deleteStatistics( REPO_ID );
233 repositoryStatisticsManagerControl.replay();
235 Configuration configuration = prepDeletionTest( createRepository(), 5 );
236 List<String> repoIds = new ArrayList<String>();
237 repoIds.add( REPO_ID );
238 configuration.addRepositoryGroup( createRepoGroup( repoIds, "repo.group" ) );
240 prepareRoleManagerMock();
242 assertEquals( 1, configuration.getRepositoryGroups().size() );
244 String status = action.deleteContents();
245 assertEquals( Action.SUCCESS, status );
247 assertTrue( configuration.getManagedRepositories().isEmpty() );
248 assertEquals( 0, ( ( RepositoryGroupConfiguration ) configuration.getRepositoryGroups().get( 0 ) ).getRepositories().size() );
250 assertFalse( location.exists() );
252 repositoryStatisticsManagerControl.verify();
255 private Configuration prepDeletionTest( ManagedRepositoryConfiguration originalRepository, int expectCountGetConfig )
256 throws RegistryException, IndeterminateConfigurationException
260 Configuration configuration = createConfigurationForEditing( originalRepository );
262 archivaConfiguration.getConfiguration();
263 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
265 archivaConfiguration.save( configuration );
266 archivaConfigurationControl.replay();
268 action.setRepoid( REPO_ID );
271 assertEquals( REPO_ID, action.getRepoid() );
272 ManagedRepositoryConfiguration repository = action.getRepository();
273 assertNotNull( repository );
274 assertRepositoryEquals( repository, createRepository() );
276 assertTrue( location.exists() );
277 return configuration;
280 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
281 ManagedRepositoryConfiguration actualRepository )
283 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
284 assertEquals( expectedRepository.getId(), actualRepository.getId() );
285 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
286 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
287 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
288 assertEquals( expectedRepository.getName(), actualRepository.getName() );
289 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
290 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
291 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
292 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
293 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
294 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
297 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
299 Configuration configuration = new Configuration();
300 configuration.addManagedRepository( repositoryConfiguration );
301 return configuration;
304 private ManagedRepositoryConfiguration createRepository()
306 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
308 r.setName( "repo name" );
309 r.setLocation( location.getAbsolutePath() );
310 r.setLayout( "default" );
311 r.setRefreshCronExpression( "* 0/5 * * * ?" );
313 r.setRetentionCount( 0 );
314 r.setReleases( true );
315 r.setSnapshots( true );
316 r.setScanned( false );
317 r.setDeleteReleasedSnapshots( false );
321 private RemoteRepositoryConfiguration createRemoteRepository(String id, String url)
323 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
326 r.setLayout( "default" );
331 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
333 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
334 connector.setSourceRepoId( managedRepoId );
335 connector.setTargetRepoId( remoteRepoId );
340 private RepositoryGroupConfiguration createRepoGroup( List<String> repoIds, String repoGroupId )
342 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
343 repoGroup.setId( repoGroupId );
344 repoGroup.setRepositories( repoIds );
349 private void prepareRoleManagerMock()
350 throws RoleManagerException
352 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
353 roleManagerControl.setReturnValue( true );
354 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
355 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
356 roleManagerControl.setReturnValue( true );
357 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
358 roleManagerControl.replay();