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.configuration.ArchivaConfiguration;
31 import org.apache.archiva.configuration.Configuration;
32 import org.apache.archiva.configuration.IndeterminateConfigurationException;
33 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
34 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
35 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
36 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
37 import org.apache.archiva.metadata.repository.MetadataRepository;
38 import org.apache.archiva.metadata.repository.RepositorySession;
39 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
40 import org.apache.archiva.redback.components.registry.RegistryException;
41 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
42 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
43 import org.apache.archiva.redback.role.RoleManager;
44 import org.apache.archiva.redback.role.RoleManagerException;
45 import org.apache.archiva.security.common.ArchivaRoleConstants;
46 import org.apache.archiva.web.action.AbstractActionTestCase;
47 import org.apache.archiva.web.action.AuditEventArgumentsMatcher;
48 import org.apache.archiva.webtest.memory.TestRepositorySessionFactory;
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( System.getProperty( "basedir" ), "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 );
188 archivaConfiguration.getConfiguration();
189 archivaConfigurationControl.setReturnValue( configuration );
191 archivaConfigurationControl.replay();
193 action.setRepoid( REPO_ID );
196 assertEquals( REPO_ID, action.getRepoid() );
197 ManagedRepository repository = action.getRepository();
198 assertNotNull( repository );
199 assertRepositoryEquals( repository, createRepository() );
201 String status = action.execute();
202 assertEquals( Action.SUCCESS, status );
204 repository = action.getRepository();
205 assertRepositoryEquals( repository, createRepository() );
206 assertEquals( Collections.singletonList( originalRepository ),
207 action.getManagedRepositoryAdmin().getManagedRepositories() );
210 public void testDeleteRepositoryKeepContent()
213 // even when we keep the content, we don't keep the metadata at this point
214 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
215 repositoryStatisticsManagerControl.replay();
217 prepareRoleManagerMock();
219 Configuration configuration = prepDeletionTest( createRepository(), 4 );
221 MockControl control = mockAuditListeners();
223 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
225 String status = action.deleteEntry();
227 assertEquals( Action.SUCCESS, status );
229 assertTrue( configuration.getManagedRepositories().isEmpty() );
231 assertTrue( location.exists() );
233 repositoryStatisticsManagerControl.verify();
235 metadataRepositoryControl.verify();
238 private MockControl mockAuditListeners()
240 MockControl control = MockControl.createControl( AuditListener.class );
241 AuditListener listener = (AuditListener) control.getMock();
242 listener.auditEvent( new AuditEvent( REPO_ID, "guest", null, AuditEvent.DELETE_MANAGED_REPO ) );
243 control.setMatcher( new AuditEventArgumentsMatcher() );
245 action.setAuditListeners( Arrays.asList( listener ) );
247 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setAuditListeners( Arrays.asList( listener ) );
251 public void testDeleteRepositoryDeleteContent()
254 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
255 repositoryStatisticsManagerControl.replay();
257 prepareRoleManagerMock();
259 Configuration configuration = prepDeletionTest( createRepository(), 4 );
261 MockControl control = mockAuditListeners();
263 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
265 String status = action.deleteContents();
267 assertEquals( Action.SUCCESS, status );
269 assertTrue( configuration.getManagedRepositories().isEmpty() );
271 assertFalse( location.exists() );
273 repositoryStatisticsManagerControl.verify();
275 metadataRepositoryControl.verify();
278 public void testDeleteRepositoryAndAssociatedProxyConnectors()
281 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
282 repositoryStatisticsManagerControl.replay();
284 Configuration configuration = prepDeletionTest( createRepository(), 5 );
285 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
286 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
287 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
289 prepareRoleManagerMock();
291 assertEquals( 1, configuration.getProxyConnectors().size() );
293 MockControl control = mockAuditListeners();
294 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
295 String status = action.deleteContents();
297 assertEquals( Action.SUCCESS, status );
299 assertTrue( configuration.getManagedRepositories().isEmpty() );
300 assertEquals( 0, configuration.getProxyConnectors().size() );
302 assertFalse( location.exists() );
304 repositoryStatisticsManagerControl.verify();
306 metadataRepositoryControl.verify();
309 public void testDeleteRepositoryCancelled()
312 repositoryStatisticsManagerControl.replay();
314 ManagedRepository originalRepository = createRepository();
315 Configuration configuration = prepDeletionTest( originalRepository, 3 );
317 String status = action.execute();
318 assertEquals( Action.SUCCESS, status );
320 ManagedRepository repository = action.getRepository();
321 assertRepositoryEquals( repository, createRepository() );
322 assertEquals( Collections.singletonList( originalRepository ),
323 action.getManagedRepositoryAdmin().getManagedRepositories() );
325 assertTrue( location.exists() );
327 repositoryStatisticsManagerControl.verify();
331 private Configuration prepDeletionTest( ManagedRepository originalRepository, int expectCountGetConfig )
332 throws RegistryException, IndeterminateConfigurationException, RepositoryAdminException
335 //Configuration originalConfiguration =
336 // ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).getArchivaConfiguration().getConfiguration();
340 Configuration configuration = createConfigurationForEditing( originalRepository );
342 archivaConfiguration.getConfiguration();
343 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
345 Configuration stageRepoConfiguration = new Configuration();
346 stageRepoConfiguration.addManagedRepository( createStagingRepository() );
347 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
349 archivaConfiguration.save( configuration );
351 // save for staging repo delete
352 archivaConfiguration.save( configuration );
354 archivaConfigurationControl.replay();
356 action.setRepoid( REPO_ID );
359 assertEquals( REPO_ID, action.getRepoid() );
360 ManagedRepository repository = action.getRepository();
361 assertNotNull( repository );
362 assertRepositoryEquals( repository, createRepository() );
364 assertTrue( location.exists() );
365 return configuration;
368 private void assertRepositoryEquals( ManagedRepository expectedRepository, ManagedRepository actualRepository )
370 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
371 assertEquals( expectedRepository.getId(), actualRepository.getId() );
372 assertEquals( expectedRepository.getIndexDirectory(), actualRepository.getIndexDirectory() );
373 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
374 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
375 assertEquals( expectedRepository.getName(), actualRepository.getName() );
376 assertEquals( expectedRepository.getCronExpression(), actualRepository.getCronExpression() );
377 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
378 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
379 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
380 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
381 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
384 private Configuration createConfigurationForEditing( ManagedRepository repositoryConfiguration )
386 Configuration configuration = new Configuration();
387 ManagedRepositoryConfiguration managedRepositoryConfiguration =
388 new BeanReplicator().replicateBean( repositoryConfiguration, ManagedRepositoryConfiguration.class );
389 managedRepositoryConfiguration.setRefreshCronExpression( repositoryConfiguration.getCronExpression() );
390 configuration.addManagedRepository( managedRepositoryConfiguration );
391 return configuration;
394 private ManagedRepository createRepository()
396 ManagedRepository r = new ManagedRepository();
398 r.setName( "repo name" );
399 r.setLocation( location.getAbsolutePath() );
400 r.setLayout( "default" );
401 r.setCronExpression( "* 0/5 * * * ?" );
403 r.setRetentionCount( 0 );
404 r.setReleases( true );
405 r.setSnapshots( true );
406 r.setScanned( false );
407 r.setDeleteReleasedSnapshots( false );
411 private ManagedRepositoryConfiguration createStagingRepository()
413 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
414 r.setId( REPO_ID + "-stage" );
415 r.setName( "repo name" );
416 r.setLocation( location.getAbsolutePath() );
417 r.setLayout( "default" );
418 r.setRefreshCronExpression( "* 0/5 * * * ?" );
420 r.setRetentionCount( 0 );
421 r.setReleases( true );
422 r.setSnapshots( true );
423 r.setScanned( false );
424 r.setDeleteReleasedSnapshots( false );
428 private RemoteRepositoryConfiguration createRemoteRepository( String id, String url )
430 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
433 r.setLayout( "default" );
438 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
440 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
441 connector.setSourceRepoId( managedRepoId );
442 connector.setTargetRepoId( remoteRepoId );
447 private RepositoryGroupConfiguration createRepoGroup( List<String> repoIds, String repoGroupId )
449 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
450 repoGroup.setId( repoGroupId );
451 repoGroup.setRepositories( repoIds );
456 private void prepareRoleManagerMock()
457 throws RoleManagerException
459 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
460 roleManagerControl.setReturnValue( true );
461 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
462 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
463 roleManagerControl.setReturnValue( true );
464 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
465 roleManagerControl.replay();
468 protected ManagedRepositoryAdmin getManagedRepositoryAdmin()
470 return applicationContext.getBean( ManagedRepositoryAdmin.class );