]> source.dussan.org Git - archiva.git/blob
7f2fc6ec3c79e4ddc98ff17ec38beceff4624a28
[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 java.io.File;
23 import java.io.IOException;
24 import java.util.Collections;
25
26 import com.opensymphony.xwork2.Action;
27 import org.apache.archiva.metadata.repository.stats.RepositoryStatisticsManager;
28 import org.apache.commons.io.FileUtils;
29 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
30 import org.apache.maven.archiva.configuration.Configuration;
31 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
32 import org.apache.maven.archiva.security.ArchivaRoleConstants;
33 import org.codehaus.plexus.redback.role.RoleManager;
34 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
35 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
36 import org.codehaus.redback.integration.interceptor.SecureActionException;
37 import org.easymock.MockControl;
38
39 /**
40  * EditManagedRepositoryActionTest
41  *
42  * @version $Id$
43  */
44 public class EditManagedRepositoryActionTest
45     extends PlexusInSpringTestCase
46 {
47     private EditManagedRepositoryAction action;
48
49     private RoleManager roleManager;
50
51     private MockControl roleManagerControl;
52
53     private MockControl archivaConfigurationControl;
54
55     private ArchivaConfiguration archivaConfiguration;
56
57     private static final String REPO_ID = "repo-ident";
58
59     private File location;
60
61     protected void setUp()
62         throws Exception
63     {
64         super.setUp();
65
66         action = new EditManagedRepositoryAction();
67
68         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
69         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
70         action.setArchivaConfiguration( archivaConfiguration );
71
72         roleManagerControl = MockControl.createControl( RoleManager.class );
73         roleManager = (RoleManager) roleManagerControl.getMock();
74         action.setRoleManager( roleManager );
75         location = getTestFile( "target/test/location" );
76     }
77
78     public void testSecureActionBundle()
79         throws SecureActionException
80     {
81         archivaConfiguration.getConfiguration();
82         archivaConfigurationControl.setReturnValue( new Configuration() );
83         archivaConfigurationControl.replay();
84
85         action.prepare();
86         SecureActionBundle bundle = action.getSecureActionBundle();
87         assertTrue( bundle.requiresAuthentication() );
88         assertEquals( 1, bundle.getAuthorizationTuples().size() );
89     }
90
91     public void testEditRepositoryInitialPage()
92         throws Exception
93     {
94         Configuration configuration = createConfigurationForEditing( createRepository() );
95
96         archivaConfiguration.getConfiguration();
97         archivaConfigurationControl.setReturnValue( configuration );
98         Configuration stageRepoConfiguration = new Configuration();
99         stageRepoConfiguration.addManagedRepository( createStagingRepository() );
100         archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
101
102         archivaConfigurationControl.replay();
103
104         action.setRepoid( REPO_ID );
105
106         action.prepare();
107         assertEquals( REPO_ID, action.getRepoid() );
108         ManagedRepositoryConfiguration repository = action.getRepository();
109         assertNotNull( repository );
110         assertRepositoryEquals( repository, createRepository() );
111
112         String status = action.input();
113         assertEquals( Action.INPUT, status );
114         repository = action.getRepository();
115         assertRepositoryEquals( repository, createRepository() );
116     }
117
118     public void testEditRepository()
119         throws Exception
120     {
121         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
122         roleManagerControl.setReturnValue( false );
123
124          roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID +"-stage" );
125         roleManagerControl.setReturnValue( false );
126
127         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
128         roleManagerControl.setVoidCallable();
129         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
130         roleManagerControl.setReturnValue( false );
131
132         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID +"-stage");
133         roleManagerControl.setReturnValue( false );
134
135         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
136         roleManagerControl.setVoidCallable();
137
138         roleManagerControl.replay();
139
140         Configuration configuration = createConfigurationForEditing( createRepository() );
141         archivaConfiguration.getConfiguration();
142         archivaConfigurationControl.setReturnValue( configuration );
143
144         Configuration stageRepoConfiguration = new Configuration();
145         stageRepoConfiguration.addManagedRepository( createStagingRepository() );
146         archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
147         archivaConfigurationControl.setReturnValue( configuration );
148         archivaConfigurationControl.setReturnValue( configuration );
149
150         archivaConfiguration.save( configuration );
151
152         archivaConfigurationControl.replay();
153
154         action.setRepoid( REPO_ID );
155         action.prepare();
156         assertEquals( REPO_ID, action.getRepoid() );
157         ManagedRepositoryConfiguration repository = action.getRepository();
158         populateRepository( repository );
159         repository.setName( "new repo name" );
160
161         MockControl repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
162         RepositoryStatisticsManager repositoryStatisticsManager =
163             (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
164         action.setRepositoryStatisticsManager( repositoryStatisticsManager );
165         // no deletion
166         repositoryStatisticsManagerControl.replay();
167
168         String status = action.commit();
169         assertEquals( Action.SUCCESS, status );
170
171         ManagedRepositoryConfiguration newRepository = createRepository();
172         newRepository.setName( "new repo name" );
173         assertRepositoryEquals( repository, newRepository );
174         assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
175
176         roleManagerControl.verify();
177         archivaConfigurationControl.verify();
178         repositoryStatisticsManagerControl.verify();
179     }
180
181     public void testEditRepositoryLocationChanged()
182         throws Exception
183     {
184         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
185         roleManagerControl.setReturnValue( false );
186
187         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID +"-stage");
188         roleManagerControl.setReturnValue( false );
189
190         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
191         roleManagerControl.setVoidCallable();
192         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
193         roleManagerControl.setReturnValue( false );
194
195         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID +"-stage");
196         roleManagerControl.setReturnValue( false );
197
198         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
199         roleManagerControl.setVoidCallable();
200
201         roleManagerControl.replay();
202
203         Configuration configuration = createConfigurationForEditing( createRepository() );
204         archivaConfiguration.getConfiguration();
205         archivaConfigurationControl.setReturnValue( configuration );
206         Configuration stageRepoConfiguration = new Configuration();
207         stageRepoConfiguration.addManagedRepository( createStagingRepository() );
208         archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
209
210
211         archivaConfigurationControl.setReturnValue( configuration );
212         archivaConfigurationControl.setReturnValue( configuration );
213
214         archivaConfiguration.save( configuration );
215
216         archivaConfigurationControl.replay();
217
218         MockControl repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
219         RepositoryStatisticsManager repositoryStatisticsManager =
220             (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
221         action.setRepositoryStatisticsManager( repositoryStatisticsManager );
222         repositoryStatisticsManager.deleteStatistics( REPO_ID );
223         repositoryStatisticsManagerControl.replay();
224
225         action.setRepoid( REPO_ID );
226         action.prepare();
227         assertEquals( REPO_ID, action.getRepoid() );
228
229         ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
230         populateRepository( repository );
231         File testFile = getTestFile( "target/test/location/new" );
232         FileUtils.deleteDirectory( testFile );
233         repository.setLocation( testFile.getCanonicalPath() );
234         action.setRepository( repository );
235         String status = action.commit();
236         assertEquals( Action.SUCCESS, status );
237         assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
238
239         roleManagerControl.verify();
240         archivaConfigurationControl.verify();
241         repositoryStatisticsManagerControl.verify();
242     }
243
244     private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
245                                          ManagedRepositoryConfiguration actualRepository )
246     {
247         assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
248         assertEquals( expectedRepository.getId(), actualRepository.getId() );
249         assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
250         assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
251         assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
252         assertEquals( expectedRepository.getName(), actualRepository.getName() );
253         assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
254         assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
255         assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
256         assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
257         assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
258         assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
259     }
260
261     private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
262         throws Exception
263     {
264         Configuration configuration = new Configuration();
265         configuration.addManagedRepository( repositoryConfiguration );
266 //        configuration.addManagedRepository( createStagingRepository() );
267         return configuration;
268     }
269
270     private ManagedRepositoryConfiguration createRepository()
271         throws IOException
272     {
273         ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
274         r.setId( REPO_ID );
275         populateRepository( r );
276         return r;
277     }
278
279     private ManagedRepositoryConfiguration createStagingRepository()
280         throws IOException
281     {
282         ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
283         r.setId( REPO_ID + "-stage" );
284         populateStagingRepository( r );
285         return r;
286     }
287
288     private void populateRepository( ManagedRepositoryConfiguration repository )
289         throws IOException
290     {
291         repository.setId( REPO_ID );
292         repository.setName( "repo name" );
293         repository.setLocation( location.getCanonicalPath() );
294         repository.setLayout( "default" );
295         repository.setRefreshCronExpression( "* 0/5 * * * ?" );
296         repository.setDaysOlder( 31 );
297         repository.setRetentionCount( 20 );
298         repository.setReleases( true );
299         repository.setSnapshots( true );
300         repository.setScanned( false );
301         repository.setDeleteReleasedSnapshots( true );
302     }
303     private void populateStagingRepository( ManagedRepositoryConfiguration repository )
304         throws IOException
305     {
306         repository.setId( REPO_ID + "-stage");
307         repository.setName( "repo name" );
308         repository.setLocation( location.getCanonicalPath() );
309         repository.setLayout( "default" );
310         repository.setRefreshCronExpression( "* 0/5 * * * ?" );
311         repository.setDaysOlder( 31 );
312         repository.setRetentionCount( 20 );
313         repository.setReleases( true );
314         repository.setSnapshots( true );
315         repository.setScanned( false );
316         repository.setDeleteReleasedSnapshots( true );
317     }
318 }