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.audit.AuditEvent;
24 import org.apache.archiva.audit.AuditListener;
25 import org.apache.archiva.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
27 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
28 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.configuration.Configuration;
30 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
33 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
34 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
35 import org.apache.maven.archiva.security.ArchivaRoleConstants;
36 import org.apache.maven.archiva.web.action.AuditEventArgumentsMatcher;
37 import org.codehaus.plexus.redback.role.RoleManager;
38 import org.codehaus.plexus.redback.role.RoleManagerException;
39 import org.codehaus.plexus.registry.RegistryException;
40 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
41 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
42 import org.codehaus.redback.integration.interceptor.SecureActionException;
43 import org.easymock.MockControl;
46 import java.util.Arrays;
47 import java.util.Collections;
48 import java.util.List;
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.removeRepository( 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 );
126 Configuration stageRepoConfiguration = new Configuration();
127 stageRepoConfiguration.addManagedRepository( createSatingRepository() );
128 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
130 archivaConfigurationControl.replay();
132 action.setRepoid( REPO_ID );
135 assertEquals( REPO_ID, action.getRepoid() );
136 ManagedRepositoryConfiguration repository = action.getRepository();
137 assertNotNull( repository );
138 assertRepositoryEquals( repository, createRepository() );
140 String status = action.execute();
141 assertEquals( Action.SUCCESS, status );
143 repository = action.getRepository();
144 assertRepositoryEquals( repository, createRepository() );
145 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
148 public void testDeleteRepositoryKeepContent()
151 // even when we keep the content, we don't keep the metadata at this point
152 repositoryStatisticsManager.deleteStatistics( REPO_ID );
153 repositoryStatisticsManagerControl.replay();
155 prepareRoleManagerMock();
157 Configuration configuration = prepDeletionTest( createRepository(), 4 );
159 MockControl control = mockAuditListeners();
161 MockControl metadataRepositoryControl = mockMetadataRepository();
163 String status = action.deleteEntry();
165 assertEquals( Action.SUCCESS, status );
167 assertTrue( configuration.getManagedRepositories().isEmpty() );
169 assertTrue( location.exists() );
171 repositoryStatisticsManagerControl.verify();
173 metadataRepositoryControl.verify();
176 private MockControl mockMetadataRepository()
177 throws MetadataRepositoryException
179 MockControl metadataRepositoryControl = MockControl.createControl( MetadataRepository.class );
180 MetadataRepository metadataRepository = (MetadataRepository) metadataRepositoryControl.getMock();
181 metadataRepository.removeRepository( REPO_ID );
182 metadataRepositoryControl.replay();
183 action.setMetadataRepository( metadataRepository );
184 return metadataRepositoryControl;
187 private MockControl mockAuditListeners()
189 MockControl control = MockControl.createControl( AuditListener.class );
190 AuditListener listener = (AuditListener) control.getMock();
191 listener.auditEvent( new AuditEvent( REPO_ID, "guest", null, AuditEvent.DELETE_MANAGED_REPO ) );
192 control.setMatcher( new AuditEventArgumentsMatcher() );
194 action.setAuditListeners( Arrays.asList( listener ) );
198 public void testDeleteRepositoryDeleteContent()
201 repositoryStatisticsManager.deleteStatistics( REPO_ID );
202 repositoryStatisticsManagerControl.replay();
204 prepareRoleManagerMock();
206 Configuration configuration = prepDeletionTest( createRepository(), 4 );
208 MockControl control = mockAuditListeners();
210 MockControl metadataRepositoryControl = mockMetadataRepository();
212 String status = action.deleteContents();
214 assertEquals( Action.SUCCESS, status );
216 assertTrue( configuration.getManagedRepositories().isEmpty() );
218 assertFalse( location.exists() );
220 repositoryStatisticsManagerControl.verify();
222 metadataRepositoryControl.verify();
225 public void testDeleteRepositoryAndAssociatedProxyConnectors()
228 repositoryStatisticsManager.deleteStatistics( REPO_ID );
229 repositoryStatisticsManagerControl.replay();
231 Configuration configuration = prepDeletionTest( createRepository(), 5 );
232 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
233 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
234 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
236 prepareRoleManagerMock();
238 assertEquals( 1, configuration.getProxyConnectors().size() );
240 MockControl control = mockAuditListeners();
241 MockControl metadataRepositoryControl = mockMetadataRepository();
242 String status = action.deleteContents();
244 assertEquals( Action.SUCCESS, status );
246 assertTrue( configuration.getManagedRepositories().isEmpty() );
247 assertEquals( 0, configuration.getProxyConnectors().size() );
249 assertFalse( location.exists() );
251 repositoryStatisticsManagerControl.verify();
253 metadataRepositoryControl.verify();
256 public void testDeleteRepositoryCancelled()
259 repositoryStatisticsManagerControl.replay();
261 ManagedRepositoryConfiguration originalRepository = createRepository();
262 Configuration configuration = prepDeletionTest( originalRepository, 3 );
264 String status = action.execute();
265 assertEquals( Action.SUCCESS, status );
267 ManagedRepositoryConfiguration repository = action.getRepository();
268 assertRepositoryEquals( repository, createRepository() );
269 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
271 assertTrue( location.exists() );
273 repositoryStatisticsManagerControl.verify();
277 private Configuration prepDeletionTest( ManagedRepositoryConfiguration originalRepository,
278 int expectCountGetConfig )
279 throws RegistryException, IndeterminateConfigurationException
283 Configuration configuration = createConfigurationForEditing( originalRepository );
285 archivaConfiguration.getConfiguration();
286 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
288 Configuration stageRepoConfiguration = new Configuration();
289 stageRepoConfiguration.addManagedRepository( createSatingRepository() );
290 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
292 archivaConfiguration.save( configuration );
293 archivaConfigurationControl.replay();
295 action.setRepoid( REPO_ID );
298 assertEquals( REPO_ID, action.getRepoid() );
299 ManagedRepositoryConfiguration repository = action.getRepository();
300 assertNotNull( repository );
301 assertRepositoryEquals( repository, createRepository() );
303 assertTrue( location.exists() );
304 return configuration;
307 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
308 ManagedRepositoryConfiguration actualRepository )
310 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
311 assertEquals( expectedRepository.getId(), actualRepository.getId() );
312 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
313 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
314 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
315 assertEquals( expectedRepository.getName(), actualRepository.getName() );
316 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
317 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
318 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
319 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
320 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
321 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
324 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
326 Configuration configuration = new Configuration();
327 configuration.addManagedRepository( repositoryConfiguration );
328 return configuration;
331 private ManagedRepositoryConfiguration createRepository()
333 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
335 r.setName( "repo name" );
336 r.setLocation( location.getAbsolutePath() );
337 r.setLayout( "default" );
338 r.setRefreshCronExpression( "* 0/5 * * * ?" );
340 r.setRetentionCount( 0 );
341 r.setReleases( true );
342 r.setSnapshots( true );
343 r.setScanned( false );
344 r.setDeleteReleasedSnapshots( false );
348 private ManagedRepositoryConfiguration createSatingRepository()
350 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
351 r.setId( REPO_ID + "-stage" );
352 r.setName( "repo name" );
353 r.setLocation( location.getAbsolutePath() );
354 r.setLayout( "default" );
355 r.setRefreshCronExpression( "* 0/5 * * * ?" );
357 r.setRetentionCount( 0 );
358 r.setReleases( true );
359 r.setSnapshots( true );
360 r.setScanned( false );
361 r.setDeleteReleasedSnapshots( false );
365 private RemoteRepositoryConfiguration createRemoteRepository( String id, String url )
367 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
370 r.setLayout( "default" );
375 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
377 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
378 connector.setSourceRepoId( managedRepoId );
379 connector.setTargetRepoId( remoteRepoId );
384 private RepositoryGroupConfiguration createRepoGroup( List<String> repoIds, String repoGroupId )
386 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
387 repoGroup.setId( repoGroupId );
388 repoGroup.setRepositories( repoIds );
393 private void prepareRoleManagerMock()
394 throws RoleManagerException
396 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
397 roleManagerControl.setReturnValue( true );
398 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
399 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
400 roleManagerControl.setReturnValue( true );
401 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
402 roleManagerControl.replay();