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;
28 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
29 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
30 import org.apache.maven.archiva.security.ArchivaRoleConstants;
31 import org.codehaus.plexus.PlexusTestCase;
32 import org.codehaus.plexus.redback.role.RoleManager;
33 import org.codehaus.plexus.redback.role.RoleManagerException;
34 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
35 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
36 import org.codehaus.plexus.registry.RegistryException;
37 import org.easymock.MockControl;
40 import java.util.Collections;
43 * DeleteManagedRepositoryActionTest
45 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
48 public class DeleteManagedRepositoryActionTest
49 extends PlexusTestCase
51 private DeleteManagedRepositoryAction action;
53 private RoleManager roleManager;
55 private MockControl roleManagerControl;
57 private MockControl archivaConfigurationControl;
59 private ArchivaConfiguration archivaConfiguration;
61 private static final String REPO_ID = "repo-ident";
63 private File location;
65 protected void setUp()
70 action = (DeleteManagedRepositoryAction) lookup( Action.class.getName(), "deleteManagedRepositoryAction" );
72 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
73 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
74 action.setArchivaConfiguration( archivaConfiguration );
76 roleManagerControl = MockControl.createControl( RoleManager.class );
77 roleManager = (RoleManager) roleManagerControl.getMock();
78 action.setRoleManager( roleManager );
79 location = getTestFile( "target/test/location" );
82 public void testSecureActionBundle()
83 throws SecureActionException
85 archivaConfiguration.getConfiguration();
86 archivaConfigurationControl.setReturnValue( new Configuration() );
87 archivaConfigurationControl.replay();
90 SecureActionBundle bundle = action.getSecureActionBundle();
91 assertTrue( bundle.requiresAuthentication() );
92 assertEquals( 1, bundle.getAuthorizationTuples().size() );
95 public void testDeleteRepositoryConfirmation()
98 ManagedRepositoryConfiguration originalRepository = createRepository();
99 Configuration configuration = createConfigurationForEditing( originalRepository );
101 archivaConfiguration.getConfiguration();
102 archivaConfigurationControl.setReturnValue( configuration );
103 archivaConfigurationControl.replay();
105 action.setRepoid( REPO_ID );
108 assertEquals( REPO_ID, action.getRepoid() );
109 ManagedRepositoryConfiguration repository = action.getRepository();
110 assertNotNull( repository );
111 assertRepositoryEquals( repository, createRepository() );
113 String status = action.execute();
114 assertEquals( Action.SUCCESS, status );
115 repository = action.getRepository();
116 assertRepositoryEquals( repository, createRepository() );
117 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
120 public void testDeleteRepositoryKeepContent()
123 prepareRoleManagerMock();
125 Configuration configuration = prepDeletionTest( createRepository(), 3 );
126 String status = action.deleteEntry();
127 assertEquals( Action.SUCCESS, status );
129 assertTrue( configuration.getManagedRepositories().isEmpty() );
131 assertTrue( location.exists() );
134 public void testDeleteRepositoryDeleteContent()
137 prepareRoleManagerMock();
139 Configuration configuration = prepDeletionTest( createRepository(), 3 );
140 String status = action.deleteContents();
141 assertEquals( Action.SUCCESS, status );
143 assertTrue( configuration.getManagedRepositories().isEmpty() );
145 assertFalse( location.exists() );
148 public void testDeleteRepositoryAndAssociatedProxyConnectors()
151 Configuration configuration = prepDeletionTest( createRepository(), 4 );
152 configuration.addRemoteRepository( createRemoteRepository( "codehaus", "http://repository.codehaus.org" ) );
153 configuration.addRemoteRepository( createRemoteRepository( "java.net", "http://dev.java.net/maven2" ) );
154 configuration.addProxyConnector( createProxyConnector( REPO_ID, "codehaus" ) );
156 prepareRoleManagerMock();
158 assertEquals( 1, configuration.getProxyConnectors().size() );
160 String status = action.deleteContents();
161 assertEquals( Action.SUCCESS, status );
163 assertTrue( configuration.getManagedRepositories().isEmpty() );
164 assertEquals( 0, configuration.getProxyConnectors().size() );
166 assertFalse( location.exists() );
169 public void testDeleteRepositoryCancelled()
172 ManagedRepositoryConfiguration originalRepository = createRepository();
173 Configuration configuration = prepDeletionTest( originalRepository, 3 );
174 String status = action.execute();
175 assertEquals( Action.SUCCESS, status );
177 ManagedRepositoryConfiguration repository = action.getRepository();
178 assertRepositoryEquals( repository, createRepository() );
179 assertEquals( Collections.singletonList( originalRepository ), configuration.getManagedRepositories() );
181 assertTrue( location.exists() );
184 private Configuration prepDeletionTest( ManagedRepositoryConfiguration originalRepository, int expectCountGetConfig )
185 throws RegistryException, IndeterminateConfigurationException
189 Configuration configuration = createConfigurationForEditing( originalRepository );
191 archivaConfiguration.getConfiguration();
192 archivaConfigurationControl.setReturnValue( configuration, expectCountGetConfig );
194 archivaConfiguration.save( configuration );
195 archivaConfigurationControl.replay();
197 action.setRepoid( REPO_ID );
200 assertEquals( REPO_ID, action.getRepoid() );
201 ManagedRepositoryConfiguration repository = action.getRepository();
202 assertNotNull( repository );
203 assertRepositoryEquals( repository, createRepository() );
205 assertTrue( location.exists() );
206 return configuration;
209 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
210 ManagedRepositoryConfiguration actualRepository )
212 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
213 assertEquals( expectedRepository.getId(), actualRepository.getId() );
214 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
215 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
216 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
217 assertEquals( expectedRepository.getName(), actualRepository.getName() );
218 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
219 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
220 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
221 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
222 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
223 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
226 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
228 Configuration configuration = new Configuration();
229 configuration.addManagedRepository( repositoryConfiguration );
230 return configuration;
233 private ManagedRepositoryConfiguration createRepository()
235 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
237 r.setName( "repo name" );
238 r.setLocation( location.getAbsolutePath() );
239 r.setLayout( "default" );
240 r.setRefreshCronExpression( "* 0/5 * * * ?" );
242 r.setRetentionCount( 0 );
243 r.setReleases( true );
244 r.setSnapshots( true );
245 r.setScanned( false );
246 r.setDeleteReleasedSnapshots( false );
250 private RemoteRepositoryConfiguration createRemoteRepository(String id, String url)
252 RemoteRepositoryConfiguration r = new RemoteRepositoryConfiguration();
255 r.setLayout( "default" );
260 private ProxyConnectorConfiguration createProxyConnector( String managedRepoId, String remoteRepoId )
262 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
263 connector.setSourceRepoId( managedRepoId );
264 connector.setTargetRepoId( remoteRepoId );
269 private void prepareRoleManagerMock()
270 throws RoleManagerException
272 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
273 roleManagerControl.setReturnValue( true );
274 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
275 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
276 roleManagerControl.setReturnValue( true );
277 roleManager.removeTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
278 roleManagerControl.replay();