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;
23 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
24 import org.apache.maven.archiva.configuration.Configuration;
25 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
28 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
29 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
30 import org.apache.maven.archiva.policies.ChecksumPolicy;
31 import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
32 import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
33 import org.apache.maven.archiva.policies.ReleasesPolicy;
34 import org.apache.maven.archiva.policies.SnapshotsPolicy;
35 import org.apache.maven.archiva.web.action.AbstractWebworkTestCase;
36 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
37 import org.codehaus.plexus.registry.RegistryException;
38 import org.easymock.MockControl;
40 import java.util.List;
44 * EditProxyConnectorActionTest
46 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
49 public class EditProxyConnectorActionTest
50 extends AbstractWebworkTestCase
52 private static final String TEST_TARGET_ID = "central";
54 private static final String TEST_SOURCE_ID = "corporate";
56 private EditProxyConnectorAction action;
58 private MockControl archivaConfigurationControl;
60 private ArchivaConfiguration archivaConfiguration;
62 public void testAddBlackListPattern()
65 expectConfigurationRequests( 7 );
66 archivaConfigurationControl.replay();
69 action.setSource( TEST_SOURCE_ID );
70 action.setTarget( TEST_TARGET_ID );
72 ProxyConnectorConfiguration connector = action.getConnector();
73 assertInitialProxyConnector( connector );
75 // Perform Test w/no values.
77 String status = action.addBlackListPattern();
78 assertEquals( Action.INPUT, status );
80 // Should have returned an error, with no blacklist pattern added.
81 assertHasErrors( action );
82 assertEquals( 0, connector.getBlackListPatterns().size() );
84 // Try again, but now with a pattern to add.
85 action.setBlackListPattern( "**/*-javadoc.jar" );
87 status = action.addBlackListPattern();
88 assertEquals( Action.INPUT, status );
90 // Should have no error, and 1 blacklist pattern added.
91 assertNoErrors( action );
92 assertEquals( 1, connector.getBlackListPatterns().size() );
95 public void testAddProperty()
98 expectConfigurationRequests( 7 );
99 archivaConfigurationControl.replay();
102 action.setSource( TEST_SOURCE_ID );
103 action.setTarget( TEST_TARGET_ID );
105 ProxyConnectorConfiguration connector = action.getConnector();
106 assertInitialProxyConnector( connector );
108 // Perform Test w/no values.
109 preRequest( action );
110 String status = action.addProperty();
111 assertEquals( Action.INPUT, status );
113 // Should have returned an error, with no property pattern added.
114 assertHasErrors( action );
115 assertEquals( 0, connector.getProperties().size() );
117 // Try again, but now with a property key/value to add.
118 action.setPropertyKey( "eat-a" );
119 action.setPropertyValue( "gramov-a-bits" );
120 preRequest( action );
121 status = action.addProperty();
122 assertEquals( Action.INPUT, status );
124 // Should have no error, and 1 property added.
125 assertNoErrors( action );
126 assertEquals( 1, connector.getProperties().size() );
127 assertEquals( "gramov-a-bits", connector.getProperties().get( "eat-a" ) );
130 public void testAddWhiteListPattern()
133 expectConfigurationRequests( 7 );
134 archivaConfigurationControl.replay();
137 action.setSource( TEST_SOURCE_ID );
138 action.setTarget( TEST_TARGET_ID );
140 ProxyConnectorConfiguration connector = action.getConnector();
141 assertInitialProxyConnector( connector );
143 // Perform Test w/no values.
144 preRequest( action );
145 String status = action.addWhiteListPattern();
146 assertEquals( Action.INPUT, status );
148 // Should have returned an error, with no whitelist pattern added.
149 assertHasErrors( action );
150 assertEquals( 0, connector.getWhiteListPatterns().size() );
152 // Try again, but now with a pattern to add.
153 action.setWhiteListPattern( "**/*.jar" );
154 preRequest( action );
155 status = action.addWhiteListPattern();
156 assertEquals( Action.INPUT, status );
158 // Should have no error, and 1 whitelist pattern added.
159 assertNoErrors( action );
160 assertEquals( 1, connector.getWhiteListPatterns().size() );
163 public void testEditProxyConnectorCommit()
166 expectConfigurationRequests( 7 );
167 archivaConfigurationControl.replay();
170 action.setSource( TEST_SOURCE_ID );
171 action.setTarget( TEST_TARGET_ID );
173 ProxyConnectorConfiguration connector = action.getConnector();
174 assertInitialProxyConnector( connector );
175 // forms will use an array
176 connector.getProperties().put( "eat-a", new String[] { "gramov-a-bits" } );
178 // Create the input screen.
179 assertRequestStatus( action, Action.SUCCESS, "commit" );
180 assertNoErrors( action );
182 // Test configuration.
183 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
184 assertNotNull( proxyConfigs );
185 assertEquals( 1, proxyConfigs.size() );
187 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
189 assertNotNull( actualConnector );
190 // The use of "(direct connection)" should result in a proxyId which is <null>.
191 assertNull( actualConnector.getProxyId() );
192 assertEquals( "corporate", actualConnector.getSourceRepoId() );
193 assertEquals( "central", actualConnector.getTargetRepoId() );
197 public void testEditProxyConnectorInitialPage()
200 expectConfigurationRequests( 3 );
201 archivaConfigurationControl.replay();
203 action.setSource( TEST_SOURCE_ID );
204 action.setTarget( TEST_TARGET_ID );
206 ProxyConnectorConfiguration connector = action.getConnector();
207 assertInitialProxyConnector( connector );
209 String status = action.input();
210 assertEquals( Action.INPUT, status );
213 public void testRemoveBlackListPattern()
216 expectConfigurationRequests( 7 );
217 archivaConfigurationControl.replay();
220 action.setSource( TEST_SOURCE_ID );
221 action.setTarget( TEST_TARGET_ID );
223 ProxyConnectorConfiguration connector = action.getConnector();
224 assertInitialProxyConnector( connector );
226 // Add some arbitrary blacklist patterns.
227 connector.addBlackListPattern( "**/*-javadoc.jar" );
228 connector.addBlackListPattern( "**/*.war" );
230 // Perform Test w/no pattern value.
231 preRequest( action );
232 String status = action.removeBlackListPattern();
233 assertEquals( Action.INPUT, status );
235 // Should have returned an error, with no blacklist pattern removed.
236 assertHasErrors( action );
237 assertEquals( 2, connector.getBlackListPatterns().size() );
239 // Perform test w/invalid (non-existant) pattern value to remove.
240 preRequest( action );
241 action.setPattern( "**/*oops*" );
242 status = action.removeBlackListPattern();
243 assertEquals( Action.INPUT, status );
245 // Should have returned an error, with no blacklist pattern removed.
246 assertHasErrors( action );
247 assertEquals( 2, connector.getBlackListPatterns().size() );
249 // Try again, but now with a valid pattern to remove.
250 action.setPattern( "**/*-javadoc.jar" );
251 preRequest( action );
252 status = action.removeBlackListPattern();
253 assertEquals( Action.INPUT, status );
255 // Should have no error, and 1 blacklist pattern left.
256 assertNoErrors( action );
257 assertEquals( 1, connector.getBlackListPatterns().size() );
258 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
261 public void testRemoveProperty()
264 expectConfigurationRequests( 7 );
265 archivaConfigurationControl.replay();
268 action.setSource( TEST_SOURCE_ID );
269 action.setTarget( TEST_TARGET_ID );
271 ProxyConnectorConfiguration connector = action.getConnector();
272 assertInitialProxyConnector( connector );
274 // Add some arbitrary properties.
275 connector.addProperty( "username", "general-tso" );
276 connector.addProperty( "password", "chicken" );
278 // Perform Test w/no property key.
279 preRequest( action );
280 String status = action.removeProperty();
281 assertEquals( Action.INPUT, status );
283 // Should have returned an error, with no properties removed.
284 assertHasErrors( action );
285 assertEquals( 2, connector.getProperties().size() );
287 // Perform test w/invalid (non-existant) property key to remove.
288 preRequest( action );
289 action.setPropertyKey( "slurm" );
290 status = action.removeProperty();
291 assertEquals( Action.INPUT, status );
293 // Should have returned an error, with no properties removed.
294 assertHasErrors( action );
295 assertEquals( 2, connector.getProperties().size() );
297 // Try again, but now with a valid property to remove.
298 preRequest( action );
299 action.setPropertyKey( "password" );
300 status = action.removeProperty();
301 assertEquals( Action.INPUT, status );
303 // Should have no error, and 1 property left.
304 assertNoErrors( action );
305 assertEquals( 1, connector.getProperties().size() );
306 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
309 public void testRemoveWhiteListPattern()
312 expectConfigurationRequests( 7 );
313 archivaConfigurationControl.replay();
316 action.setSource( TEST_SOURCE_ID );
317 action.setTarget( TEST_TARGET_ID );
319 ProxyConnectorConfiguration connector = action.getConnector();
320 assertInitialProxyConnector( connector );
322 // Add some arbitrary whitelist patterns.
323 connector.addWhiteListPattern( "javax/**/*" );
324 connector.addWhiteListPattern( "com/sun/**/*" );
326 // Perform Test w/no pattern value.
327 preRequest( action );
328 String status = action.removeWhiteListPattern();
329 assertEquals( Action.INPUT, status );
331 // Should have returned an error, with no whitelist pattern removed.
332 assertHasErrors( action );
333 assertEquals( 2, connector.getWhiteListPatterns().size() );
335 // Perform test w/invalid (non-existant) pattern value to remove.
336 preRequest( action );
337 action.setPattern( "**/*oops*" );
338 status = action.removeWhiteListPattern();
339 assertEquals( Action.INPUT, status );
341 // Should have returned an error, with no whitelist pattern removed.
342 assertHasErrors( action );
343 assertEquals( 2, connector.getWhiteListPatterns().size() );
345 // Try again, but now with a valid pattern to remove.
346 action.setPattern( "com/sun/**/*" );
347 preRequest( action );
348 status = action.removeWhiteListPattern();
349 assertEquals( Action.INPUT, status );
351 // Should have no error, and 1 whitelist pattern left.
352 assertNoErrors( action );
353 assertEquals( 1, connector.getWhiteListPatterns().size() );
354 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
357 public void testSecureActionBundle()
360 archivaConfigurationControl.replay();
363 SecureActionBundle bundle = action.getSecureActionBundle();
364 assertTrue( bundle.requiresAuthentication() );
365 assertEquals( 1, bundle.getAuthorizationTuples().size() );
368 private void assertInitialProxyConnector( ProxyConnectorConfiguration connector )
370 assertNotNull( connector );
371 assertNotNull( connector.getBlackListPatterns() );
372 assertNotNull( connector.getWhiteListPatterns() );
373 assertNotNull( connector.getProperties() );
375 assertEquals( TEST_SOURCE_ID, connector.getSourceRepoId() );
376 assertEquals( TEST_TARGET_ID, connector.getTargetRepoId() );
379 private Configuration createInitialConfiguration()
381 Configuration config = new Configuration();
383 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
384 managedRepo.setId( TEST_SOURCE_ID );
385 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
386 managedRepo.setReleases( true );
388 config.addManagedRepository( managedRepo );
390 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
391 remoteRepo.setId( TEST_TARGET_ID );
392 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
394 config.addRemoteRepository( remoteRepo );
396 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
397 connector.setSourceRepoId( TEST_SOURCE_ID );
398 connector.setTargetRepoId( TEST_TARGET_ID );
400 // TODO: Set these options programatically via list of available policies.
401 Map<String, String> policies = connector.getPolicies();
402 policies.put( "releases", new ReleasesPolicy().getDefaultOption() );
403 policies.put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
404 policies.put( "checksum", new ChecksumPolicy().getDefaultOption() );
405 policies.put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
406 policies.put( "propagate-errors", new PropagateErrorsDownloadPolicy().getDefaultOption() );
407 policies.put( "propagate-errors-on-update", new PropagateErrorsOnUpdateDownloadPolicy().getDefaultOption() );
409 config.addProxyConnector( connector );
414 private void expectConfigurationRequests( int requestConfigCount )
415 throws RegistryException, IndeterminateConfigurationException
417 Configuration config = createInitialConfiguration();
419 for ( int i = 0; i < requestConfigCount; i++ )
421 archivaConfiguration.getConfiguration();
422 archivaConfigurationControl.setReturnValue( config );
425 archivaConfiguration.save( config );
429 protected void setUp()
434 action = (EditProxyConnectorAction) lookup( Action.class.getName(), "editProxyConnectorAction" );
436 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
437 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
438 action.setArchivaConfiguration( archivaConfiguration );
440 /* Configuration will be requested at least 3 times. */
441 for ( int i = 0; i < 3; i++ )
443 archivaConfiguration.getConfiguration();
444 archivaConfigurationControl.setReturnValue( new Configuration() );