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.opensymphony.xwork2.Action;
23 import org.apache.archiva.admin.model.managed.ManagedRepository;
24 import org.apache.archiva.admin.repository.RepositoryCommonValidator;
25 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
26 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
27 import org.apache.archiva.scheduler.repository.RepositoryTask;
28 import org.apache.archiva.security.ArchivaRoleConstants;
29 import org.apache.archiva.web.validator.utils.ValidatorUtil;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.Configuration;
33 import org.codehaus.plexus.redback.role.RoleManager;
34 import org.codehaus.plexus.registry.Registry;
35 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
36 import org.codehaus.redback.integration.interceptor.SecureActionException;
37 import org.easymock.MockControl;
38 import org.easymock.classextension.MockClassControl;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.HashMap;
44 import java.util.List;
48 * AddManagedRepositoryActionTest
52 public class AddManagedRepositoryActionTest
53 extends AbstractManagedRepositoryActionTest
55 private AddManagedRepositoryAction action;
57 private RoleManager roleManager;
59 private MockControl roleManagerControl;
61 private MockControl archivaConfigurationControl;
63 private Registry registry;
65 private MockControl registryControl;
67 private ArchivaConfiguration archivaConfiguration;
69 private MockControl repositoryTaskSchedulerControl;
71 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
75 protected void setUp()
80 action = new AddManagedRepositoryAction();
82 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
83 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
84 action.setArchivaConfiguration( archivaConfiguration );
86 roleManagerControl = MockControl.createControl( RoleManager.class );
87 roleManager = (RoleManager) roleManagerControl.getMock();
89 registryControl = MockControl.createControl( Registry.class );
90 registry = (Registry) registryControl.getMock();
91 //action.setRegistry( registry );
93 repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
94 repositoryTaskScheduler = (RepositoryArchivaTaskScheduler) repositoryTaskSchedulerControl.getMock();
96 location = new File( "target/test/location" );
97 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfiguration );
98 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRoleManager( roleManager );
99 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRegistry( registry );
100 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryTaskScheduler(
101 repositoryTaskScheduler );
103 RepositoryCommonValidator repositoryCommonValidator = new RepositoryCommonValidator();
104 repositoryCommonValidator.setArchivaConfiguration( archivaConfiguration );
105 repositoryCommonValidator.setRegistry( registry );
107 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryCommonValidator(
108 repositoryCommonValidator );
110 action.setRepositoryCommonValidator( repositoryCommonValidator );
112 action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
116 public void testSecureActionBundle()
117 throws SecureActionException
119 archivaConfiguration.getConfiguration();
120 archivaConfigurationControl.setReturnValue( new Configuration() );
121 archivaConfigurationControl.replay();
124 SecureActionBundle bundle = action.getSecureActionBundle();
125 assertTrue( bundle.requiresAuthentication() );
126 assertEquals( 1, bundle.getAuthorizationTuples().size() );
129 public void testAddRepositoryInitialPage()
132 archivaConfiguration.getConfiguration();
133 archivaConfigurationControl.setReturnValue( new Configuration() );
134 archivaConfiguration.getConfiguration();
135 archivaConfigurationControl.setReturnValue( new Configuration() );
136 archivaConfigurationControl.replay();
139 ManagedRepository configuration = action.getRepository();
140 assertNotNull( configuration );
141 assertNull( configuration.getId() );
142 // check all booleans are false
143 assertFalse( configuration.isDeleteReleasedSnapshots() );
144 assertFalse( configuration.isScanned() );
145 assertFalse( configuration.isReleases() );
146 assertFalse( configuration.isSnapshots() );
148 String status = action.input();
149 assertEquals( Action.INPUT, status );
152 assertFalse( configuration.isDeleteReleasedSnapshots() );
153 assertTrue( configuration.isScanned() );
154 assertTrue( configuration.isReleases() );
155 assertFalse( configuration.isSnapshots() );
158 public void testAddRepository()
161 FileUtils.deleteDirectory( location );
163 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
164 roleManagerControl.setReturnValue( false );
165 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
166 roleManagerControl.setVoidCallable();
167 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
168 roleManagerControl.setReturnValue( false );
169 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
170 roleManagerControl.setVoidCallable();
172 roleManagerControl.replay();
174 registry.getString( "appserver.base", "${appserver.base}" );
175 registryControl.setReturnValue( "target/test" );
176 registry.getString( "appserver.home", "${appserver.home}" );
177 registryControl.setReturnValue( "target/test" );
179 registry.getString( "appserver.base", "${appserver.base}" );
180 registryControl.setReturnValue( "target/test" );
181 registry.getString( "appserver.home", "${appserver.home}" );
182 registryControl.setReturnValue( "target/test" );
184 registryControl.replay();
186 RepositoryTask task = new RepositoryTask();
187 task.setRepositoryId( REPO_ID );
188 repositoryTaskScheduler.isProcessingRepositoryTask( REPO_ID );
189 repositoryTaskSchedulerControl.setReturnValue( false );
190 repositoryTaskScheduler.queueTask( task );
191 repositoryTaskSchedulerControl.setVoidCallable();
192 repositoryTaskSchedulerControl.replay();
194 Configuration configuration = new Configuration();
195 archivaConfiguration.getConfiguration();
196 archivaConfigurationControl.setReturnValue( configuration );
198 archivaConfiguration.getConfiguration();
199 archivaConfigurationControl.setReturnValue( configuration );
201 archivaConfiguration.getConfiguration();
202 archivaConfigurationControl.setReturnValue( configuration );
204 archivaConfiguration.save( configuration );
206 archivaConfigurationControl.replay();
209 ManagedRepository repository = action.getRepository();
210 populateRepository( repository );
212 assertFalse( location.exists() );
213 String status = action.commit();
214 assertEquals( Action.SUCCESS, status );
215 assertTrue( location.exists() );
217 assertEquals( Collections.singletonList( repository ), getManagedRepositoryAdmin().getManagedRepositories() );
218 assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
220 roleManagerControl.verify();
221 archivaConfigurationControl.verify();
222 registryControl.verify();
226 public void testAddRepositoryExistingLocation()
229 if ( !location.exists() )
234 registry.getString( "appserver.base", "${appserver.base}" );
235 registryControl.setReturnValue( "target/test" );
236 registry.getString( "appserver.home", "${appserver.home}" );
237 registryControl.setReturnValue( "target/test" );
239 registryControl.replay();
242 ManagedRepository repository = action.getRepository();
243 populateRepository( repository );
245 assertTrue( location.exists() );
246 String status = action.commit();
247 assertEquals( AddManagedRepositoryAction.CONFIRM, status );
248 assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
249 registryControl.verify();
252 public void testStruts2ValidationFrameworkWithNullInputs()
256 // 0 is the default value for primitive int; null for objects
257 ManagedRepository managedRepositoryConfiguration = createManagedRepository( null, null, null, null );
258 action.setRepository( managedRepositoryConfiguration );
261 actionValidatorManager.validate( action, EMPTY_STRING );
264 assertTrue( action.hasFieldErrors() );
266 Map<String, List<String>> fieldErrors = action.getFieldErrors();
268 // make an expected field error object
269 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
272 List<String> expectedErrorMessages = new ArrayList<String>();
273 expectedErrorMessages.add( "You must enter a repository identifier." );
274 expectedFieldErrors.put( "repository.id", expectedErrorMessages );
276 expectedErrorMessages = new ArrayList<String>();
277 expectedErrorMessages.add( "You must enter a directory." );
278 expectedFieldErrors.put( "repository.location", expectedErrorMessages );
280 expectedErrorMessages = new ArrayList<String>();
281 expectedErrorMessages.add( "You must enter a repository name." );
282 expectedFieldErrors.put( "repository.name", expectedErrorMessages );
284 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
287 public void testStruts2ValidationFrameworkWithBlankInputs()
291 // 0 is the default value for primitive int
292 ManagedRepository managedRepositoryConfiguration =
293 createManagedRepository( EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING );
294 action.setRepository( managedRepositoryConfiguration );
297 actionValidatorManager.validate( action, EMPTY_STRING );
300 assertTrue( action.hasFieldErrors() );
302 Map<String, List<String>> fieldErrors = action.getFieldErrors();
304 // make an expected field error object
305 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
308 List<String> expectedErrorMessages = new ArrayList<String>();
309 expectedErrorMessages.add( "You must enter a repository identifier." );
310 expectedFieldErrors.put( "repository.id", expectedErrorMessages );
312 expectedErrorMessages = new ArrayList<String>();
313 expectedErrorMessages.add( "You must enter a directory." );
314 expectedFieldErrors.put( "repository.location", expectedErrorMessages );
316 expectedErrorMessages = new ArrayList<String>();
317 expectedErrorMessages.add( "You must enter a repository name." );
318 expectedFieldErrors.put( "repository.name", expectedErrorMessages );
320 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
323 public void testStruts2ValidationFrameworkWithInvalidInputs()
327 ManagedRepository managedRepositoryConfiguration =
328 createManagedRepository( REPOSITORY_ID_INVALID_INPUT, REPOSITORY_NAME_INVALID_INPUT,
329 REPOSITORY_LOCATION_INVALID_INPUT, REPOSITORY_INDEX_DIR_INVALID_INPUT,
330 REPOSITORY_DAYS_OLDER_INVALID_INPUT, REPOSITORY_RETENTION_COUNT_INVALID_INPUT );
331 action.setRepository( managedRepositoryConfiguration );
334 actionValidatorManager.validate( action, EMPTY_STRING );
337 assertTrue( action.hasFieldErrors() );
339 Map<String, List<String>> fieldErrors = action.getFieldErrors();
341 // make an expected field error object
342 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
345 List<String> expectedErrorMessages = new ArrayList<String>();
346 expectedErrorMessages.add(
347 "Identifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
348 expectedFieldErrors.put( "repository.id", expectedErrorMessages );
350 expectedErrorMessages = new ArrayList<String>();
351 expectedErrorMessages.add(
352 "Directory must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-)." );
353 expectedFieldErrors.put( "repository.location", expectedErrorMessages );
355 expectedErrorMessages = new ArrayList<String>();
356 expectedErrorMessages.add(
357 "Repository Name must only contain alphanumeric characters, white-spaces(' '), forward-slashes(/), open-parenthesis('('), close-parenthesis(')'), underscores(_), dots(.), and dashes(-)." );
358 expectedFieldErrors.put( "repository.name", expectedErrorMessages );
360 expectedErrorMessages = new ArrayList<String>();
361 expectedErrorMessages.add(
362 "Index directory must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-)." );
363 expectedFieldErrors.put( "repository.indexDirectory", expectedErrorMessages );
365 expectedErrorMessages = new ArrayList<String>();
366 expectedErrorMessages.add( "Repository Purge By Retention Count needs to be between 1 and 100." );
367 expectedFieldErrors.put( "repository.retentionCount", expectedErrorMessages );
369 expectedErrorMessages = new ArrayList<String>();
370 expectedErrorMessages.add( "Repository Purge By Days Older Than needs to be larger than 0." );
371 expectedFieldErrors.put( "repository.daysOlder", expectedErrorMessages );
373 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
376 public void testStruts2ValidationFrameworkWithValidInputs()
380 ManagedRepository managedRepositoryConfiguration =
381 createManagedRepository( REPOSITORY_ID_VALID_INPUT, REPOSITORY_NAME_VALID_INPUT,
382 REPOSITORY_LOCATION_VALID_INPUT, REPOSITORY_INDEX_DIR_VALID_INPUT,
383 REPOSITORY_DAYS_OLDER_VALID_INPUT, REPOSITORY_RETENTION_COUNT_VALID_INPUT );
384 action.setRepository( managedRepositoryConfiguration );
387 actionValidatorManager.validate( action, EMPTY_STRING );
390 assertFalse( action.hasFieldErrors() );
393 // TODO: test errors during add, other actions