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 public void testEditProxyConnectorCommit()
165 expectConfigurationRequests( 7 );
166 archivaConfigurationControl.replay();
169 action.setSource( TEST_SOURCE_ID );
170 action.setTarget( TEST_TARGET_ID );
172 ProxyConnectorConfiguration connector = action.getConnector();
173 assertInitialProxyConnector( connector );
174 // forms will use an array
175 connector.getProperties().put( "eat-a", new String[] { "gramov-a-bits" } );
177 // Create the input screen.
178 assertRequestStatus( action, Action.SUCCESS, "commit" );
179 assertNoErrors( action );
181 // Test configuration.
182 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
183 assertNotNull( proxyConfigs );
184 assertEquals( 1, proxyConfigs.size() );
186 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
188 assertNotNull( actualConnector );
189 // The use of "(direct connection)" should result in a proxyId which is <null>.
190 assertNull( actualConnector.getProxyId() );
191 assertEquals( "corporate", actualConnector.getSourceRepoId() );
192 assertEquals( "central", actualConnector.getTargetRepoId() );
196 public void testEditProxyConnectorInitialPage()
199 expectConfigurationRequests( 3 );
200 archivaConfigurationControl.replay();
202 action.setSource( TEST_SOURCE_ID );
203 action.setTarget( TEST_TARGET_ID );
205 ProxyConnectorConfiguration connector = action.getConnector();
206 assertInitialProxyConnector( connector );
208 String status = action.input();
209 assertEquals( Action.INPUT, status );
212 public void testRemoveBlackListPattern()
215 expectConfigurationRequests( 7 );
216 archivaConfigurationControl.replay();
219 action.setSource( TEST_SOURCE_ID );
220 action.setTarget( TEST_TARGET_ID );
222 ProxyConnectorConfiguration connector = action.getConnector();
223 assertInitialProxyConnector( connector );
225 // Add some arbitrary blacklist patterns.
226 connector.addBlackListPattern( "**/*-javadoc.jar" );
227 connector.addBlackListPattern( "**/*.war" );
229 // Perform Test w/no pattern value.
230 preRequest( action );
231 String status = action.removeBlackListPattern();
232 assertEquals( Action.INPUT, status );
234 // Should have returned an error, with no blacklist pattern removed.
235 assertHasErrors( action );
236 assertEquals( 2, connector.getBlackListPatterns().size() );
238 // Perform test w/invalid (non-existant) pattern value to remove.
239 preRequest( action );
240 action.setPattern( "**/*oops*" );
241 status = action.removeBlackListPattern();
242 assertEquals( Action.INPUT, status );
244 // Should have returned an error, with no blacklist pattern removed.
245 assertHasErrors( action );
246 assertEquals( 2, connector.getBlackListPatterns().size() );
248 // Try again, but now with a valid pattern to remove.
249 action.setPattern( "**/*-javadoc.jar" );
250 preRequest( action );
251 status = action.removeBlackListPattern();
252 assertEquals( Action.INPUT, status );
254 // Should have no error, and 1 blacklist pattern left.
255 assertNoErrors( action );
256 assertEquals( 1, connector.getBlackListPatterns().size() );
257 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
260 public void testRemoveProperty()
263 expectConfigurationRequests( 7 );
264 archivaConfigurationControl.replay();
267 action.setSource( TEST_SOURCE_ID );
268 action.setTarget( TEST_TARGET_ID );
270 ProxyConnectorConfiguration connector = action.getConnector();
271 assertInitialProxyConnector( connector );
273 // Add some arbitrary properties.
274 connector.addProperty( "username", "general-tso" );
275 connector.addProperty( "password", "chicken" );
277 // Perform Test w/no property key.
278 preRequest( action );
279 String status = action.removeProperty();
280 assertEquals( Action.INPUT, status );
282 // Should have returned an error, with no properties removed.
283 assertHasErrors( action );
284 assertEquals( 2, connector.getProperties().size() );
286 // Perform test w/invalid (non-existant) property key to remove.
287 preRequest( action );
288 action.setPropertyKey( "slurm" );
289 status = action.removeProperty();
290 assertEquals( Action.INPUT, status );
292 // Should have returned an error, with no properties removed.
293 assertHasErrors( action );
294 assertEquals( 2, connector.getProperties().size() );
296 // Try again, but now with a valid property to remove.
297 preRequest( action );
298 action.setPropertyKey( "password" );
299 status = action.removeProperty();
300 assertEquals( Action.INPUT, status );
302 // Should have no error, and 1 property left.
303 assertNoErrors( action );
304 assertEquals( 1, connector.getProperties().size() );
305 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
308 public void testRemoveWhiteListPattern()
311 expectConfigurationRequests( 7 );
312 archivaConfigurationControl.replay();
315 action.setSource( TEST_SOURCE_ID );
316 action.setTarget( TEST_TARGET_ID );
318 ProxyConnectorConfiguration connector = action.getConnector();
319 assertInitialProxyConnector( connector );
321 // Add some arbitrary whitelist patterns.
322 connector.addWhiteListPattern( "javax/**/*" );
323 connector.addWhiteListPattern( "com/sun/**/*" );
325 // Perform Test w/no pattern value.
326 preRequest( action );
327 String status = action.removeWhiteListPattern();
328 assertEquals( Action.INPUT, status );
330 // Should have returned an error, with no whitelist pattern removed.
331 assertHasErrors( action );
332 assertEquals( 2, connector.getWhiteListPatterns().size() );
334 // Perform test w/invalid (non-existant) pattern value to remove.
335 preRequest( action );
336 action.setPattern( "**/*oops*" );
337 status = action.removeWhiteListPattern();
338 assertEquals( Action.INPUT, status );
340 // Should have returned an error, with no whitelist pattern removed.
341 assertHasErrors( action );
342 assertEquals( 2, connector.getWhiteListPatterns().size() );
344 // Try again, but now with a valid pattern to remove.
345 action.setPattern( "com/sun/**/*" );
346 preRequest( action );
347 status = action.removeWhiteListPattern();
348 assertEquals( Action.INPUT, status );
350 // Should have no error, and 1 whitelist pattern left.
351 assertNoErrors( action );
352 assertEquals( 1, connector.getWhiteListPatterns().size() );
353 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
356 public void testSecureActionBundle()
359 archivaConfigurationControl.replay();
362 SecureActionBundle bundle = action.getSecureActionBundle();
363 assertTrue( bundle.requiresAuthentication() );
364 assertEquals( 1, bundle.getAuthorizationTuples().size() );
367 private void assertInitialProxyConnector( ProxyConnectorConfiguration connector )
369 assertNotNull( connector );
370 assertNotNull( connector.getBlackListPatterns() );
371 assertNotNull( connector.getWhiteListPatterns() );
372 assertNotNull( connector.getProperties() );
374 assertEquals( TEST_SOURCE_ID, connector.getSourceRepoId() );
375 assertEquals( TEST_TARGET_ID, connector.getTargetRepoId() );
378 private Configuration createInitialConfiguration()
380 Configuration config = new Configuration();
382 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
383 managedRepo.setId( TEST_SOURCE_ID );
384 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
385 managedRepo.setReleases( true );
387 config.addManagedRepository( managedRepo );
389 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
390 remoteRepo.setId( TEST_TARGET_ID );
391 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
393 config.addRemoteRepository( remoteRepo );
395 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
396 connector.setSourceRepoId( TEST_SOURCE_ID );
397 connector.setTargetRepoId( TEST_TARGET_ID );
399 // TODO: Set these options programatically via list of available policies.
400 Map<String, String> policies = connector.getPolicies();
401 policies.put( "releases", new ReleasesPolicy().getDefaultOption() );
402 policies.put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
403 policies.put( "checksum", new ChecksumPolicy().getDefaultOption() );
404 policies.put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
405 policies.put( "propagate-errors", new PropagateErrorsDownloadPolicy().getDefaultOption() );
406 policies.put( "propagate-errors-on-update", new PropagateErrorsOnUpdateDownloadPolicy().getDefaultOption() );
408 config.addProxyConnector( connector );
413 private void expectConfigurationRequests( int requestConfigCount )
414 throws RegistryException, IndeterminateConfigurationException
416 Configuration config = createInitialConfiguration();
418 for ( int i = 0; i < requestConfigCount; i++ )
420 archivaConfiguration.getConfiguration();
421 archivaConfigurationControl.setReturnValue( config );
424 archivaConfiguration.save( config );
428 protected void setUp()
433 action = (EditProxyConnectorAction) lookup( Action.class.getName(), "editProxyConnectorAction" );
435 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
436 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
437 action.setArchivaConfiguration( archivaConfiguration );
439 /* Configuration will be requested at least 3 times. */
440 for ( int i = 0; i < 3; i++ )
442 archivaConfiguration.getConfiguration();
443 archivaConfigurationControl.setReturnValue( new Configuration() );