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 * AddProxyConnectorActionTest
48 public class AddProxyConnectorActionTest
49 extends AbstractWebworkTestCase
51 private AddProxyConnectorAction action;
53 private MockControl archivaConfigurationControl;
55 private ArchivaConfiguration archivaConfiguration;
58 protected void setUp()
63 action = (AddProxyConnectorAction) getActionProxy( "/admin/addProxyConnector.action" ).getAction();
64 //action = (AddProxyConnectorAction) lookup( Action.class.getName(), "addProxyConnectorAction" );
66 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
67 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
68 action.setArchivaConfiguration( archivaConfiguration );
71 public void testAddBlackListPattern()
74 expectConfigurationRequests( 7 );
75 archivaConfigurationControl.replay();
79 ProxyConnectorConfiguration connector = action.getConnector();
80 populateProxyConnector( connector );
82 // Perform Test w/no values.
84 String status = action.addBlackListPattern();
85 assertEquals( Action.INPUT, status );
87 // Should have returned an error, with no blacklist pattern added.
88 assertHasErrors( action );
89 assertEquals( 0, connector.getBlackListPatterns().size() );
91 // Try again, but now with a pattern to add.
92 action.setBlackListPattern( "**/*-javadoc.jar" );
94 status = action.addBlackListPattern();
95 assertEquals( Action.INPUT, status );
97 // Should have no error, and 1 blacklist pattern added.
98 assertNoErrors( action );
99 assertEquals( 1, connector.getBlackListPatterns().size() );
102 public void testAddProperty()
105 expectConfigurationRequests( 7 );
106 archivaConfigurationControl.replay();
110 ProxyConnectorConfiguration connector = action.getConnector();
111 populateProxyConnector( connector );
113 // Perform Test w/no values.
114 preRequest( action );
115 String status = action.addProperty();
116 assertEquals( Action.INPUT, status );
118 // Should have returned an error, with no property pattern added.
119 assertHasErrors( action );
120 assertEquals( 0, connector.getProperties().size() );
122 // Try again, but now with a property key/value to add.
123 action.setPropertyKey( "eat-a" );
124 action.setPropertyValue( "gramov-a-bits" );
125 preRequest( action );
126 status = action.addProperty();
127 assertEquals( Action.INPUT, status );
129 // Should have no error, and 1 property added.
130 assertNoErrors( action );
131 assertEquals( 1, connector.getProperties().size() );
134 @SuppressWarnings("unchecked")
135 public void testAddProxyConnectorCommit()
138 expectConfigurationRequests( 7 );
139 archivaConfigurationControl.replay();
143 ProxyConnectorConfiguration connector = action.getConnector();
144 populateProxyConnector( connector );
145 // forms will use an array
146 connector.getProperties().put( "eat-a", new String[] { "gramov-a-bits" } );
148 // Create the input screen.
149 assertRequestStatus( action, Action.SUCCESS, "commit" );
150 assertNoErrors( action );
152 // Test configuration.
153 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
154 assertNotNull( proxyConfigs );
155 assertEquals( 1, proxyConfigs.size() );
157 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
159 assertNotNull( actualConnector );
160 // The use of "(direct connection)" should result in a proxyId which is <null>.
161 assertNull( actualConnector.getProxyId() );
162 assertEquals( "corporate", actualConnector.getSourceRepoId() );
163 assertEquals( "central", actualConnector.getTargetRepoId() );
164 assertEquals( "gramov-a-bits", actualConnector.getProperties().get( "eat-a" ) );
167 public void testAddProxyConnectorInitialPage()
170 expectConfigurationRequests( 3 );
171 archivaConfigurationControl.replay();
174 ProxyConnectorConfiguration configuration = action.getConnector();
175 assertNotNull( configuration );
176 assertNull( configuration.getProxyId() );
177 assertNull( configuration.getSourceRepoId() );
178 assertNull( configuration.getTargetRepoId() );
179 assertTrue( configuration.getPolicies().isEmpty() );
180 assertTrue( configuration.getProperties().isEmpty() );
181 assertTrue( configuration.getBlackListPatterns().isEmpty() );
182 assertTrue( configuration.getWhiteListPatterns().isEmpty() );
184 String status = action.input();
185 assertEquals( Action.INPUT, status );
188 public void testAddWhiteListPattern()
191 expectConfigurationRequests( 7 );
192 archivaConfigurationControl.replay();
196 ProxyConnectorConfiguration connector = action.getConnector();
197 populateProxyConnector( connector );
199 // Perform Test w/no values.
200 preRequest( action );
201 String status = action.addWhiteListPattern();
202 assertEquals( Action.INPUT, status );
204 // Should have returned an error, with no whitelist pattern added.
205 assertHasErrors( action );
206 assertEquals( 0, connector.getWhiteListPatterns().size() );
208 // Try again, but now with a pattern to add.
209 action.setWhiteListPattern( "**/*.jar" );
210 preRequest( action );
211 status = action.addWhiteListPattern();
212 assertEquals( Action.INPUT, status );
214 // Should have no error, and 1 whitelist pattern added.
215 assertNoErrors( action );
216 assertEquals( 1, connector.getWhiteListPatterns().size() );
219 public void testRemoveBlackListPattern()
222 expectConfigurationRequests( 7 );
223 archivaConfigurationControl.replay();
227 ProxyConnectorConfiguration connector = action.getConnector();
228 populateProxyConnector( connector );
230 // Add some arbitrary blacklist patterns.
231 connector.addBlackListPattern( "**/*-javadoc.jar" );
232 connector.addBlackListPattern( "**/*.war" );
234 // Perform Test w/no pattern value.
235 preRequest( action );
236 String 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 // Perform test w/invalid (non-existant) pattern value to remove.
244 preRequest( action );
245 action.setPattern( "**/*oops*" );
246 status = action.removeBlackListPattern();
247 assertEquals( Action.INPUT, status );
249 // Should have returned an error, with no blacklist pattern removed.
250 assertHasErrors( action );
251 assertEquals( 2, connector.getBlackListPatterns().size() );
253 // Try again, but now with a valid pattern to remove.
254 action.setPattern( "**/*-javadoc.jar" );
255 preRequest( action );
256 status = action.removeBlackListPattern();
257 assertEquals( Action.INPUT, status );
259 // Should have no error, and 1 blacklist pattern left.
260 assertNoErrors( action );
261 assertEquals( 1, connector.getBlackListPatterns().size() );
262 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
265 public void testRemoveProperty()
268 expectConfigurationRequests( 7 );
269 archivaConfigurationControl.replay();
273 ProxyConnectorConfiguration connector = action.getConnector();
274 populateProxyConnector( connector );
276 // Add some arbitrary properties.
277 connector.addProperty( "username", "general-tso" );
278 connector.addProperty( "password", "chicken" );
280 // Perform Test w/no property key.
281 preRequest( action );
282 String status = action.removeProperty();
283 assertEquals( Action.INPUT, status );
285 // Should have returned an error, with no properties removed.
286 assertHasErrors( action );
287 assertEquals( 2, connector.getProperties().size() );
289 // Perform test w/invalid (non-existant) property key to remove.
290 preRequest( action );
291 action.setPropertyKey( "slurm" );
292 status = action.removeProperty();
293 assertEquals( Action.INPUT, status );
295 // Should have returned an error, with no properties removed.
296 assertHasErrors( action );
297 assertEquals( 2, connector.getProperties().size() );
299 // Try again, but now with a valid property to remove.
300 preRequest( action );
301 action.setPropertyKey( "password" );
302 status = action.removeProperty();
303 assertEquals( Action.INPUT, status );
305 // Should have no error, and 1 property left.
306 assertNoErrors( action );
307 assertEquals( 1, connector.getProperties().size() );
308 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
311 public void testRemoveWhiteListPattern()
314 expectConfigurationRequests( 7 );
315 archivaConfigurationControl.replay();
319 ProxyConnectorConfiguration connector = action.getConnector();
320 populateProxyConnector( 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 expectConfigurationRequests( 3 );
361 archivaConfigurationControl.replay();
364 SecureActionBundle bundle = action.getSecureActionBundle();
365 assertTrue( bundle.requiresAuthentication() );
366 assertEquals( 1, bundle.getAuthorizationTuples().size() );
369 private Configuration createInitialConfiguration()
371 Configuration config = new Configuration();
373 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
374 managedRepo.setId( "corporate" );
375 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
376 managedRepo.setReleases( true );
378 config.addManagedRepository( managedRepo );
380 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
381 remoteRepo.setId( "central" );
382 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
384 config.addRemoteRepository( remoteRepo );
389 private void expectConfigurationRequests( int requestConfigCount )
390 throws RegistryException, IndeterminateConfigurationException
392 Configuration config = createInitialConfiguration();
394 for ( int i = 0; i < requestConfigCount; i++ )
396 archivaConfiguration.getConfiguration();
397 archivaConfigurationControl.setReturnValue( config );
400 archivaConfiguration.save( config );
403 @SuppressWarnings("unchecked")
404 private void populateProxyConnector( ProxyConnectorConfiguration connector )
406 connector.setProxyId( AbstractProxyConnectorFormAction.DIRECT_CONNECTION );
407 connector.setSourceRepoId( "corporate" );
408 connector.setTargetRepoId( "central" );
410 // TODO: Set these options programatically via list of available policies.
411 Map<String, String> policies = connector.getPolicies();
412 policies.put( "releases", new ReleasesPolicy().getDefaultOption() );
413 policies.put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
414 policies.put( "checksum", new ChecksumPolicy().getDefaultOption() );
415 policies.put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
416 policies.put( "propagate-errors", new PropagateErrorsDownloadPolicy().getDefaultOption() );
417 policies.put( "propagate-errors-on-update", new PropagateErrorsOnUpdateDownloadPolicy().getDefaultOption() );