]> source.dussan.org Git - archiva.git/blob
1f5024fb5d9d5206ebcfba77fa1961e1763aafc3
[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         archivaConfigurationControl.replay();
99
100         action.setRepoid( REPO_ID );
101
102         action.prepare();
103         assertEquals( REPO_ID, action.getRepoid() );
104         ManagedRepositoryConfiguration repository = action.getRepository();
105         assertNotNull( repository );
106         assertRepositoryEquals( repository, createRepository() );
107
108         String status = action.input();
109         assertEquals( Action.INPUT, status );
110         repository = action.getRepository();
111         assertRepositoryEquals( repository, createRepository() );
112     }
113
114     public void testEditRepository()
115         throws Exception
116     {
117         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
118         roleManagerControl.setReturnValue( false );
119         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
120         roleManagerControl.setVoidCallable();
121         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
122         roleManagerControl.setReturnValue( false );
123         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
124         roleManagerControl.setVoidCallable();
125
126         roleManagerControl.replay();
127
128         Configuration configuration = createConfigurationForEditing( createRepository() );
129         archivaConfiguration.getConfiguration();
130         archivaConfigurationControl.setReturnValue( configuration );
131         archivaConfigurationControl.setReturnValue( configuration );
132         archivaConfigurationControl.setReturnValue( configuration );
133
134         archivaConfiguration.save( configuration );
135
136         archivaConfigurationControl.replay();
137
138         action.setRepoid( REPO_ID );
139         action.prepare();
140         assertEquals( REPO_ID, action.getRepoid() );
141         ManagedRepositoryConfiguration repository = action.getRepository();
142         populateRepository( repository );
143         repository.setName( "new repo name" );
144
145         MockControl repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
146         RepositoryStatisticsManager repositoryStatisticsManager =
147             (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
148         action.setRepositoryStatisticsManager( repositoryStatisticsManager );
149         // no deletion
150         repositoryStatisticsManagerControl.replay();
151
152         String status = action.commit();
153         assertEquals( Action.SUCCESS, status );
154
155         ManagedRepositoryConfiguration newRepository = createRepository();
156         newRepository.setName( "new repo name" );
157         assertRepositoryEquals( repository, newRepository );
158         assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
159
160         roleManagerControl.verify();
161         archivaConfigurationControl.verify();
162         repositoryStatisticsManagerControl.verify();
163     }
164
165     public void testEditRepositoryLocationChanged()
166         throws Exception
167     {
168         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
169         roleManagerControl.setReturnValue( false );
170         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
171         roleManagerControl.setVoidCallable();
172         roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
173         roleManagerControl.setReturnValue( false );
174         roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
175         roleManagerControl.setVoidCallable();
176
177         roleManagerControl.replay();
178
179         Configuration configuration = createConfigurationForEditing( createRepository() );
180         archivaConfiguration.getConfiguration();
181         archivaConfigurationControl.setReturnValue( configuration );
182         archivaConfigurationControl.setReturnValue( configuration );
183         archivaConfigurationControl.setReturnValue( configuration );
184
185         archivaConfiguration.save( configuration );
186
187         archivaConfigurationControl.replay();
188
189         MockControl repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
190         RepositoryStatisticsManager repositoryStatisticsManager =
191             (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
192         action.setRepositoryStatisticsManager( repositoryStatisticsManager );
193         repositoryStatisticsManager.deleteStatistics( REPO_ID );
194         repositoryStatisticsManagerControl.replay();
195
196         action.setRepoid( REPO_ID );
197         action.prepare();
198         assertEquals( REPO_ID, action.getRepoid() );
199
200         ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
201         populateRepository( repository );
202         File testFile = getTestFile( "target/test/location/new" );
203         FileUtils.deleteDirectory( testFile );
204         repository.setLocation( testFile.getCanonicalPath() );
205         action.setRepository( repository );
206         String status = action.commit();
207         assertEquals( Action.SUCCESS, status );
208         assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
209
210         roleManagerControl.verify();
211         archivaConfigurationControl.verify();
212         repositoryStatisticsManagerControl.verify();
213     }
214
215     private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
216                                          ManagedRepositoryConfiguration actualRepository )
217     {
218         assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
219         assertEquals( expectedRepository.getId(), actualRepository.getId() );
220         assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
221         assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
222         assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
223         assertEquals( expectedRepository.getName(), actualRepository.getName() );
224         assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
225         assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
226         assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
227         assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
228         assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
229         assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
230     }
231
232     private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
233     {
234         Configuration configuration = new Configuration();
235         configuration.addManagedRepository( repositoryConfiguration );
236         return configuration;
237     }
238
239     private ManagedRepositoryConfiguration createRepository()
240         throws IOException
241     {
242         ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
243         r.setId( REPO_ID );
244         populateRepository( r );
245         return r;
246     }
247
248     private void populateRepository( ManagedRepositoryConfiguration repository )
249         throws IOException
250     {
251         repository.setId( REPO_ID );
252         repository.setName( "repo name" );
253         repository.setLocation( location.getCanonicalPath() );
254         repository.setLayout( "default" );
255         repository.setRefreshCronExpression( "* 0/5 * * * ?" );
256         repository.setDaysOlder( 31 );
257         repository.setRetentionCount( 20 );
258         repository.setReleases( true );
259         repository.setSnapshots( true );
260         repository.setScanned( false );
261         repository.setDeleteReleasedSnapshots( true );
262     }
263 }