1 package org.apache.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.archiva.admin.model.beans.ProxyConnector;
24 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
25 import org.apache.archiva.admin.repository.proxyconnector.DefaultProxyConnectorAdmin;
26 import org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.configuration.Configuration;
29 import org.apache.archiva.configuration.IndeterminateConfigurationException;
30 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
33 import org.apache.archiva.policies.CachedFailuresPolicy;
34 import org.apache.archiva.policies.ChecksumPolicy;
35 import org.apache.archiva.policies.PropagateErrorsDownloadPolicy;
36 import org.apache.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
37 import org.apache.archiva.policies.ReleasesPolicy;
38 import org.apache.archiva.policies.SnapshotsPolicy;
39 import org.apache.archiva.web.action.AbstractWebworkTestCase;
40 import org.codehaus.plexus.registry.RegistryException;
41 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
42 import org.easymock.MockControl;
44 import java.util.List;
48 * AddProxyConnectorActionTest
52 public class AddProxyConnectorActionTest
53 extends AbstractWebworkTestCase
55 private AddProxyConnectorAction action;
57 private MockControl archivaConfigurationControl;
59 private ArchivaConfiguration archivaConfiguration;
62 protected void setUp()
67 action = (AddProxyConnectorAction) getActionProxy( "/admin/addProxyConnector.action" ).getAction();
69 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
70 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
71 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
72 archivaConfiguration );
73 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
74 archivaConfiguration );
75 ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration(
76 archivaConfiguration );
79 public void testAddBlackListPattern()
82 expectConfigurationRequests( 7 );
83 archivaConfigurationControl.replay();
87 ProxyConnector connector = action.getConnector();
88 populateProxyConnector( connector );
90 // Perform Test w/no values.
92 String status = action.addBlackListPattern();
93 assertEquals( Action.INPUT, status );
95 // Should have returned an error, with no blacklist pattern added.
96 assertHasErrors( action );
97 assertEquals( 0, connector.getBlackListPatterns().size() );
99 // Try again, but now with a pattern to add.
100 action.setBlackListPattern( "**/*-javadoc.jar" );
101 preRequest( action );
102 status = action.addBlackListPattern();
103 assertEquals( Action.INPUT, status );
105 // Should have no error, and 1 blacklist pattern added.
106 assertNoErrors( action );
107 assertEquals( 1, connector.getBlackListPatterns().size() );
110 public void testAddProperty()
113 expectConfigurationRequests( 7 );
114 archivaConfigurationControl.replay();
118 ProxyConnector connector = action.getConnector();
119 populateProxyConnector( connector );
121 // Perform Test w/no values.
122 preRequest( action );
123 String status = action.addProperty();
124 assertEquals( Action.INPUT, status );
126 // Should have returned an error, with no property pattern added.
127 assertHasErrors( action );
128 assertEquals( 0, connector.getProperties().size() );
130 // Try again, but now with a property key/value to add.
131 action.setPropertyKey( "eat-a" );
132 action.setPropertyValue( "gramov-a-bits" );
133 preRequest( action );
134 status = action.addProperty();
135 assertEquals( Action.INPUT, status );
137 // Should have no error, and 1 property added.
138 assertNoErrors( action );
139 assertEquals( 1, connector.getProperties().size() );
142 @SuppressWarnings( "unchecked" )
143 public void testAddProxyConnectorCommit()
146 expectConfigurationRequests( 9 );
147 archivaConfigurationControl.replay();
151 ProxyConnector connector = action.getConnector();
152 populateProxyConnector( connector );
153 // forms will use an array
154 //connector.getProperties().put( "eat-a", new String[] { "gramov-a-bits" } );
155 // FIXME olamy check the array mode !!!
156 connector.getProperties().put( "eat-a", "gramov-a-bits" );
158 // Create the input screen.
159 assertRequestStatus( action, Action.SUCCESS, "commit" );
160 assertNoErrors( action );
162 // Test configuration.
163 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
164 assertNotNull( proxyConfigs );
165 assertEquals( 1, proxyConfigs.size() );
167 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
169 assertNotNull( actualConnector );
170 // The use of "(direct connection)" should result in a proxyId which is <null>.
171 assertNull( actualConnector.getProxyId() );
172 assertEquals( "corporate", actualConnector.getSourceRepoId() );
173 assertEquals( "central", actualConnector.getTargetRepoId() );
174 assertEquals( "gramov-a-bits", actualConnector.getProperties().get( "eat-a" ) );
177 public void testAddProxyConnectorInitialPage()
180 expectConfigurationRequests( 3 );
181 archivaConfigurationControl.replay();
184 ProxyConnector configuration = action.getConnector();
185 assertNotNull( configuration );
186 assertNull( configuration.getProxyId() );
187 assertNull( configuration.getSourceRepoId() );
188 assertNull( configuration.getTargetRepoId() );
189 assertTrue( configuration.getPolicies().isEmpty() );
190 assertTrue( configuration.getProperties().isEmpty() );
191 assertTrue( configuration.getBlackListPatterns().isEmpty() );
192 assertTrue( configuration.getWhiteListPatterns().isEmpty() );
194 String status = action.input();
195 assertEquals( Action.INPUT, status );
198 public void testAddWhiteListPattern()
201 expectConfigurationRequests( 7 );
202 archivaConfigurationControl.replay();
206 ProxyConnector connector = action.getConnector();
207 populateProxyConnector( connector );
209 // Perform Test w/no values.
210 preRequest( action );
211 String status = action.addWhiteListPattern();
212 assertEquals( Action.INPUT, status );
214 // Should have returned an error, with no whitelist pattern added.
215 assertHasErrors( action );
216 assertEquals( 0, connector.getWhiteListPatterns().size() );
218 // Try again, but now with a pattern to add.
219 action.setWhiteListPattern( "**/*.jar" );
220 preRequest( action );
221 status = action.addWhiteListPattern();
222 assertEquals( Action.INPUT, status );
224 // Should have no error, and 1 whitelist pattern added.
225 assertNoErrors( action );
226 assertEquals( 1, connector.getWhiteListPatterns().size() );
229 public void testRemoveBlackListPattern()
232 expectConfigurationRequests( 7 );
233 archivaConfigurationControl.replay();
237 ProxyConnector connector = action.getConnector();
238 populateProxyConnector( connector );
240 // Add some arbitrary blacklist patterns.
241 connector.addBlackListPattern( "**/*-javadoc.jar" );
242 connector.addBlackListPattern( "**/*.war" );
244 // Perform Test w/no pattern value.
245 preRequest( action );
246 String 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 // Perform test w/invalid (non-existant) pattern value to remove.
254 preRequest( action );
255 action.setPattern( "**/*oops*" );
256 status = action.removeBlackListPattern();
257 assertEquals( Action.INPUT, status );
259 // Should have returned an error, with no blacklist pattern removed.
260 assertHasErrors( action );
261 assertEquals( 2, connector.getBlackListPatterns().size() );
263 // Try again, but now with a valid pattern to remove.
264 action.setPattern( "**/*-javadoc.jar" );
265 preRequest( action );
266 status = action.removeBlackListPattern();
267 assertEquals( Action.INPUT, status );
269 // Should have no error, and 1 blacklist pattern left.
270 assertNoErrors( action );
271 assertEquals( 1, connector.getBlackListPatterns().size() );
272 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
275 public void testRemoveProperty()
278 expectConfigurationRequests( 7 );
279 archivaConfigurationControl.replay();
283 ProxyConnector connector = action.getConnector();
284 populateProxyConnector( connector );
286 // Add some arbitrary properties.
287 connector.addProperty( "username", "general-tso" );
288 connector.addProperty( "password", "chicken" );
290 // Perform Test w/no property key.
291 preRequest( action );
292 String 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 // Perform test w/invalid (non-existant) property key to remove.
300 preRequest( action );
301 action.setPropertyKey( "slurm" );
302 status = action.removeProperty();
303 assertEquals( Action.INPUT, status );
305 // Should have returned an error, with no properties removed.
306 assertHasErrors( action );
307 assertEquals( 2, connector.getProperties().size() );
309 // Try again, but now with a valid property to remove.
310 preRequest( action );
311 action.setPropertyKey( "password" );
312 status = action.removeProperty();
313 assertEquals( Action.INPUT, status );
315 // Should have no error, and 1 property left.
316 assertNoErrors( action );
317 assertEquals( 1, connector.getProperties().size() );
318 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
321 public void testRemoveWhiteListPattern()
324 expectConfigurationRequests( 7 );
325 archivaConfigurationControl.replay();
329 ProxyConnector connector = action.getConnector();
330 populateProxyConnector( connector );
332 // Add some arbitrary whitelist patterns.
333 connector.addWhiteListPattern( "javax/**/*" );
334 connector.addWhiteListPattern( "com/sun/**/*" );
336 // Perform Test w/no pattern value.
337 preRequest( action );
338 String 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 // Perform test w/invalid (non-existant) pattern value to remove.
346 preRequest( action );
347 action.setPattern( "**/*oops*" );
348 status = action.removeWhiteListPattern();
349 assertEquals( Action.INPUT, status );
351 // Should have returned an error, with no whitelist pattern removed.
352 assertHasErrors( action );
353 assertEquals( 2, connector.getWhiteListPatterns().size() );
355 // Try again, but now with a valid pattern to remove.
356 action.setPattern( "com/sun/**/*" );
357 preRequest( action );
358 status = action.removeWhiteListPattern();
359 assertEquals( Action.INPUT, status );
361 // Should have no error, and 1 whitelist pattern left.
362 assertNoErrors( action );
363 assertEquals( 1, connector.getWhiteListPatterns().size() );
364 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
367 public void testSecureActionBundle()
370 expectConfigurationRequests( 3 );
371 archivaConfigurationControl.replay();
374 SecureActionBundle bundle = action.getSecureActionBundle();
375 assertTrue( bundle.requiresAuthentication() );
376 assertEquals( 1, bundle.getAuthorizationTuples().size() );
379 private Configuration createInitialConfiguration()
381 Configuration config = new Configuration();
383 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
384 managedRepo.setId( "corporate" );
385 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
386 managedRepo.setReleases( true );
388 config.addManagedRepository( managedRepo );
390 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
391 remoteRepo.setId( "central" );
392 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
394 config.addRemoteRepository( remoteRepo );
399 private void expectConfigurationRequests( int requestConfigCount )
400 throws RegistryException, IndeterminateConfigurationException
402 Configuration config = createInitialConfiguration();
404 for ( int i = 0; i < requestConfigCount; i++ )
406 archivaConfiguration.getConfiguration();
407 archivaConfigurationControl.setReturnValue( config );
410 archivaConfiguration.save( config );
413 @SuppressWarnings( "unchecked" )
414 private void populateProxyConnector( ProxyConnector connector )
416 connector.setProxyId( AbstractProxyConnectorFormAction.DIRECT_CONNECTION );
417 connector.setSourceRepoId( "corporate" );
418 connector.setTargetRepoId( "central" );
420 // TODO: Set these options programatically via list of available policies.
421 Map<String, String> policies = connector.getPolicies();
422 policies.put( "releases", new ReleasesPolicy().getDefaultOption() );
423 policies.put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
424 policies.put( "checksum", new ChecksumPolicy().getDefaultOption() );
425 policies.put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
426 policies.put( "propagate-errors", new PropagateErrorsDownloadPolicy().getDefaultOption() );
427 policies.put( "propagate-errors-on-update", new PropagateErrorsOnUpdateDownloadPolicy().getDefaultOption() );