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 org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
24 import org.apache.archiva.admin.repository.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.audit.AuditEvent;
26 import org.apache.archiva.audit.AuditListener;
27 import org.apache.archiva.metadata.repository.MetadataRepository;
28 import org.apache.archiva.metadata.repository.RepositorySession;
29 import org.apache.archiva.metadata.repository.memory.TestRepositorySessionFactory;
30 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.Configuration;
33 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
34 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
36 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
37 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
38 import org.apache.archiva.security.ArchivaRoleConstants;
39 import org.apache.maven.archiva.web.action.AbstractActionTestCase;
40 import org.apache.maven.archiva.web.action.AuditEventArgumentsMatcher;
41 import org.codehaus.plexus.redback.role.RoleManager;
42 import org.codehaus.plexus.redback.role.RoleManagerException;
43 import org.codehaus.plexus.registry.RegistryException;
44 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
45 import org.codehaus.redback.integration.interceptor.SecureActionException;
46 import org.easymock.MockControl;
49 import java.util.ArrayList;
50 import java.util.Arrays;
51 import java.util.Collections;
52 import java.util.List;
54 import static org.mockito.Mockito.mock;
55 import static org.mockito.Mockito.when;
58 * DeleteManagedRepositoryActionTest
62 public class DeleteManagedRepositoryActionTest
63 extends AbstractActionTestCase
65 private DeleteManagedRepositoryAction action;
67 private RoleManager roleManager;
69 private MockControl roleManagerControl;
71 private MockControl archivaConfigurationControl;
73 private ArchivaConfiguration archivaConfiguration;
75 private static final String REPO_ID = "repo-ident";
77 private File location;
79 private MockControl repositoryStatisticsManagerControl;
81 private RepositoryStatisticsManager repositoryStatisticsManager;
83 private MetadataRepository metadataRepository;
85 private RepositorySession respositorySession;
87 private MockControl metadataRepositoryControl;
89 protected void setUp()
94 // TODO use getAction .??
95 action = new DeleteManagedRepositoryAction();
97 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
98 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
99 action.setArchivaConfiguration( archivaConfiguration );
101 roleManagerControl = MockControl.createControl( RoleManager.class );
102 roleManager = (RoleManager) roleManagerControl.getMock();
103 //action.setRoleManager( roleManager );
104 location = new File( "target/test/location" );
106 repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
107 repositoryStatisticsManager = (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
109 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
110 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
111 metadataRepository.removeRepository( REPO_ID );
113 respositorySession = mock( RepositorySession.class );
114 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
116 TestRepositorySessionFactory factory = new TestRepositorySessionFactory();
117 factory.setRepositorySession( respositorySession );
118 action.setRepositorySessionFactory( factory );
120 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfiguration );
121 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRoleManager( roleManager );
122 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryStatisticsManager(
123 repositoryStatisticsManager );
124 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositorySessionFactory( factory );
125 action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
127 metadataRepositoryControl.replay();
130 public void testSecureActionBundle()
131 throws SecureActionException
133 archivaConfiguration.getConfiguration();
134 archivaConfigurationControl.setReturnValue( new Configuration() );
135 archivaConfigurationControl.replay();
138 SecureActionBundle bundle = action.getSecureActionBundle();
139 assertTrue( bundle.requiresAuthentication() );
140 assertEquals( 1, bundle.getAuthorizationTuples().size() );
143 public void testDeleteRepositoryAndReposUnderRepoGroup()
146 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
147 repositoryStatisticsManagerControl.replay();
149 Configuration configuration = prepDeletionTest( createRepository(), 6 );
150 List<String> repoIds = new ArrayList<String>();
151 repoIds.add( REPO_ID );
152 configuration.addRepositoryGroup( createRepoGroup( repoIds, "repo.group" ) );
154 prepareRoleManagerMock();
156 assertEquals( 1, configuration.getRepositoryGroups().size() );
158 MockControl control = mockAuditListeners();
159 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
160 String status = action.deleteContents();
161 assertEquals( Action.SUCCESS, status );
163 assertTrue( configuration.getManagedRepositories().isEmpty() );
164 assertEquals( 0, configuration.getRepositoryGroups().get( 0 ).getRepositories().size() );
166 assertFalse( location.exists() );
168 repositoryStatisticsManagerControl.verify();
170 metadataRepositoryControl.verify();
173 public void testDeleteRepositoryConfirmation()
176 ManagedRepositoryConfiguration originalRepository = createRepository();
177 Configuration configuration = createConfigurationForEditing( originalRepository );
179 archivaConfiguration.getConfiguration();
180 archivaConfigurationControl.setReturnValue( configuration );
182 Configuration stageRepoConfiguration = new Configuration();
183 stageRepoConfiguration.addManagedRepository( createStagingRepository() );
184 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
186 archivaConfigurationControl.replay();
188 action.setRepoid( REPO_ID );
191 assertEquals( REPO_ID, action.getRepoid() );
192 ManagedRepositoryConfiguration repository = action.getRepository();
193 assertNotNull( repository );
194 assertRepositoryEquals( repository, createRepository() );
196 String status = action.execute();
197 assertEquals( Action.SUCCESS, status );
199 repository = action.getRepository();
200 assertRepositoryEquals( repository, createRepository() );
201 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
204 public void testDeleteRepositoryKeepContent()
207 // even when we keep the content, we don't keep the metadata at this point
208 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
209 repositoryStatisticsManagerControl.replay();
211 prepareRoleManagerMock();
213 Configuration configuration = prepDeletionTest( createRepository(), 4 );
215 MockControl control = mockAuditListeners();
217 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
219 String status = action.deleteEntry();
221 assertEquals( Action.SUCCESS, status );
223 assertTrue( configuration.getManagedRepositories().isEmpty() );
225 assertTrue( location.exists() );
227 repositoryStatisticsManagerControl.verify();
229 metadataRepositoryControl.verify();
232 private MockControl mockAuditListeners()
234 MockControl control = MockControl.createControl( AuditListener.class );
235 AuditListener listener = (AuditListener) control.getMock();
236 listener.auditEvent( new AuditEvent( REPO_ID, "guest", null, AuditEvent.DELETE_MANAGED_REPO ) );
237 control.setMatcher( new AuditEventArgumentsMatcher() );
239 action.setAuditListeners( Arrays.asList( listener ) );
241 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setAuditListeners( Arrays.asList( listener ) );
245 public void testDeleteRepositoryDeleteContent()
248 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
249 repositoryStatisticsManagerControl.replay();
251 prepareRoleManagerMock();
253 Configuration configuration = prepDeletionTest( createRepository(), 4 );
255 MockControl control = mockAuditListeners();
257 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
259 String status = action.deleteContents();
261 assertEquals( Action.SUCCESS, status );
263 assertTrue( configuration.getManagedRepositories().isEmpty() );
265 assertFalse( location.exists() );
267 repositoryStatisticsManagerControl.verify();
269 metadataRepositoryControl.verify();
272 public void testDeleteRepositoryAndAssociatedProxyConnectors()
275 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
276 repositoryStatisticsManagerControl.replay();
278 Configuration configuration = prepDeletionTest( createRepository(), 5 );
279 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
280 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
281 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
283 prepareRoleManagerMock();
285 assertEquals( 1, configuration.getProxyConnectors().size() );
287 MockControl control = mockAuditListeners();
288 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
289 String status = action.deleteContents();
291 assertEquals( Action.SUCCESS, status );
293 assertTrue( configuration.getManagedRepositories().isEmpty() );
294 assertEquals( 0, configuration.getProxyConnectors().size() );
296 assertFalse( location.exists() );
298 repositoryStatisticsManagerControl.verify();
300 metadataRepositoryControl.verify();
303 public void testDeleteRepositoryCancelled()
306 repositoryStatisticsManagerControl.replay();
308 ManagedRepositoryConfiguration originalRepository = createRepository();
309 Configuration configuration = prepDeletionTest( originalRepository, 3 );
311 String status = action.execute();
312 assertEquals( Action.SUCCESS, status );
314 ManagedRepositoryConfiguration repository = action.getRepository();
315 assertRepositoryEquals( repository, createRepository() );
316 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
318 assertTrue( location.exists() );
320 repositoryStatisticsManagerControl.verify();
324 private Configuration prepDeletionTest( ManagedRepositoryConfiguration originalRepository,
325 int expectCountGetConfig )
326 throws RegistryException, IndeterminateConfigurationException
330 Configuration configuration = createConfigurationForEditing( originalRepository );
332 archivaConfiguration.getConfiguration();
333 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
335 Configuration stageRepoConfiguration = new Configuration();
336 stageRepoConfiguration.addManagedRepository( createStagingRepository() );
337 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
339 archivaConfiguration.save( configuration );
341 // save for staging repo delete
342 archivaConfiguration.save( configuration );
344 archivaConfigurationControl.replay();
346 action.setRepoid( REPO_ID );
349 assertEquals( REPO_ID, action.getRepoid() );
350 ManagedRepositoryConfiguration repository = action.getRepository();
351 assertNotNull( repository );
352 assertRepositoryEquals( repository, createRepository() );
354 assertTrue( location.exists() );
355 return configuration;
358 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
359 ManagedRepositoryConfiguration actualRepository )
361 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
362 assertEquals( expectedRepository.getId(), actualRepository.getId() );
363 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
364 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
365 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
366 assertEquals( expectedRepository.getName(), actualRepository.getName() );
367 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
368 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
369 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
370 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
371 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
372 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
375 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
377 Configuration configuration = new Configuration();
378 configuration.addManagedRepository( repositoryConfiguration );
379 return configuration;
382 private ManagedRepositoryConfiguration createRepository()
384 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
386 r.setName( "repo name" );
387 r.setLocation( location.getAbsolutePath() );
388 r.setLayout( "default" );
389 r.setRefreshCronExpression( "* 0/5 * * * ?" );
391 r.setRetentionCount( 0 );
392 r.setReleases( true );
393 r.setSnapshots( true );
394 r.setScanned( false );
395 r.setDeleteReleasedSnapshots( false );
399 private ManagedRepositoryConfiguration createStagingRepository()
401 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
402 r.setId( REPO_ID + "-stage" );
403 r.setName( "repo name" );
404 r.setLocation( location.getAbsolutePath() );
405 r.setLayout( "default" );
406 r.setRefreshCronExpression( "* 0/5 * * * ?" );
408 r.setRetentionCount( 0 );
409 r.setReleases( true );
410 r.setSnapshots( true );
411 r.setScanned( false );
412 r.setDeleteReleasedSnapshots( false );
416 private RemoteRepositoryConfiguration createRemoteRepository( String id, String url )
418 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
421 r.setLayout( "default" );
426 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
428 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
429 connector.setSourceRepoId( managedRepoId );
430 connector.setTargetRepoId( remoteRepoId );
435 private RepositoryGroupConfiguration createRepoGroup( List<String> repoIds, String repoGroupId )
437 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
438 repoGroup.setId( repoGroupId );
439 repoGroup.setRepositories( repoIds );
444 private void prepareRoleManagerMock()
445 throws RoleManagerException
447 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
448 roleManagerControl.setReturnValue( true );
449 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
450 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
451 roleManagerControl.setReturnValue( true );
452 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
453 roleManagerControl.replay();
456 protected ManagedRepositoryAdmin getManagedRepositoryAdmin()
458 return applicationContext.getBean( ManagedRepositoryAdmin.class );