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 );
189 archivaConfigurationControl.replay();
191 action.setRepoid( REPO_ID );
194 assertEquals( REPO_ID, action.getRepoid() );
195 ManagedRepository repository = action.getRepository();
196 assertNotNull( repository );
197 assertRepositoryEquals( repository, createRepository() );
199 String status = action.execute();
200 assertEquals( Action.SUCCESS, status );
202 repository = action.getRepository();
203 assertRepositoryEquals( repository, createRepository() );
204 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
207 public void testDeleteRepositoryKeepContent()
210 // even when we keep the content, we don't keep the metadata at this point
211 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
212 repositoryStatisticsManagerControl.replay();
214 prepareRoleManagerMock();
216 Configuration configuration = prepDeletionTest( createRepository(), 4 );
218 MockControl control = mockAuditListeners();
220 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
222 String status = action.deleteEntry();
224 assertEquals( Action.SUCCESS, status );
226 assertTrue( configuration.getManagedRepositories().isEmpty() );
228 assertTrue( location.exists() );
230 repositoryStatisticsManagerControl.verify();
232 metadataRepositoryControl.verify();
235 private MockControl mockAuditListeners()
237 MockControl control = MockControl.createControl( AuditListener.class );
238 AuditListener listener = (AuditListener) control.getMock();
239 listener.auditEvent( new AuditEvent( REPO_ID, "guest", null, AuditEvent.DELETE_MANAGED_REPO ) );
240 control.setMatcher( new AuditEventArgumentsMatcher() );
242 action.setAuditListeners( Arrays.asList( listener ) );
244 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setAuditListeners( Arrays.asList( listener ) );
248 public void testDeleteRepositoryDeleteContent()
251 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
252 repositoryStatisticsManagerControl.replay();
254 prepareRoleManagerMock();
256 Configuration configuration = prepDeletionTest( createRepository(), 4 );
258 MockControl control = mockAuditListeners();
260 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
262 String status = action.deleteContents();
264 assertEquals( Action.SUCCESS, status );
266 assertTrue( configuration.getManagedRepositories().isEmpty() );
268 assertFalse( location.exists() );
270 repositoryStatisticsManagerControl.verify();
272 metadataRepositoryControl.verify();
275 public void testDeleteRepositoryAndAssociatedProxyConnectors()
278 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
279 repositoryStatisticsManagerControl.replay();
281 Configuration configuration = prepDeletionTest( createRepository(), 5 );
282 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
283 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
284 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
286 prepareRoleManagerMock();
288 assertEquals( 1, configuration.getProxyConnectors().size() );
290 MockControl control = mockAuditListeners();
291 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
292 String status = action.deleteContents();
294 assertEquals( Action.SUCCESS, status );
296 assertTrue( configuration.getManagedRepositories().isEmpty() );
297 assertEquals( 0, configuration.getProxyConnectors().size() );
299 assertFalse( location.exists() );
301 repositoryStatisticsManagerControl.verify();
303 metadataRepositoryControl.verify();
306 public void testDeleteRepositoryCancelled()
309 repositoryStatisticsManagerControl.replay();
311 ManagedRepository originalRepository = createRepository();
312 Configuration configuration = prepDeletionTest( originalRepository, 3 );
314 String status = action.execute();
315 assertEquals( Action.SUCCESS, status );
317 ManagedRepository repository = action.getRepository();
318 assertRepositoryEquals( repository, createRepository() );
319 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
321 assertTrue( location.exists() );
323 repositoryStatisticsManagerControl.verify();
327 private Configuration prepDeletionTest( ManagedRepository originalRepository, int expectCountGetConfig )
328 throws RegistryException, IndeterminateConfigurationException, RepositoryAdminException
332 Configuration configuration = createConfigurationForEditing( originalRepository );
334 archivaConfiguration.getConfiguration();
335 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
337 Configuration stageRepoConfiguration = new Configuration();
338 stageRepoConfiguration.addManagedRepository( createStagingRepository() );
339 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
341 archivaConfiguration.save( configuration );
343 // save for staging repo delete
344 archivaConfiguration.save( configuration );
346 archivaConfigurationControl.replay();
348 action.setRepoid( REPO_ID );
351 assertEquals( REPO_ID, action.getRepoid() );
352 ManagedRepository repository = action.getRepository();
353 assertNotNull( repository );
354 assertRepositoryEquals( repository, createRepository() );
356 assertTrue( location.exists() );
357 return configuration;
360 private void assertRepositoryEquals( ManagedRepository expectedRepository, ManagedRepository actualRepository )
362 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
363 assertEquals( expectedRepository.getId(), actualRepository.getId() );
364 assertEquals( expectedRepository.getIndexDirectory(), actualRepository.getIndexDirectory() );
365 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
366 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
367 assertEquals( expectedRepository.getName(), actualRepository.getName() );
368 assertEquals( expectedRepository.getCronExpression(), actualRepository.getCronExpression() );
369 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
370 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
371 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
372 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
373 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
376 private Configuration createConfigurationForEditing( ManagedRepository repositoryConfiguration )
378 Configuration configuration = new Configuration();
379 configuration.addManagedRepository(
380 new BeanReplicator().replicateBean( repositoryConfiguration, ManagedRepositoryConfiguration.class ) );
381 return configuration;
384 private ManagedRepository createRepository()
386 ManagedRepository r = new ManagedRepository();
388 r.setName( "repo name" );
389 r.setLocation( location.getAbsolutePath() );
390 r.setLayout( "default" );
391 r.setCronExpression( "* 0/5 * * * ?" );
393 r.setRetentionCount( 0 );
394 r.setReleases( true );
395 r.setSnapshots( true );
396 r.setScanned( false );
397 r.setDeleteReleasedSnapshots( false );
401 private ManagedRepositoryConfiguration createStagingRepository()
403 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
404 r.setId( REPO_ID + "-stage" );
405 r.setName( "repo name" );
406 r.setLocation( location.getAbsolutePath() );
407 r.setLayout( "default" );
408 r.setRefreshCronExpression( "* 0/5 * * * ?" );
410 r.setRetentionCount( 0 );
411 r.setReleases( true );
412 r.setSnapshots( true );
413 r.setScanned( false );
414 r.setDeleteReleasedSnapshots( false );
418 private RemoteRepositoryConfiguration createRemoteRepository( String id, String url )
420 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
423 r.setLayout( "default" );
428 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
430 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
431 connector.setSourceRepoId( managedRepoId );
432 connector.setTargetRepoId( remoteRepoId );
437 private RepositoryGroupConfiguration createRepoGroup( List<String> repoIds, String repoGroupId )
439 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
440 repoGroup.setId( repoGroupId );
441 repoGroup.setRepositories( repoIds );
446 private void prepareRoleManagerMock()
447 throws RoleManagerException
449 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
450 roleManagerControl.setReturnValue( true );
451 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
452 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
453 roleManagerControl.setReturnValue( true );
454 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
455 roleManagerControl.replay();
458 protected ManagedRepositoryAdmin getManagedRepositoryAdmin()
460 return applicationContext.getBean( ManagedRepositoryAdmin.class );