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.xwork2.Action;
23 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
24 import org.apache.archiva.admin.repository.proxyconnector.DefaultProxyConnectorAdmin;
25 import org.apache.archiva.admin.repository.proxyconnector.ProxyConnector;
26 import org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.Configuration;
29 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
33 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
34 import org.apache.maven.archiva.policies.ChecksumPolicy;
35 import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
36 import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
37 import org.apache.maven.archiva.policies.ReleasesPolicy;
38 import org.apache.maven.archiva.policies.SnapshotsPolicy;
39 import org.apache.maven.archiva.web.action.AbstractWebworkTestCase;
40 import org.codehaus.plexus.registry.RegistryException;
41 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
42 import org.easymock.MockControl;
44 import java.util.List;
48 * EditProxyConnectorActionTest
52 public class EditProxyConnectorActionTest
53 extends AbstractWebworkTestCase
55 private static final String TEST_TARGET_ID = "central";
57 private static final String TEST_SOURCE_ID = "corporate";
59 private EditProxyConnectorAction action;
61 private MockControl archivaConfigurationControl;
63 private ArchivaConfiguration archivaConfiguration;
66 protected void setUp()
71 //action = (EditProxyConnectorAction) lookup( Action.class.getName(), "editProxyConnectorAction" );
72 action = (EditProxyConnectorAction) getActionProxy( "/admin/editProxyConnector.action" ).getAction();
74 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
75 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
76 action.setArchivaConfiguration( archivaConfiguration );
78 /* Configuration will be requested at least 3 times. */
79 archivaConfiguration.getConfiguration();
80 archivaConfigurationControl.setReturnValue( new Configuration(), 3 );
82 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
83 archivaConfiguration );
84 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
85 archivaConfiguration );
86 ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration(
87 archivaConfiguration );
90 private void expectConfigurationRequests( int requestConfigCount )
91 throws RegistryException, IndeterminateConfigurationException
93 expectConfigurationRequests( requestConfigCount, 1 );
96 private void expectConfigurationRequests( int requestConfigCount, int saveRequestCount )
97 throws RegistryException, IndeterminateConfigurationException
99 Configuration config = createInitialConfiguration();
101 archivaConfiguration.getConfiguration();
102 archivaConfigurationControl.setReturnValue( config, requestConfigCount );
104 for ( int i = 0; i <= saveRequestCount; i++ )
106 archivaConfiguration.save( config );
110 public void testAddBlackListPattern()
113 expectConfigurationRequests( 7 );
114 archivaConfigurationControl.replay();
117 action.setSource( TEST_SOURCE_ID );
118 action.setTarget( TEST_TARGET_ID );
120 ProxyConnector connector = action.getConnector();
121 assertInitialProxyConnector( connector );
123 // Perform Test w/no values.
124 preRequest( action );
125 String status = action.addBlackListPattern();
126 assertEquals( Action.INPUT, status );
128 // Should have returned an error, with no blacklist pattern added.
129 assertHasErrors( action );
130 assertEquals( 0, connector.getBlackListPatterns().size() );
132 // Try again, but now with a pattern to add.
133 action.setBlackListPattern( "**/*-javadoc.jar" );
134 preRequest( action );
135 status = action.addBlackListPattern();
136 assertEquals( Action.INPUT, status );
138 // Should have no error, and 1 blacklist pattern added.
139 assertNoErrors( action );
140 assertEquals( 1, connector.getBlackListPatterns().size() );
143 public void testAddProperty()
146 expectConfigurationRequests( 7 );
147 archivaConfigurationControl.replay();
150 action.setSource( TEST_SOURCE_ID );
151 action.setTarget( TEST_TARGET_ID );
153 ProxyConnector connector = action.getConnector();
154 assertInitialProxyConnector( connector );
156 // Perform Test w/no values.
157 preRequest( action );
158 String status = action.addProperty();
159 assertEquals( Action.INPUT, status );
161 // Should have returned an error, with no property pattern added.
162 assertHasErrors( action );
163 assertEquals( 0, connector.getProperties().size() );
165 // Try again, but now with a property key/value to add.
166 action.setPropertyKey( "eat-a" );
167 action.setPropertyValue( "gramov-a-bits" );
168 preRequest( action );
169 status = action.addProperty();
170 assertEquals( Action.INPUT, status );
172 // Should have no error, and 1 property added.
173 assertNoErrors( action );
174 assertEquals( 1, connector.getProperties().size() );
175 assertEquals( "gramov-a-bits", connector.getProperties().get( "eat-a" ) );
178 public void testAddWhiteListPattern()
181 expectConfigurationRequests( 7 );
182 archivaConfigurationControl.replay();
185 action.setSource( TEST_SOURCE_ID );
186 action.setTarget( TEST_TARGET_ID );
188 ProxyConnector connector = action.getConnector();
189 assertInitialProxyConnector( connector );
191 // Perform Test w/no values.
192 preRequest( action );
193 String status = action.addWhiteListPattern();
194 assertEquals( Action.INPUT, status );
196 // Should have returned an error, with no whitelist pattern added.
197 assertHasErrors( action );
198 assertEquals( 0, connector.getWhiteListPatterns().size() );
200 // Try again, but now with a pattern to add.
201 action.setWhiteListPattern( "**/*.jar" );
202 preRequest( action );
203 status = action.addWhiteListPattern();
204 assertEquals( Action.INPUT, status );
206 // Should have no error, and 1 whitelist pattern added.
207 assertNoErrors( action );
208 assertEquals( 1, connector.getWhiteListPatterns().size() );
211 @SuppressWarnings( "unchecked" )
212 public void testEditProxyConnectorCommit()
215 expectConfigurationRequests( 9, 2 );
216 archivaConfigurationControl.replay();
219 action.setSource( TEST_SOURCE_ID );
220 action.setTarget( TEST_TARGET_ID );
222 ProxyConnector connector = action.getConnector();
223 assertInitialProxyConnector( connector );
224 // forms will use an array
225 //connector.getProperties().put( "eat-a", new String[]{ "gramov-a-bits" } );
226 // FIXME check the array mode
227 connector.getProperties().put( "eat-a", "gramov-a-bits" );
229 // Create the input screen.
230 assertRequestStatus( action, Action.SUCCESS, "commit" );
231 assertNoErrors( action );
233 // Test configuration.
234 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
235 assertNotNull( proxyConfigs );
236 assertEquals( 1, proxyConfigs.size() );
238 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
240 assertNotNull( actualConnector );
241 // The use of "(direct connection)" should result in a proxyId which is <null>.
242 assertNull( actualConnector.getProxyId() );
243 assertEquals( "corporate", actualConnector.getSourceRepoId() );
244 assertEquals( "central", actualConnector.getTargetRepoId() );
248 public void testEditProxyConnectorInitialPage()
251 expectConfigurationRequests( 3 );
252 archivaConfigurationControl.replay();
254 action.setSource( TEST_SOURCE_ID );
255 action.setTarget( TEST_TARGET_ID );
257 ProxyConnector connector = action.getConnector();
258 assertInitialProxyConnector( connector );
260 String status = action.input();
261 assertEquals( Action.INPUT, status );
264 public void testRemoveBlackListPattern()
267 expectConfigurationRequests( 7 );
268 archivaConfigurationControl.replay();
271 action.setSource( TEST_SOURCE_ID );
272 action.setTarget( TEST_TARGET_ID );
274 ProxyConnector connector = action.getConnector();
275 assertInitialProxyConnector( connector );
277 // Add some arbitrary blacklist patterns.
278 connector.addBlackListPattern( "**/*-javadoc.jar" );
279 connector.addBlackListPattern( "**/*.war" );
281 // Perform Test w/no pattern value.
282 preRequest( action );
283 String status = action.removeBlackListPattern();
284 assertEquals( Action.INPUT, status );
286 // Should have returned an error, with no blacklist pattern removed.
287 assertHasErrors( action );
288 assertEquals( 2, connector.getBlackListPatterns().size() );
290 // Perform test w/invalid (non-existant) pattern value to remove.
291 preRequest( action );
292 action.setPattern( "**/*oops*" );
293 status = action.removeBlackListPattern();
294 assertEquals( Action.INPUT, status );
296 // Should have returned an error, with no blacklist pattern removed.
297 assertHasErrors( action );
298 assertEquals( 2, connector.getBlackListPatterns().size() );
300 // Try again, but now with a valid pattern to remove.
301 action.setPattern( "**/*-javadoc.jar" );
302 preRequest( action );
303 status = action.removeBlackListPattern();
304 assertEquals( Action.INPUT, status );
306 // Should have no error, and 1 blacklist pattern left.
307 assertNoErrors( action );
308 assertEquals( 1, connector.getBlackListPatterns().size() );
309 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
312 public void testRemoveProperty()
315 expectConfigurationRequests( 7 );
316 archivaConfigurationControl.replay();
319 action.setSource( TEST_SOURCE_ID );
320 action.setTarget( TEST_TARGET_ID );
322 ProxyConnector connector = action.getConnector();
323 assertInitialProxyConnector( connector );
325 // Add some arbitrary properties.
326 connector.addProperty( "username", "general-tso" );
327 connector.addProperty( "password", "chicken" );
329 // Perform Test w/no property key.
330 preRequest( action );
331 String status = action.removeProperty();
332 assertEquals( Action.INPUT, status );
334 // Should have returned an error, with no properties removed.
335 assertHasErrors( action );
336 assertEquals( 2, connector.getProperties().size() );
338 // Perform test w/invalid (non-existant) property key to remove.
339 preRequest( action );
340 action.setPropertyKey( "slurm" );
341 status = action.removeProperty();
342 assertEquals( Action.INPUT, status );
344 // Should have returned an error, with no properties removed.
345 assertHasErrors( action );
346 assertEquals( 2, connector.getProperties().size() );
348 // Try again, but now with a valid property to remove.
349 preRequest( action );
350 action.setPropertyKey( "password" );
351 status = action.removeProperty();
352 assertEquals( Action.INPUT, status );
354 // Should have no error, and 1 property left.
355 assertNoErrors( action );
356 assertEquals( 1, connector.getProperties().size() );
357 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
360 public void testRemoveWhiteListPattern()
363 expectConfigurationRequests( 7 );
364 archivaConfigurationControl.replay();
367 action.setSource( TEST_SOURCE_ID );
368 action.setTarget( TEST_TARGET_ID );
370 ProxyConnector connector = action.getConnector();
371 assertInitialProxyConnector( connector );
373 // Add some arbitrary whitelist patterns.
374 connector.addWhiteListPattern( "javax/**/*" );
375 connector.addWhiteListPattern( "com/sun/**/*" );
377 // Perform Test w/no pattern value.
378 preRequest( action );
379 String status = action.removeWhiteListPattern();
380 assertEquals( Action.INPUT, status );
382 // Should have returned an error, with no whitelist pattern removed.
383 assertHasErrors( action );
384 assertEquals( 2, connector.getWhiteListPatterns().size() );
386 // Perform test w/invalid (non-existant) pattern value to remove.
387 preRequest( action );
388 action.setPattern( "**/*oops*" );
389 status = action.removeWhiteListPattern();
390 assertEquals( Action.INPUT, status );
392 // Should have returned an error, with no whitelist pattern removed.
393 assertHasErrors( action );
394 assertEquals( 2, connector.getWhiteListPatterns().size() );
396 // Try again, but now with a valid pattern to remove.
397 action.setPattern( "com/sun/**/*" );
398 preRequest( action );
399 status = action.removeWhiteListPattern();
400 assertEquals( Action.INPUT, status );
402 // Should have no error, and 1 whitelist pattern left.
403 assertNoErrors( action );
404 assertEquals( 1, connector.getWhiteListPatterns().size() );
405 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
408 public void testSecureActionBundle()
411 archivaConfigurationControl.replay();
414 SecureActionBundle bundle = action.getSecureActionBundle();
415 assertTrue( bundle.requiresAuthentication() );
416 assertEquals( 1, bundle.getAuthorizationTuples().size() );
419 private void assertInitialProxyConnector( ProxyConnector connector )
421 assertNotNull( connector );
422 assertNotNull( connector.getBlackListPatterns() );
423 assertNotNull( connector.getWhiteListPatterns() );
424 assertNotNull( connector.getProperties() );
426 assertEquals( TEST_SOURCE_ID, connector.getSourceRepoId() );
427 assertEquals( TEST_TARGET_ID, connector.getTargetRepoId() );
430 @SuppressWarnings( "unchecked" )
431 private Configuration createInitialConfiguration()
433 Configuration config = new Configuration();
435 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
436 managedRepo.setId( TEST_SOURCE_ID );
437 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
438 managedRepo.setReleases( true );
440 config.addManagedRepository( managedRepo );
442 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
443 remoteRepo.setId( TEST_TARGET_ID );
444 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
446 config.addRemoteRepository( remoteRepo );
448 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
449 connector.setSourceRepoId( TEST_SOURCE_ID );
450 connector.setTargetRepoId( TEST_TARGET_ID );
452 // TODO: Set these options programatically via list of available policies.
453 Map<String, String> policies = connector.getPolicies();
454 policies.put( "releases", new ReleasesPolicy().getDefaultOption() );
455 policies.put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
456 policies.put( "checksum", new ChecksumPolicy().getDefaultOption() );
457 policies.put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
458 policies.put( "propagate-errors", new PropagateErrorsDownloadPolicy().getDefaultOption() );
459 policies.put( "propagate-errors-on-update", new PropagateErrorsOnUpdateDownloadPolicy().getDefaultOption() );
461 config.addProxyConnector( connector );