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.xwork.Action;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.model.ArchivaProjectModel;
31 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
34 import org.apache.maven.archiva.security.ArchivaRoleConstants;
35 import org.codehaus.plexus.PlexusTestCase;
36 import org.codehaus.plexus.redback.role.RoleManager;
37 import org.codehaus.plexus.redback.role.RoleManagerException;
38 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
39 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
40 import org.codehaus.plexus.registry.RegistryException;
41 import org.easymock.MockControl;
44 import java.util.Collections;
47 * DeleteManagedRepositoryActionTest
49 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
52 public class DeleteManagedRepositoryActionTest
53 extends PlexusTestCase
55 private DeleteManagedRepositoryAction action;
57 private RoleManager roleManager;
59 private MockControl roleManagerControl;
61 private MockControl archivaConfigurationControl;
63 private ArchivaConfiguration archivaConfiguration;
65 private static final String REPO_ID = "repo-ident";
67 private File location;
69 protected void setUp()
74 action = (DeleteManagedRepositoryAction) lookup( Action.class.getName(), "deleteManagedRepositoryAction" );
76 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
77 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
78 action.setArchivaConfiguration( archivaConfiguration );
80 roleManagerControl = MockControl.createControl( RoleManager.class );
81 roleManager = (RoleManager) roleManagerControl.getMock();
82 action.setRoleManager( roleManager );
83 location = getTestFile( "target/test/location" );
86 public void testSecureActionBundle()
87 throws SecureActionException
89 archivaConfiguration.getConfiguration();
90 archivaConfigurationControl.setReturnValue( new Configuration() );
91 archivaConfigurationControl.replay();
94 SecureActionBundle bundle = action.getSecureActionBundle();
95 assertTrue( bundle.requiresAuthentication() );
96 assertEquals( 1, bundle.getAuthorizationTuples().size() );
99 public void testDeleteRepositoryConfirmation()
102 ManagedRepositoryConfiguration originalRepository = createRepository();
103 Configuration configuration = createConfigurationForEditing( originalRepository );
105 archivaConfiguration.getConfiguration();
106 archivaConfigurationControl.setReturnValue( configuration );
107 archivaConfigurationControl.replay();
109 action.setRepoid( REPO_ID );
112 assertEquals( REPO_ID, action.getRepoid() );
113 ManagedRepositoryConfiguration repository = action.getRepository();
114 assertNotNull( repository );
115 assertRepositoryEquals( repository, createRepository() );
117 String status = action.execute();
118 assertEquals( Action.SUCCESS, status );
119 repository = action.getRepository();
120 assertRepositoryEquals( repository, createRepository() );
121 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
124 public void testDeleteRepositoryKeepContent()
127 prepareRoleManagerMock();
129 Configuration configuration = prepDeletionTest( createRepository(), 3 );
131 String status = action.deleteEntry();
133 assertEquals( Action.SUCCESS, status );
135 assertTrue( configuration.getManagedRepositories().isEmpty() );
137 assertTrue( location.exists() );
140 public void testDeleteRepositoryDeleteContent()
143 prepareRoleManagerMock();
145 Configuration configuration = prepDeletionTest( createRepository(), 3 );
147 String status = action.deleteContents();
149 assertEquals( Action.SUCCESS, status );
151 assertTrue( configuration.getManagedRepositories().isEmpty() );
153 assertFalse( location.exists() );
156 public void testDeleteRepositoryAndAssociatedProxyConnectors()
159 Configuration configuration = prepDeletionTest( createRepository(), 4 );
160 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
161 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
162 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
164 prepareRoleManagerMock();
166 assertEquals( 1, configuration.getProxyConnectors().size() );
168 String status = action.deleteContents();
169 assertEquals( Action.SUCCESS, status );
171 assertTrue( configuration.getManagedRepositories().isEmpty() );
172 assertEquals( 0, configuration.getProxyConnectors().size() );
174 assertFalse( location.exists() );
177 public void testDeleteRepositoryCancelled()
180 ManagedRepositoryConfiguration originalRepository = createRepository();
181 Configuration configuration = prepDeletionTest( originalRepository, 3 );
182 String status = action.execute();
183 assertEquals( Action.SUCCESS, status );
185 ManagedRepositoryConfiguration repository = action.getRepository();
186 assertRepositoryEquals( repository, createRepository() );
187 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
189 assertTrue( location.exists() );
192 private Configuration prepDeletionTest( ManagedRepositoryConfiguration originalRepository, int expectCountGetConfig )
193 throws RegistryException, IndeterminateConfigurationException
197 Configuration configuration = createConfigurationForEditing( originalRepository );
199 archivaConfiguration.getConfiguration();
200 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
202 archivaConfiguration.save( configuration );
203 archivaConfigurationControl.replay();
205 action.setRepoid( REPO_ID );
208 assertEquals( REPO_ID, action.getRepoid() );
209 ManagedRepositoryConfiguration repository = action.getRepository();
210 assertNotNull( repository );
211 assertRepositoryEquals( repository, createRepository() );
213 assertTrue( location.exists() );
214 return configuration;
217 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
218 ManagedRepositoryConfiguration actualRepository )
220 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
221 assertEquals( expectedRepository.getId(), actualRepository.getId() );
222 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
223 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
224 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
225 assertEquals( expectedRepository.getName(), actualRepository.getName() );
226 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
227 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
228 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
229 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
230 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
231 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
234 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
236 Configuration configuration = new Configuration();
237 configuration.addManagedRepository( repositoryConfiguration );
238 return configuration;
241 private ManagedRepositoryConfiguration createRepository()
243 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
245 r.setName( "repo name" );
246 r.setLocation( location.getAbsolutePath() );
247 r.setLayout( "default" );
248 r.setRefreshCronExpression( "* 0/5 * * * ?" );
250 r.setRetentionCount( 0 );
251 r.setReleases( true );
252 r.setSnapshots( true );
253 r.setScanned( false );
254 r.setDeleteReleasedSnapshots( false );
258 private RemoteRepositoryConfiguration createRemoteRepository(String id, String url)
260 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
263 r.setLayout( "default" );
268 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
270 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
271 connector.setSourceRepoId( managedRepoId );
272 connector.setTargetRepoId( remoteRepoId );
277 private void prepareRoleManagerMock()
278 throws RoleManagerException
280 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
281 roleManagerControl.setReturnValue( true );
282 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
283 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
284 roleManagerControl.setReturnValue( true );
285 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
286 roleManagerControl.replay();
289 protected ArchivaProjectModel createProjectModel( String groupId, String artifactId, String version )
291 ArchivaProjectModel projectModel = new ArchivaProjectModel();
292 projectModel.setGroupId( groupId );
293 projectModel.setArtifactId( artifactId );
294 projectModel.setVersion( version );