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.archiva.admin.model.RepositoryAdminException;
26 import org.apache.archiva.admin.model.group.RepositoryGroup;
27 import org.apache.archiva.admin.repository.group.DefaultRepositoryGroupAdmin;
28 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
29 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
30 import org.apache.maven.archiva.configuration.Configuration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
33 import org.apache.maven.archiva.web.action.AbstractActionTestCase;
34 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
35 import org.codehaus.redback.integration.interceptor.SecureActionException;
36 import org.easymock.MockControl;
38 import java.util.Collections;
39 import java.util.List;
42 * RepositoryGroupsActionTest
44 public class RepositoryGroupsActionTest
45 extends AbstractActionTestCase
47 private static final String REPO_GROUP_ID = "repo-group-ident";
49 private static final String REPO1_ID = "managed-repo-ident-1";
51 private static final String REPO2_ID = "managed-repo-ident-2";
53 private RepositoryGroupsAction action;
55 private MockControl archivaConfigurationControl;
57 private ArchivaConfiguration archivaConfiguration;
59 protected void setUp()
64 action = (RepositoryGroupsAction) getActionProxy( "/admin/repositoryGroups.action" ).getAction();
66 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
67 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
68 action.setArchivaConfiguration( archivaConfiguration );
69 ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).setArchivaConfiguration(
70 archivaConfiguration );
71 ( (DefaultManagedRepositoryAdmin) ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).getManagedRepositoryAdmin() ).setArchivaConfiguration(
72 archivaConfiguration );
75 public void testSecureActionBundle()
76 throws SecureActionException, RepositoryAdminException
78 archivaConfiguration.getConfiguration();
79 archivaConfigurationControl.setReturnValue( new Configuration(), 3 );
80 archivaConfigurationControl.replay();
83 SecureActionBundle bundle = action.getSecureActionBundle();
84 assertTrue( bundle.requiresAuthentication() );
85 assertEquals( 1, bundle.getAuthorizationTuples().size() );
88 public void testAddRepositoryGroup()
91 Configuration configuration = new Configuration();
92 archivaConfiguration.getConfiguration();
93 archivaConfigurationControl.setReturnValue( configuration, 6 );
95 archivaConfiguration.save( configuration );
96 archivaConfigurationControl.replay();
99 RepositoryGroup repositoryGroup = action.getRepositoryGroup();
100 repositoryGroup.setId( REPO_GROUP_ID );
102 String status = action.addRepositoryGroup();
103 assertEquals( Action.SUCCESS, status );
105 assertEquals( Collections.singletonList( repositoryGroup ),
106 action.getRepositoryGroupAdmin().getRepositoriesGroups() );
108 archivaConfigurationControl.verify();
111 public void testAddEmptyRepositoryGroup()
114 Configuration configuration = new Configuration();
115 archivaConfiguration.getConfiguration();
116 archivaConfigurationControl.setReturnValue( configuration, 5 );
118 archivaConfiguration.save( configuration );
120 archivaConfigurationControl.replay();
124 String status = action.addRepositoryGroup();
125 assertEquals( Action.ERROR, status );
127 assertEquals( 0, configuration.getRepositoryGroups().size() );
130 public void testAddDuplicateRepositoryGroup()
133 Configuration configuration = new Configuration();
134 archivaConfiguration.getConfiguration();
135 archivaConfigurationControl.setReturnValue( configuration, 8 );
137 archivaConfiguration.save( configuration );
139 archivaConfigurationControl.replay();
142 RepositoryGroup repositoryGroup = action.getRepositoryGroup();
143 repositoryGroup.setId( REPO_GROUP_ID );
145 String status = action.addRepositoryGroup();
146 assertEquals( Action.SUCCESS, status );
148 assertEquals( Collections.singletonList( repositoryGroup ),
149 action.getRepositoryGroupAdmin().getRepositoriesGroups() );
151 repositoryGroup.setId( REPO_GROUP_ID );
152 status = action.addRepositoryGroup();
154 assertEquals( Action.ERROR, status );
155 assertEquals( Collections.singletonList( repositoryGroup ),
156 action.getRepositoryGroupAdmin().getRepositoriesGroups() );
159 public void testGetRepositoryGroups()
162 ServletRunner sr = new ServletRunner();
163 ServletUnitClient sc = sr.newClient();
165 Configuration configuration = createInitialConfiguration();
167 archivaConfiguration.getConfiguration();
168 archivaConfigurationControl.setReturnValue( configuration, 6 );
169 archivaConfigurationControl.replay();
171 action.setServletRequest( sc.newInvocation( "http://localhost/admin/repositoryGroups.action" ).getRequest() );
173 String result = action.execute();
174 assertEquals( Action.SUCCESS, result );
176 assertEquals( "http://localhost:0/repository", action.getBaseUrl() );
178 assertNotNull( action.getRepositoryGroups() );
179 assertEquals( 1, action.getRepositoryGroups().size() );
180 assertEquals( 2, action.getManagedRepositories().size() );
182 RepositoryGroup repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
184 assertEquals( 1, repoGroup.getRepositories().size() );
185 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
186 assertNotNull( action.getGroupToRepositoryMap() );
187 assertEquals( 1, action.getGroupToRepositoryMap().size() );
189 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
190 assertEquals( 1, repos.size() );
191 assertEquals( REPO2_ID, repos.get( 0 ) );
194 public void testAddRepositoryToGroup()
197 Configuration configuration = createInitialConfiguration();
199 archivaConfiguration.getConfiguration();
200 archivaConfigurationControl.setReturnValue( configuration, 17 );
201 archivaConfiguration.save( configuration );
202 archivaConfigurationControl.replay();
205 String result = action.execute();
206 assertEquals( Action.SUCCESS, result );
208 assertNotNull( action.getRepositoryGroups() );
209 assertEquals( 1, action.getRepositoryGroups().size() );
210 assertEquals( 2, action.getManagedRepositories().size() );
212 RepositoryGroup repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
213 assertEquals( 1, repoGroup.getRepositories().size() );
214 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
216 assertNotNull( action.getGroupToRepositoryMap() );
217 assertEquals( 1, action.getGroupToRepositoryMap().size() );
219 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
220 assertEquals( 1, repos.size() );
221 assertEquals( REPO2_ID, repos.get( 0 ) );
223 action.setRepoGroupId( REPO_GROUP_ID );
224 action.setRepoId( REPO2_ID );
226 result = action.addRepositoryToGroup();
227 assertEquals( Action.SUCCESS, result );
230 result = action.execute();
231 assertEquals( Action.SUCCESS, result );
233 assertEquals( 1, action.getRepositoryGroups().size() );
234 repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
235 assertEquals( 2, repoGroup.getRepositories().size() );
236 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
237 assertEquals( REPO2_ID, repoGroup.getRepositories().get( 1 ) );
239 assertEquals( 0, action.getGroupToRepositoryMap().size() );
240 assertNull( action.getGroupToRepositoryMap().get( repoGroup.getId() ) );
243 public void testRemoveRepositoryFromGroup()
246 Configuration configuration = createInitialConfiguration();
248 archivaConfiguration.getConfiguration();
249 archivaConfigurationControl.setReturnValue( configuration, 13 );
250 archivaConfiguration.save( configuration );
251 archivaConfigurationControl.replay();
254 String result = action.execute();
255 assertEquals( Action.SUCCESS, result );
257 assertNotNull( action.getRepositoryGroups() );
258 assertEquals( 1, action.getRepositoryGroups().size() );
259 assertEquals( 2, action.getManagedRepositories().size() );
261 RepositoryGroup repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
262 assertEquals( 1, repoGroup.getRepositories().size() );
263 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
265 assertNotNull( action.getGroupToRepositoryMap() );
266 assertEquals( 1, action.getGroupToRepositoryMap().size() );
268 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
269 assertEquals( 1, repos.size() );
270 assertEquals( REPO2_ID, repos.get( 0 ) );
272 action.setRepoGroupId( REPO_GROUP_ID );
273 action.setRepoId( REPO1_ID );
275 result = action.removeRepositoryFromGroup();
276 assertEquals( Action.SUCCESS, result );
279 result = action.execute();
280 assertEquals( Action.SUCCESS, result );
282 repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
283 assertEquals( 0, repoGroup.getRepositories().size() );
285 assertNotNull( action.getGroupToRepositoryMap() );
286 assertEquals( 1, action.getGroupToRepositoryMap().size() );
288 repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
289 assertEquals( 2, repos.size() );
290 assertEquals( REPO1_ID, repos.get( 0 ) );
291 assertEquals( REPO2_ID, repos.get( 1 ) );
294 public void testAddDuplicateRepositoryToGroup()
297 Configuration configuration = createInitialConfiguration();
299 archivaConfiguration.getConfiguration();
300 archivaConfigurationControl.setReturnValue( configuration, 6 );
301 archivaConfiguration.save( configuration );
302 archivaConfigurationControl.replay();
305 String result = action.execute();
306 assertEquals( Action.SUCCESS, result );
308 assertNotNull( action.getRepositoryGroups() );
309 assertEquals( 1, action.getRepositoryGroups().size() );
310 assertEquals( 2, action.getManagedRepositories().size() );
312 RepositoryGroup repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
313 assertEquals( 1, repoGroup.getRepositories().size() );
314 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
316 assertNotNull( action.getGroupToRepositoryMap() );
317 assertEquals( 1, action.getGroupToRepositoryMap().size() );
319 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
320 assertEquals( 1, repos.size() );
321 assertEquals( REPO2_ID, repos.get( 0 ) );
323 action.setRepoGroupId( REPO_GROUP_ID );
324 action.setRepoId( REPO1_ID );
326 result = action.addRepositoryToGroup();
327 assertEquals( Action.ERROR, result );
330 public void testRemoveRepositoryNotInGroup()
333 Configuration configuration = createInitialConfiguration();
335 archivaConfiguration.getConfiguration();
336 archivaConfigurationControl.setReturnValue( configuration, 6 );
337 archivaConfiguration.save( configuration );
338 archivaConfigurationControl.replay();
341 String result = action.execute();
342 assertEquals( Action.SUCCESS, result );
344 assertNotNull( action.getRepositoryGroups() );
345 assertEquals( 1, action.getRepositoryGroups().size() );
346 assertEquals( 2, action.getManagedRepositories().size() );
348 RepositoryGroup repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
349 assertEquals( 1, repoGroup.getRepositories().size() );
350 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
352 assertNotNull( action.getGroupToRepositoryMap() );
353 assertEquals( 1, action.getGroupToRepositoryMap().size() );
355 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
356 assertEquals( 1, repos.size() );
357 assertEquals( REPO2_ID, repos.get( 0 ) );
359 action.setRepoGroupId( REPO_GROUP_ID );
360 action.setRepoId( REPO2_ID );
362 result = action.removeRepositoryFromGroup();
363 assertEquals( Action.ERROR, result );
366 private Configuration createInitialConfiguration()
368 Configuration config = new Configuration();
370 ManagedRepositoryConfiguration managedRepo1 = new ManagedRepositoryConfiguration();
371 managedRepo1.setId( REPO1_ID );
373 config.addManagedRepository( managedRepo1 );
375 ManagedRepositoryConfiguration managedRepo2 = new ManagedRepositoryConfiguration();
376 managedRepo2.setId( REPO2_ID );
378 config.addManagedRepository( managedRepo2 );
380 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
381 repoGroup.setId( REPO_GROUP_ID );
382 repoGroup.addRepository( REPO1_ID );
384 config.addRepositoryGroup( repoGroup );