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.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
24 import org.apache.archiva.admin.repository.proxyconnector.DefaultProxyConnectorAdmin;
25 import org.apache.archiva.admin.repository.proxyconnector.ProxyConnector;
26 import org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.Configuration;
29 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
30 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
33 import org.apache.maven.archiva.policies.CachedFailuresPolicy;
34 import org.apache.maven.archiva.policies.ChecksumPolicy;
35 import org.apache.maven.archiva.policies.PropagateErrorsDownloadPolicy;
36 import org.apache.maven.archiva.policies.PropagateErrorsOnUpdateDownloadPolicy;
37 import org.apache.maven.archiva.policies.ReleasesPolicy;
38 import org.apache.maven.archiva.policies.SnapshotsPolicy;
39 import org.apache.maven.archiva.web.action.AbstractWebworkTestCase;
40 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
41 import org.codehaus.plexus.registry.RegistryException;
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 action.setArchivaConfiguration( archivaConfiguration );
72 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
73 archivaConfiguration );
74 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
75 archivaConfiguration );
76 ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration(
77 archivaConfiguration );
80 public void testAddBlackListPattern()
83 expectConfigurationRequests( 7 );
84 archivaConfigurationControl.replay();
88 ProxyConnector connector = action.getConnector();
89 populateProxyConnector( connector );
91 // Perform Test w/no values.
93 String status = action.addBlackListPattern();
94 assertEquals( Action.INPUT, status );
96 // Should have returned an error, with no blacklist pattern added.
97 assertHasErrors( action );
98 assertEquals( 0, connector.getBlackListPatterns().size() );
100 // Try again, but now with a pattern to add.
101 action.setBlackListPattern( "**/*-javadoc.jar" );
102 preRequest( action );
103 status = action.addBlackListPattern();
104 assertEquals( Action.INPUT, status );
106 // Should have no error, and 1 blacklist pattern added.
107 assertNoErrors( action );
108 assertEquals( 1, connector.getBlackListPatterns().size() );
111 public void testAddProperty()
114 expectConfigurationRequests( 7 );
115 archivaConfigurationControl.replay();
119 ProxyConnector connector = action.getConnector();
120 populateProxyConnector( connector );
122 // Perform Test w/no values.
123 preRequest( action );
124 String status = action.addProperty();
125 assertEquals( Action.INPUT, status );
127 // Should have returned an error, with no property pattern added.
128 assertHasErrors( action );
129 assertEquals( 0, connector.getProperties().size() );
131 // Try again, but now with a property key/value to add.
132 action.setPropertyKey( "eat-a" );
133 action.setPropertyValue( "gramov-a-bits" );
134 preRequest( action );
135 status = action.addProperty();
136 assertEquals( Action.INPUT, status );
138 // Should have no error, and 1 property added.
139 assertNoErrors( action );
140 assertEquals( 1, connector.getProperties().size() );
143 @SuppressWarnings("unchecked")
144 public void testAddProxyConnectorCommit()
147 expectConfigurationRequests( 9 );
148 archivaConfigurationControl.replay();
152 ProxyConnector connector = action.getConnector();
153 populateProxyConnector( connector );
154 // forms will use an array
155 //connector.getProperties().put( "eat-a", new String[] { "gramov-a-bits" } );
156 // FIXME olamy check the array mode !!!
157 connector.getProperties().put( "eat-a", "gramov-a-bits" );
159 // Create the input screen.
160 assertRequestStatus( action, Action.SUCCESS, "commit" );
161 assertNoErrors( action );
163 // Test configuration.
164 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
165 assertNotNull( proxyConfigs );
166 assertEquals( 1, proxyConfigs.size() );
168 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
170 assertNotNull( actualConnector );
171 // The use of "(direct connection)" should result in a proxyId which is <null>.
172 assertNull( actualConnector.getProxyId() );
173 assertEquals( "corporate", actualConnector.getSourceRepoId() );
174 assertEquals( "central", actualConnector.getTargetRepoId() );
175 assertEquals( "gramov-a-bits", actualConnector.getProperties().get( "eat-a" ) );
178 public void testAddProxyConnectorInitialPage()
181 expectConfigurationRequests( 3 );
182 archivaConfigurationControl.replay();
185 ProxyConnector configuration = action.getConnector();
186 assertNotNull( configuration );
187 assertNull( configuration.getProxyId() );
188 assertNull( configuration.getSourceRepoId() );
189 assertNull( configuration.getTargetRepoId() );
190 assertTrue( configuration.getPolicies().isEmpty() );
191 assertTrue( configuration.getProperties().isEmpty() );
192 assertTrue( configuration.getBlackListPatterns().isEmpty() );
193 assertTrue( configuration.getWhiteListPatterns().isEmpty() );
195 String status = action.input();
196 assertEquals( Action.INPUT, status );
199 public void testAddWhiteListPattern()
202 expectConfigurationRequests( 7 );
203 archivaConfigurationControl.replay();
207 ProxyConnector connector = action.getConnector();
208 populateProxyConnector( connector );
210 // Perform Test w/no values.
211 preRequest( action );
212 String status = action.addWhiteListPattern();
213 assertEquals( Action.INPUT, status );
215 // Should have returned an error, with no whitelist pattern added.
216 assertHasErrors( action );
217 assertEquals( 0, connector.getWhiteListPatterns().size() );
219 // Try again, but now with a pattern to add.
220 action.setWhiteListPattern( "**/*.jar" );
221 preRequest( action );
222 status = action.addWhiteListPattern();
223 assertEquals( Action.INPUT, status );
225 // Should have no error, and 1 whitelist pattern added.
226 assertNoErrors( action );
227 assertEquals( 1, connector.getWhiteListPatterns().size() );
230 public void testRemoveBlackListPattern()
233 expectConfigurationRequests( 7 );
234 archivaConfigurationControl.replay();
238 ProxyConnector connector = action.getConnector();
239 populateProxyConnector( connector );
241 // Add some arbitrary blacklist patterns.
242 connector.addBlackListPattern( "**/*-javadoc.jar" );
243 connector.addBlackListPattern( "**/*.war" );
245 // Perform Test w/no pattern value.
246 preRequest( action );
247 String status = action.removeBlackListPattern();
248 assertEquals( Action.INPUT, status );
250 // Should have returned an error, with no blacklist pattern removed.
251 assertHasErrors( action );
252 assertEquals( 2, connector.getBlackListPatterns().size() );
254 // Perform test w/invalid (non-existant) pattern value to remove.
255 preRequest( action );
256 action.setPattern( "**/*oops*" );
257 status = action.removeBlackListPattern();
258 assertEquals( Action.INPUT, status );
260 // Should have returned an error, with no blacklist pattern removed.
261 assertHasErrors( action );
262 assertEquals( 2, connector.getBlackListPatterns().size() );
264 // Try again, but now with a valid pattern to remove.
265 action.setPattern( "**/*-javadoc.jar" );
266 preRequest( action );
267 status = action.removeBlackListPattern();
268 assertEquals( Action.INPUT, status );
270 // Should have no error, and 1 blacklist pattern left.
271 assertNoErrors( action );
272 assertEquals( 1, connector.getBlackListPatterns().size() );
273 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
276 public void testRemoveProperty()
279 expectConfigurationRequests( 7 );
280 archivaConfigurationControl.replay();
284 ProxyConnector connector = action.getConnector();
285 populateProxyConnector( connector );
287 // Add some arbitrary properties.
288 connector.addProperty( "username", "general-tso" );
289 connector.addProperty( "password", "chicken" );
291 // Perform Test w/no property key.
292 preRequest( action );
293 String status = action.removeProperty();
294 assertEquals( Action.INPUT, status );
296 // Should have returned an error, with no properties removed.
297 assertHasErrors( action );
298 assertEquals( 2, connector.getProperties().size() );
300 // Perform test w/invalid (non-existant) property key to remove.
301 preRequest( action );
302 action.setPropertyKey( "slurm" );
303 status = action.removeProperty();
304 assertEquals( Action.INPUT, status );
306 // Should have returned an error, with no properties removed.
307 assertHasErrors( action );
308 assertEquals( 2, connector.getProperties().size() );
310 // Try again, but now with a valid property to remove.
311 preRequest( action );
312 action.setPropertyKey( "password" );
313 status = action.removeProperty();
314 assertEquals( Action.INPUT, status );
316 // Should have no error, and 1 property left.
317 assertNoErrors( action );
318 assertEquals( 1, connector.getProperties().size() );
319 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
322 public void testRemoveWhiteListPattern()
325 expectConfigurationRequests( 7 );
326 archivaConfigurationControl.replay();
330 ProxyConnector connector = action.getConnector();
331 populateProxyConnector( connector );
333 // Add some arbitrary whitelist patterns.
334 connector.addWhiteListPattern( "javax/**/*" );
335 connector.addWhiteListPattern( "com/sun/**/*" );
337 // Perform Test w/no pattern value.
338 preRequest( action );
339 String status = action.removeWhiteListPattern();
340 assertEquals( Action.INPUT, status );
342 // Should have returned an error, with no whitelist pattern removed.
343 assertHasErrors( action );
344 assertEquals( 2, connector.getWhiteListPatterns().size() );
346 // Perform test w/invalid (non-existant) pattern value to remove.
347 preRequest( action );
348 action.setPattern( "**/*oops*" );
349 status = action.removeWhiteListPattern();
350 assertEquals( Action.INPUT, status );
352 // Should have returned an error, with no whitelist pattern removed.
353 assertHasErrors( action );
354 assertEquals( 2, connector.getWhiteListPatterns().size() );
356 // Try again, but now with a valid pattern to remove.
357 action.setPattern( "com/sun/**/*" );
358 preRequest( action );
359 status = action.removeWhiteListPattern();
360 assertEquals( Action.INPUT, status );
362 // Should have no error, and 1 whitelist pattern left.
363 assertNoErrors( action );
364 assertEquals( 1, connector.getWhiteListPatterns().size() );
365 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
368 public void testSecureActionBundle()
371 expectConfigurationRequests( 3 );
372 archivaConfigurationControl.replay();
375 SecureActionBundle bundle = action.getSecureActionBundle();
376 assertTrue( bundle.requiresAuthentication() );
377 assertEquals( 1, bundle.getAuthorizationTuples().size() );
380 private Configuration createInitialConfiguration()
382 Configuration config = new Configuration();
384 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
385 managedRepo.setId( "corporate" );
386 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
387 managedRepo.setReleases( true );
389 config.addManagedRepository( managedRepo );
391 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
392 remoteRepo.setId( "central" );
393 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
395 config.addRemoteRepository( remoteRepo );
400 private void expectConfigurationRequests( int requestConfigCount )
401 throws RegistryException, IndeterminateConfigurationException
403 Configuration config = createInitialConfiguration();
405 for ( int i = 0; i < requestConfigCount; i++ )
407 archivaConfiguration.getConfiguration();
408 archivaConfigurationControl.setReturnValue( config );
411 archivaConfiguration.save( config );
414 @SuppressWarnings("unchecked")
415 private void populateProxyConnector( ProxyConnector connector )
417 connector.setProxyId( AbstractProxyConnectorFormAction.DIRECT_CONNECTION );
418 connector.setSourceRepoId( "corporate" );
419 connector.setTargetRepoId( "central" );
421 // TODO: Set these options programatically via list of available policies.
422 Map<String, String> policies = connector.getPolicies();
423 policies.put( "releases", new ReleasesPolicy().getDefaultOption() );
424 policies.put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
425 policies.put( "checksum", new ChecksumPolicy().getDefaultOption() );
426 policies.put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
427 policies.put( "propagate-errors", new PropagateErrorsDownloadPolicy().getDefaultOption() );
428 policies.put( "propagate-errors-on-update", new PropagateErrorsOnUpdateDownloadPolicy().getDefaultOption() );