]> source.dussan.org Git - archiva.git/blob
e3bab24e65de857ee82a6cec76f46803a192b736
[archiva.git] /
1 package org.apache.maven.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.managed.ManagedRepository;
24 import org.apache.archiva.admin.repository.RepositoryCommonValidator;
25 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
26 import org.apache.archiva.scheduler.repository.RepositoryArchivaTaskScheduler;
27 import org.apache.archiva.scheduler.repository.RepositoryTask;
28 import org.apache.archiva.security.ArchivaRoleConstants;
29 import org.apache.archiva.web.validator.utils.ValidatorUtil;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
32 import org.apache.maven.archiva.configuration.Configuration;
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;
39
40 import java.io.File;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46
47 /**
48  * AddManagedRepositoryActionTest
49  *
50  * @version $Id$
51  */
52 public class AddManagedRepositoryActionTest
53     extends AbstractManagedRepositoryActionTest
54 {
55     private AddManagedRepositoryAction action;
56
57     private RoleManager roleManager;
58
59     private MockControl roleManagerControl;
60
61     private MockControl archivaConfigurationControl;
62
63     private Registry registry;
64
65     private MockControl registryControl;
66
67     private ArchivaConfiguration archivaConfiguration;
68
69     private MockControl repositoryTaskSchedulerControl;
70
71     private RepositoryArchivaTaskScheduler repositoryTaskScheduler;
72
73
74     @Override
75     protected void setUp()
76         throws Exception
77     {
78         super.setUp();
79
80         action = new AddManagedRepositoryAction();
81
82         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
83         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
84         action.setArchivaConfiguration( archivaConfiguration );
85
86         roleManagerControl = MockControl.createControl( RoleManager.class );
87         roleManager = (RoleManager) roleManagerControl.getMock();
88
89         registryControl = MockControl.createControl( Registry.class );
90         registry = (Registry) registryControl.getMock();
91         //action.setRegistry( registry );
92
93         repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
94         repositoryTaskScheduler = (RepositoryArchivaTaskScheduler) repositoryTaskSchedulerControl.getMock();
95
96         location = new File( "target/test/location" );
97         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setArchivaConfiguration( archivaConfiguration );
98         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRoleManager( roleManager );
99         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRegistry( registry );
100         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryTaskScheduler(
101             repositoryTaskScheduler );
102
103         RepositoryCommonValidator repositoryCommonValidator = new RepositoryCommonValidator();
104         repositoryCommonValidator.setArchivaConfiguration( archivaConfiguration );
105         repositoryCommonValidator.setRegistry( registry );
106
107         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryCommonValidator(
108             repositoryCommonValidator );
109
110         action.setRepositoryCommonValidator( repositoryCommonValidator );
111
112         action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
113
114     }
115
116     public void testSecureActionBundle()
117         throws SecureActionException
118     {
119         archivaConfiguration.getConfiguration();
120         archivaConfigurationControl.setReturnValue( new Configuration() );
121         archivaConfigurationControl.replay();
122
123         action.prepare();
124         SecureActionBundle bundle = action.getSecureActionBundle();
125         assertTrue( bundle.requiresAuthentication() );
126         assertEquals( 1, bundle.getAuthorizationTuples().size() );
127     }
128
129     public void testAddRepositoryInitialPage()
130         throws Exception
131     {
132         archivaConfiguration.getConfiguration();
133         archivaConfigurationControl.setReturnValue( new Configuration() );
134         archivaConfiguration.getConfiguration();
135         archivaConfigurationControl.setReturnValue( new Configuration() );
136         archivaConfigurationControl.replay();
137
138         action.prepare();
139         ManagedRepository configuration = action.getRepository();
140         assertNotNull( configuration );
141         assertNull( configuration.getId() );
142         // check all booleans are false
143         assertFalse( configuration.isDeleteReleasedSnapshots() );
144         assertFalse( configuration.isScanned() );
145         assertFalse( configuration.isReleases() );
146         assertFalse( configuration.isSnapshots() );
147
148         String status = action.input();
149         assertEquals( Action.INPUT, status );
150
151         // check defaults
152         assertFalse( configuration.isDeleteReleasedSnapshots() );
153         assertTrue( configuration.isScanned() );
154         assertTrue( configuration.isReleases() );
155         assertFalse( configuration.isSnapshots() );
156     }
157
158     public void testAddRepository()
159         throws Exception
160     {
161         FileUtils.deleteDirectory( location );
162
163         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
164         roleManagerControl.setReturnValue( false );
165         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
166         roleManagerControl.setVoidCallable();
167         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
168         roleManagerControl.setReturnValue( false );
169         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
170         roleManagerControl.setVoidCallable();
171
172         roleManagerControl.replay();
173
174         registry.getString( "appserver.base", "${appserver.base}" );
175         registryControl.setReturnValue( "target/test" );
176         registry.getString( "appserver.home", "${appserver.home}" );
177         registryControl.setReturnValue( "target/test" );
178
179         registry.getString( "appserver.base", "${appserver.base}" );
180         registryControl.setReturnValue( "target/test" );
181         registry.getString( "appserver.home", "${appserver.home}" );
182         registryControl.setReturnValue( "target/test" );
183
184         registryControl.replay();
185
186         RepositoryTask task = new RepositoryTask();
187         task.setRepositoryId( REPO_ID );
188         repositoryTaskScheduler.isProcessingRepositoryTask( REPO_ID );
189         repositoryTaskSchedulerControl.setReturnValue( false );
190         repositoryTaskScheduler.queueTask( task );
191         repositoryTaskSchedulerControl.setVoidCallable();
192         repositoryTaskSchedulerControl.replay();
193
194         Configuration configuration = new Configuration();
195         archivaConfiguration.getConfiguration();
196         archivaConfigurationControl.setReturnValue( configuration );
197
198         archivaConfiguration.getConfiguration();
199         archivaConfigurationControl.setReturnValue( configuration );
200
201         archivaConfiguration.getConfiguration();
202         archivaConfigurationControl.setReturnValue( configuration );
203
204         archivaConfiguration.save( configuration );
205
206         archivaConfigurationControl.replay();
207
208         action.prepare();
209         ManagedRepository repository = action.getRepository();
210         populateRepository( repository );
211
212         assertFalse( location.exists() );
213         String status = action.commit();
214         assertEquals( Action.SUCCESS, status );
215         assertTrue( location.exists() );
216
217         assertEquals( Collections.singletonList( repository ), getManagedRepositoryAdmin().getManagedRepositories() );
218         assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
219
220         roleManagerControl.verify();
221         archivaConfigurationControl.verify();
222         registryControl.verify();
223     }
224
225
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     public void testStruts2ValidationFrameworkWithNullInputs()
253         throws Exception
254     {
255         // prep
256         // 0 is the default value for primitive int; null for objects
257         ManagedRepository managedRepositoryConfiguration = createManagedRepository( null, null, null, null );
258         action.setRepository( managedRepositoryConfiguration );
259
260         // test
261         actionValidatorManager.validate( action, EMPTY_STRING );
262
263         // verify
264         assertTrue( action.hasFieldErrors() );
265
266         Map<String, List<String>> fieldErrors = action.getFieldErrors();
267
268         // make an expected field error object
269         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
270
271         // populate
272         List<String> expectedErrorMessages = new ArrayList<String>();
273         expectedErrorMessages.add( "You must enter a repository identifier." );
274         expectedFieldErrors.put( "repository.id", expectedErrorMessages );
275
276         expectedErrorMessages = new ArrayList<String>();
277         expectedErrorMessages.add( "You must enter a directory." );
278         expectedFieldErrors.put( "repository.location", expectedErrorMessages );
279
280         expectedErrorMessages = new ArrayList<String>();
281         expectedErrorMessages.add( "You must enter a repository name." );
282         expectedFieldErrors.put( "repository.name", expectedErrorMessages );
283
284         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
285     }
286
287     public void testStruts2ValidationFrameworkWithBlankInputs()
288         throws Exception
289     {
290         // prep
291         // 0 is the default value for primitive int
292         ManagedRepository managedRepositoryConfiguration =
293             createManagedRepository( EMPTY_STRING, EMPTY_STRING, EMPTY_STRING, EMPTY_STRING );
294         action.setRepository( managedRepositoryConfiguration );
295
296         // test
297         actionValidatorManager.validate( action, EMPTY_STRING );
298
299         // verify
300         assertTrue( action.hasFieldErrors() );
301
302         Map<String, List<String>> fieldErrors = action.getFieldErrors();
303
304         // make an expected field error object
305         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
306
307         // populate
308         List<String> expectedErrorMessages = new ArrayList<String>();
309         expectedErrorMessages.add( "You must enter a repository identifier." );
310         expectedFieldErrors.put( "repository.id", expectedErrorMessages );
311
312         expectedErrorMessages = new ArrayList<String>();
313         expectedErrorMessages.add( "You must enter a directory." );
314         expectedFieldErrors.put( "repository.location", expectedErrorMessages );
315
316         expectedErrorMessages = new ArrayList<String>();
317         expectedErrorMessages.add( "You must enter a repository name." );
318         expectedFieldErrors.put( "repository.name", expectedErrorMessages );
319
320         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
321     }
322
323     public void testStruts2ValidationFrameworkWithInvalidInputs()
324         throws Exception
325     {
326         // prep
327         ManagedRepository managedRepositoryConfiguration =
328             createManagedRepository( REPOSITORY_ID_INVALID_INPUT, REPOSITORY_NAME_INVALID_INPUT,
329                                      REPOSITORY_LOCATION_INVALID_INPUT, REPOSITORY_INDEX_DIR_INVALID_INPUT,
330                                      REPOSITORY_DAYS_OLDER_INVALID_INPUT, REPOSITORY_RETENTION_COUNT_INVALID_INPUT );
331         action.setRepository( managedRepositoryConfiguration );
332
333         // test
334         actionValidatorManager.validate( action, EMPTY_STRING );
335
336         // verify
337         assertTrue( action.hasFieldErrors() );
338
339         Map<String, List<String>> fieldErrors = action.getFieldErrors();
340
341         // make an expected field error object
342         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
343
344         // populate
345         List<String> expectedErrorMessages = new ArrayList<String>();
346         expectedErrorMessages.add(
347             "Identifier must only contain alphanumeric characters, underscores(_), dots(.), and dashes(-)." );
348         expectedFieldErrors.put( "repository.id", expectedErrorMessages );
349
350         expectedErrorMessages = new ArrayList<String>();
351         expectedErrorMessages.add(
352             "Directory must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-)." );
353         expectedFieldErrors.put( "repository.location", expectedErrorMessages );
354
355         expectedErrorMessages = new ArrayList<String>();
356         expectedErrorMessages.add(
357             "Repository Name must only contain alphanumeric characters, white-spaces(' '), forward-slashes(/), open-parenthesis('('), close-parenthesis(')'),  underscores(_), dots(.), and dashes(-)." );
358         expectedFieldErrors.put( "repository.name", expectedErrorMessages );
359
360         expectedErrorMessages = new ArrayList<String>();
361         expectedErrorMessages.add(
362             "Index directory must only contain alphanumeric characters, equals(=), question-marks(?), exclamation-points(!), ampersands(&), forward-slashes(/), back-slashes(\\), underscores(_), dots(.), colons(:), tildes(~), and dashes(-)." );
363         expectedFieldErrors.put( "repository.indexDirectory", expectedErrorMessages );
364
365         expectedErrorMessages = new ArrayList<String>();
366         expectedErrorMessages.add( "Repository Purge By Retention Count needs to be between 1 and 100." );
367         expectedFieldErrors.put( "repository.retentionCount", expectedErrorMessages );
368
369         expectedErrorMessages = new ArrayList<String>();
370         expectedErrorMessages.add( "Repository Purge By Days Older Than needs to be larger than 0." );
371         expectedFieldErrors.put( "repository.daysOlder", expectedErrorMessages );
372
373         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
374     }
375
376     public void testStruts2ValidationFrameworkWithValidInputs()
377         throws Exception
378     {
379         // prep
380         ManagedRepository managedRepositoryConfiguration =
381             createManagedRepository( REPOSITORY_ID_VALID_INPUT, REPOSITORY_NAME_VALID_INPUT,
382                                      REPOSITORY_LOCATION_VALID_INPUT, REPOSITORY_INDEX_DIR_VALID_INPUT,
383                                      REPOSITORY_DAYS_OLDER_VALID_INPUT, REPOSITORY_RETENTION_COUNT_VALID_INPUT );
384         action.setRepository( managedRepositoryConfiguration );
385
386         // test
387         actionValidatorManager.validate( action, EMPTY_STRING );
388
389         // verify
390         assertFalse( action.hasFieldErrors() );
391     }
392
393     // TODO: test errors during add, other actions
394 }