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.maven.archiva.configuration.ArchivaConfiguration;
29 import org.apache.maven.archiva.configuration.Configuration;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.security.ArchivaRoleConstants;
32 import org.codehaus.plexus.redback.role.RoleManager;
33 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
34 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
35 import org.codehaus.redback.integration.interceptor.SecureActionException;
36 import org.easymock.MockControl;
39 * EditManagedRepositoryActionTest
43 public class EditManagedRepositoryActionTest
44 extends PlexusInSpringTestCase
46 private EditManagedRepositoryAction action;
48 private RoleManager roleManager;
50 private MockControl roleManagerControl;
52 private MockControl archivaConfigurationControl;
54 private ArchivaConfiguration archivaConfiguration;
56 private static final String REPO_ID = "repo-ident";
58 private File location;
60 protected void setUp()
65 action = new EditManagedRepositoryAction();
67 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
68 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
69 action.setArchivaConfiguration( archivaConfiguration );
71 roleManagerControl = MockControl.createControl( RoleManager.class );
72 roleManager = (RoleManager) roleManagerControl.getMock();
73 action.setRoleManager( roleManager );
74 location = getTestFile( "target/test/location" );
77 public void testSecureActionBundle()
78 throws SecureActionException
80 archivaConfiguration.getConfiguration();
81 archivaConfigurationControl.setReturnValue( new Configuration() );
82 archivaConfigurationControl.replay();
85 SecureActionBundle bundle = action.getSecureActionBundle();
86 assertTrue( bundle.requiresAuthentication() );
87 assertEquals( 1, bundle.getAuthorizationTuples().size() );
90 public void testEditRepositoryInitialPage()
93 Configuration configuration = createConfigurationForEditing( createRepository() );
95 archivaConfiguration.getConfiguration();
96 archivaConfigurationControl.setReturnValue( configuration );
97 archivaConfigurationControl.replay();
99 action.setRepoid( REPO_ID );
102 assertEquals( REPO_ID, action.getRepoid() );
103 ManagedRepositoryConfiguration repository = action.getRepository();
104 assertNotNull( repository );
105 assertRepositoryEquals( repository, createRepository() );
107 String status = action.input();
108 assertEquals( Action.INPUT, status );
109 repository = action.getRepository();
110 assertRepositoryEquals( repository, createRepository() );
113 public void testEditRepository()
116 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
117 roleManagerControl.setReturnValue( false );
118 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
119 roleManagerControl.setVoidCallable();
120 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
121 roleManagerControl.setReturnValue( false );
122 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
123 roleManagerControl.setVoidCallable();
125 roleManagerControl.replay();
127 Configuration configuration = createConfigurationForEditing( createRepository() );
128 archivaConfiguration.getConfiguration();
129 archivaConfigurationControl.setReturnValue( configuration );
130 archivaConfigurationControl.setReturnValue( configuration );
131 archivaConfigurationControl.setReturnValue( configuration );
133 archivaConfiguration.save( configuration );
135 archivaConfigurationControl.replay();
137 action.setRepoid( REPO_ID );
139 assertEquals( REPO_ID, action.getRepoid() );
140 ManagedRepositoryConfiguration repository = action.getRepository();
141 populateRepository( repository );
142 repository.setName( "new repo name" );
144 String status = action.commit();
145 assertEquals( Action.SUCCESS, status );
147 ManagedRepositoryConfiguration newRepository = createRepository();
148 newRepository.setName( "new repo name" );
149 assertRepositoryEquals( repository, newRepository );
150 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
152 roleManagerControl.verify();
153 archivaConfigurationControl.verify();
156 public void testEditRepositoryLocationChanged()
159 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
160 roleManagerControl.setReturnValue( false );
161 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, REPO_ID );
162 roleManagerControl.setVoidCallable();
163 roleManager.templatedRoleExists( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
164 roleManagerControl.setReturnValue( false );
165 roleManager.createTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_MANAGER, REPO_ID );
166 roleManagerControl.setVoidCallable();
168 roleManagerControl.replay();
170 Configuration configuration = createConfigurationForEditing( createRepository() );
171 archivaConfiguration.getConfiguration();
172 archivaConfigurationControl.setReturnValue( configuration );
173 archivaConfigurationControl.setReturnValue( configuration );
174 archivaConfigurationControl.setReturnValue( configuration );
176 archivaConfiguration.save( configuration );
178 archivaConfigurationControl.replay();
180 MockControl repositoryStatisticsManagerControl = MockControl.createControl( RepositoryStatisticsManager.class );
181 RepositoryStatisticsManager repositoryStatisticsManager =
182 (RepositoryStatisticsManager) repositoryStatisticsManagerControl.getMock();
183 action.setRepositoryStatisticsManager( repositoryStatisticsManager );
185 repositoryStatisticsManager.deleteStatistics( REPO_ID );
187 repositoryStatisticsManagerControl.replay();
189 action.setRepoid( REPO_ID );
191 assertEquals( REPO_ID, action.getRepoid() );
193 ManagedRepositoryConfiguration repository = new ManagedRepositoryConfiguration();
194 populateRepository( repository );
195 repository.setLocation( new File( "target/test/location/new" ).getCanonicalPath() );
196 action.setRepository( repository );
197 String status = action.commit();
198 assertEquals( Action.SUCCESS, status );
199 assertEquals( Collections.singletonList( repository ), configuration.getManagedRepositories() );
201 roleManagerControl.verify();
202 archivaConfigurationControl.verify();
203 repositoryStatisticsManagerControl.verify();
206 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
207 ManagedRepositoryConfiguration actualRepository )
209 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
210 assertEquals( expectedRepository.getId(), actualRepository.getId() );
211 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
212 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
213 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
214 assertEquals( expectedRepository.getName(), actualRepository.getName() );
215 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
216 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
217 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
218 assertEquals( expectedRepository.isScanned(), actualRepository.isScanned() );
219 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
220 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
223 private Configuration createConfigurationForEditing( ManagedRepositoryConfiguration repositoryConfiguration )
225 Configuration configuration = new Configuration();
226 configuration.addManagedRepository( repositoryConfiguration );
227 return configuration;
230 private ManagedRepositoryConfiguration createRepository()
233 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
235 populateRepository( r );
239 private void populateRepository( ManagedRepositoryConfiguration repository )
242 repository.setId( REPO_ID );
243 repository.setName( "repo name" );
244 repository.setLocation( location.getCanonicalPath() );
245 repository.setLayout( "default" );
246 repository.setRefreshCronExpression( "* 0/5 * * * ?" );
247 repository.setDaysOlder( 31 );
248 repository.setRetentionCount( 20 );
249 repository.setReleases( true );
250 repository.setSnapshots( true );
251 repository.setScanned( false );
252 repository.setDeleteReleasedSnapshots( true );