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;
56 import org.junit.Before;
57 import org.junit.Test;
59 import static org.mockito.Mockito.mock;
60 import static org.mockito.Mockito.when;
63 * DeleteManagedRepositoryActionTest
67 public class DeleteManagedRepositoryActionTest
68 extends AbstractActionTestCase
70 private DeleteManagedRepositoryAction action;
72 private RoleManager roleManager;
74 private MockControl roleManagerControl;
76 private MockControl archivaConfigurationControl;
78 private ArchivaConfiguration archivaConfiguration;
80 private static final String REPO_ID = "repo-ident";
82 private File location;
84 private MockControl repositoryStatisticsManagerControl;
86 private RepositoryStatisticsManager repositoryStatisticsManager;
88 private MetadataRepository metadataRepository;
90 private RepositorySession respositorySession;
92 private MockControl metadataRepositoryControl;
101 // TODO use getAction .??
102 action = new DeleteManagedRepositoryAction();
104 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
105 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
107 roleManagerControl = MockControl.createControl( RoleManager.class );
108 roleManager = (RoleManager) roleManagerControl.getMock();
109 //action.setRoleManager( roleManager );
110 location = new File( System.getProperty( "basedir" ), "target/test/location" );
112 repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
113 repositoryStatisticsManager = (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
115 metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
116 metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
117 metadataRepository.removeRepository( REPO_ID );
119 respositorySession = mock( RepositorySession.class );
120 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
122 TestRepositorySessionFactory factory = new TestRepositorySessionFactory();
123 factory.setRepositorySession( respositorySession );
124 action.setRepositorySessionFactory( factory );
126 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfiguration );
127 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRoleManager( roleManager );
128 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryStatisticsManager(
129 repositoryStatisticsManager );
130 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositorySessionFactory( factory );
131 action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
133 metadataRepositoryControl.replay();
137 public void testSecureActionBundle()
138 throws SecureActionException, RepositoryAdminException
140 archivaConfiguration.getConfiguration();
141 archivaConfigurationControl.setReturnValue( new Configuration() );
142 archivaConfigurationControl.replay();
145 SecureActionBundle bundle = action.getSecureActionBundle();
146 assertTrue( bundle.requiresAuthentication() );
147 assertEquals( 1, bundle.getAuthorizationTuples().size() );
151 public void testDeleteRepositoryAndReposUnderRepoGroup()
154 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
155 repositoryStatisticsManagerControl.replay();
157 Configuration configuration = prepDeletionTest( createRepository(), 6 );
158 List<String> repoIds = new ArrayList<String>();
159 repoIds.add( REPO_ID );
160 configuration.addRepositoryGroup( createRepoGroup( repoIds, "repo.group" ) );
162 prepareRoleManagerMock();
164 assertEquals( 1, configuration.getRepositoryGroups().size() );
166 MockControl control = mockAuditListeners();
167 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
168 String status = action.deleteContents();
169 assertEquals( Action.SUCCESS, status );
171 assertTrue( configuration.getManagedRepositories().isEmpty() );
172 assertEquals( 0, configuration.getRepositoryGroups().get( 0 ).getRepositories().size() );
174 assertFalse( location.exists() );
176 repositoryStatisticsManagerControl.verify();
178 metadataRepositoryControl.verify();
182 public void testDeleteRepositoryConfirmation()
185 ManagedRepository originalRepository = createRepository();
186 Configuration configuration = createConfigurationForEditing( originalRepository );
188 archivaConfiguration.getConfiguration();
189 archivaConfigurationControl.setReturnValue( configuration );
191 Configuration stageRepoConfiguration = new Configuration();
192 stageRepoConfiguration.addManagedRepository( createStagingRepository() );
193 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
195 archivaConfiguration.getConfiguration();
196 archivaConfigurationControl.setReturnValue( configuration );
198 archivaConfigurationControl.replay();
200 action.setRepoid( REPO_ID );
203 assertEquals( REPO_ID, action.getRepoid() );
204 ManagedRepository repository = action.getRepository();
205 assertNotNull( repository );
206 assertRepositoryEquals( repository, createRepository() );
208 String status = action.execute();
209 assertEquals( Action.SUCCESS, status );
211 repository = action.getRepository();
212 assertRepositoryEquals( repository, createRepository() );
213 assertEquals( Collections.singletonList( originalRepository ),
214 action.getManagedRepositoryAdmin().getManagedRepositories() );
218 public void testDeleteRepositoryKeepContent()
221 // even when we keep the content, we don't keep the metadata at this point
222 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
223 repositoryStatisticsManagerControl.replay();
225 prepareRoleManagerMock();
227 Configuration configuration = prepDeletionTest( createRepository(), 4 );
229 MockControl control = mockAuditListeners();
231 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
233 String status = action.deleteEntry();
235 assertEquals( Action.SUCCESS, status );
237 assertTrue( configuration.getManagedRepositories().isEmpty() );
239 assertTrue( location.exists() );
241 repositoryStatisticsManagerControl.verify();
243 metadataRepositoryControl.verify();
246 private MockControl mockAuditListeners()
248 MockControl control = MockControl.createControl( AuditListener.class );
249 AuditListener listener = (AuditListener) control.getMock();
250 listener.auditEvent( new AuditEvent( REPO_ID, "guest", null, AuditEvent.DELETE_MANAGED_REPO ) );
251 control.setMatcher( new AuditEventArgumentsMatcher() );
253 action.setAuditListeners( Arrays.asList( listener ) );
255 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setAuditListeners( Arrays.asList( listener ) );
260 public void testDeleteRepositoryDeleteContent()
263 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
264 repositoryStatisticsManagerControl.replay();
266 prepareRoleManagerMock();
268 Configuration configuration = prepDeletionTest( createRepository(), 4 );
270 MockControl control = mockAuditListeners();
272 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
274 String status = action.deleteContents();
276 assertEquals( Action.SUCCESS, status );
278 assertTrue( configuration.getManagedRepositories().isEmpty() );
280 assertFalse( location.exists() );
282 repositoryStatisticsManagerControl.verify();
284 metadataRepositoryControl.verify();
288 public void testDeleteRepositoryAndAssociatedProxyConnectors()
291 repositoryStatisticsManager.deleteStatistics( metadataRepository, REPO_ID );
292 repositoryStatisticsManagerControl.replay();
294 Configuration configuration = prepDeletionTest( createRepository(), 5 );
295 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
296 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
297 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
299 prepareRoleManagerMock();
301 assertEquals( 1, configuration.getProxyConnectors().size() );
303 MockControl control = mockAuditListeners();
304 when( respositorySession.getRepository() ).thenReturn( metadataRepository );
305 String status = action.deleteContents();
307 assertEquals( Action.SUCCESS, status );
309 assertTrue( configuration.getManagedRepositories().isEmpty() );
310 assertEquals( 0, configuration.getProxyConnectors().size() );
312 assertFalse( location.exists() );
314 repositoryStatisticsManagerControl.verify();
316 metadataRepositoryControl.verify();
320 public void testDeleteRepositoryCancelled()
323 repositoryStatisticsManagerControl.replay();
325 ManagedRepository originalRepository = createRepository();
326 Configuration configuration = prepDeletionTest( originalRepository, 3 );
328 String status = action.execute();
329 assertEquals( Action.SUCCESS, status );
331 ManagedRepository repository = action.getRepository();
332 assertRepositoryEquals( repository, createRepository() );
333 assertEquals( Collections.singletonList( originalRepository ),
334 action.getManagedRepositoryAdmin().getManagedRepositories() );
336 assertTrue( location.exists() );
338 repositoryStatisticsManagerControl.verify();
342 private Configuration prepDeletionTest( ManagedRepository originalRepository, int expectCountGetConfig )
343 throws RegistryException, IndeterminateConfigurationException, RepositoryAdminException
346 //Configuration originalConfiguration =
347 // ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).getArchivaConfiguration().getConfiguration();
351 Configuration configuration = createConfigurationForEditing( originalRepository );
353 archivaConfiguration.getConfiguration();
354 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
356 Configuration stageRepoConfiguration = new Configuration();
357 stageRepoConfiguration.addManagedRepository( createStagingRepository() );
358 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
360 archivaConfiguration.save( configuration );
362 // save for staging repo delete
363 archivaConfiguration.save( configuration );
365 archivaConfigurationControl.replay();
367 action.setRepoid( REPO_ID );
370 assertEquals( REPO_ID, action.getRepoid() );
371 ManagedRepository repository = action.getRepository();
372 assertNotNull( repository );
373 assertRepositoryEquals( repository, createRepository() );
375 assertTrue( location.exists() );
376 return configuration;
379 private void assertRepositoryEquals( ManagedRepository expectedRepository, ManagedRepository actualRepository )
381 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
382 assertEquals( expectedRepository.getId(), actualRepository.getId() );
383 assertEquals( expectedRepository.getIndexDirectory(), actualRepository.getIndexDirectory() );
384 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
385 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
386 assertEquals( expectedRepository.getName(), actualRepository.getName() );
387 assertEquals( expectedRepository.getCronExpression(), actualRepository.getCronExpression() );
388 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
389 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
390 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
391 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
392 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
395 private Configuration createConfigurationForEditing( ManagedRepository repositoryConfiguration )
397 Configuration configuration = new Configuration();
398 ManagedRepositoryConfiguration managedRepositoryConfiguration =
399 new BeanReplicator().replicateBean( repositoryConfiguration, ManagedRepositoryConfiguration.class );
400 managedRepositoryConfiguration.setRefreshCronExpression( repositoryConfiguration.getCronExpression() );
401 configuration.addManagedRepository( managedRepositoryConfiguration );
402 return configuration;
405 private ManagedRepository createRepository()
407 ManagedRepository r = new ManagedRepository();
409 r.setName( "repo name" );
410 r.setLocation( location.getAbsolutePath() );
411 r.setLayout( "default" );
412 r.setCronExpression( "* 0/5 * * * ?" );
414 r.setRetentionCount( 0 );
415 r.setReleases( true );
416 r.setSnapshots( true );
417 r.setScanned( false );
418 r.setDeleteReleasedSnapshots( false );
422 private ManagedRepositoryConfiguration createStagingRepository()
424 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
425 r.setId( REPO_ID + "-stage" );
426 r.setName( "repo name" );
427 r.setLocation( location.getAbsolutePath() );
428 r.setLayout( "default" );
429 r.setRefreshCronExpression( "* 0/5 * * * ?" );
431 r.setRetentionCount( 0 );
432 r.setReleases( true );
433 r.setSnapshots( true );
434 r.setScanned( false );
435 r.setDeleteReleasedSnapshots( false );
439 private RemoteRepositoryConfiguration createRemoteRepository( String id, String url )
441 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
444 r.setLayout( "default" );
449 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
451 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
452 connector.setSourceRepoId( managedRepoId );
453 connector.setTargetRepoId( remoteRepoId );
458 private RepositoryGroupConfiguration createRepoGroup( List<String> repoIds, String repoGroupId )
460 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
461 repoGroup.setId( repoGroupId );
462 repoGroup.setRepositories( repoIds );
467 private void prepareRoleManagerMock()
468 throws RoleManagerException
470 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
471 roleManagerControl.setReturnValue( true );
472 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
473 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
474 roleManagerControl.setReturnValue( true );
475 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
476 roleManagerControl.replay();
479 protected ManagedRepositoryAdmin getManagedRepositoryAdmin()
481 return applicationContext.getBean( ManagedRepositoryAdmin.class );