]> source.dussan.org Git - archiva.git/blob
2c0266d2be82150fa71dcc4a73a6491c1c7299a9
[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
46 /**
47  * AddManagedRepositoryActionTest
48  *
49  * @version $Id$
50  */
51 public class AddManagedRepositoryActionTest
52     extends AbstractManagedRepositoryActionTest
53 {
54     private AddManagedRepositoryAction action;
55
56     private RoleManager roleManager;
57
58     private MockControl roleManagerControl;
59
60     private MockControl archivaConfigurationControl;
61
62     private org.apache.archiva.redback.components.registry.Registry registry;
63
64     private MockControl registryControl;
65
66     private ArchivaConfiguration archivaConfiguration;
67
68     private MockControl repositoryTaskSchedulerControl;
69
70     private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
71
72
73     @Override
74     protected void setUp()
75         throws Exception
76     {
77         super.setUp();
78
79         action = new AddManagedRepositoryAction();
80
81         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
82         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
83
84         roleManagerControl = MockControl.createControl( RoleManager.class );
85         roleManager = (RoleManager) roleManagerControl.getMock();
86
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 );
90
91         repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
92         repositoryTaskScheduler = (RepositoryArchivaTaskScheduler) repositoryTaskSchedulerControl.getMock();
93
94         location = new File( System.getProperty( "basedir" ), "target/test/location" );
95         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfiguration );
96         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRoleManager( roleManager );
97         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRegistry( registry );
98         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryTaskScheduler(
99             repositoryTaskScheduler );
100
101         DefaultRepositoryCommonValidator defaultRepositoryCommonValidator = new DefaultRepositoryCommonValidator();
102         defaultRepositoryCommonValidator.setArchivaConfiguration( archivaConfiguration );
103         defaultRepositoryCommonValidator.setRegistry( registry );
104
105         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryCommonValidator(
106             defaultRepositoryCommonValidator );
107
108         action.setRepositoryCommonValidator( defaultRepositoryCommonValidator );
109
110         action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
111
112     }
113
114     public void testSecureActionBundle()
115         throws SecureActionException
116     {
117         archivaConfiguration.getConfiguration();
118         archivaConfigurationControl.setReturnValue( new Configuration() );
119         archivaConfigurationControl.replay();
120
121         action.prepare();
122         SecureActionBundle bundle = action.getSecureActionBundle();
123         assertTrue( bundle.requiresAuthentication() );
124         assertEquals( 1, bundle.getAuthorizationTuples().size() );
125     }
126
127     public void testAddRepositoryInitialPage()
128         throws Exception
129     {
130         archivaConfiguration.getConfiguration();
131         archivaConfigurationControl.setReturnValue( new Configuration() );
132         archivaConfiguration.getConfiguration();
133         archivaConfigurationControl.setReturnValue( new Configuration() );
134         archivaConfigurationControl.replay();
135
136         action.prepare();
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() );
145
146         String status = action.input();
147         assertEquals( Action.INPUT, status );
148
149         // check defaults
150         assertFalse( configuration.isDeleteReleasedSnapshots() );
151         assertTrue( configuration.isScanned() );
152         assertTrue( configuration.isReleases() );
153         assertFalse( configuration.isSnapshots() );
154     }
155
156     public void testAddRepository()
157         throws Exception
158     {
159         FileUtils.deleteDirectory( location );
160
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();
169
170         roleManagerControl.replay();
171
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 );
176
177         registryControl.replay();
178
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();
186
187         Configuration configuration = new Configuration();
188         archivaConfiguration.getConfiguration();
189         archivaConfigurationControl.setReturnValue( configuration );
190
191         archivaConfiguration.getConfiguration();
192         archivaConfigurationControl.setReturnValue( configuration );
193
194         archivaConfiguration.getConfiguration();
195         archivaConfigurationControl.setReturnValue( configuration );
196
197         archivaConfiguration.save( configuration );
198
199         archivaConfigurationControl.replay();
200
201         action.prepare();
202         ManagedRepository repository = action.getRepository();
203         populateRepository( repository );
204
205         assertFalse( location.exists() );
206         String status = action.commit();
207         assertEquals( Action.SUCCESS, status );
208         assertTrue( location.exists() );
209
210         assertEquals( Collections.singletonList( repository ), getManagedRepositoryAdmin().getManagedRepositories() );
211         assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
212
213         roleManagerControl.verify();
214         archivaConfigurationControl.verify();
215         registryControl.verify();
216     }
217
218
219     public void testAddRepositoryExistingLocation()
220         throws Exception
221     {
222         if ( !location.exists() )
223         {
224             location.mkdirs();
225         }
226
227         registry.getString( "appserver.base", "${appserver.base}" );
228         registryControl.setReturnValue( "target/test" );
229         registry.getString( "appserver.home", "${appserver.home}" );
230         registryControl.setReturnValue( "target/test" );
231
232         registryControl.replay();
233
234         action.prepare();
235         ManagedRepository repository = action.getRepository();
236         populateRepository( repository );
237
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();
243     }
244
245     public void testStruts2ValidationFrameworkWithNullInputs()
246         throws Exception
247     {
248         // prep
249         // 0 is the default value for primitive int; null for objects
250         ManagedRepository managedRepositoryConfiguration = createManagedRepository( null, null, null, null );
251         action.setRepository( managedRepositoryConfiguration );
252
253         // test
254         actionValidatorManager.validate( action, EMPTY_STRING );
255
256         // verify
257         assertTrue( action.hasFieldErrors() );
258
259         Map<String, List<String>> fieldErrors = action.getFieldErrors();
260
261         // make an expected field error object
262         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
263
264         // populate
265         List<String> expectedErrorMessages = new ArrayList<String>();
266         expectedErrorMessages.add( "You must enter a repository identifier." );
267         expectedFieldErrors.put( "repository.id", expectedErrorMessages );
268
269         expectedErrorMessages = new ArrayList<String>();
270         expectedErrorMessages.add( "You must enter a directory." );
271         expectedFieldErrors.put( "repository.location", expectedErrorMessages );
272
273         expectedErrorMessages = new ArrayList<String>();
274         expectedErrorMessages.add( "You must enter a repository name." );
275         expectedFieldErrors.put( "repository.name", expectedErrorMessages );
276
277         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
278     }
279
280     public void testStruts2ValidationFrameworkWithBlankInputs()
281         throws Exception
282     {
283         // prep
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 );
288
289         // test
290         actionValidatorManager.validate( action, EMPTY_STRING );
291
292         // verify
293         assertTrue( action.hasFieldErrors() );
294
295         Map<String, List<String>> fieldErrors = action.getFieldErrors();
296
297         // make an expected field error object
298         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
299
300         // populate
301         List<String> expectedErrorMessages = new ArrayList<String>();
302         expectedErrorMessages.add( "You must enter a repository identifier." );
303         expectedFieldErrors.put( "repository.id", expectedErrorMessages );
304
305         expectedErrorMessages = new ArrayList<String>();
306         expectedErrorMessages.add( "You must enter a directory." );
307         expectedFieldErrors.put( "repository.location", expectedErrorMessages );
308
309         expectedErrorMessages = new ArrayList<String>();
310         expectedErrorMessages.add( "You must enter a repository name." );
311         expectedFieldErrors.put( "repository.name", expectedErrorMessages );
312
313         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
314     }
315
316     public void testStruts2ValidationFrameworkWithInvalidInputs()
317         throws Exception
318     {
319         // prep
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 );
325
326         // test
327         actionValidatorManager.validate( action, EMPTY_STRING );
328
329         // verify
330         assertTrue( action.hasFieldErrors() );
331
332         Map<String, List<String>> fieldErrors = action.getFieldErrors();
333
334         // make an expected field error object
335         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
336
337         // populate
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 );
342
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 );
347
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 );
352
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 );
357
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 );
361
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 );
365
366         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
367     }
368
369     public void testStruts2ValidationFrameworkWithValidInputs()
370         throws Exception
371     {
372         // prep
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 );
378
379         // test
380         actionValidatorManager.validate( action, EMPTY_STRING );
381
382         // verify
383         assertFalse( action.hasFieldErrors() );
384     }
385
386     // TODO: test errors during add, other actions
387 }