1 package org.apache.maven.archiva.web.action.admin.repositories;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
23 import java.io.IOException;
24 import java.util.Collections;
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;
40 * EditManagedRepositoryActionTest
44 public class EditManagedRepositoryActionTest
45 extends PlexusInSpringTestCase
47 private EditManagedRepositoryAction action;
49 private RoleManager roleManager;
51 private MockControl roleManagerControl;
53 private MockControl archivaConfigurationControl;
55 private ArchivaConfiguration archivaConfiguration;
57 private static final String REPO_ID = "repo-ident";
59 private File location;
61 protected void setUp()
66 action = new EditManagedRepositoryAction();
68 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
69 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
70 action.setArchivaConfiguration( archivaConfiguration );
72 roleManagerControl = MockControl.createControl( RoleManager.class );
73 roleManager = (RoleManager) roleManagerControl.getMock();
74 action.setRoleManager( roleManager );
75 location = getTestFile( "target/test/location" );
78 public void testSecureActionBundle()
79 throws SecureActionException
81 archivaConfiguration.getConfiguration();
82 archivaConfigurationControl.setReturnValue( new Configuration() );
83 archivaConfigurationControl.replay();
86 SecureActionBundle bundle = action.getSecureActionBundle();
87 assertTrue( bundle.requiresAuthentication() );
88 assertEquals( 1, bundle.getAuthorizationTuples().size() );
91 public void testEditRepositoryInitialPage()
94 Configuration configuration = createConfigurationForEditing( createRepository() );
96 archivaConfiguration.getConfiguration();
97 archivaConfigurationControl.setReturnValue( configuration );
98 Configuration stageRepoConfiguration = new Configuration();
99 stageRepoConfiguration.addManagedRepository( createStagingRepository() );
100 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
102 archivaConfigurationControl.replay();
104 action.setRepoid( REPO_ID );
107 assertEquals( REPO_ID, action.getRepoid() );
108 ManagedRepositoryConfiguration repository = action.getRepository();
109 assertNotNull( repository );
110 assertRepositoryEquals( repository, createRepository() );
112 String status = action.input();
113 assertEquals( Action.INPUT, status );
114 repository = action.getRepository();
115 assertRepositoryEquals( repository, createRepository() );
118 public void testEditRepository()
121 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
122 roleManagerControl.setReturnValue( false );
124 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID +"-stage" );
125 roleManagerControl.setReturnValue( false );
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 );
132 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID +"-stage");
133 roleManagerControl.setReturnValue( false );
135 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
136 roleManagerControl.setVoidCallable();
138 roleManagerControl.replay();
140 Configuration configuration = createConfigurationForEditing( createRepository() );
141 archivaConfiguration.getConfiguration();
142 archivaConfigurationControl.setReturnValue( configuration );
144 Configuration stageRepoConfiguration = new Configuration();
145 stageRepoConfiguration.addManagedRepository( createStagingRepository() );
146 archivaConfigurationControl.setReturnValue( stageRepoConfiguration );
147 archivaConfigurationControl.setReturnValue( configuration );
148 archivaConfigurationControl.setReturnValue( configuration );
150 archivaConfiguration.save( configuration );
152 archivaConfigurationControl.replay();
154 action.setRepoid( REPO_ID );
156 assertEquals( REPO_ID, action.getRepoid() );
157 ManagedRepositoryConfiguration repository = action.getRepository();
158 populateRepository( repository );
159 repository.setName( "new repo name" );
161 MockControl repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
162 RepositoryStatisticsManager repositoryStatisticsManager =
163 (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
164 action.setRepositoryStatisticsManager( repositoryStatisticsManager );
166 repositoryStatisticsManagerControl.replay();
168 String status = action.commit();
169 assertEquals( Action.SUCCESS, status );
171 ManagedRepositoryConfiguration newRepository = createRepository();
172 newRepository.setName( "new repo name" );
173 assertRepositoryEquals( repository, newRepository );
174 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
176 roleManagerControl.verify();
177 archivaConfigurationControl.verify();
178 repositoryStatisticsManagerControl.verify();
181 public void testEditRepositoryLocationChanged()
184 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
185 roleManagerControl.setReturnValue( false );
187 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID +"-stage");
188 roleManagerControl.setReturnValue( false );
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 );
195 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID +"-stage");
196 roleManagerControl.setReturnValue( false );
198 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
199 roleManagerControl.setVoidCallable();
201 roleManagerControl.replay();
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 );
211 archivaConfigurationControl.setReturnValue( configuration );
212 archivaConfigurationControl.setReturnValue( configuration );
214 archivaConfiguration.save( configuration );
216 archivaConfigurationControl.replay();
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();
225 action.setRepoid( REPO_ID );
227 assertEquals( REPO_ID, action.getRepoid() );
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() );
239 roleManagerControl.verify();
240 archivaConfigurationControl.verify();
241 repositoryStatisticsManagerControl.verify();
244 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
245 ManagedRepositoryConfiguration actualRepository )
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() );
261 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
264 Configuration configuration = new Configuration();
265 configuration.addManagedRepository( repositoryConfiguration );
266 // configuration.addManagedRepository( createStagingRepository() );
267 return configuration;
270 private ManagedRepositoryConfiguration createRepository()
273 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
275 populateRepository( r );
279 private ManagedRepositoryConfiguration createStagingRepository()
282 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
283 r.setId( REPO_ID + "-stage" );
284 populateStagingRepository( r );
288 private void populateRepository( ManagedRepositoryConfiguration repository )
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 );
303 private void populateStagingRepository( ManagedRepositoryConfiguration repository )
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 );