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;
46 import org.junit.Before;
47 import org.junit.Test;
50 * EditProxyConnectorActionTest
54 public class EditProxyConnectorActionTest
55 extends AbstractWebworkTestCase
57 private static final String TEST_TARGET_ID = "central";
59 private static final String TEST_SOURCE_ID = "corporate";
61 private EditProxyConnectorAction action;
63 private MockControl archivaConfigurationControl;
65 private ArchivaConfiguration archivaConfiguration;
74 action = (EditProxyConnectorAction) getActionProxy( "/admin/editProxyConnector.action" ).getAction();
76 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
77 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
81 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
82 archivaConfiguration );
83 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
84 archivaConfiguration );
85 ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration(
86 archivaConfiguration );
89 private void expectConfigurationRequests( int requestConfigCount )
90 throws org.apache.archiva.redback.components.registry.RegistryException, IndeterminateConfigurationException
92 expectConfigurationRequests( requestConfigCount, 1 );
95 private void expectConfigurationRequests( int requestConfigCount, int saveRequestCount )
96 throws RegistryException, IndeterminateConfigurationException
98 Configuration config = createInitialConfiguration();
100 archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config,
101 requestConfigCount , 20);
102 //archivaConfiguration.getConfiguration();
103 //archivaConfigurationControl.setReturnValue( config, requestConfigCount );
105 for ( int i = 0; i <= saveRequestCount; i++ )
107 archivaConfiguration.save( config );
110 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
111 archivaConfiguration );
112 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
113 archivaConfiguration );
114 ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration(
115 archivaConfiguration );
119 public void testAddBlackListPattern()
122 expectConfigurationRequests( 7 );
123 archivaConfigurationControl.replay();
126 action.setSource( TEST_SOURCE_ID );
127 action.setTarget( TEST_TARGET_ID );
129 ProxyConnector connector = action.getConnector();
130 assertInitialProxyConnector( connector );
132 // Perform Test w/no values.
133 preRequest( action );
134 String status = action.addBlackListPattern();
135 assertEquals( Action.INPUT, status );
137 // Should have returned an error, with no blacklist pattern added.
138 assertHasErrors( action );
139 assertEquals( 0, connector.getBlackListPatterns().size() );
141 // Try again, but now with a pattern to add.
142 action.setBlackListPattern( "**/*-javadoc.jar" );
143 preRequest( action );
144 status = action.addBlackListPattern();
145 assertEquals( Action.INPUT, status );
147 // Should have no error, and 1 blacklist pattern added.
148 assertNoErrors( action );
149 assertEquals( 1, connector.getBlackListPatterns().size() );
153 public void testAddProperty()
156 expectConfigurationRequests( 7 );
157 archivaConfigurationControl.replay();
160 action.setSource( TEST_SOURCE_ID );
161 action.setTarget( TEST_TARGET_ID );
163 ProxyConnector connector = action.getConnector();
164 assertInitialProxyConnector( connector );
166 // Perform Test w/no values.
167 preRequest( action );
168 String status = action.addProperty();
169 assertEquals( Action.INPUT, status );
171 // Should have returned an error, with no property pattern added.
172 assertHasErrors( action );
173 assertEquals( 0, connector.getProperties().size() );
175 // Try again, but now with a property key/value to add.
176 action.setPropertyKey( "eat-a" );
177 action.setPropertyValue( "gramov-a-bits" );
178 preRequest( action );
179 status = action.addProperty();
180 assertEquals( Action.INPUT, status );
182 // Should have no error, and 1 property added.
183 assertNoErrors( action );
184 assertEquals( 1, connector.getProperties().size() );
185 assertEquals( "gramov-a-bits", connector.getProperties().get( "eat-a" ) );
189 public void testAddWhiteListPattern()
192 expectConfigurationRequests( 7 );
193 archivaConfigurationControl.replay();
196 action.setSource( TEST_SOURCE_ID );
197 action.setTarget( TEST_TARGET_ID );
199 ProxyConnector connector = action.getConnector();
200 assertInitialProxyConnector( connector );
202 // Perform Test w/no values.
203 preRequest( action );
204 String status = action.addWhiteListPattern();
205 assertEquals( Action.INPUT, status );
207 // Should have returned an error, with no whitelist pattern added.
208 assertHasErrors( action );
209 assertEquals( 0, connector.getWhiteListPatterns().size() );
211 // Try again, but now with a pattern to add.
212 action.setWhiteListPattern( "**/*.jar" );
213 preRequest( action );
214 status = action.addWhiteListPattern();
215 assertEquals( Action.INPUT, status );
217 // Should have no error, and 1 whitelist pattern added.
218 assertNoErrors( action );
219 assertEquals( 1, connector.getWhiteListPatterns().size() );
222 @SuppressWarnings( "unchecked" )
224 public void testEditProxyConnectorCommit()
227 expectConfigurationRequests( 9, 2 );
228 archivaConfigurationControl.replay();
231 action.setSource( TEST_SOURCE_ID );
232 action.setTarget( TEST_TARGET_ID );
234 ProxyConnector connector = action.getConnector();
235 assertInitialProxyConnector( connector );
236 // forms will use an array
237 //connector.getProperties().put( "eat-a", new String[]{ "gramov-a-bits" } );
238 // FIXME check the array mode
239 connector.getProperties().put( "eat-a", "gramov-a-bits" );
241 // Create the input screen.
242 assertRequestStatus( action, Action.SUCCESS, "commit" );
243 assertNoErrors( action );
245 // Test configuration.
246 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
247 assertNotNull( proxyConfigs );
248 assertEquals( 1, proxyConfigs.size() );
250 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
252 assertNotNull( actualConnector );
253 // The use of "(direct connection)" should result in a proxyId which is <null>.
254 assertNull( actualConnector.getProxyId() );
255 assertEquals( "corporate", actualConnector.getSourceRepoId() );
256 assertEquals( "central", actualConnector.getTargetRepoId() );
261 public void testEditProxyConnectorInitialPage()
264 expectConfigurationRequests( 3 );
265 archivaConfigurationControl.replay();
267 action.setSource( TEST_SOURCE_ID );
268 action.setTarget( TEST_TARGET_ID );
270 ProxyConnector connector = action.getConnector();
271 assertInitialProxyConnector( connector );
273 String status = action.input();
274 assertEquals( Action.INPUT, status );
278 public void testRemoveBlackListPattern()
281 expectConfigurationRequests( 7 );
282 archivaConfigurationControl.replay();
285 action.setSource( TEST_SOURCE_ID );
286 action.setTarget( TEST_TARGET_ID );
288 ProxyConnector connector = action.getConnector();
289 assertInitialProxyConnector( connector );
291 // Add some arbitrary blacklist patterns.
292 connector.addBlackListPattern( "**/*-javadoc.jar" );
293 connector.addBlackListPattern( "**/*.war" );
295 // Perform Test w/no pattern value.
296 preRequest( action );
297 String status = action.removeBlackListPattern();
298 assertEquals( Action.INPUT, status );
300 // Should have returned an error, with no blacklist pattern removed.
301 assertHasErrors( action );
302 assertEquals( 2, connector.getBlackListPatterns().size() );
304 // Perform test w/invalid (non-existant) pattern value to remove.
305 preRequest( action );
306 action.setPattern( "**/*oops*" );
307 status = action.removeBlackListPattern();
308 assertEquals( Action.INPUT, status );
310 // Should have returned an error, with no blacklist pattern removed.
311 assertHasErrors( action );
312 assertEquals( 2, connector.getBlackListPatterns().size() );
314 // Try again, but now with a valid pattern to remove.
315 action.setPattern( "**/*-javadoc.jar" );
316 preRequest( action );
317 status = action.removeBlackListPattern();
318 assertEquals( Action.INPUT, status );
320 // Should have no error, and 1 blacklist pattern left.
321 assertNoErrors( action );
322 assertEquals( 1, connector.getBlackListPatterns().size() );
323 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
327 public void testRemoveProperty()
330 expectConfigurationRequests( 7 );
331 archivaConfigurationControl.replay();
334 action.setSource( TEST_SOURCE_ID );
335 action.setTarget( TEST_TARGET_ID );
337 ProxyConnector connector = action.getConnector();
338 assertInitialProxyConnector( connector );
340 // Add some arbitrary properties.
341 connector.addProperty( "username", "general-tso" );
342 connector.addProperty( "password", "chicken" );
344 // Perform Test w/no property key.
345 preRequest( action );
346 String 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 // Perform test w/invalid (non-existant) property key to remove.
354 preRequest( action );
355 action.setPropertyKey( "slurm" );
356 status = action.removeProperty();
357 assertEquals( Action.INPUT, status );
359 // Should have returned an error, with no properties removed.
360 assertHasErrors( action );
361 assertEquals( 2, connector.getProperties().size() );
363 // Try again, but now with a valid property to remove.
364 preRequest( action );
365 action.setPropertyKey( "password" );
366 status = action.removeProperty();
367 assertEquals( Action.INPUT, status );
369 // Should have no error, and 1 property left.
370 assertNoErrors( action );
371 assertEquals( 1, connector.getProperties().size() );
372 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
376 public void testRemoveWhiteListPattern()
379 expectConfigurationRequests( 7 );
380 archivaConfigurationControl.replay();
383 action.setSource( TEST_SOURCE_ID );
384 action.setTarget( TEST_TARGET_ID );
386 ProxyConnector connector = action.getConnector();
387 assertInitialProxyConnector( connector );
389 // Add some arbitrary whitelist patterns.
390 connector.addWhiteListPattern( "javax/**/*" );
391 connector.addWhiteListPattern( "com/sun/**/*" );
393 // Perform Test w/no pattern value.
394 preRequest( action );
395 String status = action.removeWhiteListPattern();
396 assertEquals( Action.INPUT, status );
398 // Should have returned an error, with no whitelist pattern removed.
399 assertHasErrors( action );
400 assertEquals( 2, connector.getWhiteListPatterns().size() );
402 // Perform test w/invalid (non-existant) pattern value to remove.
403 preRequest( action );
404 action.setPattern( "**/*oops*" );
405 status = action.removeWhiteListPattern();
406 assertEquals( Action.INPUT, status );
408 // Should have returned an error, with no whitelist pattern removed.
409 assertHasErrors( action );
410 assertEquals( 2, connector.getWhiteListPatterns().size() );
412 // Try again, but now with a valid pattern to remove.
413 action.setPattern( "com/sun/**/*" );
414 preRequest( action );
415 status = action.removeWhiteListPattern();
416 assertEquals( Action.INPUT, status );
418 // Should have no error, and 1 whitelist pattern left.
419 assertNoErrors( action );
420 assertEquals( 1, connector.getWhiteListPatterns().size() );
421 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
425 public void testSecureActionBundle()
428 /* Configuration will be requested at least 3 times. */
429 archivaConfiguration.getConfiguration();
430 archivaConfigurationControl.setReturnValue( new Configuration(), 3 );
431 archivaConfigurationControl.replay();
434 SecureActionBundle bundle = action.getSecureActionBundle();
435 assertTrue( bundle.requiresAuthentication() );
436 assertEquals( 1, bundle.getAuthorizationTuples().size() );
439 private void assertInitialProxyConnector( ProxyConnector connector )
441 assertNotNull( connector );
442 assertNotNull( connector.getBlackListPatterns() );
443 assertNotNull( connector.getWhiteListPatterns() );
444 assertNotNull( connector.getProperties() );
446 assertEquals( TEST_SOURCE_ID, connector.getSourceRepoId() );
447 assertEquals( TEST_TARGET_ID, connector.getTargetRepoId() );
450 @SuppressWarnings( "unchecked" )
451 private Configuration createInitialConfiguration()
453 Configuration config = new Configuration();
455 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
456 managedRepo.setId( TEST_SOURCE_ID );
457 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
458 managedRepo.setReleases( true );
460 config.addManagedRepository( managedRepo );
462 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
463 remoteRepo.setId( TEST_TARGET_ID );
464 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
466 config.addRemoteRepository( remoteRepo );
468 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
469 connector.setSourceRepoId( TEST_SOURCE_ID );
470 connector.setTargetRepoId( TEST_TARGET_ID );
472 // TODO: Set these options programatically via list of available policies.
473 Map<String, String> policies = connector.getPolicies();
474 policies.put( "releases", new ReleasesPolicy().getDefaultOption() );
475 policies.put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
476 policies.put( "checksum", new ChecksumPolicy().getDefaultOption() );
477 policies.put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
478 policies.put( "propagate-errors", new PropagateErrorsDownloadPolicy().getDefaultOption() );
479 policies.put( "propagate-errors-on-update", new PropagateErrorsOnUpdateDownloadPolicy().getDefaultOption() );
481 config.addProxyConnector( connector );