]> source.dussan.org Git - archiva.git/blob
46622989c8257f3f01896e06993b529c3d329484
[archiva.git] /
1 package org.apache.archiva.web.action.admin.repositories;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
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;
38
39 import java.io.File;
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import org.junit.Before;
46 import org.junit.Test;
47
48 /**
49  * AddManagedRepositoryActionTest
50  *
51  * @version $Id$
52  */
53 public class AddManagedRepositoryActionTest
54     extends AbstractManagedRepositoryActionTest
55 {
56     private AddManagedRepositoryAction action;
57
58     private RoleManager roleManager;
59
60     private MockControl roleManagerControl;
61
62     private MockControl archivaConfigurationControl;
63
64     private org.apache.archiva.redback.components.registry.Registry registry;
65
66     private MockControl registryControl;
67
68     private ArchivaConfiguration archivaConfiguration;
69
70     private MockControl repositoryTaskSchedulerControl;
71
72     private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
73
74
75     @Override
76     @Before
77     public void setUp()
78         throws Exception
79     {
80         super.setUp();
81
82         action = new AddManagedRepositoryAction();
83
84         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
85         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
86
87         roleManagerControl = MockControl.createControl( RoleManager.class );
88         roleManager = (RoleManager) roleManagerControl.getMock();
89
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 );
93
94         repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
95         repositoryTaskScheduler = (RepositoryArchivaTaskScheduler) repositoryTaskSchedulerControl.getMock();
96
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 );
103
104         DefaultRepositoryCommonValidator defaultRepositoryCommonValidator = new DefaultRepositoryCommonValidator();
105         defaultRepositoryCommonValidator.setArchivaConfiguration( archivaConfiguration );
106         defaultRepositoryCommonValidator.setRegistry( registry );
107
108         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryCommonValidator(
109             defaultRepositoryCommonValidator );
110
111         action.setRepositoryCommonValidator( defaultRepositoryCommonValidator );
112
113         action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
114
115     }
116
117     @Test
118     public void testSecureActionBundle()
119         throws SecureActionException
120     {
121         archivaConfiguration.getConfiguration();
122         archivaConfigurationControl.setReturnValue( new Configuration() );
123         archivaConfigurationControl.replay();
124
125         action.prepare();
126         SecureActionBundle bundle = action.getSecureActionBundle();
127         assertTrue( bundle.requiresAuthentication() );
128         assertEquals( 1, bundle.getAuthorizationTuples().size() );
129     }
130
131     @Test
132     public void testAddRepositoryInitialPage()
133         throws Exception
134     {
135         archivaConfiguration.getConfiguration();
136         archivaConfigurationControl.setReturnValue( new Configuration() );
137         archivaConfiguration.getConfiguration();
138         archivaConfigurationControl.setReturnValue( new Configuration() );
139         archivaConfigurationControl.replay();
140
141         action.prepare();
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() );
150
151         String status = action.input();
152         assertEquals( Action.INPUT, status );
153
154         // check defaults
155         assertFalse( configuration.isDeleteReleasedSnapshots() );
156         assertTrue( configuration.isScanned() );
157         assertTrue( configuration.isReleases() );
158         assertFalse( configuration.isSnapshots() );
159     }
160
161     @Test
162     public void testAddRepository()
163         throws Exception
164     {
165         FileUtils.deleteDirectory( location );
166
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();
175
176         roleManagerControl.replay();
177
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 );
182
183         registryControl.replay();
184
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();
192
193         Configuration configuration = new Configuration();
194         archivaConfiguration.getConfiguration();
195         archivaConfigurationControl.setReturnValue( configuration );
196
197         archivaConfiguration.getConfiguration();
198         archivaConfigurationControl.setReturnValue( configuration );
199
200         archivaConfiguration.getConfiguration();
201         archivaConfigurationControl.setReturnValue( configuration );
202
203         archivaConfiguration.save( configuration );
204
205         archivaConfigurationControl.replay();
206
207         action.prepare();
208         ManagedRepository repository = action.getRepository();
209         populateRepository( repository );
210
211         assertFalse( location.exists() );
212         String status = action.commit();
213         assertEquals( Action.SUCCESS, status );
214         assertTrue( location.exists() );
215
216         assertEquals( Collections.singletonList( repository ), getManagedRepositoryAdmin().getManagedRepositories() );
217         assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
218
219         roleManagerControl.verify();
220         archivaConfigurationControl.verify();
221         registryControl.verify();
222     }
223
224
225     @Test
226     public void testAddRepositoryExistingLocation()
227         throws Exception
228     {
229         if ( !location.exists() )
230         {
231             location.mkdirs();
232         }
233
234         registry.getString( "appserver.base", "${appserver.base}" );
235         registryControl.setReturnValue( "target/test" );
236         registry.getString( "appserver.home", "${appserver.home}" );
237         registryControl.setReturnValue( "target/test" );
238
239         registryControl.replay();
240
241         action.prepare();
242         ManagedRepository repository = action.getRepository();
243         populateRepository( repository );
244
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();
250     }
251
252     @Test
253     public void testStruts2ValidationFrameworkWithNullInputs()
254         throws Exception
255     {
256         // prep
257         // 0 is the default value for primitive int; null for objects
258         ManagedRepository managedRepositoryConfiguration = createManagedRepository( null, null, null, null );
259         action.setRepository( managedRepositoryConfiguration );
260
261         // test
262         actionValidatorManager.validate( action, EMPTY_STRING );
263
264         // verify
265         assertTrue( action.hasFieldErrors() );
266
267         Map<String, List<String>> fieldErrors = action.getFieldErrors();
268
269         // make an expected field error object
270         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
271
272         // populate
273         List<String> expectedErrorMessages = new ArrayList<String>();
274         expectedErrorMessages.add( "You must enter a repository identifier." );
275         expectedFieldErrors.put( "repository.id", expectedErrorMessages );
276
277         expectedErrorMessages = new ArrayList<String>();
278         expectedErrorMessages.add( "You must enter a directory." );
279         expectedFieldErrors.put( "repository.location", expectedErrorMessages );
280
281         expectedErrorMessages = new ArrayList<String>();
282         expectedErrorMessages.add( "You must enter a repository name." );
283         expectedFieldErrors.put( "repository.name", expectedErrorMessages );
284
285         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
286     }
287
288     @Test
289     public void testStruts2ValidationFrameworkWithBlankInputs()
290         throws Exception
291     {
292         // prep
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 );
297
298         // test
299         actionValidatorManager.validate( action, EMPTY_STRING );
300
301         // verify
302         assertTrue( action.hasFieldErrors() );
303
304         Map<String, List<String>> fieldErrors = action.getFieldErrors();
305
306         // make an expected field error object
307         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
308
309         // populate
310         List<String> expectedErrorMessages = new ArrayList<String>();
311         expectedErrorMessages.add( "You must enter a repository identifier." );
312         expectedFieldErrors.put( "repository.id", expectedErrorMessages );
313
314         expectedErrorMessages = new ArrayList<String>();
315         expectedErrorMessages.add( "You must enter a directory." );
316         expectedFieldErrors.put( "repository.location", expectedErrorMessages );
317
318         expectedErrorMessages = new ArrayList<String>();
319         expectedErrorMessages.add( "You must enter a repository name." );
320         expectedFieldErrors.put( "repository.name", expectedErrorMessages );
321
322         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
323     }
324
325     @Test
326     public void testStruts2ValidationFrameworkWithInvalidInputs()
327         throws Exception
328     {
329         // prep
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 );
335
336         // test
337         actionValidatorManager.validate( action, EMPTY_STRING );
338
339         // verify
340         assertTrue( action.hasFieldErrors() );
341
342         Map<String, List<String>> fieldErrors = action.getFieldErrors();
343
344         // make an expected field error object
345         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
346
347         // populate
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 );
352
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 );
357
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 );
362
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 );
367
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 );
371
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 );
375
376         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
377     }
378
379     @Test
380     public void testStruts2ValidationFrameworkWithValidInputs()
381         throws Exception
382     {
383         // prep
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 );
389
390         // test
391         actionValidatorManager.validate( action, EMPTY_STRING );
392
393         // verify
394         assertFalse( action.hasFieldErrors() );
395     }
396
397     // TODO: test errors during add, other actions
398 }