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.xwork.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.ReleasesPolicy;
32 import org.apache.maven.archiva.policies.SnapshotsPolicy;
33 import org.apache.maven.archiva.web.action.AbstractWebworkTestCase;
34 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
35 import org.codehaus.plexus.registry.RegistryException;
36 import org.easymock.MockControl;
38 import java.util.List;
41 * EditProxyConnectorActionTest
43 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
46 public class EditProxyConnectorActionTest
47 extends AbstractWebworkTestCase
49 private static final String TEST_TARGET_ID = "central";
51 private static final String TEST_SOURCE_ID = "corporate";
53 private EditProxyConnectorAction action;
55 private MockControl archivaConfigurationControl;
57 private ArchivaConfiguration archivaConfiguration;
59 public void testAddBlackListPattern()
62 expectConfigurationRequests( 7 );
63 archivaConfigurationControl.replay();
66 action.setSource( TEST_SOURCE_ID );
67 action.setTarget( TEST_TARGET_ID );
69 ProxyConnectorConfiguration connector = action.getConnector();
70 assertInitialProxyConnector( connector );
72 // Perform Test w/no values.
74 String status = action.addBlackListPattern();
75 assertEquals( Action.INPUT, status );
77 // Should have returned an error, with no blacklist pattern added.
78 assertHasErrors( action );
79 assertEquals( 0, connector.getBlackListPatterns().size() );
81 // Try again, but now with a pattern to add.
82 action.setBlackListPattern( "**/*-javadoc.jar" );
84 status = action.addBlackListPattern();
85 assertEquals( Action.INPUT, status );
87 // Should have no error, and 1 blacklist pattern added.
88 assertNoErrors( action );
89 assertEquals( 1, connector.getBlackListPatterns().size() );
92 public void testAddProperty()
95 expectConfigurationRequests( 7 );
96 archivaConfigurationControl.replay();
99 action.setSource( TEST_SOURCE_ID );
100 action.setTarget( TEST_TARGET_ID );
102 ProxyConnectorConfiguration connector = action.getConnector();
103 assertInitialProxyConnector( connector );
105 // Perform Test w/no values.
106 preRequest( action );
107 String status = action.addProperty();
108 assertEquals( Action.INPUT, status );
110 // Should have returned an error, with no property pattern added.
111 assertHasErrors( action );
112 assertEquals( 0, connector.getProperties().size() );
114 // Try again, but now with a property key/value to add.
115 action.setPropertyKey( "eat-a" );
116 action.setPropertyValue( "gramov-a-bits" );
117 preRequest( action );
118 status = action.addProperty();
119 assertEquals( Action.INPUT, status );
121 // Should have no error, and 1 property added.
122 assertNoErrors( action );
123 assertEquals( 1, connector.getProperties().size() );
124 assertEquals( "gramov-a-bits", connector.getProperties().get( "eat-a" ) );
127 public void testAddWhiteListPattern()
130 expectConfigurationRequests( 7 );
131 archivaConfigurationControl.replay();
134 action.setSource( TEST_SOURCE_ID );
135 action.setTarget( TEST_TARGET_ID );
137 ProxyConnectorConfiguration connector = action.getConnector();
138 assertInitialProxyConnector( connector );
140 // Perform Test w/no values.
141 preRequest( action );
142 String status = action.addWhiteListPattern();
143 assertEquals( Action.INPUT, status );
145 // Should have returned an error, with no whitelist pattern added.
146 assertHasErrors( action );
147 assertEquals( 0, connector.getWhiteListPatterns().size() );
149 // Try again, but now with a pattern to add.
150 action.setWhiteListPattern( "**/*.jar" );
151 preRequest( action );
152 status = action.addWhiteListPattern();
153 assertEquals( Action.INPUT, status );
155 // Should have no error, and 1 whitelist pattern added.
156 assertNoErrors( action );
157 assertEquals( 1, connector.getWhiteListPatterns().size() );
160 public void testEditProxyConnectorCommit()
163 expectConfigurationRequests( 7 );
164 archivaConfigurationControl.replay();
167 action.setSource( TEST_SOURCE_ID );
168 action.setTarget( TEST_TARGET_ID );
170 ProxyConnectorConfiguration connector = action.getConnector();
171 assertInitialProxyConnector( connector );
172 // forms will use an array
173 connector.getProperties().put( "eat-a", new String[] { "gramov-a-bits" } );
175 // Create the input screen.
176 assertRequestStatus( action, Action.SUCCESS, "commit" );
177 assertNoErrors( action );
179 // Test configuration.
180 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
181 assertNotNull( proxyConfigs );
182 assertEquals( 1, proxyConfigs.size() );
184 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
186 assertNotNull( actualConnector );
187 // The use of "(direct connection)" should result in a proxyId which is <null>.
188 assertNull( actualConnector.getProxyId() );
189 assertEquals( "corporate", actualConnector.getSourceRepoId() );
190 assertEquals( "central", actualConnector.getTargetRepoId() );
194 public void testEditProxyConnectorInitialPage()
197 expectConfigurationRequests( 3 );
198 archivaConfigurationControl.replay();
200 action.setSource( TEST_SOURCE_ID );
201 action.setTarget( TEST_TARGET_ID );
203 ProxyConnectorConfiguration connector = action.getConnector();
204 assertInitialProxyConnector( connector );
206 String status = action.input();
207 assertEquals( Action.INPUT, status );
210 public void testRemoveBlackListPattern()
213 expectConfigurationRequests( 7 );
214 archivaConfigurationControl.replay();
217 action.setSource( TEST_SOURCE_ID );
218 action.setTarget( TEST_TARGET_ID );
220 ProxyConnectorConfiguration connector = action.getConnector();
221 assertInitialProxyConnector( connector );
223 // Add some arbitrary blacklist patterns.
224 connector.addBlackListPattern( "**/*-javadoc.jar" );
225 connector.addBlackListPattern( "**/*.war" );
227 // Perform Test w/no pattern value.
228 preRequest( action );
229 String status = action.removeBlackListPattern();
230 assertEquals( Action.INPUT, status );
232 // Should have returned an error, with no blacklist pattern removed.
233 assertHasErrors( action );
234 assertEquals( 2, connector.getBlackListPatterns().size() );
236 // Perform test w/invalid (non-existant) pattern value to remove.
237 preRequest( action );
238 action.setPattern( "**/*oops*" );
239 status = action.removeBlackListPattern();
240 assertEquals( Action.INPUT, status );
242 // Should have returned an error, with no blacklist pattern removed.
243 assertHasErrors( action );
244 assertEquals( 2, connector.getBlackListPatterns().size() );
246 // Try again, but now with a valid pattern to remove.
247 action.setPattern( "**/*-javadoc.jar" );
248 preRequest( action );
249 status = action.removeBlackListPattern();
250 assertEquals( Action.INPUT, status );
252 // Should have no error, and 1 blacklist pattern left.
253 assertNoErrors( action );
254 assertEquals( 1, connector.getBlackListPatterns().size() );
255 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
258 public void testRemoveProperty()
261 expectConfigurationRequests( 7 );
262 archivaConfigurationControl.replay();
265 action.setSource( TEST_SOURCE_ID );
266 action.setTarget( TEST_TARGET_ID );
268 ProxyConnectorConfiguration connector = action.getConnector();
269 assertInitialProxyConnector( connector );
271 // Add some arbitrary properties.
272 connector.addProperty( "username", "general-tso" );
273 connector.addProperty( "password", "chicken" );
275 // Perform Test w/no property key.
276 preRequest( action );
277 String status = action.removeProperty();
278 assertEquals( Action.INPUT, status );
280 // Should have returned an error, with no properties removed.
281 assertHasErrors( action );
282 assertEquals( 2, connector.getProperties().size() );
284 // Perform test w/invalid (non-existant) property key to remove.
285 preRequest( action );
286 action.setPropertyKey( "slurm" );
287 status = action.removeProperty();
288 assertEquals( Action.INPUT, status );
290 // Should have returned an error, with no properties removed.
291 assertHasErrors( action );
292 assertEquals( 2, connector.getProperties().size() );
294 // Try again, but now with a valid property to remove.
295 preRequest( action );
296 action.setPropertyKey( "password" );
297 status = action.removeProperty();
298 assertEquals( Action.INPUT, status );
300 // Should have no error, and 1 property left.
301 assertNoErrors( action );
302 assertEquals( 1, connector.getProperties().size() );
303 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
306 public void testRemoveWhiteListPattern()
309 expectConfigurationRequests( 7 );
310 archivaConfigurationControl.replay();
313 action.setSource( TEST_SOURCE_ID );
314 action.setTarget( TEST_TARGET_ID );
316 ProxyConnectorConfiguration connector = action.getConnector();
317 assertInitialProxyConnector( connector );
319 // Add some arbitrary whitelist patterns.
320 connector.addWhiteListPattern( "javax/**/*" );
321 connector.addWhiteListPattern( "com/sun/**/*" );
323 // Perform Test w/no pattern value.
324 preRequest( action );
325 String status = action.removeWhiteListPattern();
326 assertEquals( Action.INPUT, status );
328 // Should have returned an error, with no whitelist pattern removed.
329 assertHasErrors( action );
330 assertEquals( 2, connector.getWhiteListPatterns().size() );
332 // Perform test w/invalid (non-existant) pattern value to remove.
333 preRequest( action );
334 action.setPattern( "**/*oops*" );
335 status = action.removeWhiteListPattern();
336 assertEquals( Action.INPUT, status );
338 // Should have returned an error, with no whitelist pattern removed.
339 assertHasErrors( action );
340 assertEquals( 2, connector.getWhiteListPatterns().size() );
342 // Try again, but now with a valid pattern to remove.
343 action.setPattern( "com/sun/**/*" );
344 preRequest( action );
345 status = action.removeWhiteListPattern();
346 assertEquals( Action.INPUT, status );
348 // Should have no error, and 1 whitelist pattern left.
349 assertNoErrors( action );
350 assertEquals( 1, connector.getWhiteListPatterns().size() );
351 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
354 public void testSecureActionBundle()
357 archivaConfigurationControl.replay();
360 SecureActionBundle bundle = action.getSecureActionBundle();
361 assertTrue( bundle.requiresAuthentication() );
362 assertEquals( 1, bundle.getAuthorizationTuples().size() );
365 private void assertInitialProxyConnector( ProxyConnectorConfiguration connector )
367 assertNotNull( connector );
368 assertNotNull( connector.getBlackListPatterns() );
369 assertNotNull( connector.getWhiteListPatterns() );
370 assertNotNull( connector.getProperties() );
372 assertEquals( TEST_SOURCE_ID, connector.getSourceRepoId() );
373 assertEquals( TEST_TARGET_ID, connector.getTargetRepoId() );
376 private Configuration createInitialConfiguration()
378 Configuration config = new Configuration();
380 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
381 managedRepo.setId( TEST_SOURCE_ID );
382 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
383 managedRepo.setReleases( true );
385 config.addManagedRepository( managedRepo );
387 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
388 remoteRepo.setId( TEST_TARGET_ID );
389 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
391 config.addRemoteRepository( remoteRepo );
393 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
394 connector.setSourceRepoId( TEST_SOURCE_ID );
395 connector.setTargetRepoId( TEST_TARGET_ID );
397 // TODO: Set these options programatically via list of available policies.
398 connector.getPolicies().put( "releases", new ReleasesPolicy().getDefaultOption() );
399 connector.getPolicies().put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
400 connector.getPolicies().put( "checksum", new ChecksumPolicy().getDefaultOption() );
401 connector.getPolicies().put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
403 config.addProxyConnector( connector );
408 private void expectConfigurationRequests( int requestConfigCount )
409 throws RegistryException, IndeterminateConfigurationException
411 Configuration config = createInitialConfiguration();
413 for ( int i = 0; i < requestConfigCount; i++ )
415 archivaConfiguration.getConfiguration();
416 archivaConfigurationControl.setReturnValue( config );
419 archivaConfiguration.save( config );
423 protected void setUp()
428 action = (EditProxyConnectorAction) lookup( Action.class.getName(), "editProxyConnectorAction" );
430 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
431 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
432 action.setArchivaConfiguration( archivaConfiguration );
434 /* Configuration will be requested at least 3 times. */
435 for ( int i = 0; i < 3; i++ )
437 archivaConfiguration.getConfiguration();
438 archivaConfigurationControl.setReturnValue( new Configuration() );