]> source.dussan.org Git - archiva.git/blob
89d2cc91bfe101b14b65c18a2493bc2eb65fb459
[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.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;
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
85         roleManagerControl = MockControl.createControl( RoleManager.class );
86         roleManager = (RoleManager) roleManagerControl.getMock();
87
88         registryControl = MockControl.createControl( Registry.class );
89         registry = (Registry) registryControl.getMock();
90         //action.setRegistry( registry );
91
92         repositoryTaskSchedulerControl = MockClassControl.createControl( RepositoryArchivaTaskScheduler.class );
93         repositoryTaskScheduler = (RepositoryArchivaTaskScheduler) repositoryTaskSchedulerControl.getMock();
94
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 );
101
102         DefaultRepositoryCommonValidator defaultRepositoryCommonValidator = new DefaultRepositoryCommonValidator();
103         defaultRepositoryCommonValidator.setArchivaConfiguration( archivaConfiguration );
104         defaultRepositoryCommonValidator.setRegistry( registry );
105
106         ( (DefaultManagedRepositoryAdmin) getManagedRepositoryAdmin() ).setRepositoryCommonValidator(
107             defaultRepositoryCommonValidator );
108
109         action.setRepositoryCommonValidator( defaultRepositoryCommonValidator );
110
111         action.setManagedRepositoryAdmin( getManagedRepositoryAdmin() );
112
113     }
114
115     public void testSecureActionBundle()
116         throws SecureActionException
117     {
118         archivaConfiguration.getConfiguration();
119         archivaConfigurationControl.setReturnValue( new Configuration() );
120         archivaConfigurationControl.replay();
121
122         action.prepare();
123         SecureActionBundle bundle = action.getSecureActionBundle();
124         assertTrue( bundle.requiresAuthentication() );
125         assertEquals( 1, bundle.getAuthorizationTuples().size() );
126     }
127
128     public void testAddRepositoryInitialPage()
129         throws Exception
130     {
131         archivaConfiguration.getConfiguration();
132         archivaConfigurationControl.setReturnValue( new Configuration() );
133         archivaConfiguration.getConfiguration();
134         archivaConfigurationControl.setReturnValue( new Configuration() );
135         archivaConfigurationControl.replay();
136
137         action.prepare();
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() );
146
147         String status = action.input();
148         assertEquals( Action.INPUT, status );
149
150         // check defaults
151         assertFalse( configuration.isDeleteReleasedSnapshots() );
152         assertTrue( configuration.isScanned() );
153         assertTrue( configuration.isReleases() );
154         assertFalse( configuration.isSnapshots() );
155     }
156
157     public void testAddRepository()
158         throws Exception
159     {
160         FileUtils.deleteDirectory( location );
161
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();
170
171         roleManagerControl.replay();
172
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 );
177
178         registryControl.replay();
179
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();
187
188         Configuration configuration = new Configuration();
189         archivaConfiguration.getConfiguration();
190         archivaConfigurationControl.setReturnValue( configuration );
191
192         archivaConfiguration.getConfiguration();
193         archivaConfigurationControl.setReturnValue( configuration );
194
195         archivaConfiguration.getConfiguration();
196         archivaConfigurationControl.setReturnValue( configuration );
197
198         archivaConfiguration.save( configuration );
199
200         archivaConfigurationControl.replay();
201
202         action.prepare();
203         ManagedRepository repository = action.getRepository();
204         populateRepository( repository );
205
206         assertFalse( location.exists() );
207         String status = action.commit();
208         assertEquals( Action.SUCCESS, status );
209         assertTrue( location.exists() );
210
211         assertEquals( Collections.singletonList( repository ), getManagedRepositoryAdmin().getManagedRepositories() );
212         assertEquals( location.getCanonicalPath(), new File( repository.getLocation() ).getCanonicalPath() );
213
214         roleManagerControl.verify();
215         archivaConfigurationControl.verify();
216         registryControl.verify();
217     }
218
219
220     public void testAddRepositoryExistingLocation()
221         throws Exception
222     {
223         if ( !location.exists() )
224         {
225             location.mkdirs();
226         }
227
228         registry.getString( "appserver.base", "${appserver.base}" );
229         registryControl.setReturnValue( "target/test" );
230         registry.getString( "appserver.home", "${appserver.home}" );
231         registryControl.setReturnValue( "target/test" );
232
233         registryControl.replay();
234
235         action.prepare();
236         ManagedRepository repository = action.getRepository();
237         populateRepository( repository );
238
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();
244     }
245
246     public void testStruts2ValidationFrameworkWithNullInputs()
247         throws Exception
248     {
249         // prep
250         // 0 is the default value for primitive int; null for objects
251         ManagedRepository managedRepositoryConfiguration = createManagedRepository( null, null, null, null );
252         action.setRepository( managedRepositoryConfiguration );
253
254         // test
255         actionValidatorManager.validate( action, EMPTY_STRING );
256
257         // verify
258         assertTrue( action.hasFieldErrors() );
259
260         Map<String, List<String>> fieldErrors = action.getFieldErrors();
261
262         // make an expected field error object
263         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
264
265         // populate
266         List<String> expectedErrorMessages = new ArrayList<String>();
267         expectedErrorMessages.add( "You must enter a repository identifier." );
268         expectedFieldErrors.put( "repository.id", expectedErrorMessages );
269
270         expectedErrorMessages = new ArrayList<String>();
271         expectedErrorMessages.add( "You must enter a directory." );
272         expectedFieldErrors.put( "repository.location", expectedErrorMessages );
273
274         expectedErrorMessages = new ArrayList<String>();
275         expectedErrorMessages.add( "You must enter a repository name." );
276         expectedFieldErrors.put( "repository.name", expectedErrorMessages );
277
278         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
279     }
280
281     public void testStruts2ValidationFrameworkWithBlankInputs()
282         throws Exception
283     {
284         // prep
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 );
289
290         // test
291         actionValidatorManager.validate( action, EMPTY_STRING );
292
293         // verify
294         assertTrue( action.hasFieldErrors() );
295
296         Map<String, List<String>> fieldErrors = action.getFieldErrors();
297
298         // make an expected field error object
299         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
300
301         // populate
302         List<String> expectedErrorMessages = new ArrayList<String>();
303         expectedErrorMessages.add( "You must enter a repository identifier." );
304         expectedFieldErrors.put( "repository.id", expectedErrorMessages );
305
306         expectedErrorMessages = new ArrayList<String>();
307         expectedErrorMessages.add( "You must enter a directory." );
308         expectedFieldErrors.put( "repository.location", expectedErrorMessages );
309
310         expectedErrorMessages = new ArrayList<String>();
311         expectedErrorMessages.add( "You must enter a repository name." );
312         expectedFieldErrors.put( "repository.name", expectedErrorMessages );
313
314         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
315     }
316
317     public void testStruts2ValidationFrameworkWithInvalidInputs()
318         throws Exception
319     {
320         // prep
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 );
326
327         // test
328         actionValidatorManager.validate( action, EMPTY_STRING );
329
330         // verify
331         assertTrue( action.hasFieldErrors() );
332
333         Map<String, List<String>> fieldErrors = action.getFieldErrors();
334
335         // make an expected field error object
336         Map<String, List<String>> expectedFieldErrors = new HashMap<String, List<String>>();
337
338         // populate
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 );
343
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 );
348
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 );
353
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 );
358
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 );
362
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 );
366
367         ValidatorUtil.assertFieldErrors( expectedFieldErrors, fieldErrors );
368     }
369
370     public void testStruts2ValidationFrameworkWithValidInputs()
371         throws Exception
372     {
373         // prep
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 );
379
380         // test
381         actionValidatorManager.validate( action, EMPTY_STRING );
382
383         // verify
384         assertFalse( action.hasFieldErrors() );
385     }
386
387     // TODO: test errors during add, other actions
388 }