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 archivaConfigurationControl.replay();
100 action.setRepoid( REPO_ID );
103 assertEquals( REPO_ID, action.getRepoid() );
104 ManagedRepositoryConfiguration repository = action.getRepository();
105 assertNotNull( repository );
106 assertRepositoryEquals( repository, createRepository() );
108 String status = action.input();
109 assertEquals( Action.INPUT, status );
110 repository = action.getRepository();
111 assertRepositoryEquals( repository, createRepository() );
114 public void testEditRepository()
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();
126 roleManagerControl.replay();
128 Configuration configuration = createConfigurationForEditing( createRepository() );
129 archivaConfiguration.getConfiguration();
130 archivaConfigurationControl.setReturnValue( configuration );
131 archivaConfigurationControl.setReturnValue( configuration );
132 archivaConfigurationControl.setReturnValue( configuration );
134 archivaConfiguration.save( configuration );
136 archivaConfigurationControl.replay();
138 action.setRepoid( REPO_ID );
140 assertEquals( REPO_ID, action.getRepoid() );
141 ManagedRepositoryConfiguration repository = action.getRepository();
142 populateRepository( repository );
143 repository.setName( "new repo name" );
145 MockControl repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
146 RepositoryStatisticsManager repositoryStatisticsManager =
147 (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
148 action.setRepositoryStatisticsManager( repositoryStatisticsManager );
150 repositoryStatisticsManagerControl.replay();
152 String status = action.commit();
153 assertEquals( Action.SUCCESS, status );
155 ManagedRepositoryConfiguration newRepository = createRepository();
156 newRepository.setName( "new repo name" );
157 assertRepositoryEquals( repository, newRepository );
158 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
160 roleManagerControl.verify();
161 archivaConfigurationControl.verify();
162 repositoryStatisticsManagerControl.verify();
165 public void testEditRepositoryLocationChanged()
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();
177 roleManagerControl.replay();
179 Configuration configuration = createConfigurationForEditing( createRepository() );
180 archivaConfiguration.getConfiguration();
181 archivaConfigurationControl.setReturnValue( configuration );
182 archivaConfigurationControl.setReturnValue( configuration );
183 archivaConfigurationControl.setReturnValue( configuration );
185 archivaConfiguration.save( configuration );
187 archivaConfigurationControl.replay();
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();
196 action.setRepoid( REPO_ID );
198 assertEquals( REPO_ID, action.getRepoid() );
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() );
210 roleManagerControl.verify();
211 archivaConfigurationControl.verify();
212 repositoryStatisticsManagerControl.verify();
215 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
216 ManagedRepositoryConfiguration actualRepository )
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() );
232 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
234 Configuration configuration = new Configuration();
235 configuration.addManagedRepository( repositoryConfiguration );
236 return configuration;
239 private ManagedRepositoryConfiguration createRepository()
242 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
244 populateRepository( r );
248 private void populateRepository( ManagedRepositoryConfiguration repository )
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 );