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 java.util.Collections;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
28 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
29 import org.codehaus.redback.integration.interceptor.SecureActionException;
30 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
31 import org.easymock.MockControl;
33 import com.meterware.servletunit.ServletRunner;
34 import com.meterware.servletunit.ServletUnitClient;
35 import com.opensymphony.xwork2.Action;
38 * RepositoryGroupsActionTest
42 public class RepositoryGroupsActionTest
43 extends PlexusInSpringTestCase
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" );
64 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
65 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
66 action.setArchivaConfiguration( archivaConfiguration );
69 public void testSecureActionBundle()
70 throws SecureActionException
72 archivaConfiguration.getConfiguration();
73 archivaConfigurationControl.setReturnValue( new Configuration() );
74 archivaConfigurationControl.replay();
77 SecureActionBundle bundle = action.getSecureActionBundle();
78 assertTrue( bundle.requiresAuthentication() );
79 assertEquals( 1, bundle.getAuthorizationTuples().size() );
82 public void testAddRepositoryGroup()
85 Configuration configuration = new Configuration();
86 archivaConfiguration.getConfiguration();
87 archivaConfigurationControl.setReturnValue( configuration, 2 );
89 archivaConfiguration.save( configuration );
90 archivaConfigurationControl.replay();
93 RepositoryGroupConfiguration repositoryGroup = action.getRepositoryGroup();
94 repositoryGroup.setId( REPO_GROUP_ID );
96 String status = action.addRepositoryGroup();
97 assertEquals( Action.SUCCESS, status );
99 assertEquals( Collections.singletonList( repositoryGroup ), configuration.getRepositoryGroups() );
101 archivaConfigurationControl.verify();
104 public void testAddEmptyRepositoryGroup()
107 Configuration configuration = new Configuration();
108 archivaConfiguration.getConfiguration();
109 archivaConfigurationControl.setReturnValue( configuration, 2 );
111 archivaConfiguration.save( configuration );
113 archivaConfigurationControl.replay();
117 String status = action.addRepositoryGroup();
118 assertEquals( Action.ERROR, status );
120 assertEquals( 0, configuration.getRepositoryGroups().size() );
123 public void testAddDuplicateRepositoryGroup()
126 Configuration configuration = new Configuration();
127 archivaConfiguration.getConfiguration();
128 archivaConfigurationControl.setReturnValue( configuration, 3 );
130 archivaConfiguration.save( configuration );
132 archivaConfigurationControl.replay();
135 RepositoryGroupConfiguration repositoryGroup = action.getRepositoryGroup();
136 repositoryGroup.setId( REPO_GROUP_ID );
138 String status = action.addRepositoryGroup();
139 assertEquals( Action.SUCCESS, status );
141 assertEquals( Collections.singletonList( repositoryGroup ), configuration.getRepositoryGroups() );
143 repositoryGroup.setId( REPO_GROUP_ID );
144 status = action.addRepositoryGroup();
146 assertEquals( Action.ERROR, status );
147 assertEquals( Collections.singletonList( repositoryGroup ), configuration.getRepositoryGroups() );
150 public void testGetRepositoryGroups()
153 ServletRunner sr = new ServletRunner();
154 ServletUnitClient sc = sr.newClient();
156 Configuration configuration = createInitialConfiguration();
158 archivaConfiguration.getConfiguration();
159 archivaConfigurationControl.setReturnValue( configuration );
160 archivaConfigurationControl.replay();
162 action.setServletRequest( sc.newInvocation( "http://localhost/admin/repositoryGroups.action" ).getRequest() );
164 String result = action.execute();
165 assertEquals( Action.SUCCESS, result );
167 assertEquals( "http://localhost:0/repository", action.getBaseUrl() );
169 assertNotNull( action.getRepositoryGroups() );
170 assertEquals( 1, action.getRepositoryGroups().size() );
171 assertEquals( 2, action.getManagedRepositories().size() );
173 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
175 assertEquals( 1, repoGroup.getRepositories().size() );
176 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
177 assertNotNull( action.getGroupToRepositoryMap() );
178 assertEquals( 1, action.getGroupToRepositoryMap().size() );
180 java.util.List repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
181 assertEquals( 1, repos.size() );
182 assertEquals( REPO2_ID, repos.get( 0 ) );
185 public void testAddRepositoryToGroup()
188 Configuration configuration = createInitialConfiguration();
190 archivaConfiguration.getConfiguration();
191 archivaConfigurationControl.setReturnValue( configuration, 6 );
192 archivaConfiguration.save( configuration );
193 archivaConfigurationControl.replay();
196 String result = action.execute();
197 assertEquals( Action.SUCCESS, result );
199 assertNotNull( action.getRepositoryGroups() );
200 assertEquals( 1, action.getRepositoryGroups().size() );
201 assertEquals( 2, action.getManagedRepositories().size() );
203 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
204 assertEquals( 1 , repoGroup.getRepositories().size() );
205 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
207 assertNotNull( action.getGroupToRepositoryMap() );
208 assertEquals( 1, action.getGroupToRepositoryMap().size() );
210 java.util.List repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
211 assertEquals( 1, repos.size() );
212 assertEquals( REPO2_ID, repos.get( 0 ) );
214 action.setRepoGroupId( REPO_GROUP_ID );
215 action.setRepoId( REPO2_ID );
217 result = action.addRepositoryToGroup();
218 assertEquals( Action.SUCCESS, result );
221 result = action.execute();
222 assertEquals( Action.SUCCESS, result );
224 assertEquals( 1, action.getRepositoryGroups().size() );
225 repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
226 assertEquals( 2, repoGroup.getRepositories().size() );
227 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
228 assertEquals( REPO2_ID, repoGroup.getRepositories().get( 1 ) );
230 assertEquals( 0, action.getGroupToRepositoryMap().size() );
231 assertNull( action.getGroupToRepositoryMap().get( repoGroup.getId() ) );
234 public void testRemoveRepositoryFromGroup()
237 Configuration configuration = createInitialConfiguration();
239 archivaConfiguration.getConfiguration();
240 archivaConfigurationControl.setReturnValue( configuration, 6 );
241 archivaConfiguration.save( configuration );
242 archivaConfigurationControl.replay();
245 String result = action.execute();
246 assertEquals( Action.SUCCESS, result );
248 assertNotNull( action.getRepositoryGroups() );
249 assertEquals( 1, action.getRepositoryGroups().size() );
250 assertEquals( 2, action.getManagedRepositories().size() );
252 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
253 assertEquals( 1 , repoGroup.getRepositories().size() );
254 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
256 assertNotNull( action.getGroupToRepositoryMap() );
257 assertEquals( 1, action.getGroupToRepositoryMap().size() );
259 java.util.List repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
260 assertEquals( 1, repos.size() );
261 assertEquals( REPO2_ID, repos.get( 0 ) );
263 action.setRepoGroupId( REPO_GROUP_ID );
264 action.setRepoId( REPO1_ID );
266 result = action.removeRepositoryFromGroup();
267 assertEquals( Action.SUCCESS, result );
270 result = action.execute();
271 assertEquals( Action.SUCCESS, result );
273 repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
274 assertEquals( 0, repoGroup.getRepositories().size() );
276 assertNotNull( action.getGroupToRepositoryMap() );
277 assertEquals( 1, action.getGroupToRepositoryMap().size() );
279 repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
280 assertEquals( 2, repos.size() );
281 assertEquals( REPO1_ID, repos.get( 0 ) );
282 assertEquals( REPO2_ID, repos.get( 1 ) );
285 public void testAddDuplicateRepositoryToGroup()
288 Configuration configuration = createInitialConfiguration();
290 archivaConfiguration.getConfiguration();
291 archivaConfigurationControl.setReturnValue( configuration, 6 );
292 archivaConfiguration.save( configuration );
293 archivaConfigurationControl.replay();
296 String result = action.execute();
297 assertEquals( Action.SUCCESS, result );
299 assertNotNull( action.getRepositoryGroups() );
300 assertEquals( 1, action.getRepositoryGroups().size() );
301 assertEquals( 2, action.getManagedRepositories().size() );
303 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
304 assertEquals( 1 , repoGroup.getRepositories().size() );
305 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
307 assertNotNull( action.getGroupToRepositoryMap() );
308 assertEquals( 1, action.getGroupToRepositoryMap().size() );
310 java.util.List repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
311 assertEquals( 1, repos.size() );
312 assertEquals( REPO2_ID, repos.get( 0 ) );
314 action.setRepoGroupId( REPO_GROUP_ID );
315 action.setRepoId( REPO1_ID );
317 result = action.addRepositoryToGroup();
318 assertEquals( Action.ERROR, result );
321 public void testRemoveRepositoryNotInGroup()
324 Configuration configuration = createInitialConfiguration();
326 archivaConfiguration.getConfiguration();
327 archivaConfigurationControl.setReturnValue( configuration, 6 );
328 archivaConfiguration.save( configuration );
329 archivaConfigurationControl.replay();
332 String result = action.execute();
333 assertEquals( Action.SUCCESS, result );
335 assertNotNull( action.getRepositoryGroups() );
336 assertEquals( 1, action.getRepositoryGroups().size() );
337 assertEquals( 2, action.getManagedRepositories().size() );
339 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
340 assertEquals( 1 , repoGroup.getRepositories().size() );
341 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
343 assertNotNull( action.getGroupToRepositoryMap() );
344 assertEquals( 1, action.getGroupToRepositoryMap().size() );
346 java.util.List repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
347 assertEquals( 1, repos.size() );
348 assertEquals( REPO2_ID, repos.get( 0 ) );
350 action.setRepoGroupId( REPO_GROUP_ID );
351 action.setRepoId( REPO2_ID );
353 result = action.removeRepositoryFromGroup();
354 assertEquals( Action.ERROR, result );
357 private Configuration createInitialConfiguration()
359 Configuration config = new Configuration();
361 ManagedRepositoryConfiguration managedRepo1 = new ManagedRepositoryConfiguration();
362 managedRepo1.setId( REPO1_ID );
364 config.addManagedRepository( managedRepo1 );
366 ManagedRepositoryConfiguration managedRepo2 = new ManagedRepositoryConfiguration();
367 managedRepo2.setId( REPO2_ID );
369 config.addManagedRepository( managedRepo2 );
371 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
372 repoGroup.setId( REPO_GROUP_ID );
373 repoGroup.addRepository( REPO1_ID );
375 config.addRepositoryGroup( repoGroup );