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.meterware.servletunit.ServletRunner;
23 import com.meterware.servletunit.ServletUnitClient;
24 import com.opensymphony.xwork2.Action;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.Configuration;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
29 import org.apache.maven.archiva.web.action.AbstractActionTestCase;
30 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
31 import org.codehaus.redback.integration.interceptor.SecureActionException;
32 import org.easymock.MockControl;
34 import java.util.Collections;
35 import java.util.List;
38 * RepositoryGroupsActionTest
42 public class RepositoryGroupsActionTest
43 extends AbstractActionTestCase
45 private static final String REPO_GROUP_ID = "repo-group-ident";
47 private static final String REPO1_ID = "managed-repo-ident-1";
49 private static final String REPO2_ID = "managed-repo-ident-2";
51 private RepositoryGroupsAction action;
53 private MockControl archivaConfigurationControl;
55 private ArchivaConfiguration archivaConfiguration;
57 protected void setUp()
62 //action = (RepositoryGroupsAction) lookup( Action.class.getName(), "repositoryGroupsAction" );
63 action = (RepositoryGroupsAction) getActionProxy( "/admin/repositoryGroups.action" );
65 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
66 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
67 action.setArchivaConfiguration( archivaConfiguration );
70 public void testSecureActionBundle()
71 throws SecureActionException
73 archivaConfiguration.getConfiguration();
74 archivaConfigurationControl.setReturnValue( new Configuration() );
75 archivaConfigurationControl.replay();
78 SecureActionBundle bundle = action.getSecureActionBundle();
79 assertTrue( bundle.requiresAuthentication() );
80 assertEquals( 1, bundle.getAuthorizationTuples().size() );
83 public void testAddRepositoryGroup()
86 Configuration configuration = new Configuration();
87 archivaConfiguration.getConfiguration();
88 archivaConfigurationControl.setReturnValue( configuration, 2 );
90 archivaConfiguration.save( configuration );
91 archivaConfigurationControl.replay();
94 RepositoryGroupConfiguration repositoryGroup = action.getRepositoryGroup();
95 repositoryGroup.setId( REPO_GROUP_ID );
97 String status = action.addRepositoryGroup();
98 assertEquals( Action.SUCCESS, status );
100 assertEquals( Collections.singletonList( repositoryGroup ), configuration.getRepositoryGroups() );
102 archivaConfigurationControl.verify();
105 public void testAddEmptyRepositoryGroup()
108 Configuration configuration = new Configuration();
109 archivaConfiguration.getConfiguration();
110 archivaConfigurationControl.setReturnValue( configuration, 2 );
112 archivaConfiguration.save( configuration );
114 archivaConfigurationControl.replay();
118 String status = action.addRepositoryGroup();
119 assertEquals( Action.ERROR, status );
121 assertEquals( 0, configuration.getRepositoryGroups().size() );
124 public void testAddDuplicateRepositoryGroup()
127 Configuration configuration = new Configuration();
128 archivaConfiguration.getConfiguration();
129 archivaConfigurationControl.setReturnValue( configuration, 3 );
131 archivaConfiguration.save( configuration );
133 archivaConfigurationControl.replay();
136 RepositoryGroupConfiguration repositoryGroup = action.getRepositoryGroup();
137 repositoryGroup.setId( REPO_GROUP_ID );
139 String status = action.addRepositoryGroup();
140 assertEquals( Action.SUCCESS, status );
142 assertEquals( Collections.singletonList( repositoryGroup ), configuration.getRepositoryGroups() );
144 repositoryGroup.setId( REPO_GROUP_ID );
145 status = action.addRepositoryGroup();
147 assertEquals( Action.ERROR, status );
148 assertEquals( Collections.singletonList( repositoryGroup ), configuration.getRepositoryGroups() );
151 public void testGetRepositoryGroups()
154 ServletRunner sr = new ServletRunner();
155 ServletUnitClient sc = sr.newClient();
157 Configuration configuration = createInitialConfiguration();
159 archivaConfiguration.getConfiguration();
160 archivaConfigurationControl.setReturnValue( configuration );
161 archivaConfigurationControl.replay();
163 action.setServletRequest( sc.newInvocation( "http://localhost/admin/repositoryGroups.action" ).getRequest() );
165 String result = action.execute();
166 assertEquals( Action.SUCCESS, result );
168 assertEquals( "http://localhost:0/repository", action.getBaseUrl() );
170 assertNotNull( action.getRepositoryGroups() );
171 assertEquals( 1, action.getRepositoryGroups().size() );
172 assertEquals( 2, action.getManagedRepositories().size() );
174 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
176 assertEquals( 1, repoGroup.getRepositories().size() );
177 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
178 assertNotNull( action.getGroupToRepositoryMap() );
179 assertEquals( 1, action.getGroupToRepositoryMap().size() );
181 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
182 assertEquals( 1, repos.size() );
183 assertEquals( REPO2_ID, repos.get( 0 ) );
186 public void testAddRepositoryToGroup()
189 Configuration configuration = createInitialConfiguration();
191 archivaConfiguration.getConfiguration();
192 archivaConfigurationControl.setReturnValue( configuration, 6 );
193 archivaConfiguration.save( configuration );
194 archivaConfigurationControl.replay();
197 String result = action.execute();
198 assertEquals( Action.SUCCESS, result );
200 assertNotNull( action.getRepositoryGroups() );
201 assertEquals( 1, action.getRepositoryGroups().size() );
202 assertEquals( 2, action.getManagedRepositories().size() );
204 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
205 assertEquals( 1 , repoGroup.getRepositories().size() );
206 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
208 assertNotNull( action.getGroupToRepositoryMap() );
209 assertEquals( 1, action.getGroupToRepositoryMap().size() );
211 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
212 assertEquals( 1, repos.size() );
213 assertEquals( REPO2_ID, repos.get( 0 ) );
215 action.setRepoGroupId( REPO_GROUP_ID );
216 action.setRepoId( REPO2_ID );
218 result = action.addRepositoryToGroup();
219 assertEquals( Action.SUCCESS, result );
222 result = action.execute();
223 assertEquals( Action.SUCCESS, result );
225 assertEquals( 1, action.getRepositoryGroups().size() );
226 repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
227 assertEquals( 2, repoGroup.getRepositories().size() );
228 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
229 assertEquals( REPO2_ID, repoGroup.getRepositories().get( 1 ) );
231 assertEquals( 0, action.getGroupToRepositoryMap().size() );
232 assertNull( action.getGroupToRepositoryMap().get( repoGroup.getId() ) );
235 public void testRemoveRepositoryFromGroup()
238 Configuration configuration = createInitialConfiguration();
240 archivaConfiguration.getConfiguration();
241 archivaConfigurationControl.setReturnValue( configuration, 6 );
242 archivaConfiguration.save( configuration );
243 archivaConfigurationControl.replay();
246 String result = action.execute();
247 assertEquals( Action.SUCCESS, result );
249 assertNotNull( action.getRepositoryGroups() );
250 assertEquals( 1, action.getRepositoryGroups().size() );
251 assertEquals( 2, action.getManagedRepositories().size() );
253 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
254 assertEquals( 1 , repoGroup.getRepositories().size() );
255 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
257 assertNotNull( action.getGroupToRepositoryMap() );
258 assertEquals( 1, action.getGroupToRepositoryMap().size() );
260 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
261 assertEquals( 1, repos.size() );
262 assertEquals( REPO2_ID, repos.get( 0 ) );
264 action.setRepoGroupId( REPO_GROUP_ID );
265 action.setRepoId( REPO1_ID );
267 result = action.removeRepositoryFromGroup();
268 assertEquals( Action.SUCCESS, result );
271 result = action.execute();
272 assertEquals( Action.SUCCESS, result );
274 repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
275 assertEquals( 0, repoGroup.getRepositories().size() );
277 assertNotNull( action.getGroupToRepositoryMap() );
278 assertEquals( 1, action.getGroupToRepositoryMap().size() );
280 repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
281 assertEquals( 2, repos.size() );
282 assertEquals( REPO1_ID, repos.get( 0 ) );
283 assertEquals( REPO2_ID, repos.get( 1 ) );
286 public void testAddDuplicateRepositoryToGroup()
289 Configuration configuration = createInitialConfiguration();
291 archivaConfiguration.getConfiguration();
292 archivaConfigurationControl.setReturnValue( configuration, 6 );
293 archivaConfiguration.save( configuration );
294 archivaConfigurationControl.replay();
297 String result = action.execute();
298 assertEquals( Action.SUCCESS, result );
300 assertNotNull( action.getRepositoryGroups() );
301 assertEquals( 1, action.getRepositoryGroups().size() );
302 assertEquals( 2, action.getManagedRepositories().size() );
304 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
305 assertEquals( 1 , repoGroup.getRepositories().size() );
306 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
308 assertNotNull( action.getGroupToRepositoryMap() );
309 assertEquals( 1, action.getGroupToRepositoryMap().size() );
311 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
312 assertEquals( 1, repos.size() );
313 assertEquals( REPO2_ID, repos.get( 0 ) );
315 action.setRepoGroupId( REPO_GROUP_ID );
316 action.setRepoId( REPO1_ID );
318 result = action.addRepositoryToGroup();
319 assertEquals( Action.ERROR, result );
322 public void testRemoveRepositoryNotInGroup()
325 Configuration configuration = createInitialConfiguration();
327 archivaConfiguration.getConfiguration();
328 archivaConfigurationControl.setReturnValue( configuration, 6 );
329 archivaConfiguration.save( configuration );
330 archivaConfigurationControl.replay();
333 String result = action.execute();
334 assertEquals( Action.SUCCESS, result );
336 assertNotNull( action.getRepositoryGroups() );
337 assertEquals( 1, action.getRepositoryGroups().size() );
338 assertEquals( 2, action.getManagedRepositories().size() );
340 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
341 assertEquals( 1 , repoGroup.getRepositories().size() );
342 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
344 assertNotNull( action.getGroupToRepositoryMap() );
345 assertEquals( 1, action.getGroupToRepositoryMap().size() );
347 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
348 assertEquals( 1, repos.size() );
349 assertEquals( REPO2_ID, repos.get( 0 ) );
351 action.setRepoGroupId( REPO_GROUP_ID );
352 action.setRepoId( REPO2_ID );
354 result = action.removeRepositoryFromGroup();
355 assertEquals( Action.ERROR, result );
358 private Configuration createInitialConfiguration()
360 Configuration config = new Configuration();
362 ManagedRepositoryConfiguration managedRepo1 = new ManagedRepositoryConfiguration();
363 managedRepo1.setId( REPO1_ID );
365 config.addManagedRepository( managedRepo1 );
367 ManagedRepositoryConfiguration managedRepo2 = new ManagedRepositoryConfiguration();
368 managedRepo2.setId( REPO2_ID );
370 config.addManagedRepository( managedRepo2 );
372 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
373 repoGroup.setId( REPO_GROUP_ID );
374 repoGroup.addRepository( REPO1_ID );
376 config.addRepositoryGroup( repoGroup );