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.xwork.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.codehaus.plexus.PlexusTestCase;
27 import org.codehaus.plexus.redback.role.RoleManager;
28 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
29 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionException;
30 import org.easymock.MockControl;
32 import java.util.Collections;
35 * Test the repositories action returns the correct data.
37 public class ConfigureRepositoryActionTest
38 extends PlexusTestCase
40 private ConfigureRepositoryAction action;
42 private RoleManager roleManager;
44 private MockControl roleManagerControl;
46 private MockControl archivaConfigurationControl;
48 private ArchivaConfiguration archivaConfiguration;
50 private static final String REPO_ID = "repo-ident";
52 protected void setUp()
57 // TODO: purely to quiet logging - shouldn't be needed
58 String appserverBase = getTestFile( "target/appserver-base" ).getAbsolutePath();
59 System.setProperty( "appserver.base", appserverBase );
60 action = (ConfigureRepositoryAction) lookup( Action.class.getName(), "configureRepositoryAction" );
62 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
63 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
64 action.setArchivaConfiguration( archivaConfiguration );
66 roleManagerControl = MockControl.createControl( RoleManager.class );
67 roleManager = (RoleManager) roleManagerControl.getMock();
68 action.setRoleManager( roleManager );
71 public void testAddRepositoryInitialPage()
74 archivaConfiguration.getConfiguration();
75 archivaConfigurationControl.setReturnValue( new Configuration() );
76 archivaConfigurationControl.replay();
79 assertNull( action.getRepoid() );
80 assertNull( action.getMode() );
81 AdminRepositoryConfiguration configuration = action.getRepository();
82 assertNotNull( configuration );
83 assertNull( configuration.getId() );
84 // check all booleans are false
85 assertFalse( configuration.isDeleteReleasedSnapshots() );
86 assertFalse( configuration.isIndexed() );
87 assertFalse( configuration.isReleases() );
88 assertFalse( configuration.isSnapshots() );
90 String status = action.add();
91 assertEquals( Action.INPUT, status );
94 assertFalse( configuration.isDeleteReleasedSnapshots() );
95 assertTrue( configuration.isIndexed() );
96 assertTrue( configuration.isReleases() );
97 assertFalse( configuration.isSnapshots() );
100 public void testAddRepository()
103 // TODO: should be in the business model
104 roleManager.createTemplatedRole( "archiva-repository-manager", REPO_ID );
105 roleManager.createTemplatedRole( "archiva-repository-observer", REPO_ID );
107 roleManagerControl.replay();
109 Configuration configuration = new Configuration();
110 archivaConfiguration.getConfiguration();
111 archivaConfigurationControl.setReturnValue( configuration );
112 archivaConfiguration.getConfiguration();
113 archivaConfigurationControl.setReturnValue( configuration );
115 archivaConfiguration.save( configuration );
117 archivaConfigurationControl.replay();
120 action.setMode( "add" );
121 AdminRepositoryConfiguration repository = action.getRepository();
122 populateRepository( repository );
124 String status = action.save();
125 assertEquals( Action.SUCCESS, status );
127 assertEquals( configuration.getManagedRepositories(), Collections.singletonList( repository ) );
129 roleManagerControl.verify();
130 archivaConfigurationControl.verify();
133 public void testEditRepositoryInitialPage()
136 Configuration configuration = createConfigurationForEditing();
138 archivaConfiguration.getConfiguration();
139 archivaConfigurationControl.setReturnValue( configuration );
140 archivaConfigurationControl.replay();
142 action.setRepoid( REPO_ID );
145 assertEquals( REPO_ID, action.getRepoid() );
146 assertNull( action.getMode() );
147 AdminRepositoryConfiguration repository = action.getRepository();
148 assertNotNull( repository );
149 assertRepositoryEquals( repository, createRepository() );
151 String status = action.edit();
152 assertEquals( Action.INPUT, status );
153 repository = action.getRepository();
154 assertRepositoryEquals( repository, createRepository() );
157 private void assertRepositoryEquals( ManagedRepositoryConfiguration expectedRepository,
158 ManagedRepositoryConfiguration actualRepository )
160 assertEquals( expectedRepository.getDaysOlder(), actualRepository.getDaysOlder() );
161 assertEquals( expectedRepository.getId(), actualRepository.getId() );
162 assertEquals( expectedRepository.getIndexDir(), actualRepository.getIndexDir() );
163 assertEquals( expectedRepository.getLayout(), actualRepository.getLayout() );
164 assertEquals( expectedRepository.getLocation(), actualRepository.getLocation() );
165 assertEquals( expectedRepository.getName(), actualRepository.getName() );
166 assertEquals( expectedRepository.getRefreshCronExpression(), actualRepository.getRefreshCronExpression() );
167 assertEquals( expectedRepository.getRetentionCount(), actualRepository.getRetentionCount() );
168 assertEquals( expectedRepository.isDeleteReleasedSnapshots(), actualRepository.isDeleteReleasedSnapshots() );
169 assertEquals( expectedRepository.isIndexed(), actualRepository.isIndexed() );
170 assertEquals( expectedRepository.isReleases(), actualRepository.isReleases() );
171 assertEquals( expectedRepository.isSnapshots(), actualRepository.isSnapshots() );
174 private static Configuration createConfigurationForEditing()
176 Configuration configuration = new Configuration();
177 ManagedRepositoryConfiguration r = createRepository();
178 configuration.addManagedRepository( r );
179 return configuration;
182 private static ManagedRepositoryConfiguration createRepository()
184 ManagedRepositoryConfiguration r = new ManagedRepositoryConfiguration();
186 populateRepository( r );
190 public void testEditRepository()
193 // TODO: should be in the business model
194 roleManager.createTemplatedRole( "archiva-repository-manager", REPO_ID );
195 roleManager.createTemplatedRole( "archiva-repository-observer", REPO_ID );
197 roleManagerControl.replay();
199 Configuration configuration = createConfigurationForEditing();
200 archivaConfiguration.getConfiguration();
201 archivaConfigurationControl.setReturnValue( configuration );
202 archivaConfiguration.getConfiguration();
203 archivaConfigurationControl.setReturnValue( configuration );
205 archivaConfiguration.save( configuration );
207 archivaConfigurationControl.replay();
210 action.setMode( "edit" );
211 AdminRepositoryConfiguration repository = action.getRepository();
212 populateRepository( repository );
213 repository.setName( "new repo name" );
215 String status = action.save();
216 assertEquals( Action.SUCCESS, status );
218 ManagedRepositoryConfiguration newRepository = createRepository();
219 newRepository.setName( "new repo name" );
220 assertRepositoryEquals( repository, newRepository );
221 assertEquals( configuration.getManagedRepositories(), Collections.singletonList( repository ) );
223 roleManagerControl.verify();
224 archivaConfigurationControl.verify();
227 private static void populateRepository( ManagedRepositoryConfiguration repository )
229 repository.setId( REPO_ID );
230 repository.setName( "repo name" );
231 repository.setLocation( getTestFile( "location" ).getAbsolutePath() );
232 repository.setLayout( "default" );
233 repository.setRefreshCronExpression( "* 0/5 * * * ?" );
234 repository.setDaysOlder( 31 );
235 repository.setRetentionCount( 20 );
236 repository.setReleases( true );
237 repository.setSnapshots( true );
238 repository.setIndexed( true );
239 repository.setDeleteReleasedSnapshots( true );
242 // TODO: test errors during add, other actions
244 public void testSecureActionBundle()
245 throws SecureActionException
247 archivaConfiguration.getConfiguration();
248 archivaConfigurationControl.setReturnValue( new Configuration() );
249 archivaConfigurationControl.replay();
252 SecureActionBundle bundle = action.getSecureActionBundle();
253 assertTrue( bundle.requiresAuthentication() );
254 assertEquals( 1, bundle.getAuthorizationTuples().size() );