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.role.RoleManager;
29 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
30 import org.apache.archiva.scheduler.repository.RepositoryTask;
31 import org.apache.archiva.security.common.ArchivaRoleConstants;
32 import org.apache.archiva.web.validator.utils.ValidatorUtil;
33 import org.apache.commons.io.FileUtils;
34 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
35 import org.apache.archiva.redback.integration.interceptor.SecureActionException;
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;
47 * AddManagedRepositoryActionTest
51 public class AddManagedRepositoryActionTest
52 extends AbstractManagedRepositoryActionTest
54 private AddManagedRepositoryAction action;
56 private RoleManager roleManager;
58 private MockControl roleManagerControl;
60 private MockControl archivaConfigurationControl;
62 private org.apache.archiva.redback.components.registry.Registry registry;
64 private MockControl registryControl;
66 private ArchivaConfiguration archivaConfiguration;
68 private MockControl repositoryTaskSchedulerControl;
70 private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
74 protected void setUp()
79 action = new AddManagedRepositoryAction();
81 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
82 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
84 roleManagerControl = MockControl.createControl( RoleManager.class );
85 roleManager = (RoleManager) roleManagerControl.getMock();
87 registryControl = MockControl.createControl( org.apache.archiva.redback.components.registry.Registry.class );
88 registry = (org.apache.archiva.redback.components.registry.Registry) registryControl.getMock();
89 //action.setRegistry( registry );
91 repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
92 repositoryTaskScheduler = (RepositoryArchivaTaskScheduler) repositoryTaskSchedulerControl.getMock();
94 location = new File( "target/test/location" );
95 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfiguration );
96 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRoleManager( roleManager );
97 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRegistry( registry );
98 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryTaskScheduler(
99 repositoryTaskScheduler );
101 DefaultRepositoryCommonValidator defaultRepositoryCommonValidator = new DefaultRepositoryCommonValidator();
102 defaultRepositoryCommonValidator.setArchivaConfiguration( archivaConfiguration );
103 defaultRepositoryCommonValidator.setRegistry( registry );
105 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryCommonValidator(
106 defaultRepositoryCommonValidator );
108 action.setRepositoryCommonValidator( defaultRepositoryCommonValidator );
110 action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
114 public void testSecureActionBundle()
115 throws SecureActionException
117 archivaConfiguration.getConfiguration();
118 archivaConfigurationControl.setReturnValue( new Configuration() );
119 archivaConfigurationControl.replay();
122 SecureActionBundle bundle = action.getSecureActionBundle();
123 assertTrue( bundle.requiresAuthentication() );
124 assertEquals( 1, bundle.getAuthorizationTuples().size() );
127 public void testAddRepositoryInitialPage()
130 archivaConfiguration.getConfiguration();
131 archivaConfigurationControl.setReturnValue( new Configuration() );
132 archivaConfiguration.getConfiguration();
133 archivaConfigurationControl.setReturnValue( new Configuration() );
134 archivaConfigurationControl.replay();
137 ManagedRepository configuration = action.getRepository();
138 assertNotNull( configuration );
139 assertNull( configuration.getId() );
140 // check all booleans are false
141 assertFalse( configuration.isDeleteReleasedSnapshots() );
142 assertFalse( configuration.isScanned() );
143 assertFalse( configuration.isReleases() );
144 assertFalse( configuration.isSnapshots() );
146 String status = action.input();
147 assertEquals( Action.INPUT, status );
150 assertFalse( configuration.isDeleteReleasedSnapshots() );
151 assertTrue( configuration.isScanned() );
152 assertTrue( configuration.isReleases() );
153 assertFalse( configuration.isSnapshots() );
156 public void testAddRepository()
159 FileUtils.deleteDirectory( location );
161 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
162 roleManagerControl.setReturnValue( false );
163 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
164 roleManagerControl.setVoidCallable();
165 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
166 roleManagerControl.setReturnValue( false );
167 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
168 roleManagerControl.setVoidCallable();
170 roleManagerControl.replay();
172 registry.getString( "appserver.base", "${appserver.base}" );
173 registryControl.setReturnValue( "target/test", 1, 3 );
174 registry.getString( "appserver.home", "${appserver.home}" );
175 registryControl.setReturnValue( "target/test", 1, 3 );
177 registryControl.replay();
179 RepositoryTask task = new RepositoryTask();
180 task.setRepositoryId( REPO_ID );
181 repositoryTaskScheduler.isProcessingRepositoryTask( REPO_ID );
182 repositoryTaskSchedulerControl.setReturnValue( false );
183 repositoryTaskScheduler.queueTask( task );
184 repositoryTaskSchedulerControl.setVoidCallable();
185 repositoryTaskSchedulerControl.replay();
187 Configuration configuration = new Configuration();
188 archivaConfiguration.getConfiguration();
189 archivaConfigurationControl.setReturnValue( configuration );
191 archivaConfiguration.getConfiguration();
192 archivaConfigurationControl.setReturnValue( configuration );
194 archivaConfiguration.getConfiguration();
195 archivaConfigurationControl.setReturnValue( configuration );
197 archivaConfiguration.save( configuration );
199 archivaConfigurationControl.replay();
202 ManagedRepository repository = action.getRepository();
203 populateRepository( repository );
205 assertFalse( location.exists() );
206 String status = action.commit();
207 assertEquals( Action.SUCCESS, status );
208 assertTrue( location.exists() );
210 assertEquals( Collections.singletonList( repository ), getManagedRepositoryAdmin().getManagedRepositories() );
211 assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
213 roleManagerControl.verify();
214 archivaConfigurationControl.verify();
215 registryControl.verify();
219 public void testAddRepositoryExistingLocation()
222 if ( !location.exists() )
227 registry.getString( "appserver.base", "${appserver.base}" );
228 registryControl.setReturnValue( "target/test" );
229 registry.getString( "appserver.home", "${appserver.home}" );
230 registryControl.setReturnValue( "target/test" );
232 registryControl.replay();
235 ManagedRepository repository = action.getRepository();
236 populateRepository( repository );
238 assertTrue( location.exists() );
239 String status = action.commit();
240 assertEquals( AddManagedRepositoryAction.CONFIRM, status );
241 assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
242 registryControl.verify();
245 public void testStruts2ValidationFrameworkWithNullInputs()
249 // 0 is the default value for primitive int; null for objects
250 ManagedRepository managedRepositoryConfiguration = createManagedRepository( null, null, null, null );
251 action.setRepository( managedRepositoryConfiguration );
254 actionValidatorManager.validate( action, EMPTY_STRING );
257 assertTrue( action.hasFieldErrors() );
259 Map<String, List<String>> fieldErrors = action.getFieldErrors();
261 // make an expected field error object
262 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
265 List<String> expectedErrorMessages = new ArrayList<String>();
266 expectedErrorMessages.add( "You must enter a repository identifier." );
267 expectedFieldErrors.put( "repository.id", expectedErrorMessages );
269 expectedErrorMessages = new ArrayList<String>();
270 expectedErrorMessages.add( "You must enter a directory." );
271 expectedFieldErrors.put( "repository.location", expectedErrorMessages );
273 expectedErrorMessages = new ArrayList<String>();
274 expectedErrorMessages.add( "You must enter a repository name." );
275 expectedFieldErrors.put( "repository.name", expectedErrorMessages );
277 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
280 public void testStruts2ValidationFrameworkWithBlankInputs()
284 // 0 is the default value for primitive int
285 ManagedRepository managedRepositoryConfiguration =
286 createManagedRepository( EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING );
287 action.setRepository( managedRepositoryConfiguration );
290 actionValidatorManager.validate( action, EMPTY_STRING );
293 assertTrue( action.hasFieldErrors() );
295 Map<String, List<String>> fieldErrors = action.getFieldErrors();
297 // make an expected field error object
298 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
301 List<String> expectedErrorMessages = new ArrayList<String>();
302 expectedErrorMessages.add( "You must enter a repository identifier." );
303 expectedFieldErrors.put( "repository.id", expectedErrorMessages );
305 expectedErrorMessages = new ArrayList<String>();
306 expectedErrorMessages.add( "You must enter a directory." );
307 expectedFieldErrors.put( "repository.location", expectedErrorMessages );
309 expectedErrorMessages = new ArrayList<String>();
310 expectedErrorMessages.add( "You must enter a repository name." );
311 expectedFieldErrors.put( "repository.name", expectedErrorMessages );
313 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
316 public void testStruts2ValidationFrameworkWithInvalidInputs()
320 ManagedRepository managedRepositoryConfiguration =
321 createManagedRepository( REPOSITORY_ID_INVALID_INPUT, REPOSITORY_NAME_INVALID_INPUT,
322 REPOSITORY_LOCATION_INVALID_INPUT, REPOSITORY_INDEX_DIR_INVALID_INPUT,
323 REPOSITORY_DAYS_OLDER_INVALID_INPUT, REPOSITORY_RETENTION_COUNT_INVALID_INPUT );
324 action.setRepository( managedRepositoryConfiguration );
327 actionValidatorManager.validate( action, EMPTY_STRING );
330 assertTrue( action.hasFieldErrors() );
332 Map<String, List<String>> fieldErrors = action.getFieldErrors();
334 // make an expected field error object
335 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
338 List<String> expectedErrorMessages = new ArrayList<String>();
339 expectedErrorMessages.add(
340 "Identifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
341 expectedFieldErrors.put( "repository.id", expectedErrorMessages );
343 expectedErrorMessages = new ArrayList<String>();
344 expectedErrorMessages.add(
345 "Directory must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-)." );
346 expectedFieldErrors.put( "repository.location", expectedErrorMessages );
348 expectedErrorMessages = new ArrayList<String>();
349 expectedErrorMessages.add(
350 "Repository Name must only contain alphanumeric characters, white-spaces(' '), forward-slashes(/), open-parenthesis('('), close-parenthesis(')'), underscores(_), dots(.), and dashes(-)." );
351 expectedFieldErrors.put( "repository.name", expectedErrorMessages );
353 expectedErrorMessages = new ArrayList<String>();
354 expectedErrorMessages.add(
355 "Index 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.indexDirectory", expectedErrorMessages );
358 expectedErrorMessages = new ArrayList<String>();
359 expectedErrorMessages.add( "Repository Purge By Retention Count needs to be between 1 and 100." );
360 expectedFieldErrors.put( "repository.retentionCount", expectedErrorMessages );
362 expectedErrorMessages = new ArrayList<String>();
363 expectedErrorMessages.add( "Repository Purge By Days Older Than needs to be larger than 0." );
364 expectedFieldErrors.put( "repository.daysOlder", expectedErrorMessages );
366 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
369 public void testStruts2ValidationFrameworkWithValidInputs()
373 ManagedRepository managedRepositoryConfiguration =
374 createManagedRepository( REPOSITORY_ID_VALID_INPUT, REPOSITORY_NAME_VALID_INPUT,
375 REPOSITORY_LOCATION_VALID_INPUT, REPOSITORY_INDEX_DIR_VALID_INPUT,
376 REPOSITORY_DAYS_OLDER_VALID_INPUT, REPOSITORY_RETENTION_COUNT_VALID_INPUT );
377 action.setRepository( managedRepositoryConfiguration );
380 actionValidatorManager.validate( action, EMPTY_STRING );
383 assertFalse( action.hasFieldErrors() );
386 // TODO: test errors during add, other actions