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.apache.archiva.redback.integration.interceptor.SecureActionBundle;
41 import org.easymock.MockControl;
43 import java.util.List;
47 * AddProxyConnectorActionTest
51 public class AddProxyConnectorActionTest
52 extends AbstractWebworkTestCase
54 private AddProxyConnectorAction action;
56 private MockControl archivaConfigurationControl;
58 private ArchivaConfiguration archivaConfiguration;
61 protected void setUp()
66 action = (AddProxyConnectorAction) getActionProxy( "/admin/addProxyConnector.action" ).getAction();
68 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
69 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
70 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
71 archivaConfiguration );
72 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
73 archivaConfiguration );
74 ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration(
75 archivaConfiguration );
78 public void testAddBlackListPattern()
81 expectConfigurationRequests( 7 );
82 archivaConfigurationControl.replay();
86 ProxyConnector connector = action.getConnector();
87 populateProxyConnector( connector );
89 // Perform Test w/no values.
91 String status = action.addBlackListPattern();
92 assertEquals( Action.INPUT, status );
94 // Should have returned an error, with no blacklist pattern added.
95 assertHasErrors( action );
96 assertEquals( 0, connector.getBlackListPatterns().size() );
98 // Try again, but now with a pattern to add.
99 action.setBlackListPattern( "**/*-javadoc.jar" );
100 preRequest( action );
101 status = action.addBlackListPattern();
102 assertEquals( Action.INPUT, status );
104 // Should have no error, and 1 blacklist pattern added.
105 assertNoErrors( action );
106 assertEquals( 1, connector.getBlackListPatterns().size() );
109 public void testAddProperty()
112 expectConfigurationRequests( 7 );
113 archivaConfigurationControl.replay();
117 ProxyConnector connector = action.getConnector();
118 populateProxyConnector( connector );
120 // Perform Test w/no values.
121 preRequest( action );
122 String status = action.addProperty();
123 assertEquals( Action.INPUT, status );
125 // Should have returned an error, with no property pattern added.
126 assertHasErrors( action );
127 assertEquals( 0, connector.getProperties().size() );
129 // Try again, but now with a property key/value to add.
130 action.setPropertyKey( "eat-a" );
131 action.setPropertyValue( "gramov-a-bits" );
132 preRequest( action );
133 status = action.addProperty();
134 assertEquals( Action.INPUT, status );
136 // Should have no error, and 1 property added.
137 assertNoErrors( action );
138 assertEquals( 1, connector.getProperties().size() );
141 @SuppressWarnings( "unchecked" )
142 public void testAddProxyConnectorCommit()
145 expectConfigurationRequests( 9 );
146 archivaConfigurationControl.replay();
150 ProxyConnector connector = action.getConnector();
151 populateProxyConnector( connector );
152 // forms will use an array
153 //connector.getProperties().put( "eat-a", new String[] { "gramov-a-bits" } );
154 // FIXME olamy check the array mode !!!
155 connector.getProperties().put( "eat-a", "gramov-a-bits" );
157 // Create the input screen.
158 assertRequestStatus( action, Action.SUCCESS, "commit" );
159 assertNoErrors( action );
161 // Test configuration.
162 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
163 assertNotNull( proxyConfigs );
164 assertEquals( 1, proxyConfigs.size() );
166 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
168 assertNotNull( actualConnector );
169 // The use of "(direct connection)" should result in a proxyId which is <null>.
170 assertNull( actualConnector.getProxyId() );
171 assertEquals( "corporate", actualConnector.getSourceRepoId() );
172 assertEquals( "central", actualConnector.getTargetRepoId() );
173 assertEquals( "gramov-a-bits", actualConnector.getProperties().get( "eat-a" ) );
176 public void testAddProxyConnectorInitialPage()
179 expectConfigurationRequests( 3 );
180 archivaConfigurationControl.replay();
183 ProxyConnector configuration = action.getConnector();
184 assertNotNull( configuration );
185 assertNull( configuration.getProxyId() );
186 assertNull( configuration.getSourceRepoId() );
187 assertNull( configuration.getTargetRepoId() );
188 assertTrue( configuration.getPolicies().isEmpty() );
189 assertTrue( configuration.getProperties().isEmpty() );
190 assertTrue( configuration.getBlackListPatterns().isEmpty() );
191 assertTrue( configuration.getWhiteListPatterns().isEmpty() );
193 String status = action.input();
194 assertEquals( Action.INPUT, status );
197 public void testAddWhiteListPattern()
200 expectConfigurationRequests( 7 );
201 archivaConfigurationControl.replay();
205 ProxyConnector connector = action.getConnector();
206 populateProxyConnector( connector );
208 // Perform Test w/no values.
209 preRequest( action );
210 String status = action.addWhiteListPattern();
211 assertEquals( Action.INPUT, status );
213 // Should have returned an error, with no whitelist pattern added.
214 assertHasErrors( action );
215 assertEquals( 0, connector.getWhiteListPatterns().size() );
217 // Try again, but now with a pattern to add.
218 action.setWhiteListPattern( "**/*.jar" );
219 preRequest( action );
220 status = action.addWhiteListPattern();
221 assertEquals( Action.INPUT, status );
223 // Should have no error, and 1 whitelist pattern added.
224 assertNoErrors( action );
225 assertEquals( 1, connector.getWhiteListPatterns().size() );
228 public void testRemoveBlackListPattern()
231 expectConfigurationRequests( 7 );
232 archivaConfigurationControl.replay();
236 ProxyConnector connector = action.getConnector();
237 populateProxyConnector( connector );
239 // Add some arbitrary blacklist patterns.
240 connector.addBlackListPattern( "**/*-javadoc.jar" );
241 connector.addBlackListPattern( "**/*.war" );
243 // Perform Test w/no pattern value.
244 preRequest( action );
245 String status = action.removeBlackListPattern();
246 assertEquals( Action.INPUT, status );
248 // Should have returned an error, with no blacklist pattern removed.
249 assertHasErrors( action );
250 assertEquals( 2, connector.getBlackListPatterns().size() );
252 // Perform test w/invalid (non-existant) pattern value to remove.
253 preRequest( action );
254 action.setPattern( "**/*oops*" );
255 status = action.removeBlackListPattern();
256 assertEquals( Action.INPUT, status );
258 // Should have returned an error, with no blacklist pattern removed.
259 assertHasErrors( action );
260 assertEquals( 2, connector.getBlackListPatterns().size() );
262 // Try again, but now with a valid pattern to remove.
263 action.setPattern( "**/*-javadoc.jar" );
264 preRequest( action );
265 status = action.removeBlackListPattern();
266 assertEquals( Action.INPUT, status );
268 // Should have no error, and 1 blacklist pattern left.
269 assertNoErrors( action );
270 assertEquals( 1, connector.getBlackListPatterns().size() );
271 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
274 public void testRemoveProperty()
277 expectConfigurationRequests( 7 );
278 archivaConfigurationControl.replay();
282 ProxyConnector connector = action.getConnector();
283 populateProxyConnector( connector );
285 // Add some arbitrary properties.
286 connector.addProperty( "username", "general-tso" );
287 connector.addProperty( "password", "chicken" );
289 // Perform Test w/no property key.
290 preRequest( action );
291 String status = action.removeProperty();
292 assertEquals( Action.INPUT, status );
294 // Should have returned an error, with no properties removed.
295 assertHasErrors( action );
296 assertEquals( 2, connector.getProperties().size() );
298 // Perform test w/invalid (non-existant) property key to remove.
299 preRequest( action );
300 action.setPropertyKey( "slurm" );
301 status = action.removeProperty();
302 assertEquals( Action.INPUT, status );
304 // Should have returned an error, with no properties removed.
305 assertHasErrors( action );
306 assertEquals( 2, connector.getProperties().size() );
308 // Try again, but now with a valid property to remove.
309 preRequest( action );
310 action.setPropertyKey( "password" );
311 status = action.removeProperty();
312 assertEquals( Action.INPUT, status );
314 // Should have no error, and 1 property left.
315 assertNoErrors( action );
316 assertEquals( 1, connector.getProperties().size() );
317 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
320 public void testRemoveWhiteListPattern()
323 expectConfigurationRequests( 7 );
324 archivaConfigurationControl.replay();
328 ProxyConnector connector = action.getConnector();
329 populateProxyConnector( connector );
331 // Add some arbitrary whitelist patterns.
332 connector.addWhiteListPattern( "javax/**/*" );
333 connector.addWhiteListPattern( "com/sun/**/*" );
335 // Perform Test w/no pattern value.
336 preRequest( action );
337 String status = action.removeWhiteListPattern();
338 assertEquals( Action.INPUT, status );
340 // Should have returned an error, with no whitelist pattern removed.
341 assertHasErrors( action );
342 assertEquals( 2, connector.getWhiteListPatterns().size() );
344 // Perform test w/invalid (non-existant) pattern value to remove.
345 preRequest( action );
346 action.setPattern( "**/*oops*" );
347 status = action.removeWhiteListPattern();
348 assertEquals( Action.INPUT, status );
350 // Should have returned an error, with no whitelist pattern removed.
351 assertHasErrors( action );
352 assertEquals( 2, connector.getWhiteListPatterns().size() );
354 // Try again, but now with a valid pattern to remove.
355 action.setPattern( "com/sun/**/*" );
356 preRequest( action );
357 status = action.removeWhiteListPattern();
358 assertEquals( Action.INPUT, status );
360 // Should have no error, and 1 whitelist pattern left.
361 assertNoErrors( action );
362 assertEquals( 1, connector.getWhiteListPatterns().size() );
363 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
366 public void testSecureActionBundle()
369 expectConfigurationRequests( 3 );
370 archivaConfigurationControl.replay();
373 SecureActionBundle bundle = action.getSecureActionBundle();
374 assertTrue( bundle.requiresAuthentication() );
375 assertEquals( 1, bundle.getAuthorizationTuples().size() );
378 private Configuration createInitialConfiguration()
380 Configuration config = new Configuration();
382 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
383 managedRepo.setId( "corporate" );
384 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
385 managedRepo.setReleases( true );
387 config.addManagedRepository( managedRepo );
389 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
390 remoteRepo.setId( "central" );
391 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
393 config.addRemoteRepository( remoteRepo );
398 private void expectConfigurationRequests( int requestConfigCount )
399 throws org.apache.archiva.redback.components.registry.RegistryException, IndeterminateConfigurationException
401 Configuration config = createInitialConfiguration();
402 archivaConfiguration.getConfiguration();
403 archivaConfigurationControl.setReturnValue( config, requestConfigCount, requestConfigCount + 1 );
404 archivaConfiguration.save( config );
407 @SuppressWarnings( "unchecked" )
408 private void populateProxyConnector( ProxyConnector connector )
410 connector.setProxyId( AbstractProxyConnectorFormAction.DIRECT_CONNECTION );
411 connector.setSourceRepoId( "corporate" );
412 connector.setTargetRepoId( "central" );
414 // TODO: Set these options programatically via list of available policies.
415 Map<String, String> policies = connector.getPolicies();
416 policies.put( "releases", new ReleasesPolicy().getDefaultOption() );
417 policies.put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
418 policies.put( "checksum", new ChecksumPolicy().getDefaultOption() );
419 policies.put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
420 policies.put( "propagate-errors", new PropagateErrorsDownloadPolicy().getDefaultOption() );
421 policies.put( "propagate-errors-on-update", new PropagateErrorsOnUpdateDownloadPolicy().getDefaultOption() );