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.scheduler.repository.RepositoryArchivaTaskScheduler;
29 import org.apache.archiva.scheduler.repository.RepositoryTask;
30 import org.apache.archiva.security.common.ArchivaRoleConstants;
31 import org.apache.archiva.web.validator.utils.ValidatorUtil;
32 import org.apache.commons.io.FileUtils;
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();
85 roleManagerControl = MockControl.createControl( RoleManager.class );
86 roleManager = (RoleManager) roleManagerControl.getMock();
88 registryControl = MockControl.createControl( Registry.class );
89 registry = (Registry) registryControl.getMock();
90 //action.setRegistry( registry );
92 repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
93 repositoryTaskScheduler = (RepositoryArchivaTaskScheduler) repositoryTaskSchedulerControl.getMock();
95 location = new File( "target/test/location" );
96 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfiguration );
97 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRoleManager( roleManager );
98 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRegistry( registry );
99 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryTaskScheduler(
100 repositoryTaskScheduler );
102 DefaultRepositoryCommonValidator defaultRepositoryCommonValidator = new DefaultRepositoryCommonValidator();
103 defaultRepositoryCommonValidator.setArchivaConfiguration( archivaConfiguration );
104 defaultRepositoryCommonValidator.setRegistry( registry );
106 ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryCommonValidator(
107 defaultRepositoryCommonValidator );
109 action.setRepositoryCommonValidator( defaultRepositoryCommonValidator );
111 action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
115 public void testSecureActionBundle()
116 throws SecureActionException
118 archivaConfiguration.getConfiguration();
119 archivaConfigurationControl.setReturnValue( new Configuration() );
120 archivaConfigurationControl.replay();
123 SecureActionBundle bundle = action.getSecureActionBundle();
124 assertTrue( bundle.requiresAuthentication() );
125 assertEquals( 1, bundle.getAuthorizationTuples().size() );
128 public void testAddRepositoryInitialPage()
131 archivaConfiguration.getConfiguration();
132 archivaConfigurationControl.setReturnValue( new Configuration() );
133 archivaConfiguration.getConfiguration();
134 archivaConfigurationControl.setReturnValue( new Configuration() );
135 archivaConfigurationControl.replay();
138 ManagedRepository configuration = action.getRepository();
139 assertNotNull( configuration );
140 assertNull( configuration.getId() );
141 // check all booleans are false
142 assertFalse( configuration.isDeleteReleasedSnapshots() );
143 assertFalse( configuration.isScanned() );
144 assertFalse( configuration.isReleases() );
145 assertFalse( configuration.isSnapshots() );
147 String status = action.input();
148 assertEquals( Action.INPUT, status );
151 assertFalse( configuration.isDeleteReleasedSnapshots() );
152 assertTrue( configuration.isScanned() );
153 assertTrue( configuration.isReleases() );
154 assertFalse( configuration.isSnapshots() );
157 public void testAddRepository()
160 FileUtils.deleteDirectory( location );
162 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
163 roleManagerControl.setReturnValue( false );
164 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
165 roleManagerControl.setVoidCallable();
166 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
167 roleManagerControl.setReturnValue( false );
168 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
169 roleManagerControl.setVoidCallable();
171 roleManagerControl.replay();
173 registry.getString( "appserver.base", "${appserver.base}" );
174 registryControl.setReturnValue( "target/test", 1, 3 );
175 registry.getString( "appserver.home", "${appserver.home}" );
176 registryControl.setReturnValue( "target/test", 1, 3 );
178 registryControl.replay();
180 RepositoryTask task = new RepositoryTask();
181 task.setRepositoryId( REPO_ID );
182 repositoryTaskScheduler.isProcessingRepositoryTask( REPO_ID );
183 repositoryTaskSchedulerControl.setReturnValue( false );
184 repositoryTaskScheduler.queueTask( task );
185 repositoryTaskSchedulerControl.setVoidCallable();
186 repositoryTaskSchedulerControl.replay();
188 Configuration configuration = new Configuration();
189 archivaConfiguration.getConfiguration();
190 archivaConfigurationControl.setReturnValue( configuration );
192 archivaConfiguration.getConfiguration();
193 archivaConfigurationControl.setReturnValue( configuration );
195 archivaConfiguration.getConfiguration();
196 archivaConfigurationControl.setReturnValue( configuration );
198 archivaConfiguration.save( configuration );
200 archivaConfigurationControl.replay();
203 ManagedRepository repository = action.getRepository();
204 populateRepository( repository );
206 assertFalse( location.exists() );
207 String status = action.commit();
208 assertEquals( Action.SUCCESS, status );
209 assertTrue( location.exists() );
211 assertEquals( Collections.singletonList( repository ), getManagedRepositoryAdmin().getManagedRepositories() );
212 assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
214 roleManagerControl.verify();
215 archivaConfigurationControl.verify();
216 registryControl.verify();
220 public void testAddRepositoryExistingLocation()
223 if ( !location.exists() )
228 registry.getString( "appserver.base", "${appserver.base}" );
229 registryControl.setReturnValue( "target/test" );
230 registry.getString( "appserver.home", "${appserver.home}" );
231 registryControl.setReturnValue( "target/test" );
233 registryControl.replay();
236 ManagedRepository repository = action.getRepository();
237 populateRepository( repository );
239 assertTrue( location.exists() );
240 String status = action.commit();
241 assertEquals( AddManagedRepositoryAction.CONFIRM, status );
242 assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
243 registryControl.verify();
246 public void testStruts2ValidationFrameworkWithNullInputs()
250 // 0 is the default value for primitive int; null for objects
251 ManagedRepository managedRepositoryConfiguration = createManagedRepository( null, null, null, null );
252 action.setRepository( managedRepositoryConfiguration );
255 actionValidatorManager.validate( action, EMPTY_STRING );
258 assertTrue( action.hasFieldErrors() );
260 Map<String, List<String>> fieldErrors = action.getFieldErrors();
262 // make an expected field error object
263 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
266 List<String> expectedErrorMessages = new ArrayList<String>();
267 expectedErrorMessages.add( "You must enter a repository identifier." );
268 expectedFieldErrors.put( "repository.id", expectedErrorMessages );
270 expectedErrorMessages = new ArrayList<String>();
271 expectedErrorMessages.add( "You must enter a directory." );
272 expectedFieldErrors.put( "repository.location", expectedErrorMessages );
274 expectedErrorMessages = new ArrayList<String>();
275 expectedErrorMessages.add( "You must enter a repository name." );
276 expectedFieldErrors.put( "repository.name", expectedErrorMessages );
278 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
281 public void testStruts2ValidationFrameworkWithBlankInputs()
285 // 0 is the default value for primitive int
286 ManagedRepository managedRepositoryConfiguration =
287 createManagedRepository( EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING );
288 action.setRepository( managedRepositoryConfiguration );
291 actionValidatorManager.validate( action, EMPTY_STRING );
294 assertTrue( action.hasFieldErrors() );
296 Map<String, List<String>> fieldErrors = action.getFieldErrors();
298 // make an expected field error object
299 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
302 List<String> expectedErrorMessages = new ArrayList<String>();
303 expectedErrorMessages.add( "You must enter a repository identifier." );
304 expectedFieldErrors.put( "repository.id", expectedErrorMessages );
306 expectedErrorMessages = new ArrayList<String>();
307 expectedErrorMessages.add( "You must enter a directory." );
308 expectedFieldErrors.put( "repository.location", expectedErrorMessages );
310 expectedErrorMessages = new ArrayList<String>();
311 expectedErrorMessages.add( "You must enter a repository name." );
312 expectedFieldErrors.put( "repository.name", expectedErrorMessages );
314 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
317 public void testStruts2ValidationFrameworkWithInvalidInputs()
321 ManagedRepository managedRepositoryConfiguration =
322 createManagedRepository( REPOSITORY_ID_INVALID_INPUT, REPOSITORY_NAME_INVALID_INPUT,
323 REPOSITORY_LOCATION_INVALID_INPUT, REPOSITORY_INDEX_DIR_INVALID_INPUT,
324 REPOSITORY_DAYS_OLDER_INVALID_INPUT, REPOSITORY_RETENTION_COUNT_INVALID_INPUT );
325 action.setRepository( managedRepositoryConfiguration );
328 actionValidatorManager.validate( action, EMPTY_STRING );
331 assertTrue( action.hasFieldErrors() );
333 Map<String, List<String>> fieldErrors = action.getFieldErrors();
335 // make an expected field error object
336 Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
339 List<String> expectedErrorMessages = new ArrayList<String>();
340 expectedErrorMessages.add(
341 "Identifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
342 expectedFieldErrors.put( "repository.id", expectedErrorMessages );
344 expectedErrorMessages = new ArrayList<String>();
345 expectedErrorMessages.add(
346 "Directory must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-)." );
347 expectedFieldErrors.put( "repository.location", expectedErrorMessages );
349 expectedErrorMessages = new ArrayList<String>();
350 expectedErrorMessages.add(
351 "Repository Name must only contain alphanumeric characters, white-spaces(' '), forward-slashes(/), open-parenthesis('('), close-parenthesis(')'), underscores(_), dots(.), and dashes(-)." );
352 expectedFieldErrors.put( "repository.name", expectedErrorMessages );
354 expectedErrorMessages = new ArrayList<String>();
355 expectedErrorMessages.add(
356 "Index directory must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-)." );
357 expectedFieldErrors.put( "repository.indexDirectory", expectedErrorMessages );
359 expectedErrorMessages = new ArrayList<String>();
360 expectedErrorMessages.add( "Repository Purge By Retention Count needs to be between 1 and 100." );
361 expectedFieldErrors.put( "repository.retentionCount", expectedErrorMessages );
363 expectedErrorMessages = new ArrayList<String>();
364 expectedErrorMessages.add( "Repository Purge By Days Older Than needs to be larger than 0." );
365 expectedFieldErrors.put( "repository.daysOlder", expectedErrorMessages );
367 ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
370 public void testStruts2ValidationFrameworkWithValidInputs()
374 ManagedRepository managedRepositoryConfiguration =
375 createManagedRepository( REPOSITORY_ID_VALID_INPUT, REPOSITORY_NAME_VALID_INPUT,
376 REPOSITORY_LOCATION_VALID_INPUT, REPOSITORY_INDEX_DIR_VALID_INPUT,
377 REPOSITORY_DAYS_OLDER_VALID_INPUT, REPOSITORY_RETENTION_COUNT_VALID_INPUT );
378 action.setRepository( managedRepositoryConfiguration );
381 actionValidatorManager.validate( action, EMPTY_STRING );
384 assertFalse( action.hasFieldErrors() );
387 // TODO: test errors during add, other actions