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.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.redback.integration.interceptor.SecureActionBundle;
37 import org.codehaus.plexus.registry.RegistryException;
38 import org.easymock.MockControl;
40 import java.util.List;
44 * EditProxyConnectorActionTest
48 public class EditProxyConnectorActionTest
49 extends AbstractWebworkTestCase
51 private static final String TEST_TARGET_ID = "central";
53 private static final String TEST_SOURCE_ID = "corporate";
55 private EditProxyConnectorAction action;
57 private MockControl archivaConfigurationControl;
59 private ArchivaConfiguration archivaConfiguration;
61 public void testAddBlackListPattern()
64 expectConfigurationRequests( 7 );
65 archivaConfigurationControl.replay();
68 action.setSource( TEST_SOURCE_ID );
69 action.setTarget( TEST_TARGET_ID );
71 ProxyConnectorConfiguration connector = action.getConnector();
72 assertInitialProxyConnector( connector );
74 // Perform Test w/no values.
76 String status = action.addBlackListPattern();
77 assertEquals( Action.INPUT, status );
79 // Should have returned an error, with no blacklist pattern added.
80 assertHasErrors( action );
81 assertEquals( 0, connector.getBlackListPatterns().size() );
83 // Try again, but now with a pattern to add.
84 action.setBlackListPattern( "**/*-javadoc.jar" );
86 status = action.addBlackListPattern();
87 assertEquals( Action.INPUT, status );
89 // Should have no error, and 1 blacklist pattern added.
90 assertNoErrors( action );
91 assertEquals( 1, connector.getBlackListPatterns().size() );
94 public void testAddProperty()
97 expectConfigurationRequests( 7 );
98 archivaConfigurationControl.replay();
101 action.setSource( TEST_SOURCE_ID );
102 action.setTarget( TEST_TARGET_ID );
104 ProxyConnectorConfiguration connector = action.getConnector();
105 assertInitialProxyConnector( connector );
107 // Perform Test w/no values.
108 preRequest( action );
109 String status = action.addProperty();
110 assertEquals( Action.INPUT, status );
112 // Should have returned an error, with no property pattern added.
113 assertHasErrors( action );
114 assertEquals( 0, connector.getProperties().size() );
116 // Try again, but now with a property key/value to add.
117 action.setPropertyKey( "eat-a" );
118 action.setPropertyValue( "gramov-a-bits" );
119 preRequest( action );
120 status = action.addProperty();
121 assertEquals( Action.INPUT, status );
123 // Should have no error, and 1 property added.
124 assertNoErrors( action );
125 assertEquals( 1, connector.getProperties().size() );
126 assertEquals( "gramov-a-bits", connector.getProperties().get( "eat-a" ) );
129 public void testAddWhiteListPattern()
132 expectConfigurationRequests( 7 );
133 archivaConfigurationControl.replay();
136 action.setSource( TEST_SOURCE_ID );
137 action.setTarget( TEST_TARGET_ID );
139 ProxyConnectorConfiguration connector = action.getConnector();
140 assertInitialProxyConnector( connector );
142 // Perform Test w/no values.
143 preRequest( action );
144 String status = action.addWhiteListPattern();
145 assertEquals( Action.INPUT, status );
147 // Should have returned an error, with no whitelist pattern added.
148 assertHasErrors( action );
149 assertEquals( 0, connector.getWhiteListPatterns().size() );
151 // Try again, but now with a pattern to add.
152 action.setWhiteListPattern( "**/*.jar" );
153 preRequest( action );
154 status = action.addWhiteListPattern();
155 assertEquals( Action.INPUT, status );
157 // Should have no error, and 1 whitelist pattern added.
158 assertNoErrors( action );
159 assertEquals( 1, connector.getWhiteListPatterns().size() );
162 @SuppressWarnings("unchecked")
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 @SuppressWarnings("unchecked")
380 private Configuration createInitialConfiguration()
382 Configuration config = new Configuration();
384 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
385 managedRepo.setId( TEST_SOURCE_ID );
386 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
387 managedRepo.setReleases( true );
389 config.addManagedRepository( managedRepo );
391 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
392 remoteRepo.setId( TEST_TARGET_ID );
393 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
395 config.addRemoteRepository( remoteRepo );
397 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
398 connector.setSourceRepoId( TEST_SOURCE_ID );
399 connector.setTargetRepoId( TEST_TARGET_ID );
401 // TODO: Set these options programatically via list of available policies.
402 Map<String, String> policies = connector.getPolicies();
403 policies.put( "releases", new ReleasesPolicy().getDefaultOption() );
404 policies.put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
405 policies.put( "checksum", new ChecksumPolicy().getDefaultOption() );
406 policies.put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
407 policies.put( "propagate-errors", new PropagateErrorsDownloadPolicy().getDefaultOption() );
408 policies.put( "propagate-errors-on-update", new PropagateErrorsOnUpdateDownloadPolicy().getDefaultOption() );
410 config.addProxyConnector( connector );
415 private void expectConfigurationRequests( int requestConfigCount )
416 throws RegistryException, IndeterminateConfigurationException
418 Configuration config = createInitialConfiguration();
420 for ( int i = 0; i < requestConfigCount; i++ )
422 archivaConfiguration.getConfiguration();
423 archivaConfigurationControl.setReturnValue( config );
426 archivaConfiguration.save( config );
430 protected void setUp()
435 action = (EditProxyConnectorAction) lookup( Action.class.getName(), "editProxyConnectorAction" );
437 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
438 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
439 action.setArchivaConfiguration( archivaConfiguration );
441 /* Configuration will be requested at least 3 times. */
442 for ( int i = 0; i < 3; i++ )
444 archivaConfiguration.getConfiguration();
445 archivaConfigurationControl.setReturnValue( new Configuration() );