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.repository.RepositoryAdminException;
26 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
27 import org.apache.maven.archiva.configuration.Configuration;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
30 import org.apache.maven.archiva.web.action.AbstractActionTestCase;
31 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
32 import org.codehaus.redback.integration.interceptor.SecureActionException;
33 import org.easymock.MockControl;
35 import java.util.Collections;
36 import java.util.List;
39 * RepositoryGroupsActionTest
41 public class RepositoryGroupsActionTest
42 extends AbstractActionTestCase
44 private static final String REPO_GROUP_ID = "repo-group-ident";
46 private static final String REPO1_ID = "managed-repo-ident-1";
48 private static final String REPO2_ID = "managed-repo-ident-2";
50 private RepositoryGroupsAction action;
52 private MockControl archivaConfigurationControl;
54 private ArchivaConfiguration archivaConfiguration;
56 protected void setUp()
61 action = (RepositoryGroupsAction) getActionProxy( "/admin/repositoryGroups.action" ).getAction();
63 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
64 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
65 action.setArchivaConfiguration( archivaConfiguration );
68 public void testSecureActionBundle()
69 throws SecureActionException, RepositoryAdminException
71 archivaConfiguration.getConfiguration();
72 archivaConfigurationControl.setReturnValue( new Configuration() );
73 archivaConfigurationControl.replay();
76 SecureActionBundle bundle = action.getSecureActionBundle();
77 assertTrue( bundle.requiresAuthentication() );
78 assertEquals( 1, bundle.getAuthorizationTuples().size() );
81 public void testAddRepositoryGroup()
84 Configuration configuration = new Configuration();
85 archivaConfiguration.getConfiguration();
86 archivaConfigurationControl.setReturnValue( configuration, 2 );
88 archivaConfiguration.save( configuration );
89 archivaConfigurationControl.replay();
92 RepositoryGroupConfiguration repositoryGroup = action.getRepositoryGroup();
93 repositoryGroup.setId( REPO_GROUP_ID );
95 String status = action.addRepositoryGroup();
96 assertEquals( Action.SUCCESS, status );
98 assertEquals( Collections.singletonList( repositoryGroup ), configuration.getRepositoryGroups() );
100 archivaConfigurationControl.verify();
103 public void testAddEmptyRepositoryGroup()
106 Configuration configuration = new Configuration();
107 archivaConfiguration.getConfiguration();
108 archivaConfigurationControl.setReturnValue( configuration, 2 );
110 archivaConfiguration.save( configuration );
112 archivaConfigurationControl.replay();
116 String status = action.addRepositoryGroup();
117 assertEquals( Action.ERROR, status );
119 assertEquals( 0, configuration.getRepositoryGroups().size() );
122 public void testAddDuplicateRepositoryGroup()
125 Configuration configuration = new Configuration();
126 archivaConfiguration.getConfiguration();
127 archivaConfigurationControl.setReturnValue( configuration, 3 );
129 archivaConfiguration.save( configuration );
131 archivaConfigurationControl.replay();
134 RepositoryGroupConfiguration repositoryGroup = action.getRepositoryGroup();
135 repositoryGroup.setId( REPO_GROUP_ID );
137 String status = action.addRepositoryGroup();
138 assertEquals( Action.SUCCESS, status );
140 assertEquals( Collections.singletonList( repositoryGroup ), configuration.getRepositoryGroups() );
142 repositoryGroup.setId( REPO_GROUP_ID );
143 status = action.addRepositoryGroup();
145 assertEquals( Action.ERROR, status );
146 assertEquals( Collections.singletonList( repositoryGroup ), configuration.getRepositoryGroups() );
149 public void testGetRepositoryGroups()
152 ServletRunner sr = new ServletRunner();
153 ServletUnitClient sc = sr.newClient();
155 Configuration configuration = createInitialConfiguration();
157 archivaConfiguration.getConfiguration();
158 archivaConfigurationControl.setReturnValue( configuration );
159 archivaConfigurationControl.replay();
161 action.setServletRequest( sc.newInvocation( "http://localhost/admin/repositoryGroups.action" ).getRequest() );
163 String result = action.execute();
164 assertEquals( Action.SUCCESS, result );
166 assertEquals( "http://localhost:0/repository", action.getBaseUrl() );
168 assertNotNull( action.getRepositoryGroups() );
169 assertEquals( 1, action.getRepositoryGroups().size() );
170 assertEquals( 2, action.getManagedRepositories().size() );
172 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
174 assertEquals( 1, repoGroup.getRepositories().size() );
175 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
176 assertNotNull( action.getGroupToRepositoryMap() );
177 assertEquals( 1, action.getGroupToRepositoryMap().size() );
179 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
180 assertEquals( 1, repos.size() );
181 assertEquals( REPO2_ID, repos.get( 0 ) );
184 public void testAddRepositoryToGroup()
187 Configuration configuration = createInitialConfiguration();
189 archivaConfiguration.getConfiguration();
190 archivaConfigurationControl.setReturnValue( configuration, 6 );
191 archivaConfiguration.save( configuration );
192 archivaConfigurationControl.replay();
195 String result = action.execute();
196 assertEquals( Action.SUCCESS, result );
198 assertNotNull( action.getRepositoryGroups() );
199 assertEquals( 1, action.getRepositoryGroups().size() );
200 assertEquals( 2, action.getManagedRepositories().size() );
202 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
203 assertEquals( 1, repoGroup.getRepositories().size() );
204 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
206 assertNotNull( action.getGroupToRepositoryMap() );
207 assertEquals( 1, action.getGroupToRepositoryMap().size() );
209 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
210 assertEquals( 1, repos.size() );
211 assertEquals( REPO2_ID, repos.get( 0 ) );
213 action.setRepoGroupId( REPO_GROUP_ID );
214 action.setRepoId( REPO2_ID );
216 result = action.addRepositoryToGroup();
217 assertEquals( Action.SUCCESS, result );
220 result = action.execute();
221 assertEquals( Action.SUCCESS, result );
223 assertEquals( 1, action.getRepositoryGroups().size() );
224 repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
225 assertEquals( 2, repoGroup.getRepositories().size() );
226 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
227 assertEquals( REPO2_ID, repoGroup.getRepositories().get( 1 ) );
229 assertEquals( 0, action.getGroupToRepositoryMap().size() );
230 assertNull( action.getGroupToRepositoryMap().get( repoGroup.getId() ) );
233 public void testRemoveRepositoryFromGroup()
236 Configuration configuration = createInitialConfiguration();
238 archivaConfiguration.getConfiguration();
239 archivaConfigurationControl.setReturnValue( configuration, 6 );
240 archivaConfiguration.save( configuration );
241 archivaConfigurationControl.replay();
244 String result = action.execute();
245 assertEquals( Action.SUCCESS, result );
247 assertNotNull( action.getRepositoryGroups() );
248 assertEquals( 1, action.getRepositoryGroups().size() );
249 assertEquals( 2, action.getManagedRepositories().size() );
251 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
252 assertEquals( 1, repoGroup.getRepositories().size() );
253 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
255 assertNotNull( action.getGroupToRepositoryMap() );
256 assertEquals( 1, action.getGroupToRepositoryMap().size() );
258 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
259 assertEquals( 1, repos.size() );
260 assertEquals( REPO2_ID, repos.get( 0 ) );
262 action.setRepoGroupId( REPO_GROUP_ID );
263 action.setRepoId( REPO1_ID );
265 result = action.removeRepositoryFromGroup();
266 assertEquals( Action.SUCCESS, result );
269 result = action.execute();
270 assertEquals( Action.SUCCESS, result );
272 repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
273 assertEquals( 0, repoGroup.getRepositories().size() );
275 assertNotNull( action.getGroupToRepositoryMap() );
276 assertEquals( 1, action.getGroupToRepositoryMap().size() );
278 repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
279 assertEquals( 2, repos.size() );
280 assertEquals( REPO1_ID, repos.get( 0 ) );
281 assertEquals( REPO2_ID, repos.get( 1 ) );
284 public void testAddDuplicateRepositoryToGroup()
287 Configuration configuration = createInitialConfiguration();
289 archivaConfiguration.getConfiguration();
290 archivaConfigurationControl.setReturnValue( configuration, 6 );
291 archivaConfiguration.save( configuration );
292 archivaConfigurationControl.replay();
295 String result = action.execute();
296 assertEquals( Action.SUCCESS, result );
298 assertNotNull( action.getRepositoryGroups() );
299 assertEquals( 1, action.getRepositoryGroups().size() );
300 assertEquals( 2, action.getManagedRepositories().size() );
302 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
303 assertEquals( 1, repoGroup.getRepositories().size() );
304 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
306 assertNotNull( action.getGroupToRepositoryMap() );
307 assertEquals( 1, action.getGroupToRepositoryMap().size() );
309 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
310 assertEquals( 1, repos.size() );
311 assertEquals( REPO2_ID, repos.get( 0 ) );
313 action.setRepoGroupId( REPO_GROUP_ID );
314 action.setRepoId( REPO1_ID );
316 result = action.addRepositoryToGroup();
317 assertEquals( Action.ERROR, result );
320 public void testRemoveRepositoryNotInGroup()
323 Configuration configuration = createInitialConfiguration();
325 archivaConfiguration.getConfiguration();
326 archivaConfigurationControl.setReturnValue( configuration, 6 );
327 archivaConfiguration.save( configuration );
328 archivaConfigurationControl.replay();
331 String result = action.execute();
332 assertEquals( Action.SUCCESS, result );
334 assertNotNull( action.getRepositoryGroups() );
335 assertEquals( 1, action.getRepositoryGroups().size() );
336 assertEquals( 2, action.getManagedRepositories().size() );
338 RepositoryGroupConfiguration repoGroup = action.getRepositoryGroups().get( REPO_GROUP_ID );
339 assertEquals( 1, repoGroup.getRepositories().size() );
340 assertEquals( REPO1_ID, repoGroup.getRepositories().get( 0 ) );
342 assertNotNull( action.getGroupToRepositoryMap() );
343 assertEquals( 1, action.getGroupToRepositoryMap().size() );
345 List<String> repos = action.getGroupToRepositoryMap().get( repoGroup.getId() );
346 assertEquals( 1, repos.size() );
347 assertEquals( REPO2_ID, repos.get( 0 ) );
349 action.setRepoGroupId( REPO_GROUP_ID );
350 action.setRepoId( REPO2_ID );
352 result = action.removeRepositoryFromGroup();
353 assertEquals( Action.ERROR, result );
356 private Configuration createInitialConfiguration()
358 Configuration config = new Configuration();
360 ManagedRepositoryConfiguration managedRepo1 = new ManagedRepositoryConfiguration();
361 managedRepo1.setId( REPO1_ID );
363 config.addManagedRepository( managedRepo1 );
365 ManagedRepositoryConfiguration managedRepo2 = new ManagedRepositoryConfiguration();
366 managedRepo2.setId( REPO2_ID );
368 config.addManagedRepository( managedRepo2 );
370 RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
371 repoGroup.setId( REPO_GROUP_ID );
372 repoGroup.addRepository( REPO1_ID );
374 config.addRepositoryGroup( repoGroup );