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
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.List;
28 import com.opensymphony.xwork2.Action;
29 import org.apache.archiva.metadata.repository.MetadataRepository;
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.maven.archiva.repository.audit.AuditEvent;
39 import org.apache.maven.archiva.repository.audit.AuditListener;
40 import org.apache.maven.archiva.security.ArchivaRoleConstants;
41 import org.apache.maven.archiva.web.action.AuditEventArgumentsMatcher;
42 import org.codehaus.plexus.redback.role.RoleManager;
43 import org.codehaus.plexus.redback.role.RoleManagerException;
44 import org.codehaus.plexus.registry.RegistryException;
45 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
46 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
47 import org.codehaus.redback.integration.interceptor.SecureActionException;
48 import org.easymock.MockControl;
51 * DeleteManagedRepositoryActionTest
55 public class DeleteManagedRepositoryActionTest
56 extends PlexusInSpringTestCase
58 private DeleteManagedRepositoryAction action;
60 private RoleManager roleManager;
62 private MockControl roleManagerControl;
64 private MockControl archivaConfigurationControl;
66 private ArchivaConfiguration archivaConfiguration;
68 private static final String REPO_ID = "repo-ident";
70 private File location;
72 private MockControl repositoryStatisticsManagerControl;
74 private RepositoryStatisticsManager repositoryStatisticsManager;
76 protected void setUp()
81 action = new DeleteManagedRepositoryAction();
83 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
84 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
85 action.setArchivaConfiguration( archivaConfiguration );
87 roleManagerControl = MockControl.createControl( RoleManager.class );
88 roleManager = (RoleManager) roleManagerControl.getMock();
89 action.setRoleManager( roleManager );
90 location = getTestFile( "target/test/location" );
92 repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
93 repositoryStatisticsManager = (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
94 action.setRepositoryStatisticsManager( repositoryStatisticsManager );
96 MockControl metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
97 MetadataRepository metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
98 metadataRepository.deleteRepository( REPO_ID );
99 action.setMetadataRepository( metadataRepository );
101 metadataRepositoryControl.replay();
104 public void testSecureActionBundle()
105 throws SecureActionException
107 archivaConfiguration.getConfiguration();
108 archivaConfigurationControl.setReturnValue( new Configuration() );
109 archivaConfigurationControl.replay();
112 SecureActionBundle bundle = action.getSecureActionBundle();
113 assertTrue( bundle.requiresAuthentication() );
114 assertEquals( 1, bundle.getAuthorizationTuples().size() );
117 public void testDeleteRepositoryConfirmation()
120 ManagedRepositoryConfiguration originalRepository = createRepository();
121 Configuration configuration = createConfigurationForEditing( originalRepository );
123 archivaConfiguration.getConfiguration();
124 archivaConfigurationControl.setReturnValue( configuration );
125 archivaConfigurationControl.replay();
127 action.setRepoid( REPO_ID );
130 assertEquals( REPO_ID, action.getRepoid() );
131 ManagedRepositoryConfiguration repository = action.getRepository();
132 assertNotNull( repository );
133 assertRepositoryEquals( repository, createRepository() );
135 String status = action.execute();
136 assertEquals( Action.SUCCESS, status );
138 repository = action.getRepository();
139 assertRepositoryEquals( repository, createRepository() );
140 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
143 public void testDeleteRepositoryKeepContent()
146 // even when we keep the content, we don't keep the metadata at this point
147 repositoryStatisticsManager.deleteStatistics( REPO_ID );
148 repositoryStatisticsManagerControl.replay();
150 prepareRoleManagerMock();
152 Configuration configuration = prepDeletionTest( createRepository(), 4 );
154 MockControl control = mockAuditListeners();
156 MockControl metadataRepositoryControl = mockMetadataRepository();
158 String status = action.deleteEntry();
160 assertEquals( Action.SUCCESS, status );
162 assertTrue( configuration.getManagedRepositories().isEmpty() );
164 assertTrue( location.exists() );
166 repositoryStatisticsManagerControl.verify();
168 metadataRepositoryControl.verify();
171 private MockControl mockMetadataRepository()
173 MockControl metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
174 MetadataRepository metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
175 metadataRepository.deleteRepository( REPO_ID );
176 metadataRepositoryControl.replay();
177 action.setMetadataRepository( metadataRepository );
178 return metadataRepositoryControl;
181 private MockControl mockAuditListeners()
183 MockControl control = MockControl.createControl( AuditListener.class );
184 AuditListener listener = (AuditListener) control.getMock();
185 listener.auditEvent( new AuditEvent( REPO_ID, "guest", null, AuditEvent.DELETE_MANAGED_REPO ) );
186 control.setMatcher( new AuditEventArgumentsMatcher() );
188 action.setAuditListeners( Arrays.asList( listener ) );
192 public void testDeleteRepositoryDeleteContent()
195 repositoryStatisticsManager.deleteStatistics( REPO_ID );
196 repositoryStatisticsManagerControl.replay();
198 prepareRoleManagerMock();
200 Configuration configuration = prepDeletionTest( createRepository(), 4 );
202 MockControl control = mockAuditListeners();
204 MockControl metadataRepositoryControl = mockMetadataRepository();
206 String status = action.deleteContents();
208 assertEquals( Action.SUCCESS, status );
210 assertTrue( configuration.getManagedRepositories().isEmpty() );
212 assertFalse( location.exists() );
214 repositoryStatisticsManagerControl.verify();
216 metadataRepositoryControl.verify();
219 public void testDeleteRepositoryAndAssociatedProxyConnectors()
222 repositoryStatisticsManager.deleteStatistics( REPO_ID );
223 repositoryStatisticsManagerControl.replay();
225 Configuration configuration = prepDeletionTest( createRepository(), 5 );
226 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
227 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
228 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
230 prepareRoleManagerMock();
232 assertEquals( 1, configuration.getProxyConnectors().size() );
234 MockControl control = mockAuditListeners();
235 MockControl metadataRepositoryControl = mockMetadataRepository();
236 String status = action.deleteContents();
238 assertEquals( Action.SUCCESS, status );
240 assertTrue( configuration.getManagedRepositories().isEmpty() );
241 assertEquals( 0, configuration.getProxyConnectors().size() );
243 assertFalse( location.exists() );
245 repositoryStatisticsManagerControl.verify();
247 metadataRepositoryControl.verify();
250 public void testDeleteRepositoryCancelled()
253 repositoryStatisticsManagerControl.replay();
255 ManagedRepositoryConfiguration originalRepository = createRepository();
256 Configuration configuration = prepDeletionTest( originalRepository, 3 );
258 String status = action.execute();
259 assertEquals( Action.SUCCESS, status );
261 ManagedRepositoryConfiguration repository = action.getRepository();
262 assertRepositoryEquals( repository, createRepository() );
263 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
265 assertTrue( location.exists() );
267 repositoryStatisticsManagerControl.verify();
270 public void testDeleteRepositoryAndReposUnderRepoGroup()
273 repositoryStatisticsManager.deleteStatistics( REPO_ID );
274 repositoryStatisticsManagerControl.replay();
276 Configuration configuration = prepDeletionTest( createRepository(), 5 );
277 List<String> repoIds = new ArrayList<String>();
278 repoIds.add( REPO_ID );
279 configuration.addRepositoryGroup( createRepoGroup( repoIds, "repo.group" ) );
281 prepareRoleManagerMock();
283 assertEquals( 1, configuration.getRepositoryGroups().size() );
285 MockControl control = mockAuditListeners();
286 MockControl metadataRepositoryControl = mockMetadataRepository();
287 String status = action.deleteContents();
288 assertEquals( Action.SUCCESS, status );
290 assertTrue( configuration.getManagedRepositories().isEmpty() );
291 assertEquals( 0, configuration.getRepositoryGroups().get( 0 ).getRepositories().size() );
293 assertFalse( location.exists() );
295 repositoryStatisticsManagerControl.verify();
297 metadataRepositoryControl.verify();
300 private Configuration prepDeletionTest( ManagedRepositoryConfiguration originalRepository, int expectCountGetConfig )
301 throws RegistryException, IndeterminateConfigurationException
305 Configuration configuration = createConfigurationForEditing( originalRepository );
307 archivaConfiguration.getConfiguration();
308 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
310 archivaConfiguration.save( configuration );
311 archivaConfigurationControl.replay();
313 action.setRepoid( REPO_ID );
316 assertEquals( REPO_ID, action.getRepoid() );
317 ManagedRepositoryConfiguration repository = action.getRepository();
318 assertNotNull( repository );
319 assertRepositoryEquals( repository, createRepository() );
321 assertTrue( location.exists() );
322 return configuration;
325 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
326 ManagedRepositoryConfiguration actualRepository )
328 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
329 assertEquals( expectedRepository.getId(), actualRepository.getId() );
330 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
331 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
332 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
333 assertEquals( expectedRepository.getName(), actualRepository.getName() );
334 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
335 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
336 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
337 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
338 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
339 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
342 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
344 Configuration configuration = new Configuration();
345 configuration.addManagedRepository( repositoryConfiguration );
346 return configuration;
349 private ManagedRepositoryConfiguration createRepository()
351 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
353 r.setName( "repo name" );
354 r.setLocation( location.getAbsolutePath() );
355 r.setLayout( "default" );
356 r.setRefreshCronExpression( "* 0/5 * * * ?" );
358 r.setRetentionCount( 0 );
359 r.setReleases( true );
360 r.setSnapshots( true );
361 r.setScanned( false );
362 r.setDeleteReleasedSnapshots( false );
366 private RemoteRepositoryConfiguration createRemoteRepository(String id, String url)
368 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
371 r.setLayout( "default" );
376 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
378 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
379 connector.setSourceRepoId( managedRepoId );
380 connector.setTargetRepoId( remoteRepoId );
385 private RepositoryGroupConfiguration createRepoGroup( List<String> repoIds, String repoGroupId )
387 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
388 repoGroup.setId( repoGroupId );
389 repoGroup.setRepositories( repoIds );
394 private void prepareRoleManagerMock()
395 throws RoleManagerException
397 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
398 roleManagerControl.setReturnValue( true );
399 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
400 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
401 roleManagerControl.setReturnValue( true );
402 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
403 roleManagerControl.replay();