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.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.managed.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.metadata.repository.memory.TestRepositorySessionFactory;
33 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
34 import org.apache.archiva.security.ArchivaRoleConstants;
35 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
36 import org.apache.maven.archiva.configuration.Configuration;
37 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
38 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
39 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
40 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
41 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
42 import org.apache.maven.archiva.web.action.AbstractActionTestCase;
43 import org.apache.maven.archiva.web.action.AuditEventArgumentsMatcher;
44 import org.codehaus.plexus.redback.role.RoleManager;
45 import org.codehaus.plexus.redback.role.RoleManagerException;
46 import org.codehaus.plexus.registry.RegistryException;
47 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
48 import org.codehaus.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();
102 action.setArchivaConfiguration( archivaConfiguration );
104 roleManagerControl = MockControl.createControl( RoleManager.class );
105 roleManager = (RoleManager) roleManagerControl.getMock();
106 //action.setRoleManager( roleManager );
107 location = new File( "target/test/location" );
109 repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
110 repositoryStatisticsManager = (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
112 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
113 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
114 metadataRepository.removeRepository( REPO_ID );
116 respositorySession = mock( RepositorySession.class );
117 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
119 TestRepositorySessionFactory factory = new TestRepositorySessionFactory();
120 factory.setRepositorySession( respositorySession );
121 action.setRepositorySessionFactory( factory );
123 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfiguration );
124 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRoleManager( roleManager );
125 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryStatisticsManager(
126 repositoryStatisticsManager );
127 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositorySessionFactory( factory );
128 action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
130 metadataRepositoryControl.replay();
133 public void testSecureActionBundle()
134 throws SecureActionException, RepositoryAdminException
136 archivaConfiguration.getConfiguration();
137 archivaConfigurationControl.setReturnValue( new Configuration() );
138 archivaConfigurationControl.replay();
141 SecureActionBundle bundle = action.getSecureActionBundle();
142 assertTrue( bundle.requiresAuthentication() );
143 assertEquals( 1, bundle.getAuthorizationTuples().size() );
146 public void testDeleteRepositoryAndReposUnderRepoGroup()
149 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
150 repositoryStatisticsManagerControl.replay();
152 Configuration configuration = prepDeletionTest( createRepository(), 6 );
153 List<String> repoIds = new ArrayList<String>();
154 repoIds.add( REPO_ID );
155 configuration.addRepositoryGroup( createRepoGroup( repoIds, "repo.group" ) );
157 prepareRoleManagerMock();
159 assertEquals( 1, configuration.getRepositoryGroups().size() );
161 MockControl control = mockAuditListeners();
162 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
163 String status = action.deleteContents();
164 assertEquals( Action.SUCCESS, status );
166 assertTrue( configuration.getManagedRepositories().isEmpty() );
167 assertEquals( 0, configuration.getRepositoryGroups().get( 0 ).getRepositories().size() );
169 assertFalse( location.exists() );
171 repositoryStatisticsManagerControl.verify();
173 metadataRepositoryControl.verify();
176 public void testDeleteRepositoryConfirmation()
179 ManagedRepository originalRepository = createRepository();
180 Configuration configuration = createConfigurationForEditing( originalRepository );
182 archivaConfiguration.getConfiguration();
183 archivaConfigurationControl.setReturnValue( configuration );
185 Configuration stageRepoConfiguration = new Configuration();
186 stageRepoConfiguration.addManagedRepository( createStagingRepository() );
187 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
190 archivaConfiguration.getConfiguration();
191 archivaConfigurationControl.setReturnValue( configuration );
194 archivaConfigurationControl.replay();
196 action.setRepoid( REPO_ID );
199 assertEquals( REPO_ID, action.getRepoid() );
200 ManagedRepository repository = action.getRepository();
201 assertNotNull( repository );
202 assertRepositoryEquals( repository, createRepository() );
204 String status = action.execute();
205 assertEquals( Action.SUCCESS, status );
207 repository = action.getRepository();
208 assertRepositoryEquals( repository, createRepository() );
209 assertEquals( Collections.singletonList( originalRepository ),
210 action.getManagedRepositoryAdmin().getManagedRepositories() );
213 public void testDeleteRepositoryKeepContent()
216 // even when we keep the content, we don't keep the metadata at this point
217 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
218 repositoryStatisticsManagerControl.replay();
220 prepareRoleManagerMock();
222 Configuration configuration = prepDeletionTest( createRepository(), 4 );
224 MockControl control = mockAuditListeners();
226 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
228 String status = action.deleteEntry();
230 assertEquals( Action.SUCCESS, status );
232 assertTrue( configuration.getManagedRepositories().isEmpty() );
234 assertTrue( location.exists() );
236 repositoryStatisticsManagerControl.verify();
238 metadataRepositoryControl.verify();
241 private MockControl mockAuditListeners()
243 MockControl control = MockControl.createControl( AuditListener.class );
244 AuditListener listener = (AuditListener) control.getMock();
245 listener.auditEvent( new AuditEvent( REPO_ID, "guest", null, AuditEvent.DELETE_MANAGED_REPO ) );
246 control.setMatcher( new AuditEventArgumentsMatcher() );
248 action.setAuditListeners( Arrays.asList( listener ) );
250 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setAuditListeners( Arrays.asList( listener ) );
254 public void testDeleteRepositoryDeleteContent()
257 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
258 repositoryStatisticsManagerControl.replay();
260 prepareRoleManagerMock();
262 Configuration configuration = prepDeletionTest( createRepository(), 4 );
264 MockControl control = mockAuditListeners();
266 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
268 String status = action.deleteContents();
270 assertEquals( Action.SUCCESS, status );
272 assertTrue( configuration.getManagedRepositories().isEmpty() );
274 assertFalse( location.exists() );
276 repositoryStatisticsManagerControl.verify();
278 metadataRepositoryControl.verify();
281 public void testDeleteRepositoryAndAssociatedProxyConnectors()
284 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
285 repositoryStatisticsManagerControl.replay();
287 Configuration configuration = prepDeletionTest( createRepository(), 5 );
288 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
289 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
290 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
292 prepareRoleManagerMock();
294 assertEquals( 1, configuration.getProxyConnectors().size() );
296 MockControl control = mockAuditListeners();
297 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
298 String status = action.deleteContents();
300 assertEquals( Action.SUCCESS, status );
302 assertTrue( configuration.getManagedRepositories().isEmpty() );
303 assertEquals( 0, configuration.getProxyConnectors().size() );
305 assertFalse( location.exists() );
307 repositoryStatisticsManagerControl.verify();
309 metadataRepositoryControl.verify();
312 public void testDeleteRepositoryCancelled()
315 repositoryStatisticsManagerControl.replay();
317 ManagedRepository originalRepository = createRepository();
318 Configuration configuration = prepDeletionTest( originalRepository, 3 );
320 String status = action.execute();
321 assertEquals( Action.SUCCESS, status );
323 ManagedRepository repository = action.getRepository();
324 assertRepositoryEquals( repository, createRepository() );
325 assertEquals( Collections.singletonList( originalRepository ),
326 action.getManagedRepositoryAdmin().getManagedRepositories() );
328 assertTrue( location.exists() );
330 repositoryStatisticsManagerControl.verify();
334 private Configuration prepDeletionTest( ManagedRepository originalRepository, int expectCountGetConfig )
335 throws RegistryException, IndeterminateConfigurationException, RepositoryAdminException
338 //Configuration originalConfiguration =
339 // ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).getArchivaConfiguration().getConfiguration();
343 Configuration configuration = createConfigurationForEditing( originalRepository );
345 archivaConfiguration.getConfiguration();
346 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
348 Configuration stageRepoConfiguration = new Configuration();
349 stageRepoConfiguration.addManagedRepository( createStagingRepository() );
350 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
352 archivaConfiguration.save( configuration );
354 // save for staging repo delete
355 archivaConfiguration.save( configuration );
357 archivaConfigurationControl.replay();
359 action.setRepoid( REPO_ID );
362 assertEquals( REPO_ID, action.getRepoid() );
363 ManagedRepository repository = action.getRepository();
364 assertNotNull( repository );
365 assertRepositoryEquals( repository, createRepository() );
367 assertTrue( location.exists() );
368 return configuration;
371 private void assertRepositoryEquals( ManagedRepository expectedRepository, ManagedRepository actualRepository )
373 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
374 assertEquals( expectedRepository.getId(), actualRepository.getId() );
375 assertEquals( expectedRepository.getIndexDirectory(), actualRepository.getIndexDirectory() );
376 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
377 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
378 assertEquals( expectedRepository.getName(), actualRepository.getName() );
379 assertEquals( expectedRepository.getCronExpression(), actualRepository.getCronExpression() );
380 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
381 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
382 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
383 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
384 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
387 private Configuration createConfigurationForEditing( ManagedRepository repositoryConfiguration )
389 Configuration configuration = new Configuration();
390 ManagedRepositoryConfiguration managedRepositoryConfiguration =
391 new BeanReplicator().replicateBean( repositoryConfiguration, ManagedRepositoryConfiguration.class );
392 managedRepositoryConfiguration.setRefreshCronExpression( repositoryConfiguration.getCronExpression() );
393 configuration.addManagedRepository( managedRepositoryConfiguration );
394 return configuration;
397 private ManagedRepository createRepository()
399 ManagedRepository r = new ManagedRepository();
401 r.setName( "repo name" );
402 r.setLocation( location.getAbsolutePath() );
403 r.setLayout( "default" );
404 r.setCronExpression( "* 0/5 * * * ?" );
406 r.setRetentionCount( 0 );
407 r.setReleases( true );
408 r.setSnapshots( true );
409 r.setScanned( false );
410 r.setDeleteReleasedSnapshots( false );
414 private ManagedRepositoryConfiguration createStagingRepository()
416 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
417 r.setId( REPO_ID + "-stage" );
418 r.setName( "repo name" );
419 r.setLocation( location.getAbsolutePath() );
420 r.setLayout( "default" );
421 r.setRefreshCronExpression( "* 0/5 * * * ?" );
423 r.setRetentionCount( 0 );
424 r.setReleases( true );
425 r.setSnapshots( true );
426 r.setScanned( false );
427 r.setDeleteReleasedSnapshots( false );
431 private RemoteRepositoryConfiguration createRemoteRepository( String id, String url )
433 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
436 r.setLayout( "default" );
441 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
443 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
444 connector.setSourceRepoId( managedRepoId );
445 connector.setTargetRepoId( remoteRepoId );
450 private RepositoryGroupConfiguration createRepoGroup( List<String> repoIds, String repoGroupId )
452 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
453 repoGroup.setId( repoGroupId );
454 repoGroup.setRepositories( repoIds );
459 private void prepareRoleManagerMock()
460 throws RoleManagerException
462 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
463 roleManagerControl.setReturnValue( true );
464 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
465 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
466 roleManagerControl.setReturnValue( true );
467 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
468 roleManagerControl.replay();
471 protected ManagedRepositoryAdmin getManagedRepositoryAdmin()
473 return applicationContext.getBean( ManagedRepositoryAdmin.class );