1 package org.apache.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.beans.RepositoryGroup;
27 import org.apache.archiva.admin.repository.group.DefaultRepositoryGroupAdmin;
28 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
29 import org.apache.archiva.configuration.ArchivaConfiguration;
30 import org.apache.archiva.configuration.Configuration;
31 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.archiva.configuration.RepositoryGroupConfiguration;
33 import org.apache.archiva.web.action.AbstractActionTestCase;
34 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
35 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
36 import org.easymock.MockControl;
38 import java.util.Collections;
39 import java.util.List;
40 import org.junit.Test;
43 * RepositoryGroupsActionTest
45 public class RepositoryGroupsActionTest
46 extends AbstractActionTestCase
48 private static final String REPO_GROUP_ID = "repo-group-ident";
50 private static final String REPO1_ID = "managed-repo-ident-1";
52 private static final String REPO2_ID = "managed-repo-ident-2";
54 private RepositoryGroupsAction action;
56 private MockControl archivaConfigurationControl;
58 private ArchivaConfiguration archivaConfiguration;
66 action = (RepositoryGroupsAction) getActionProxy( "/admin/repositoryGroups.action" ).getAction();
68 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
69 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
71 ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).setArchivaConfiguration(
72 archivaConfiguration );
73 ( (DefaultManagedRepositoryAdmin) ( (DefaultRepositoryGroupAdmin) action.getRepositoryGroupAdmin() ).getManagedRepositoryAdmin() ).setArchivaConfiguration(
74 archivaConfiguration );
78 public void testSecureActionBundle()
79 throws SecureActionException, RepositoryAdminException
81 archivaConfiguration.getConfiguration();
82 archivaConfigurationControl.setReturnValue( new Configuration(), 3, 5 );
83 archivaConfigurationControl.replay();
86 SecureActionBundle bundle = action.getSecureActionBundle();
87 assertTrue( bundle.requiresAuthentication() );
88 assertEquals( 1, bundle.getAuthorizationTuples().size() );
92 public void testAddRepositoryGroup()
95 Configuration configuration = new Configuration();
96 archivaConfiguration.getConfiguration();
97 archivaConfigurationControl.setReturnValue( configuration, 6, 8 );
99 archivaConfiguration.save( configuration );
100 archivaConfigurationControl.replay();
103 RepositoryGroup repositoryGroup = action.getRepositoryGroup();
104 repositoryGroup.setId( REPO_GROUP_ID );
106 String status = action.addRepositoryGroup();
107 assertEquals( Action.SUCCESS, status );
109 assertEquals( Collections.singletonList( repositoryGroup ),
110 action.getRepositoryGroupAdmin().getRepositoriesGroups() );
112 archivaConfigurationControl.verify();
116 public void testAddEmptyRepositoryGroup()
119 Configuration configuration = new Configuration();
120 archivaConfiguration.getConfiguration();
121 archivaConfigurationControl.setReturnValue( configuration, 5, 7 );
123 archivaConfiguration.save( configuration );
125 archivaConfigurationControl.replay();
129 String status = action.addRepositoryGroup();
130 assertEquals( Action.ERROR, status );
132 assertEquals( 0, configuration.getRepositoryGroups().size() );
136 public void testAddDuplicateRepositoryGroup()
139 Configuration configuration = new Configuration();
140 archivaConfiguration.getConfiguration();
141 archivaConfigurationControl.setReturnValue( configuration, 10, 12 );
143 archivaConfiguration.save( configuration );
145 archivaConfigurationControl.replay();
148 RepositoryGroup repositoryGroup = action.getRepositoryGroup();
149 repositoryGroup.setId( REPO_GROUP_ID );
151 String status = action.addRepositoryGroup();
152 assertEquals( Action.SUCCESS, status );
154 assertEquals( Collections.singletonList( repositoryGroup ),
155 action.getRepositoryGroupAdmin().getRepositoriesGroups() );
157 repositoryGroup.setId( REPO_GROUP_ID );
158 status = action.addRepositoryGroup();
160 assertEquals( Action.ERROR, status );
161 assertEquals( Collections.singletonList( repositoryGroup ),
162 action.getRepositoryGroupAdmin().getRepositoriesGroups() );
166 public void testGetRepositoryGroups()
169 ServletRunner sr = new ServletRunner();
170 ServletUnitClient sc = sr.newClient();
172 Configuration configuration = createInitialConfiguration();
174 archivaConfiguration.getConfiguration();
175 archivaConfigurationControl.setReturnValue( configuration, 6, 8 );
176 archivaConfigurationControl.replay();
178 action.setServletRequest( sc.newInvocation( "http://localhost/admin/repositoryGroups.action" ).getRequest() );
180 String result = action.execute();
181 assertEquals( Action.SUCCESS, result );
183 assertEquals( "http://localhost:0/repository", action.getBaseUrl() );
185 assertNotNull( action.getRepositoryGroups() );
186 assertEquals( 1, action.getRepositoryGroups().size() );
187 assertEquals( 2, action.getManagedRepositories().size() );
189 RepositoryGroup repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
191 assertEquals( 1, repoGroup.getRepositories().size() );
192 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
193 assertNotNull( action.getGroupToRepositoryMap() );
194 assertEquals( 1, action.getGroupToRepositoryMap().size() );
196 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
197 assertEquals( 1, repos.size() );
198 assertEquals( REPO2_ID, repos.get( 0 ) );
202 public void testAddRepositoryToGroup()
205 Configuration configuration = createInitialConfiguration();
207 archivaConfiguration.getConfiguration();
208 archivaConfigurationControl.setReturnValue( configuration, 20, 24 );
209 archivaConfiguration.save( configuration );
210 archivaConfigurationControl.replay();
213 String result = action.execute();
214 assertEquals( Action.SUCCESS, result );
216 assertNotNull( action.getRepositoryGroups() );
217 assertEquals( 1, action.getRepositoryGroups().size() );
218 assertEquals( 2, action.getManagedRepositories().size() );
220 RepositoryGroup repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
221 assertEquals( 1, repoGroup.getRepositories().size() );
222 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
224 assertNotNull( action.getGroupToRepositoryMap() );
225 assertEquals( 1, action.getGroupToRepositoryMap().size() );
227 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
228 assertEquals( 1, repos.size() );
229 assertEquals( REPO2_ID, repos.get( 0 ) );
231 action.setRepoGroupId( REPO_GROUP_ID );
232 action.setRepoId( REPO2_ID );
234 result = action.addRepositoryToGroup();
235 assertEquals( Action.SUCCESS, result );
238 result = action.execute();
239 assertEquals( Action.SUCCESS, result );
241 assertEquals( 1, action.getRepositoryGroups().size() );
242 repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
243 assertEquals( 2, repoGroup.getRepositories().size() );
244 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
245 assertEquals( REPO2_ID, repoGroup.getRepositories().get( 1 ) );
247 assertEquals( 0, action.getGroupToRepositoryMap().size() );
248 assertNull( action.getGroupToRepositoryMap().get( repoGroup.getId() ) );
252 public void testRemoveRepositoryFromGroup()
255 Configuration configuration = createInitialConfiguration();
257 archivaConfiguration.getConfiguration();
258 archivaConfigurationControl.setReturnValue( configuration, 19, 22 );
259 archivaConfiguration.save( configuration );
260 archivaConfigurationControl.replay();
263 String result = action.execute();
264 assertEquals( Action.SUCCESS, result );
266 assertNotNull( action.getRepositoryGroups() );
267 assertEquals( 1, action.getRepositoryGroups().size() );
268 assertEquals( 2, action.getManagedRepositories().size() );
270 RepositoryGroup repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
271 assertEquals( 1, repoGroup.getRepositories().size() );
272 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
274 assertNotNull( action.getGroupToRepositoryMap() );
275 assertEquals( 1, action.getGroupToRepositoryMap().size() );
277 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
278 assertEquals( 1, repos.size() );
279 assertEquals( REPO2_ID, repos.get( 0 ) );
281 action.setRepoGroupId( REPO_GROUP_ID );
282 action.setRepoId( REPO1_ID );
284 result = action.removeRepositoryFromGroup();
285 assertEquals( Action.SUCCESS, result );
288 result = action.execute();
289 assertEquals( Action.SUCCESS, result );
291 repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
292 assertEquals( 0, repoGroup.getRepositories().size() );
294 assertNotNull( action.getGroupToRepositoryMap() );
295 assertEquals( 1, action.getGroupToRepositoryMap().size() );
297 repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
298 assertEquals( 2, repos.size() );
299 assertEquals( REPO1_ID, repos.get( 0 ) );
300 assertEquals( REPO2_ID, repos.get( 1 ) );
304 public void testAddDuplicateRepositoryToGroup()
307 Configuration configuration = createInitialConfiguration();
309 archivaConfiguration.getConfiguration();
310 archivaConfigurationControl.setReturnValue( configuration, 6, 10 );
311 archivaConfiguration.save( configuration );
312 archivaConfigurationControl.replay();
315 String result = action.execute();
316 assertEquals( Action.SUCCESS, result );
318 assertNotNull( action.getRepositoryGroups() );
319 assertEquals( 1, action.getRepositoryGroups().size() );
320 assertEquals( 2, action.getManagedRepositories().size() );
322 RepositoryGroup repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
323 assertEquals( 1, repoGroup.getRepositories().size() );
324 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
326 assertNotNull( action.getGroupToRepositoryMap() );
327 assertEquals( 1, action.getGroupToRepositoryMap().size() );
329 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
330 assertEquals( 1, repos.size() );
331 assertEquals( REPO2_ID, repos.get( 0 ) );
333 action.setRepoGroupId( REPO_GROUP_ID );
334 action.setRepoId( REPO1_ID );
336 result = action.addRepositoryToGroup();
337 assertEquals( Action.ERROR, result );
341 public void testRemoveRepositoryNotInGroup()
344 Configuration configuration = createInitialConfiguration();
346 archivaConfiguration.getConfiguration();
347 archivaConfigurationControl.setReturnValue( configuration, 6, 10 );
348 archivaConfiguration.save( configuration );
349 archivaConfigurationControl.replay();
352 String result = action.execute();
353 assertEquals( Action.SUCCESS, result );
355 assertNotNull( action.getRepositoryGroups() );
356 assertEquals( 1, action.getRepositoryGroups().size() );
357 assertEquals( 2, action.getManagedRepositories().size() );
359 RepositoryGroup repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
360 assertEquals( 1, repoGroup.getRepositories().size() );
361 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
363 assertNotNull( action.getGroupToRepositoryMap() );
364 assertEquals( 1, action.getGroupToRepositoryMap().size() );
366 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
367 assertEquals( 1, repos.size() );
368 assertEquals( REPO2_ID, repos.get( 0 ) );
370 action.setRepoGroupId( REPO_GROUP_ID );
371 action.setRepoId( REPO2_ID );
373 result = action.removeRepositoryFromGroup();
374 assertEquals( Action.ERROR, result );
377 private Configuration createInitialConfiguration()
379 Configuration config = new Configuration();
381 ManagedRepositoryConfiguration managedRepo1 = new ManagedRepositoryConfiguration();
382 managedRepo1.setId( REPO1_ID );
384 config.addManagedRepository( managedRepo1 );
386 ManagedRepositoryConfiguration managedRepo2 = new ManagedRepositoryConfiguration();
387 managedRepo2.setId( REPO2_ID );
389 config.addManagedRepository( managedRepo2 );
391 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
392 repoGroup.setId( REPO_GROUP_ID );
393 repoGroup.addRepository( REPO1_ID );
395 config.addRepositoryGroup( repoGroup );