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.opensymphony.xwork2.Action;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.repository.DefaultRepositoryCommonValidator;
25 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.Configuration;
28 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
29 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
30 import org.apache.archiva.redback.role.RoleManager;
31 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
32 import org.apache.archiva.scheduler.repository.RepositoryTask;
33 import org.apache.archiva.security.common.ArchivaRoleConstants;
34 import org.apache.archiva.web.validator.utils.ValidatorUtil;
35 import org.apache.commons.io.FileUtils;
36 import org.easymock.MockControl;
37 import org.easymock.classextension.MockClassControl;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.HashMap;
43 import java.util.List;
45 import org.junit.Before;
46 import org.junit.Test;
49 * AddManagedRepositoryActionTest
53 public class AddManagedRepositoryActionTest
54 extends AbstractManagedRepositoryActionTest
56 private AddManagedRepositoryAction action;
58 private RoleManager roleManager;
60 private MockControl roleManagerControl;
62 private MockControl archivaConfigurationControl;
64 private org.apache.archiva.redback.components.registry.Registry registry;
66 private MockControl registryControl;
68 private ArchivaConfiguration archivaConfiguration;
70 private MockControl repositoryTaskSchedulerControl;
72 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
82 action = new AddManagedRepositoryAction();
84 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
85 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
87 roleManagerControl = MockControl.createControl( RoleManager.class );
88 roleManager = (RoleManager) roleManagerControl.getMock();
90 registryControl = MockControl.createControl( org.apache.archiva.redback.components.registry.Registry.class );
91 registry = (org.apache.archiva.redback.components.registry.Registry) registryControl.getMock();
92 //action.setRegistry( registry );
94 repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
95 repositoryTaskScheduler = (RepositoryArchivaTaskScheduler) repositoryTaskSchedulerControl.getMock();
97 location = new File( System.getProperty( "basedir" ), "target/test/location" );
98 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfiguration );
99 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRoleManager( roleManager );
100 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRegistry( registry );
101 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryTaskScheduler(
102 repositoryTaskScheduler );
104 DefaultRepositoryCommonValidator defaultRepositoryCommonValidator = new DefaultRepositoryCommonValidator();
105 defaultRepositoryCommonValidator.setArchivaConfiguration( archivaConfiguration );
106 defaultRepositoryCommonValidator.setRegistry( registry );
108 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryCommonValidator(
109 defaultRepositoryCommonValidator );
111 action.setRepositoryCommonValidator( defaultRepositoryCommonValidator );
113 action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
118 public void testSecureActionBundle()
119 throws SecureActionException
121 archivaConfiguration.getConfiguration();
122 archivaConfigurationControl.setReturnValue( new Configuration() );
123 archivaConfigurationControl.replay();
126 SecureActionBundle bundle = action.getSecureActionBundle();
127 assertTrue( bundle.requiresAuthentication() );
128 assertEquals( 1, bundle.getAuthorizationTuples().size() );
132 public void testAddRepositoryInitialPage()
135 archivaConfiguration.getConfiguration();
136 archivaConfigurationControl.setReturnValue( new Configuration() );
137 archivaConfiguration.getConfiguration();
138 archivaConfigurationControl.setReturnValue( new Configuration() );
139 archivaConfigurationControl.replay();
142 ManagedRepository configuration = action.getRepository();
143 assertNotNull( configuration );
144 assertNull( configuration.getId() );
145 // check all booleans are false
146 assertFalse( configuration.isDeleteReleasedSnapshots() );
147 assertFalse( configuration.isScanned() );
148 assertFalse( configuration.isReleases() );
149 assertFalse( configuration.isSnapshots() );
151 String status = action.input();
152 assertEquals( Action.INPUT, status );
155 assertFalse( configuration.isDeleteReleasedSnapshots() );
156 assertTrue( configuration.isScanned() );
157 assertTrue( configuration.isReleases() );
158 assertFalse( configuration.isSnapshots() );
162 public void testAddRepository()
165 FileUtils.deleteDirectory( location );
167 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
168 roleManagerControl.setReturnValue( false );
169 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
170 roleManagerControl.setVoidCallable();
171 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
172 roleManagerControl.setReturnValue( false );
173 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
174 roleManagerControl.setVoidCallable();
176 roleManagerControl.replay();
178 registry.getString( "appserver.base", "${appserver.base}" );
179 registryControl.setReturnValue( "target/test", 1, 3 );
180 registry.getString( "appserver.home", "${appserver.home}" );
181 registryControl.setReturnValue( "target/test", 1, 3 );
183 registryControl.replay();
185 RepositoryTask task = new RepositoryTask();
186 task.setRepositoryId( REPO_ID );
187 repositoryTaskScheduler.isProcessingRepositoryTask( REPO_ID );
188 repositoryTaskSchedulerControl.setReturnValue( false );
189 repositoryTaskScheduler.queueTask( task );
190 repositoryTaskSchedulerControl.setVoidCallable();
191 repositoryTaskSchedulerControl.replay();
193 Configuration configuration = new Configuration();
194 archivaConfiguration.getConfiguration();
195 archivaConfigurationControl.setReturnValue( configuration );
197 archivaConfiguration.getConfiguration();
198 archivaConfigurationControl.setReturnValue( configuration );
200 archivaConfiguration.getConfiguration();
201 archivaConfigurationControl.setReturnValue( configuration );
203 archivaConfiguration.save( configuration );
205 archivaConfigurationControl.replay();
208 ManagedRepository repository = action.getRepository();
209 populateRepository( repository );
211 assertFalse( location.exists() );
212 String status = action.commit();
213 assertEquals( Action.SUCCESS, status );
214 assertTrue( location.exists() );
216 assertEquals( Collections.singletonList( repository ), getManagedRepositoryAdmin().getManagedRepositories() );
217 assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
219 roleManagerControl.verify();
220 archivaConfigurationControl.verify();
221 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();
253 public void testStruts2ValidationFrameworkWithNullInputs()
257 // 0 is the default value for primitive int; null for objects
258 ManagedRepository managedRepositoryConfiguration = createManagedRepository( null, null, null, null );
259 action.setRepository( managedRepositoryConfiguration );
262 actionValidatorManager.validate( action, EMPTY_STRING );
265 assertTrue( action.hasFieldErrors() );
267 Map<String, List<String>> fieldErrors = action.getFieldErrors();
269 // make an expected field error object
270 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
273 List<String> expectedErrorMessages = new ArrayList<String>();
274 expectedErrorMessages.add( "You must enter a repository identifier." );
275 expectedFieldErrors.put( "repository.id", expectedErrorMessages );
277 expectedErrorMessages = new ArrayList<String>();
278 expectedErrorMessages.add( "You must enter a directory." );
279 expectedFieldErrors.put( "repository.location", expectedErrorMessages );
281 expectedErrorMessages = new ArrayList<String>();
282 expectedErrorMessages.add( "You must enter a repository name." );
283 expectedFieldErrors.put( "repository.name", expectedErrorMessages );
285 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
289 public void testStruts2ValidationFrameworkWithBlankInputs()
293 // 0 is the default value for primitive int
294 ManagedRepository managedRepositoryConfiguration =
295 createManagedRepository( EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING );
296 action.setRepository( managedRepositoryConfiguration );
299 actionValidatorManager.validate( action, EMPTY_STRING );
302 assertTrue( action.hasFieldErrors() );
304 Map<String, List<String>> fieldErrors = action.getFieldErrors();
306 // make an expected field error object
307 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
310 List<String> expectedErrorMessages = new ArrayList<String>();
311 expectedErrorMessages.add( "You must enter a repository identifier." );
312 expectedFieldErrors.put( "repository.id", expectedErrorMessages );
314 expectedErrorMessages = new ArrayList<String>();
315 expectedErrorMessages.add( "You must enter a directory." );
316 expectedFieldErrors.put( "repository.location", expectedErrorMessages );
318 expectedErrorMessages = new ArrayList<String>();
319 expectedErrorMessages.add( "You must enter a repository name." );
320 expectedFieldErrors.put( "repository.name", expectedErrorMessages );
322 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
326 public void testStruts2ValidationFrameworkWithInvalidInputs()
330 ManagedRepository managedRepositoryConfiguration =
331 createManagedRepository( REPOSITORY_ID_INVALID_INPUT, REPOSITORY_NAME_INVALID_INPUT,
332 REPOSITORY_LOCATION_INVALID_INPUT, REPOSITORY_INDEX_DIR_INVALID_INPUT,
333 REPOSITORY_DAYS_OLDER_INVALID_INPUT, REPOSITORY_RETENTION_COUNT_INVALID_INPUT );
334 action.setRepository( managedRepositoryConfiguration );
337 actionValidatorManager.validate( action, EMPTY_STRING );
340 assertTrue( action.hasFieldErrors() );
342 Map<String, List<String>> fieldErrors = action.getFieldErrors();
344 // make an expected field error object
345 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
348 List<String> expectedErrorMessages = new ArrayList<String>();
349 expectedErrorMessages.add(
350 "Identifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
351 expectedFieldErrors.put( "repository.id", expectedErrorMessages );
353 expectedErrorMessages = new ArrayList<String>();
354 expectedErrorMessages.add(
355 "Directory must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-)." );
356 expectedFieldErrors.put( "repository.location", expectedErrorMessages );
358 expectedErrorMessages = new ArrayList<String>();
359 expectedErrorMessages.add(
360 "Repository Name must only contain alphanumeric characters, white-spaces(' '), forward-slashes(/), open-parenthesis('('), close-parenthesis(')'), underscores(_), dots(.), and dashes(-)." );
361 expectedFieldErrors.put( "repository.name", expectedErrorMessages );
363 expectedErrorMessages = new ArrayList<String>();
364 expectedErrorMessages.add(
365 "Index directory must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-)." );
366 expectedFieldErrors.put( "repository.indexDirectory", expectedErrorMessages );
368 expectedErrorMessages = new ArrayList<String>();
369 expectedErrorMessages.add( "Repository Purge By Retention Count needs to be between 1 and 100." );
370 expectedFieldErrors.put( "repository.retentionCount", expectedErrorMessages );
372 expectedErrorMessages = new ArrayList<String>();
373 expectedErrorMessages.add( "Repository Purge By Days Older Than needs to be larger than 0." );
374 expectedFieldErrors.put( "repository.daysOlder", expectedErrorMessages );
376 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
380 public void testStruts2ValidationFrameworkWithValidInputs()
384 ManagedRepository managedRepositoryConfiguration =
385 createManagedRepository( REPOSITORY_ID_VALID_INPUT, REPOSITORY_NAME_VALID_INPUT,
386 REPOSITORY_LOCATION_VALID_INPUT, REPOSITORY_INDEX_DIR_VALID_INPUT,
387 REPOSITORY_DAYS_OLDER_VALID_INPUT, REPOSITORY_RETENTION_COUNT_VALID_INPUT );
388 action.setRepository( managedRepositoryConfiguration );
391 actionValidatorManager.validate( action, EMPTY_STRING );
394 assertFalse( action.hasFieldErrors() );
397 // TODO: test errors during add, other actions