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;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
29 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
30 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
31 import org.apache.maven.archiva.policies.ChecksumPolicy;
32 import org.apache.maven.archiva.policies.ReleasesPolicy;
33 import org.apache.maven.archiva.policies.SnapshotsPolicy;
34 import org.apache.maven.archiva.web.action.AbstractWebworkTestCase;
35 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
36 import org.codehaus.plexus.registry.RegistryException;
37 import org.easymock.MockControl;
39 import java.util.List;
42 * EditProxyConnectorActionTest
44 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
47 public class EditProxyConnectorActionTest
48 extends AbstractWebworkTestCase
50 private static final String TEST_TARGET_ID = "central";
52 private static final String TEST_SOURCE_ID = "corporate";
54 private EditProxyConnectorAction action;
56 private MockControl archivaConfigurationControl;
58 private ArchivaConfiguration archivaConfiguration;
60 public void testAddBlackListPattern()
63 expectConfigurationRequests( 7 );
64 archivaConfigurationControl.replay();
67 action.setSource( TEST_SOURCE_ID );
68 action.setTarget( TEST_TARGET_ID );
70 ProxyConnectorConfiguration connector = action.getConnector();
71 assertInitialProxyConnector( connector );
73 // Perform Test w/no values.
75 String status = action.addBlackListPattern();
76 assertEquals( Action.INPUT, status );
78 // Should have returned an error, with no blacklist pattern added.
79 assertHasErrors( action );
80 assertEquals( 0, connector.getBlackListPatterns().size() );
82 // Try again, but now with a pattern to add.
83 action.setBlackListPattern( "**/*-javadoc.jar" );
85 status = action.addBlackListPattern();
86 assertEquals( Action.INPUT, status );
88 // Should have no error, and 1 blacklist pattern added.
89 assertNoErrors( action );
90 assertEquals( 1, connector.getBlackListPatterns().size() );
93 public void testAddProperty()
96 expectConfigurationRequests( 7 );
97 archivaConfigurationControl.replay();
100 action.setSource( TEST_SOURCE_ID );
101 action.setTarget( TEST_TARGET_ID );
103 ProxyConnectorConfiguration connector = action.getConnector();
104 assertInitialProxyConnector( connector );
106 // Perform Test w/no values.
107 preRequest( action );
108 String status = action.addProperty();
109 assertEquals( Action.INPUT, status );
111 // Should have returned an error, with no property pattern added.
112 assertHasErrors( action );
113 assertEquals( 0, connector.getProperties().size() );
115 // Try again, but now with a property key/value to add.
116 action.setPropertyKey( "eat-a" );
117 action.setPropertyValue( "gramov-a-bits" );
118 preRequest( action );
119 status = action.addProperty();
120 assertEquals( Action.INPUT, status );
122 // Should have no error, and 1 property added.
123 assertNoErrors( action );
124 assertEquals( 1, connector.getProperties().size() );
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 );
173 // Create the input screen.
174 assertRequestStatus( action, Action.SUCCESS, "commit" );
175 assertNoErrors( action );
177 // Test configuration.
178 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
179 assertNotNull( proxyConfigs );
180 assertEquals( 1, proxyConfigs.size() );
182 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
184 assertNotNull( actualConnector );
185 // The use of "(direct connection)" should result in a proxyId which is <null>.
186 assertNull( actualConnector.getProxyId() );
187 assertEquals( "corporate", actualConnector.getSourceRepoId() );
188 assertEquals( "central", actualConnector.getTargetRepoId() );
191 public void testEditProxyConnectorInitialPage()
194 expectConfigurationRequests( 3 );
195 archivaConfigurationControl.replay();
197 action.setSource( TEST_SOURCE_ID );
198 action.setTarget( TEST_TARGET_ID );
200 ProxyConnectorConfiguration connector = action.getConnector();
201 assertInitialProxyConnector( connector );
203 String status = action.input();
204 assertEquals( Action.INPUT, status );
207 public void testRemoveBlackListPattern()
210 expectConfigurationRequests( 7 );
211 archivaConfigurationControl.replay();
214 action.setSource( TEST_SOURCE_ID );
215 action.setTarget( TEST_TARGET_ID );
217 ProxyConnectorConfiguration connector = action.getConnector();
218 assertInitialProxyConnector( connector );
220 // Add some arbitrary blacklist patterns.
221 connector.addBlackListPattern( "**/*-javadoc.jar" );
222 connector.addBlackListPattern( "**/*.war" );
224 // Perform Test w/no pattern value.
225 preRequest( action );
226 String status = action.removeBlackListPattern();
227 assertEquals( Action.INPUT, status );
229 // Should have returned an error, with no blacklist pattern removed.
230 assertHasErrors( action );
231 assertEquals( 2, connector.getBlackListPatterns().size() );
233 // Perform test w/invalid (non-existant) pattern value to remove.
234 preRequest( action );
235 action.setPattern( "**/*oops*" );
236 status = action.removeBlackListPattern();
237 assertEquals( Action.INPUT, status );
239 // Should have returned an error, with no blacklist pattern removed.
240 assertHasErrors( action );
241 assertEquals( 2, connector.getBlackListPatterns().size() );
243 // Try again, but now with a valid pattern to remove.
244 action.setPattern( "**/*-javadoc.jar" );
245 preRequest( action );
246 status = action.removeBlackListPattern();
247 assertEquals( Action.INPUT, status );
249 // Should have no error, and 1 blacklist pattern left.
250 assertNoErrors( action );
251 assertEquals( 1, connector.getBlackListPatterns().size() );
252 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
255 public void testRemoveProperty()
258 expectConfigurationRequests( 7 );
259 archivaConfigurationControl.replay();
262 action.setSource( TEST_SOURCE_ID );
263 action.setTarget( TEST_TARGET_ID );
265 ProxyConnectorConfiguration connector = action.getConnector();
266 assertInitialProxyConnector( connector );
268 // Add some arbitrary properties.
269 connector.addProperty( "username", "general-tso" );
270 connector.addProperty( "password", "chicken" );
272 // Perform Test w/no property key.
273 preRequest( action );
274 String status = action.removeProperty();
275 assertEquals( Action.INPUT, status );
277 // Should have returned an error, with no properties removed.
278 assertHasErrors( action );
279 assertEquals( 2, connector.getProperties().size() );
281 // Perform test w/invalid (non-existant) property key to remove.
282 preRequest( action );
283 action.setPropertyKey( "slurm" );
284 status = action.removeProperty();
285 assertEquals( Action.INPUT, status );
287 // Should have returned an error, with no properties removed.
288 assertHasErrors( action );
289 assertEquals( 2, connector.getProperties().size() );
291 // Try again, but now with a valid property to remove.
292 preRequest( action );
293 action.setPropertyKey( "password" );
294 status = action.removeProperty();
295 assertEquals( Action.INPUT, status );
297 // Should have no error, and 1 property left.
298 assertNoErrors( action );
299 assertEquals( 1, connector.getProperties().size() );
300 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
303 public void testRemoveWhiteListPattern()
306 expectConfigurationRequests( 7 );
307 archivaConfigurationControl.replay();
310 action.setSource( TEST_SOURCE_ID );
311 action.setTarget( TEST_TARGET_ID );
313 ProxyConnectorConfiguration connector = action.getConnector();
314 assertInitialProxyConnector( connector );
316 // Add some arbitrary whitelist patterns.
317 connector.addWhiteListPattern( "javax/**/*" );
318 connector.addWhiteListPattern( "com/sun/**/*" );
320 // Perform Test w/no pattern value.
321 preRequest( action );
322 String status = action.removeWhiteListPattern();
323 assertEquals( Action.INPUT, status );
325 // Should have returned an error, with no whitelist pattern removed.
326 assertHasErrors( action );
327 assertEquals( 2, connector.getWhiteListPatterns().size() );
329 // Perform test w/invalid (non-existant) pattern value to remove.
330 preRequest( action );
331 action.setPattern( "**/*oops*" );
332 status = action.removeWhiteListPattern();
333 assertEquals( Action.INPUT, status );
335 // Should have returned an error, with no whitelist pattern removed.
336 assertHasErrors( action );
337 assertEquals( 2, connector.getWhiteListPatterns().size() );
339 // Try again, but now with a valid pattern to remove.
340 action.setPattern( "com/sun/**/*" );
341 preRequest( action );
342 status = action.removeWhiteListPattern();
343 assertEquals( Action.INPUT, status );
345 // Should have no error, and 1 whitelist pattern left.
346 assertNoErrors( action );
347 assertEquals( 1, connector.getWhiteListPatterns().size() );
348 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
351 public void testSecureActionBundle()
354 archivaConfigurationControl.replay();
357 SecureActionBundle bundle = action.getSecureActionBundle();
358 assertTrue( bundle.requiresAuthentication() );
359 assertEquals( 1, bundle.getAuthorizationTuples().size() );
362 private void assertInitialProxyConnector( ProxyConnectorConfiguration connector )
364 assertNotNull( connector );
365 assertNotNull( connector.getBlackListPatterns() );
366 assertNotNull( connector.getWhiteListPatterns() );
367 assertNotNull( connector.getProperties() );
369 assertEquals( TEST_SOURCE_ID, connector.getSourceRepoId() );
370 assertEquals( TEST_TARGET_ID, connector.getTargetRepoId() );
373 private Configuration createInitialConfiguration()
375 Configuration config = new Configuration();
377 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
378 managedRepo.setId( TEST_SOURCE_ID );
379 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
380 managedRepo.setReleases( true );
382 config.addManagedRepository( managedRepo );
384 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
385 remoteRepo.setId( TEST_TARGET_ID );
386 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
388 config.addRemoteRepository( remoteRepo );
390 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
391 connector.setSourceRepoId( TEST_SOURCE_ID );
392 connector.setTargetRepoId( TEST_TARGET_ID );
394 // TODO: Set these options programatically via list of available policies.
395 connector.getPolicies().put( "releases", new ReleasesPolicy().getDefaultOption() );
396 connector.getPolicies().put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
397 connector.getPolicies().put( "checksum", new ChecksumPolicy().getDefaultOption() );
398 connector.getPolicies().put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
400 config.addProxyConnector( connector );
405 private void expectConfigurationRequests( int requestConfigCount )
406 throws RegistryException, IndeterminateConfigurationException
408 Configuration config = createInitialConfiguration();
410 for ( int i = 0; i < requestConfigCount; i++ )
412 archivaConfiguration.getConfiguration();
413 archivaConfigurationControl.setReturnValue( config );
416 archivaConfiguration.save( config );
420 protected void setUp()
425 action = (EditProxyConnectorAction) lookup( Action.class.getName(), "editProxyConnectorAction" );
427 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
428 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
429 action.setArchivaConfiguration( archivaConfiguration );
431 /* Configuration will be requested at least 3 times. */
432 for ( int i = 0; i < 3; i++ )
434 archivaConfiguration.getConfiguration();
435 archivaConfigurationControl.setReturnValue( new Configuration() );