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
22 import com.opensymphony.xwork2.Action;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.Configuration;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.database.ArchivaAuditLogsDao;
27 import org.apache.maven.archiva.database.ArchivaDAO;
28 import org.apache.maven.archiva.database.RepositoryContentStatisticsDAO;
29 import org.apache.maven.archiva.database.constraints.RepositoryContentStatisticsByRepositoryConstraint;
30 import org.apache.maven.archiva.model.ArchivaAuditLogs;
31 import org.apache.maven.archiva.model.RepositoryContentStatistics;
32 import org.apache.maven.archiva.security.ArchivaRoleConstants;
33 import org.codehaus.plexus.redback.role.RoleManager;
34 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
35 import org.codehaus.redback.integration.interceptor.SecureActionException;
36 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
37 import org.easymock.MockControl;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.Date;
44 import java.util.List;
47 * EditManagedRepositoryActionTest
51 public class EditManagedRepositoryActionTest
52 extends PlexusInSpringTestCase
54 private EditManagedRepositoryAction action;
56 private RoleManager roleManager;
58 private MockControl roleManagerControl;
60 private MockControl archivaConfigurationControl;
62 private ArchivaConfiguration archivaConfiguration;
64 private MockControl archivaDaoControl;
66 private ArchivaDAO archivaDao;
68 private MockControl repoContentStatsDaoControl;
70 private RepositoryContentStatisticsDAO repoContentStatsDao;
72 private ArchivaAuditLogsDao auditLogsDao;
74 private MockControl auditLogsDaoControl;
76 private static final String REPO_ID = "repo-ident";
78 private File location;
81 protected String getPlexusConfigLocation()
83 return AbstractManagedRepositoriesAction.class.getName().replace( '.', '/' ) + "Test.xml";
86 protected void setUp()
91 action = (EditManagedRepositoryAction) lookup( Action.class.getName(), "editManagedRepositoryAction" );
93 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
94 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
95 action.setArchivaConfiguration( archivaConfiguration );
97 roleManagerControl = MockControl.createControl( RoleManager.class );
98 roleManager = (RoleManager) roleManagerControl.getMock();
99 action.setRoleManager( roleManager );
100 location = getTestFile( "target/test/location" );
102 archivaDaoControl = MockControl.createControl( ArchivaDAO.class );
103 archivaDao = (ArchivaDAO) archivaDaoControl.getMock();
104 action.setArchivaDAO( archivaDao );
106 auditLogsDaoControl = MockControl.createControl( ArchivaAuditLogsDao.class );
107 auditLogsDaoControl.setDefaultMatcher( MockControl.ALWAYS_MATCHER );
108 auditLogsDao = (ArchivaAuditLogsDao) auditLogsDaoControl.getMock();
109 action.setAuditLogsDao( auditLogsDao );
111 repoContentStatsDaoControl = MockControl.createControl( RepositoryContentStatisticsDAO.class );
112 repoContentStatsDao = (RepositoryContentStatisticsDAO) repoContentStatsDaoControl.getMock();
115 public void testSecureActionBundle()
116 throws SecureActionException
118 archivaConfiguration.getConfiguration();
119 archivaConfigurationControl.setReturnValue( new Configuration() );
120 archivaConfigurationControl.replay();
123 SecureActionBundle bundle = action.getSecureActionBundle();
124 assertTrue( bundle.requiresAuthentication() );
125 assertEquals( 1, bundle.getAuthorizationTuples().size() );
128 public void testEditRepositoryInitialPage()
131 Configuration configuration = createConfigurationForEditing( createRepository() );
133 archivaConfiguration.getConfiguration();
134 archivaConfigurationControl.setReturnValue( configuration );
135 archivaConfigurationControl.replay();
137 action.setRepoid( REPO_ID );
140 assertEquals( REPO_ID, action.getRepoid() );
141 ManagedRepositoryConfiguration repository = action.getRepository();
142 assertNotNull( repository );
143 assertRepositoryEquals( repository, createRepository() );
145 String status = action.input();
146 assertEquals( Action.INPUT, status );
147 repository = action.getRepository();
148 assertRepositoryEquals( repository, createRepository() );
151 public void testEditRepository()
154 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
155 roleManagerControl.setReturnValue( false );
156 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
157 roleManagerControl.setVoidCallable();
158 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
159 roleManagerControl.setReturnValue( false );
160 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
161 roleManagerControl.setVoidCallable();
163 roleManagerControl.replay();
165 Configuration configuration = createConfigurationForEditing( createRepository() );
166 archivaConfiguration.getConfiguration();
167 archivaConfigurationControl.setReturnValue( configuration );
168 archivaConfigurationControl.setReturnValue( configuration );
169 archivaConfigurationControl.setReturnValue( configuration );
171 archivaConfiguration.save( configuration );
173 archivaConfigurationControl.replay();
175 action.setRepoid( REPO_ID );
177 assertEquals( REPO_ID, action.getRepoid() );
178 ManagedRepositoryConfiguration repository = action.getRepository();
179 populateRepository( repository );
180 repository.setName( "new repo name" );
182 auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
183 auditLogsDaoControl.replay();
185 String status = action.commit();
186 assertEquals( Action.SUCCESS, status );
188 ManagedRepositoryConfiguration newRepository = createRepository();
189 newRepository.setName( "new repo name" );
190 assertRepositoryEquals( repository, newRepository );
191 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
193 roleManagerControl.verify();
194 archivaConfigurationControl.verify();
195 auditLogsDaoControl.verify();
198 public void testEditRepositoryLocationChanged()
201 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
202 roleManagerControl.setReturnValue( false );
203 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
204 roleManagerControl.setVoidCallable();
205 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
206 roleManagerControl.setReturnValue( false );
207 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
208 roleManagerControl.setVoidCallable();
210 roleManagerControl.replay();
212 Configuration configuration = createConfigurationForEditing( createRepository() );
213 archivaConfiguration.getConfiguration();
214 archivaConfigurationControl.setReturnValue( configuration );
215 archivaConfigurationControl.setReturnValue( configuration );
216 archivaConfigurationControl.setReturnValue( configuration );
218 archivaConfiguration.save( configuration );
220 archivaConfigurationControl.replay();
222 archivaDaoControl.expectAndReturn( archivaDao.getRepositoryContentStatisticsDAO(), repoContentStatsDao );
224 archivaDaoControl.replay();
226 repoContentStatsDao.queryRepositoryContentStatistics(
227 new RepositoryContentStatisticsByRepositoryConstraint( REPO_ID ) );
228 repoContentStatsDaoControl.setMatcher( MockControl.ALWAYS_MATCHER );
230 List<RepositoryContentStatistics> repoStats = createRepositoryContentStatisticsList();
231 repoContentStatsDaoControl.setReturnValue( repoStats );
233 repoContentStatsDao.deleteRepositoryContentStatistics( repoStats.get( 0 ) );
234 repoContentStatsDaoControl.setVoidCallable();
235 repoContentStatsDao.deleteRepositoryContentStatistics( repoStats.get( 1 ) );
236 repoContentStatsDaoControl.setVoidCallable();
238 repoContentStatsDaoControl.replay();
240 action.setRepoid( REPO_ID );
242 assertEquals( REPO_ID, action.getRepoid() );
244 auditLogsDaoControl.expectAndReturn( auditLogsDao.saveAuditLogs( new ArchivaAuditLogs() ), null );
245 auditLogsDaoControl.replay();
247 ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
248 populateRepository( repository );
249 repository.setLocation( new File( "target/test/location/new" ).getCanonicalPath() );
250 action.setRepository( repository );
251 String status = action.commit();
252 assertEquals( Action.SUCCESS, status );
253 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
255 roleManagerControl.verify();
256 archivaConfigurationControl.verify();
257 archivaDaoControl.verify();
258 repoContentStatsDaoControl.verify();
259 auditLogsDaoControl.verify();
262 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
263 ManagedRepositoryConfiguration actualRepository )
265 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
266 assertEquals( expectedRepository.getId(), actualRepository.getId() );
267 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
268 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
269 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
270 assertEquals( expectedRepository.getName(), actualRepository.getName() );
271 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
272 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
273 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
274 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
275 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
276 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
279 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
281 Configuration configuration = new Configuration();
282 configuration.addManagedRepository( repositoryConfiguration );
283 return configuration;
286 private ManagedRepositoryConfiguration createRepository()
289 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
291 populateRepository( r );
295 private void populateRepository( ManagedRepositoryConfiguration repository )
298 repository.setId( REPO_ID );
299 repository.setName( "repo name" );
300 repository.setLocation( location.getCanonicalPath() );
301 repository.setLayout( "default" );
302 repository.setRefreshCronExpression( "* 0/5 * * * ?" );
303 repository.setDaysOlder( 31 );
304 repository.setRetentionCount( 20 );
305 repository.setReleases( true );
306 repository.setSnapshots( true );
307 repository.setScanned( false );
308 repository.setDeleteReleasedSnapshots( true );
311 private List<RepositoryContentStatistics> createRepositoryContentStatisticsList()
313 List<RepositoryContentStatistics> repoStatsList = new ArrayList<RepositoryContentStatistics>();
315 repoStatsList.add( createRepositoryContentStatistics() );
316 repoStatsList.add( createRepositoryContentStatistics() );
318 return repoStatsList;
321 private RepositoryContentStatistics createRepositoryContentStatistics()
323 RepositoryContentStatistics repoStats = new RepositoryContentStatistics();
324 repoStats.setRepositoryId( REPO_ID );
325 repoStats.setDuration( 1000 );
326 repoStats.setTotalArtifactCount( 100 );
327 repoStats.setTotalSize( 10 );
328 repoStats.setTotalFileCount( 10 );
329 repoStats.setTotalProjectCount( 2 );
330 repoStats.setTotalGroupCount( 1 );
331 repoStats.setNewFileCount( 3 );
332 repoStats.setWhenGathered( new Date( System.currentTimeMillis() ) );