1 package org.apache.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.model.beans.ProxyConnector;
24 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
25 import org.apache.archiva.admin.repository.proxyconnector.DefaultProxyConnectorAdmin;
26 import org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.configuration.Configuration;
29 import org.apache.archiva.configuration.IndeterminateConfigurationException;
30 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
33 import org.apache.archiva.policies.CachedFailuresPolicy;
34 import org.apache.archiva.policies.ChecksumPolicy;
35 import org.apache.archiva.policies.PropagateErrorsDownloadPolicy;
36 import org.apache.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
37 import org.apache.archiva.policies.ReleasesPolicy;
38 import org.apache.archiva.policies.SnapshotsPolicy;
39 import org.apache.archiva.web.action.AbstractWebworkTestCase;
40 import org.apache.archiva.redback.components.registry.RegistryException;
41 import org.apache.archiva.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) getActionProxy( "/admin/editProxyConnector.action" ).getAction();
73 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
74 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
78 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
79 archivaConfiguration );
80 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
81 archivaConfiguration );
82 ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration(
83 archivaConfiguration );
86 private void expectConfigurationRequests( int requestConfigCount )
87 throws org.apache.archiva.redback.components.registry.RegistryException, IndeterminateConfigurationException
89 expectConfigurationRequests( requestConfigCount, 1 );
92 private void expectConfigurationRequests( int requestConfigCount, int saveRequestCount )
93 throws RegistryException, IndeterminateConfigurationException
95 Configuration config = createInitialConfiguration();
97 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config,
98 requestConfigCount , 20);
99 //archivaConfiguration.getConfiguration();
100 //archivaConfigurationControl.setReturnValue( config, requestConfigCount );
102 for ( int i = 0; i <= saveRequestCount; i++ )
104 archivaConfiguration.save( config );
107 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
108 archivaConfiguration );
109 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
110 archivaConfiguration );
111 ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration(
112 archivaConfiguration );
115 public void testAddBlackListPattern()
118 expectConfigurationRequests( 7 );
119 archivaConfigurationControl.replay();
122 action.setSource( TEST_SOURCE_ID );
123 action.setTarget( TEST_TARGET_ID );
125 ProxyConnector connector = action.getConnector();
126 assertInitialProxyConnector( connector );
128 // Perform Test w/no values.
129 preRequest( action );
130 String status = action.addBlackListPattern();
131 assertEquals( Action.INPUT, status );
133 // Should have returned an error, with no blacklist pattern added.
134 assertHasErrors( action );
135 assertEquals( 0, connector.getBlackListPatterns().size() );
137 // Try again, but now with a pattern to add.
138 action.setBlackListPattern( "**/*-javadoc.jar" );
139 preRequest( action );
140 status = action.addBlackListPattern();
141 assertEquals( Action.INPUT, status );
143 // Should have no error, and 1 blacklist pattern added.
144 assertNoErrors( action );
145 assertEquals( 1, connector.getBlackListPatterns().size() );
148 public void testAddProperty()
151 expectConfigurationRequests( 7 );
152 archivaConfigurationControl.replay();
155 action.setSource( TEST_SOURCE_ID );
156 action.setTarget( TEST_TARGET_ID );
158 ProxyConnector connector = action.getConnector();
159 assertInitialProxyConnector( connector );
161 // Perform Test w/no values.
162 preRequest( action );
163 String status = action.addProperty();
164 assertEquals( Action.INPUT, status );
166 // Should have returned an error, with no property pattern added.
167 assertHasErrors( action );
168 assertEquals( 0, connector.getProperties().size() );
170 // Try again, but now with a property key/value to add.
171 action.setPropertyKey( "eat-a" );
172 action.setPropertyValue( "gramov-a-bits" );
173 preRequest( action );
174 status = action.addProperty();
175 assertEquals( Action.INPUT, status );
177 // Should have no error, and 1 property added.
178 assertNoErrors( action );
179 assertEquals( 1, connector.getProperties().size() );
180 assertEquals( "gramov-a-bits", connector.getProperties().get( "eat-a" ) );
183 public void testAddWhiteListPattern()
186 expectConfigurationRequests( 7 );
187 archivaConfigurationControl.replay();
190 action.setSource( TEST_SOURCE_ID );
191 action.setTarget( TEST_TARGET_ID );
193 ProxyConnector connector = action.getConnector();
194 assertInitialProxyConnector( connector );
196 // Perform Test w/no values.
197 preRequest( action );
198 String status = action.addWhiteListPattern();
199 assertEquals( Action.INPUT, status );
201 // Should have returned an error, with no whitelist pattern added.
202 assertHasErrors( action );
203 assertEquals( 0, connector.getWhiteListPatterns().size() );
205 // Try again, but now with a pattern to add.
206 action.setWhiteListPattern( "**/*.jar" );
207 preRequest( action );
208 status = action.addWhiteListPattern();
209 assertEquals( Action.INPUT, status );
211 // Should have no error, and 1 whitelist pattern added.
212 assertNoErrors( action );
213 assertEquals( 1, connector.getWhiteListPatterns().size() );
216 @SuppressWarnings( "unchecked" )
217 public void testEditProxyConnectorCommit()
220 expectConfigurationRequests( 9, 2 );
221 archivaConfigurationControl.replay();
224 action.setSource( TEST_SOURCE_ID );
225 action.setTarget( TEST_TARGET_ID );
227 ProxyConnector connector = action.getConnector();
228 assertInitialProxyConnector( connector );
229 // forms will use an array
230 //connector.getProperties().put( "eat-a", new String[]{ "gramov-a-bits" } );
231 // FIXME check the array mode
232 connector.getProperties().put( "eat-a", "gramov-a-bits" );
234 // Create the input screen.
235 assertRequestStatus( action, Action.SUCCESS, "commit" );
236 assertNoErrors( action );
238 // Test configuration.
239 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
240 assertNotNull( proxyConfigs );
241 assertEquals( 1, proxyConfigs.size() );
243 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
245 assertNotNull( actualConnector );
246 // The use of "(direct connection)" should result in a proxyId which is <null>.
247 assertNull( actualConnector.getProxyId() );
248 assertEquals( "corporate", actualConnector.getSourceRepoId() );
249 assertEquals( "central", actualConnector.getTargetRepoId() );
253 public void testEditProxyConnectorInitialPage()
256 expectConfigurationRequests( 3 );
257 archivaConfigurationControl.replay();
259 action.setSource( TEST_SOURCE_ID );
260 action.setTarget( TEST_TARGET_ID );
262 ProxyConnector connector = action.getConnector();
263 assertInitialProxyConnector( connector );
265 String status = action.input();
266 assertEquals( Action.INPUT, status );
269 public void testRemoveBlackListPattern()
272 expectConfigurationRequests( 7 );
273 archivaConfigurationControl.replay();
276 action.setSource( TEST_SOURCE_ID );
277 action.setTarget( TEST_TARGET_ID );
279 ProxyConnector connector = action.getConnector();
280 assertInitialProxyConnector( connector );
282 // Add some arbitrary blacklist patterns.
283 connector.addBlackListPattern( "**/*-javadoc.jar" );
284 connector.addBlackListPattern( "**/*.war" );
286 // Perform Test w/no pattern value.
287 preRequest( action );
288 String status = action.removeBlackListPattern();
289 assertEquals( Action.INPUT, status );
291 // Should have returned an error, with no blacklist pattern removed.
292 assertHasErrors( action );
293 assertEquals( 2, connector.getBlackListPatterns().size() );
295 // Perform test w/invalid (non-existant) pattern value to remove.
296 preRequest( action );
297 action.setPattern( "**/*oops*" );
298 status = action.removeBlackListPattern();
299 assertEquals( Action.INPUT, status );
301 // Should have returned an error, with no blacklist pattern removed.
302 assertHasErrors( action );
303 assertEquals( 2, connector.getBlackListPatterns().size() );
305 // Try again, but now with a valid pattern to remove.
306 action.setPattern( "**/*-javadoc.jar" );
307 preRequest( action );
308 status = action.removeBlackListPattern();
309 assertEquals( Action.INPUT, status );
311 // Should have no error, and 1 blacklist pattern left.
312 assertNoErrors( action );
313 assertEquals( 1, connector.getBlackListPatterns().size() );
314 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
317 public void testRemoveProperty()
320 expectConfigurationRequests( 7 );
321 archivaConfigurationControl.replay();
324 action.setSource( TEST_SOURCE_ID );
325 action.setTarget( TEST_TARGET_ID );
327 ProxyConnector connector = action.getConnector();
328 assertInitialProxyConnector( connector );
330 // Add some arbitrary properties.
331 connector.addProperty( "username", "general-tso" );
332 connector.addProperty( "password", "chicken" );
334 // Perform Test w/no property key.
335 preRequest( action );
336 String status = action.removeProperty();
337 assertEquals( Action.INPUT, status );
339 // Should have returned an error, with no properties removed.
340 assertHasErrors( action );
341 assertEquals( 2, connector.getProperties().size() );
343 // Perform test w/invalid (non-existant) property key to remove.
344 preRequest( action );
345 action.setPropertyKey( "slurm" );
346 status = action.removeProperty();
347 assertEquals( Action.INPUT, status );
349 // Should have returned an error, with no properties removed.
350 assertHasErrors( action );
351 assertEquals( 2, connector.getProperties().size() );
353 // Try again, but now with a valid property to remove.
354 preRequest( action );
355 action.setPropertyKey( "password" );
356 status = action.removeProperty();
357 assertEquals( Action.INPUT, status );
359 // Should have no error, and 1 property left.
360 assertNoErrors( action );
361 assertEquals( 1, connector.getProperties().size() );
362 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
365 public void testRemoveWhiteListPattern()
368 expectConfigurationRequests( 7 );
369 archivaConfigurationControl.replay();
372 action.setSource( TEST_SOURCE_ID );
373 action.setTarget( TEST_TARGET_ID );
375 ProxyConnector connector = action.getConnector();
376 assertInitialProxyConnector( connector );
378 // Add some arbitrary whitelist patterns.
379 connector.addWhiteListPattern( "javax/**/*" );
380 connector.addWhiteListPattern( "com/sun/**/*" );
382 // Perform Test w/no pattern value.
383 preRequest( action );
384 String status = action.removeWhiteListPattern();
385 assertEquals( Action.INPUT, status );
387 // Should have returned an error, with no whitelist pattern removed.
388 assertHasErrors( action );
389 assertEquals( 2, connector.getWhiteListPatterns().size() );
391 // Perform test w/invalid (non-existant) pattern value to remove.
392 preRequest( action );
393 action.setPattern( "**/*oops*" );
394 status = action.removeWhiteListPattern();
395 assertEquals( Action.INPUT, status );
397 // Should have returned an error, with no whitelist pattern removed.
398 assertHasErrors( action );
399 assertEquals( 2, connector.getWhiteListPatterns().size() );
401 // Try again, but now with a valid pattern to remove.
402 action.setPattern( "com/sun/**/*" );
403 preRequest( action );
404 status = action.removeWhiteListPattern();
405 assertEquals( Action.INPUT, status );
407 // Should have no error, and 1 whitelist pattern left.
408 assertNoErrors( action );
409 assertEquals( 1, connector.getWhiteListPatterns().size() );
410 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
413 public void testSecureActionBundle()
416 /* Configuration will be requested at least 3 times. */
417 archivaConfiguration.getConfiguration();
418 archivaConfigurationControl.setReturnValue( new Configuration(), 3 );
419 archivaConfigurationControl.replay();
422 SecureActionBundle bundle = action.getSecureActionBundle();
423 assertTrue( bundle.requiresAuthentication() );
424 assertEquals( 1, bundle.getAuthorizationTuples().size() );
427 private void assertInitialProxyConnector( ProxyConnector connector )
429 assertNotNull( connector );
430 assertNotNull( connector.getBlackListPatterns() );
431 assertNotNull( connector.getWhiteListPatterns() );
432 assertNotNull( connector.getProperties() );
434 assertEquals( TEST_SOURCE_ID, connector.getSourceRepoId() );
435 assertEquals( TEST_TARGET_ID, connector.getTargetRepoId() );
438 @SuppressWarnings( "unchecked" )
439 private Configuration createInitialConfiguration()
441 Configuration config = new Configuration();
443 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
444 managedRepo.setId( TEST_SOURCE_ID );
445 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
446 managedRepo.setReleases( true );
448 config.addManagedRepository( managedRepo );
450 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
451 remoteRepo.setId( TEST_TARGET_ID );
452 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
454 config.addRemoteRepository( remoteRepo );
456 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
457 connector.setSourceRepoId( TEST_SOURCE_ID );
458 connector.setTargetRepoId( TEST_TARGET_ID );
460 // TODO: Set these options programatically via list of available policies.
461 Map<String, String> policies = connector.getPolicies();
462 policies.put( "releases", new ReleasesPolicy().getDefaultOption() );
463 policies.put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
464 policies.put( "checksum", new ChecksumPolicy().getDefaultOption() );
465 policies.put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
466 policies.put( "propagate-errors", new PropagateErrorsDownloadPolicy().getDefaultOption() );
467 policies.put( "propagate-errors-on-update", new PropagateErrorsOnUpdateDownloadPolicy().getDefaultOption() );
469 config.addProxyConnector( connector );