1 package org.apache.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.xwork2.Action;
23 import net.sf.beanlib.provider.replicator.BeanReplicator;
24 import org.apache.archiva.admin.model.RepositoryAdminException;
25 import org.apache.archiva.admin.model.beans.ManagedRepository;
26 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
27 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
28 import org.apache.archiva.audit.AuditEvent;
29 import org.apache.archiva.audit.AuditListener;
30 import org.apache.archiva.metadata.repository.MetadataRepository;
31 import org.apache.archiva.metadata.repository.RepositorySession;
32 import org.apache.archiva.security.common.ArchivaRoleConstants;
33 import org.apache.archiva.webtest.memory.TestRepositorySessionFactory;
34 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
35 import org.apache.archiva.configuration.ArchivaConfiguration;
36 import org.apache.archiva.configuration.Configuration;
37 import org.apache.archiva.configuration.IndeterminateConfigurationException;
38 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
39 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
40 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
41 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
42 import org.apache.archiva.web.action.AbstractActionTestCase;
43 import org.apache.archiva.web.action.AuditEventArgumentsMatcher;
44 import org.apache.archiva.redback.role.RoleManager;
45 import org.apache.archiva.redback.role.RoleManagerException;
46 import org.codehaus.plexus.registry.RegistryException;
47 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
48 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
49 import org.easymock.MockControl;
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.Collections;
55 import java.util.List;
57 import static org.mockito.Mockito.mock;
58 import static org.mockito.Mockito.when;
61 * DeleteManagedRepositoryActionTest
65 public class DeleteManagedRepositoryActionTest
66 extends AbstractActionTestCase
68 private DeleteManagedRepositoryAction action;
70 private RoleManager roleManager;
72 private MockControl roleManagerControl;
74 private MockControl archivaConfigurationControl;
76 private ArchivaConfiguration archivaConfiguration;
78 private static final String REPO_ID = "repo-ident";
80 private File location;
82 private MockControl repositoryStatisticsManagerControl;
84 private RepositoryStatisticsManager repositoryStatisticsManager;
86 private MetadataRepository metadataRepository;
88 private RepositorySession respositorySession;
90 private MockControl metadataRepositoryControl;
92 protected void setUp()
97 // TODO use getAction .??
98 action = new DeleteManagedRepositoryAction();
100 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
101 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
103 roleManagerControl = MockControl.createControl( RoleManager.class );
104 roleManager = (RoleManager) roleManagerControl.getMock();
105 //action.setRoleManager( roleManager );
106 location = new File( "target/test/location" );
108 repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
109 repositoryStatisticsManager = (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
111 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
112 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
113 metadataRepository.removeRepository( REPO_ID );
115 respositorySession = mock( RepositorySession.class );
116 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
118 TestRepositorySessionFactory factory = new TestRepositorySessionFactory();
119 factory.setRepositorySession( respositorySession );
120 action.setRepositorySessionFactory( factory );
122 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfiguration );
123 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRoleManager( roleManager );
124 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryStatisticsManager(
125 repositoryStatisticsManager );
126 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositorySessionFactory( factory );
127 action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
129 metadataRepositoryControl.replay();
132 public void testSecureActionBundle()
133 throws SecureActionException, RepositoryAdminException
135 archivaConfiguration.getConfiguration();
136 archivaConfigurationControl.setReturnValue( new Configuration() );
137 archivaConfigurationControl.replay();
140 SecureActionBundle bundle = action.getSecureActionBundle();
141 assertTrue( bundle.requiresAuthentication() );
142 assertEquals( 1, bundle.getAuthorizationTuples().size() );
145 public void testDeleteRepositoryAndReposUnderRepoGroup()
148 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
149 repositoryStatisticsManagerControl.replay();
151 Configuration configuration = prepDeletionTest( createRepository(), 6 );
152 List<String> repoIds = new ArrayList<String>();
153 repoIds.add( REPO_ID );
154 configuration.addRepositoryGroup( createRepoGroup( repoIds, "repo.group" ) );
156 prepareRoleManagerMock();
158 assertEquals( 1, configuration.getRepositoryGroups().size() );
160 MockControl control = mockAuditListeners();
161 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
162 String status = action.deleteContents();
163 assertEquals( Action.SUCCESS, status );
165 assertTrue( configuration.getManagedRepositories().isEmpty() );
166 assertEquals( 0, configuration.getRepositoryGroups().get( 0 ).getRepositories().size() );
168 assertFalse( location.exists() );
170 repositoryStatisticsManagerControl.verify();
172 metadataRepositoryControl.verify();
175 public void testDeleteRepositoryConfirmation()
178 ManagedRepository originalRepository = createRepository();
179 Configuration configuration = createConfigurationForEditing( originalRepository );
181 archivaConfiguration.getConfiguration();
182 archivaConfigurationControl.setReturnValue( configuration );
184 Configuration stageRepoConfiguration = new Configuration();
185 stageRepoConfiguration.addManagedRepository( createStagingRepository() );
186 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
189 archivaConfiguration.getConfiguration();
190 archivaConfigurationControl.setReturnValue( configuration );
193 archivaConfigurationControl.replay();
195 action.setRepoid( REPO_ID );
198 assertEquals( REPO_ID, action.getRepoid() );
199 ManagedRepository repository = action.getRepository();
200 assertNotNull( repository );
201 assertRepositoryEquals( repository, createRepository() );
203 String status = action.execute();
204 assertEquals( Action.SUCCESS, status );
206 repository = action.getRepository();
207 assertRepositoryEquals( repository, createRepository() );
208 assertEquals( Collections.singletonList( originalRepository ),
209 action.getManagedRepositoryAdmin().getManagedRepositories() );
212 public void testDeleteRepositoryKeepContent()
215 // even when we keep the content, we don't keep the metadata at this point
216 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
217 repositoryStatisticsManagerControl.replay();
219 prepareRoleManagerMock();
221 Configuration configuration = prepDeletionTest( createRepository(), 4 );
223 MockControl control = mockAuditListeners();
225 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
227 String status = action.deleteEntry();
229 assertEquals( Action.SUCCESS, status );
231 assertTrue( configuration.getManagedRepositories().isEmpty() );
233 assertTrue( location.exists() );
235 repositoryStatisticsManagerControl.verify();
237 metadataRepositoryControl.verify();
240 private MockControl mockAuditListeners()
242 MockControl control = MockControl.createControl( AuditListener.class );
243 AuditListener listener = (AuditListener) control.getMock();
244 listener.auditEvent( new AuditEvent( REPO_ID, "guest", null, AuditEvent.DELETE_MANAGED_REPO ) );
245 control.setMatcher( new AuditEventArgumentsMatcher() );
247 action.setAuditListeners( Arrays.asList( listener ) );
249 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setAuditListeners( Arrays.asList( listener ) );
253 public void testDeleteRepositoryDeleteContent()
256 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
257 repositoryStatisticsManagerControl.replay();
259 prepareRoleManagerMock();
261 Configuration configuration = prepDeletionTest( createRepository(), 4 );
263 MockControl control = mockAuditListeners();
265 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
267 String status = action.deleteContents();
269 assertEquals( Action.SUCCESS, status );
271 assertTrue( configuration.getManagedRepositories().isEmpty() );
273 assertFalse( location.exists() );
275 repositoryStatisticsManagerControl.verify();
277 metadataRepositoryControl.verify();
280 public void testDeleteRepositoryAndAssociatedProxyConnectors()
283 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
284 repositoryStatisticsManagerControl.replay();
286 Configuration configuration = prepDeletionTest( createRepository(), 5 );
287 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
288 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
289 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
291 prepareRoleManagerMock();
293 assertEquals( 1, configuration.getProxyConnectors().size() );
295 MockControl control = mockAuditListeners();
296 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
297 String status = action.deleteContents();
299 assertEquals( Action.SUCCESS, status );
301 assertTrue( configuration.getManagedRepositories().isEmpty() );
302 assertEquals( 0, configuration.getProxyConnectors().size() );
304 assertFalse( location.exists() );
306 repositoryStatisticsManagerControl.verify();
308 metadataRepositoryControl.verify();
311 public void testDeleteRepositoryCancelled()
314 repositoryStatisticsManagerControl.replay();
316 ManagedRepository originalRepository = createRepository();
317 Configuration configuration = prepDeletionTest( originalRepository, 3 );
319 String status = action.execute();
320 assertEquals( Action.SUCCESS, status );
322 ManagedRepository repository = action.getRepository();
323 assertRepositoryEquals( repository, createRepository() );
324 assertEquals( Collections.singletonList( originalRepository ),
325 action.getManagedRepositoryAdmin().getManagedRepositories() );
327 assertTrue( location.exists() );
329 repositoryStatisticsManagerControl.verify();
333 private Configuration prepDeletionTest( ManagedRepository originalRepository, int expectCountGetConfig )
334 throws RegistryException, IndeterminateConfigurationException, RepositoryAdminException
337 //Configuration originalConfiguration =
338 // ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).getArchivaConfiguration().getConfiguration();
342 Configuration configuration = createConfigurationForEditing( originalRepository );
344 archivaConfiguration.getConfiguration();
345 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
347 Configuration stageRepoConfiguration = new Configuration();
348 stageRepoConfiguration.addManagedRepository( createStagingRepository() );
349 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
351 archivaConfiguration.save( configuration );
353 // save for staging repo delete
354 archivaConfiguration.save( configuration );
356 archivaConfigurationControl.replay();
358 action.setRepoid( REPO_ID );
361 assertEquals( REPO_ID, action.getRepoid() );
362 ManagedRepository repository = action.getRepository();
363 assertNotNull( repository );
364 assertRepositoryEquals( repository, createRepository() );
366 assertTrue( location.exists() );
367 return configuration;
370 private void assertRepositoryEquals( ManagedRepository expectedRepository, ManagedRepository actualRepository )
372 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
373 assertEquals( expectedRepository.getId(), actualRepository.getId() );
374 assertEquals( expectedRepository.getIndexDirectory(), actualRepository.getIndexDirectory() );
375 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
376 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
377 assertEquals( expectedRepository.getName(), actualRepository.getName() );
378 assertEquals( expectedRepository.getCronExpression(), actualRepository.getCronExpression() );
379 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
380 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
381 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
382 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
383 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
386 private Configuration createConfigurationForEditing( ManagedRepository repositoryConfiguration )
388 Configuration configuration = new Configuration();
389 ManagedRepositoryConfiguration managedRepositoryConfiguration =
390 new BeanReplicator().replicateBean( repositoryConfiguration, ManagedRepositoryConfiguration.class );
391 managedRepositoryConfiguration.setRefreshCronExpression( repositoryConfiguration.getCronExpression() );
392 configuration.addManagedRepository( managedRepositoryConfiguration );
393 return configuration;
396 private ManagedRepository createRepository()
398 ManagedRepository r = new ManagedRepository();
400 r.setName( "repo name" );
401 r.setLocation( location.getAbsolutePath() );
402 r.setLayout( "default" );
403 r.setCronExpression( "* 0/5 * * * ?" );
405 r.setRetentionCount( 0 );
406 r.setReleases( true );
407 r.setSnapshots( true );
408 r.setScanned( false );
409 r.setDeleteReleasedSnapshots( false );
413 private ManagedRepositoryConfiguration createStagingRepository()
415 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
416 r.setId( REPO_ID + "-stage" );
417 r.setName( "repo name" );
418 r.setLocation( location.getAbsolutePath() );
419 r.setLayout( "default" );
420 r.setRefreshCronExpression( "* 0/5 * * * ?" );
422 r.setRetentionCount( 0 );
423 r.setReleases( true );
424 r.setSnapshots( true );
425 r.setScanned( false );
426 r.setDeleteReleasedSnapshots( false );
430 private RemoteRepositoryConfiguration createRemoteRepository( String id, String url )
432 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
435 r.setLayout( "default" );
440 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
442 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
443 connector.setSourceRepoId( managedRepoId );
444 connector.setTargetRepoId( remoteRepoId );
449 private RepositoryGroupConfiguration createRepoGroup( List<String> repoIds, String repoGroupId )
451 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
452 repoGroup.setId( repoGroupId );
453 repoGroup.setRepositories( repoIds );
458 private void prepareRoleManagerMock()
459 throws RoleManagerException
461 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
462 roleManagerControl.setReturnValue( true );
463 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
464 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
465 roleManagerControl.setReturnValue( true );
466 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
467 roleManagerControl.replay();
470 protected ManagedRepositoryAdmin getManagedRepositoryAdmin()
472 return applicationContext.getBean( ManagedRepositoryAdmin.class );