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;
45 import org.junit.Before;
46 import org.junit.Test;
49 * AddProxyConnectorActionTest
53 public class AddProxyConnectorActionTest
54 extends AbstractWebworkTestCase
56 private AddProxyConnectorAction action;
58 private MockControl archivaConfigurationControl;
60 private ArchivaConfiguration archivaConfiguration;
69 action = (AddProxyConnectorAction) getActionProxy( "/admin/addProxyConnector.action" ).getAction();
71 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
72 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
73 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
74 archivaConfiguration );
75 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
76 archivaConfiguration );
77 ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration(
78 archivaConfiguration );
82 public void testAddBlackListPattern()
85 expectConfigurationRequests( 7 );
86 archivaConfigurationControl.replay();
90 ProxyConnector connector = action.getConnector();
91 populateProxyConnector( connector );
93 // Perform Test w/no values.
95 String status = action.addBlackListPattern();
96 assertEquals( Action.INPUT, status );
98 // Should have returned an error, with no blacklist pattern added.
99 assertHasErrors( action );
100 assertEquals( 0, connector.getBlackListPatterns().size() );
102 // Try again, but now with a pattern to add.
103 action.setBlackListPattern( "**/*-javadoc.jar" );
104 preRequest( action );
105 status = action.addBlackListPattern();
106 assertEquals( Action.INPUT, status );
108 // Should have no error, and 1 blacklist pattern added.
109 assertNoErrors( action );
110 assertEquals( 1, connector.getBlackListPatterns().size() );
114 public void testAddProperty()
117 expectConfigurationRequests( 7 );
118 archivaConfigurationControl.replay();
122 ProxyConnector connector = action.getConnector();
123 populateProxyConnector( connector );
125 // Perform Test w/no values.
126 preRequest( action );
127 String status = action.addProperty();
128 assertEquals( Action.INPUT, status );
130 // Should have returned an error, with no property pattern added.
131 assertHasErrors( action );
132 assertEquals( 0, connector.getProperties().size() );
134 // Try again, but now with a property key/value to add.
135 action.setPropertyKey( "eat-a" );
136 action.setPropertyValue( "gramov-a-bits" );
137 preRequest( action );
138 status = action.addProperty();
139 assertEquals( Action.INPUT, status );
141 // Should have no error, and 1 property added.
142 assertNoErrors( action );
143 assertEquals( 1, connector.getProperties().size() );
146 @SuppressWarnings( "unchecked" )
148 public void testAddProxyConnectorCommit()
151 expectConfigurationRequests( 9 );
152 archivaConfigurationControl.replay();
156 ProxyConnector connector = action.getConnector();
157 populateProxyConnector( connector );
158 // forms will use an array
159 //connector.getProperties().put( "eat-a", new String[] { "gramov-a-bits" } );
160 // FIXME olamy check the array mode !!!
161 connector.getProperties().put( "eat-a", "gramov-a-bits" );
163 // Create the input screen.
164 assertRequestStatus( action, Action.SUCCESS, "commit" );
165 assertNoErrors( action );
167 // Test configuration.
168 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
169 assertNotNull( proxyConfigs );
170 assertEquals( 1, proxyConfigs.size() );
172 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
174 assertNotNull( actualConnector );
175 // The use of "(direct connection)" should result in a proxyId which is <null>.
176 assertNull( actualConnector.getProxyId() );
177 assertEquals( "corporate", actualConnector.getSourceRepoId() );
178 assertEquals( "central", actualConnector.getTargetRepoId() );
179 assertEquals( "gramov-a-bits", actualConnector.getProperties().get( "eat-a" ) );
183 public void testAddProxyConnectorInitialPage()
186 expectConfigurationRequests( 3 );
187 archivaConfigurationControl.replay();
190 ProxyConnector configuration = action.getConnector();
191 assertNotNull( configuration );
192 assertNull( configuration.getProxyId() );
193 assertNull( configuration.getSourceRepoId() );
194 assertNull( configuration.getTargetRepoId() );
195 assertTrue( configuration.getPolicies().isEmpty() );
196 assertTrue( configuration.getProperties().isEmpty() );
197 assertTrue( configuration.getBlackListPatterns().isEmpty() );
198 assertTrue( configuration.getWhiteListPatterns().isEmpty() );
200 String status = action.input();
201 assertEquals( Action.INPUT, status );
205 public void testAddWhiteListPattern()
208 expectConfigurationRequests( 7 );
209 archivaConfigurationControl.replay();
213 ProxyConnector connector = action.getConnector();
214 populateProxyConnector( connector );
216 // Perform Test w/no values.
217 preRequest( action );
218 String status = action.addWhiteListPattern();
219 assertEquals( Action.INPUT, status );
221 // Should have returned an error, with no whitelist pattern added.
222 assertHasErrors( action );
223 assertEquals( 0, connector.getWhiteListPatterns().size() );
225 // Try again, but now with a pattern to add.
226 action.setWhiteListPattern( "**/*.jar" );
227 preRequest( action );
228 status = action.addWhiteListPattern();
229 assertEquals( Action.INPUT, status );
231 // Should have no error, and 1 whitelist pattern added.
232 assertNoErrors( action );
233 assertEquals( 1, connector.getWhiteListPatterns().size() );
237 public void testRemoveBlackListPattern()
240 expectConfigurationRequests( 7 );
241 archivaConfigurationControl.replay();
245 ProxyConnector connector = action.getConnector();
246 populateProxyConnector( connector );
248 // Add some arbitrary blacklist patterns.
249 connector.addBlackListPattern( "**/*-javadoc.jar" );
250 connector.addBlackListPattern( "**/*.war" );
252 // Perform Test w/no pattern value.
253 preRequest( action );
254 String status = action.removeBlackListPattern();
255 assertEquals( Action.INPUT, status );
257 // Should have returned an error, with no blacklist pattern removed.
258 assertHasErrors( action );
259 assertEquals( 2, connector.getBlackListPatterns().size() );
261 // Perform test w/invalid (non-existant) pattern value to remove.
262 preRequest( action );
263 action.setPattern( "**/*oops*" );
264 status = action.removeBlackListPattern();
265 assertEquals( Action.INPUT, status );
267 // Should have returned an error, with no blacklist pattern removed.
268 assertHasErrors( action );
269 assertEquals( 2, connector.getBlackListPatterns().size() );
271 // Try again, but now with a valid pattern to remove.
272 action.setPattern( "**/*-javadoc.jar" );
273 preRequest( action );
274 status = action.removeBlackListPattern();
275 assertEquals( Action.INPUT, status );
277 // Should have no error, and 1 blacklist pattern left.
278 assertNoErrors( action );
279 assertEquals( 1, connector.getBlackListPatterns().size() );
280 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
284 public void testRemoveProperty()
287 expectConfigurationRequests( 7 );
288 archivaConfigurationControl.replay();
292 ProxyConnector connector = action.getConnector();
293 populateProxyConnector( connector );
295 // Add some arbitrary properties.
296 connector.addProperty( "username", "general-tso" );
297 connector.addProperty( "password", "chicken" );
299 // Perform Test w/no property key.
300 preRequest( action );
301 String 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 // Perform test w/invalid (non-existant) property key to remove.
309 preRequest( action );
310 action.setPropertyKey( "slurm" );
311 status = action.removeProperty();
312 assertEquals( Action.INPUT, status );
314 // Should have returned an error, with no properties removed.
315 assertHasErrors( action );
316 assertEquals( 2, connector.getProperties().size() );
318 // Try again, but now with a valid property to remove.
319 preRequest( action );
320 action.setPropertyKey( "password" );
321 status = action.removeProperty();
322 assertEquals( Action.INPUT, status );
324 // Should have no error, and 1 property left.
325 assertNoErrors( action );
326 assertEquals( 1, connector.getProperties().size() );
327 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
331 public void testRemoveWhiteListPattern()
334 expectConfigurationRequests( 7 );
335 archivaConfigurationControl.replay();
339 ProxyConnector connector = action.getConnector();
340 populateProxyConnector( connector );
342 // Add some arbitrary whitelist patterns.
343 connector.addWhiteListPattern( "javax/**/*" );
344 connector.addWhiteListPattern( "com/sun/**/*" );
346 // Perform Test w/no pattern value.
347 preRequest( action );
348 String 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 // Perform test w/invalid (non-existant) pattern value to remove.
356 preRequest( action );
357 action.setPattern( "**/*oops*" );
358 status = action.removeWhiteListPattern();
359 assertEquals( Action.INPUT, status );
361 // Should have returned an error, with no whitelist pattern removed.
362 assertHasErrors( action );
363 assertEquals( 2, connector.getWhiteListPatterns().size() );
365 // Try again, but now with a valid pattern to remove.
366 action.setPattern( "com/sun/**/*" );
367 preRequest( action );
368 status = action.removeWhiteListPattern();
369 assertEquals( Action.INPUT, status );
371 // Should have no error, and 1 whitelist pattern left.
372 assertNoErrors( action );
373 assertEquals( 1, connector.getWhiteListPatterns().size() );
374 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
378 public void testSecureActionBundle()
381 expectConfigurationRequests( 3 );
382 archivaConfigurationControl.replay();
385 SecureActionBundle bundle = action.getSecureActionBundle();
386 assertTrue( bundle.requiresAuthentication() );
387 assertEquals( 1, bundle.getAuthorizationTuples().size() );
390 private Configuration createInitialConfiguration()
392 Configuration config = new Configuration();
394 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
395 managedRepo.setId( "corporate" );
396 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
397 managedRepo.setReleases( true );
399 config.addManagedRepository( managedRepo );
401 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
402 remoteRepo.setId( "central" );
403 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
405 config.addRemoteRepository( remoteRepo );
410 private void expectConfigurationRequests( int requestConfigCount )
411 throws org.apache.archiva.redback.components.registry.RegistryException, IndeterminateConfigurationException
413 Configuration config = createInitialConfiguration();
414 archivaConfiguration.getConfiguration();
415 archivaConfigurationControl.setReturnValue( config, requestConfigCount, requestConfigCount + 1 );
416 archivaConfiguration.save( config );
419 @SuppressWarnings( "unchecked" )
420 private void populateProxyConnector( ProxyConnector connector )
422 connector.setProxyId( AbstractProxyConnectorFormAction.DIRECT_CONNECTION );
423 connector.setSourceRepoId( "corporate" );
424 connector.setTargetRepoId( "central" );
426 // TODO: Set these options programatically via list of available policies.
427 Map<String, String> policies = connector.getPolicies();
428 policies.put( "releases", new ReleasesPolicy().getDefaultOption() );
429 policies.put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
430 policies.put( "checksum", new ChecksumPolicy().getDefaultOption() );
431 policies.put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
432 policies.put( "propagate-errors", new PropagateErrorsDownloadPolicy().getDefaultOption() );
433 policies.put( "propagate-errors-on-update", new PropagateErrorsOnUpdateDownloadPolicy().getDefaultOption() );