1 package org.apache.maven.archiva.web.action.admin.connectors.proxy;
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;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
29 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
30 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
31 import org.apache.maven.archiva.policies.ChecksumPolicy;
32 import org.apache.maven.archiva.policies.ReleasesPolicy;
33 import org.apache.maven.archiva.policies.SnapshotsPolicy;
34 import org.apache.maven.archiva.web.action.AbstractWebworkTestCase;
35 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
36 import org.codehaus.plexus.registry.RegistryException;
37 import org.easymock.MockControl;
39 import java.util.List;
42 * AddProxyConnectorActionTest
44 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
47 public class AddProxyConnectorActionTest
48 extends AbstractWebworkTestCase
50 private AddProxyConnectorAction action;
52 private MockControl archivaConfigurationControl;
54 private ArchivaConfiguration archivaConfiguration;
56 public void testAddBlackListPattern()
59 expectConfigurationRequests( 7 );
60 archivaConfigurationControl.replay();
64 ProxyConnectorConfiguration connector = action.getConnector();
65 populateProxyConnector( connector );
67 // Perform Test w/no values.
69 String status = action.addBlackListPattern();
70 assertEquals( Action.INPUT, status );
72 // Should have returned an error, with no blacklist pattern added.
73 assertHasErrors( action );
74 assertEquals( 0, connector.getBlackListPatterns().size() );
76 // Try again, but now with a pattern to add.
77 action.setBlackListPattern( "**/*-javadoc.jar" );
79 status = action.addBlackListPattern();
80 assertEquals( Action.INPUT, status );
82 // Should have no error, and 1 blacklist pattern added.
83 assertNoErrors( action );
84 assertEquals( 1, connector.getBlackListPatterns().size() );
87 public void testAddProperty()
90 expectConfigurationRequests( 7 );
91 archivaConfigurationControl.replay();
95 ProxyConnectorConfiguration connector = action.getConnector();
96 populateProxyConnector( connector );
98 // Perform Test w/no values.
100 String status = action.addProperty();
101 assertEquals( Action.INPUT, status );
103 // Should have returned an error, with no property pattern added.
104 assertHasErrors( action );
105 assertEquals( 0, connector.getProperties().size() );
107 // Try again, but now with a property key/value to add.
108 action.setPropertyKey( "eat-a" );
109 action.setPropertyValue( "gramov-a-bits" );
110 preRequest( action );
111 status = action.addProperty();
112 assertEquals( Action.INPUT, status );
114 // Should have no error, and 1 property added.
115 assertNoErrors( action );
116 assertEquals( 1, connector.getProperties().size() );
119 public void testAddProxyConnectorCommit()
122 expectConfigurationRequests( 7 );
123 archivaConfigurationControl.replay();
127 ProxyConnectorConfiguration connector = action.getConnector();
128 populateProxyConnector( connector );
130 // Create the input screen.
131 assertRequestStatus( action, Action.SUCCESS, "commit" );
132 assertNoErrors( action );
134 // Test configuration.
135 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
136 assertNotNull( proxyConfigs );
137 assertEquals( 1, proxyConfigs.size() );
139 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
141 assertNotNull( actualConnector );
142 // The use of "(direct connection)" should result in a proxyId which is <null>.
143 assertNull( actualConnector.getProxyId() );
144 assertEquals( "corporate", actualConnector.getSourceRepoId() );
145 assertEquals( "central", actualConnector.getTargetRepoId() );
148 public void testAddProxyConnectorInitialPage()
151 expectConfigurationRequests( 3 );
152 archivaConfigurationControl.replay();
155 ProxyConnectorConfiguration configuration = action.getConnector();
156 assertNotNull( configuration );
157 assertNull( configuration.getProxyId() );
158 assertNull( configuration.getSourceRepoId() );
159 assertNull( configuration.getTargetRepoId() );
160 assertTrue( configuration.getPolicies().isEmpty() );
161 assertTrue( configuration.getProperties().isEmpty() );
162 assertTrue( configuration.getBlackListPatterns().isEmpty() );
163 assertTrue( configuration.getWhiteListPatterns().isEmpty() );
165 String status = action.input();
166 assertEquals( Action.INPUT, status );
169 public void testAddWhiteListPattern()
172 expectConfigurationRequests( 7 );
173 archivaConfigurationControl.replay();
177 ProxyConnectorConfiguration connector = action.getConnector();
178 populateProxyConnector( connector );
180 // Perform Test w/no values.
181 preRequest( action );
182 String status = action.addWhiteListPattern();
183 assertEquals( Action.INPUT, status );
185 // Should have returned an error, with no whitelist pattern added.
186 assertHasErrors( action );
187 assertEquals( 0, connector.getWhiteListPatterns().size() );
189 // Try again, but now with a pattern to add.
190 action.setWhiteListPattern( "**/*.jar" );
191 preRequest( action );
192 status = action.addWhiteListPattern();
193 assertEquals( Action.INPUT, status );
195 // Should have no error, and 1 whitelist pattern added.
196 assertNoErrors( action );
197 assertEquals( 1, connector.getWhiteListPatterns().size() );
200 public void testRemoveBlackListPattern()
203 expectConfigurationRequests( 7 );
204 archivaConfigurationControl.replay();
208 ProxyConnectorConfiguration connector = action.getConnector();
209 populateProxyConnector( connector );
211 // Add some arbitrary blacklist patterns.
212 connector.addBlackListPattern( "**/*-javadoc.jar" );
213 connector.addBlackListPattern( "**/*.war" );
215 // Perform Test w/no pattern value.
216 preRequest( action );
217 String status = action.removeBlackListPattern();
218 assertEquals( Action.INPUT, status );
220 // Should have returned an error, with no blacklist pattern removed.
221 assertHasErrors( action );
222 assertEquals( 2, connector.getBlackListPatterns().size() );
224 // Perform test w/invalid (non-existant) pattern value to remove.
225 preRequest( action );
226 action.setPattern( "**/*oops*" );
227 status = action.removeBlackListPattern();
228 assertEquals( Action.INPUT, status );
230 // Should have returned an error, with no blacklist pattern removed.
231 assertHasErrors( action );
232 assertEquals( 2, connector.getBlackListPatterns().size() );
234 // Try again, but now with a valid pattern to remove.
235 action.setPattern( "**/*-javadoc.jar" );
236 preRequest( action );
237 status = action.removeBlackListPattern();
238 assertEquals( Action.INPUT, status );
240 // Should have no error, and 1 blacklist pattern left.
241 assertNoErrors( action );
242 assertEquals( 1, connector.getBlackListPatterns().size() );
243 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
246 public void testRemoveProperty()
249 expectConfigurationRequests( 7 );
250 archivaConfigurationControl.replay();
254 ProxyConnectorConfiguration connector = action.getConnector();
255 populateProxyConnector( connector );
257 // Add some arbitrary properties.
258 connector.addProperty( "username", "general-tso" );
259 connector.addProperty( "password", "chicken" );
261 // Perform Test w/no property key.
262 preRequest( action );
263 String status = action.removeProperty();
264 assertEquals( Action.INPUT, status );
266 // Should have returned an error, with no properties removed.
267 assertHasErrors( action );
268 assertEquals( 2, connector.getProperties().size() );
270 // Perform test w/invalid (non-existant) property key to remove.
271 preRequest( action );
272 action.setPropertyKey( "slurm" );
273 status = action.removeProperty();
274 assertEquals( Action.INPUT, status );
276 // Should have returned an error, with no properties removed.
277 assertHasErrors( action );
278 assertEquals( 2, connector.getProperties().size() );
280 // Try again, but now with a valid property to remove.
281 preRequest( action );
282 action.setPropertyKey( "password" );
283 status = action.removeProperty();
284 assertEquals( Action.INPUT, status );
286 // Should have no error, and 1 property left.
287 assertNoErrors( action );
288 assertEquals( 1, connector.getProperties().size() );
289 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
292 public void testRemoveWhiteListPattern()
295 expectConfigurationRequests( 7 );
296 archivaConfigurationControl.replay();
300 ProxyConnectorConfiguration connector = action.getConnector();
301 populateProxyConnector( connector );
303 // Add some arbitrary whitelist patterns.
304 connector.addWhiteListPattern( "javax/**/*" );
305 connector.addWhiteListPattern( "com/sun/**/*" );
307 // Perform Test w/no pattern value.
308 preRequest( action );
309 String status = action.removeWhiteListPattern();
310 assertEquals( Action.INPUT, status );
312 // Should have returned an error, with no whitelist pattern removed.
313 assertHasErrors( action );
314 assertEquals( 2, connector.getWhiteListPatterns().size() );
316 // Perform test w/invalid (non-existant) pattern value to remove.
317 preRequest( action );
318 action.setPattern( "**/*oops*" );
319 status = action.removeWhiteListPattern();
320 assertEquals( Action.INPUT, status );
322 // Should have returned an error, with no whitelist pattern removed.
323 assertHasErrors( action );
324 assertEquals( 2, connector.getWhiteListPatterns().size() );
326 // Try again, but now with a valid pattern to remove.
327 action.setPattern( "com/sun/**/*" );
328 preRequest( action );
329 status = action.removeWhiteListPattern();
330 assertEquals( Action.INPUT, status );
332 // Should have no error, and 1 whitelist pattern left.
333 assertNoErrors( action );
334 assertEquals( 1, connector.getWhiteListPatterns().size() );
335 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
338 public void testSecureActionBundle()
341 expectConfigurationRequests( 3 );
342 archivaConfigurationControl.replay();
345 SecureActionBundle bundle = action.getSecureActionBundle();
346 assertTrue( bundle.requiresAuthentication() );
347 assertEquals( 1, bundle.getAuthorizationTuples().size() );
350 private Configuration createInitialConfiguration()
352 Configuration config = new Configuration();
354 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
355 managedRepo.setId( "corporate" );
356 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
357 managedRepo.setReleases( true );
359 config.addManagedRepository( managedRepo );
361 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
362 remoteRepo.setId( "central" );
363 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
365 config.addRemoteRepository( remoteRepo );
370 private void expectConfigurationRequests( int requestConfigCount )
371 throws RegistryException, IndeterminateConfigurationException
373 Configuration config = createInitialConfiguration();
375 for ( int i = 0; i < requestConfigCount; i++ )
377 archivaConfiguration.getConfiguration();
378 archivaConfigurationControl.setReturnValue( config );
381 archivaConfiguration.save( config );
384 private void populateProxyConnector( ProxyConnectorConfiguration connector )
386 connector.setProxyId( AbstractProxyConnectorFormAction.DIRECT_CONNECTION );
387 connector.setSourceRepoId( "corporate" );
388 connector.setTargetRepoId( "central" );
390 // TODO: Set these options programatically via list of available policies.
391 connector.getPolicies().put( "releases", new ReleasesPolicy().getDefaultOption() );
392 connector.getPolicies().put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
393 connector.getPolicies().put( "checksum", new ChecksumPolicy().getDefaultOption() );
394 connector.getPolicies().put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
398 protected void setUp()
403 action = (AddProxyConnectorAction) lookup( Action.class.getName(), "addProxyConnectorAction" );
405 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
406 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
407 action.setArchivaConfiguration( archivaConfiguration );