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;
57 public void testAddBlackListPattern()
60 expectConfigurationRequests( 7 );
61 archivaConfigurationControl.replay();
65 ProxyConnectorConfiguration connector = action.getConnector();
66 populateProxyConnector( connector );
68 // Perform Test w/no values.
70 String status = action.addBlackListPattern();
71 assertEquals( Action.INPUT, status );
73 // Should have returned an error, with no blacklist pattern added.
74 assertHasErrors( action );
75 assertEquals( 0, connector.getBlackListPatterns().size() );
77 // Try again, but now with a pattern to add.
78 action.setBlackListPattern( "**/*-javadoc.jar" );
80 status = action.addBlackListPattern();
81 assertEquals( Action.INPUT, status );
83 // Should have no error, and 1 blacklist pattern added.
84 assertNoErrors( action );
85 assertEquals( 1, connector.getBlackListPatterns().size() );
88 public void testAddProperty()
91 expectConfigurationRequests( 7 );
92 archivaConfigurationControl.replay();
96 ProxyConnectorConfiguration connector = action.getConnector();
97 populateProxyConnector( connector );
99 // Perform Test w/no values.
100 preRequest( action );
101 String status = action.addProperty();
102 assertEquals( Action.INPUT, status );
104 // Should have returned an error, with no property pattern added.
105 assertHasErrors( action );
106 assertEquals( 0, connector.getProperties().size() );
108 // Try again, but now with a property key/value to add.
109 action.setPropertyKey( "eat-a" );
110 action.setPropertyValue( "gramov-a-bits" );
111 preRequest( action );
112 status = action.addProperty();
113 assertEquals( Action.INPUT, status );
115 // Should have no error, and 1 property added.
116 assertNoErrors( action );
117 assertEquals( 1, connector.getProperties().size() );
120 public void testAddProxyConnectorCommit()
123 expectConfigurationRequests( 7 );
124 archivaConfigurationControl.replay();
128 ProxyConnectorConfiguration connector = action.getConnector();
129 populateProxyConnector( connector );
130 // forms will use an array
131 connector.getProperties().put( "eat-a", new String[] { "gramov-a-bits" } );
133 // Create the input screen.
134 assertRequestStatus( action, Action.SUCCESS, "commit" );
135 assertNoErrors( action );
137 // Test configuration.
138 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
139 assertNotNull( proxyConfigs );
140 assertEquals( 1, proxyConfigs.size() );
142 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
144 assertNotNull( actualConnector );
145 // The use of "(direct connection)" should result in a proxyId which is <null>.
146 assertNull( actualConnector.getProxyId() );
147 assertEquals( "corporate", actualConnector.getSourceRepoId() );
148 assertEquals( "central", actualConnector.getTargetRepoId() );
149 assertEquals( "gramov-a-bits", actualConnector.getProperties().get( "eat-a" ) );
152 public void testAddProxyConnectorInitialPage()
155 expectConfigurationRequests( 3 );
156 archivaConfigurationControl.replay();
159 ProxyConnectorConfiguration configuration = action.getConnector();
160 assertNotNull( configuration );
161 assertNull( configuration.getProxyId() );
162 assertNull( configuration.getSourceRepoId() );
163 assertNull( configuration.getTargetRepoId() );
164 assertTrue( configuration.getPolicies().isEmpty() );
165 assertTrue( configuration.getProperties().isEmpty() );
166 assertTrue( configuration.getBlackListPatterns().isEmpty() );
167 assertTrue( configuration.getWhiteListPatterns().isEmpty() );
169 String status = action.input();
170 assertEquals( Action.INPUT, status );
173 public void testAddWhiteListPattern()
176 expectConfigurationRequests( 7 );
177 archivaConfigurationControl.replay();
181 ProxyConnectorConfiguration connector = action.getConnector();
182 populateProxyConnector( connector );
184 // Perform Test w/no values.
185 preRequest( action );
186 String status = action.addWhiteListPattern();
187 assertEquals( Action.INPUT, status );
189 // Should have returned an error, with no whitelist pattern added.
190 assertHasErrors( action );
191 assertEquals( 0, connector.getWhiteListPatterns().size() );
193 // Try again, but now with a pattern to add.
194 action.setWhiteListPattern( "**/*.jar" );
195 preRequest( action );
196 status = action.addWhiteListPattern();
197 assertEquals( Action.INPUT, status );
199 // Should have no error, and 1 whitelist pattern added.
200 assertNoErrors( action );
201 assertEquals( 1, connector.getWhiteListPatterns().size() );
204 public void testRemoveBlackListPattern()
207 expectConfigurationRequests( 7 );
208 archivaConfigurationControl.replay();
212 ProxyConnectorConfiguration connector = action.getConnector();
213 populateProxyConnector( connector );
215 // Add some arbitrary blacklist patterns.
216 connector.addBlackListPattern( "**/*-javadoc.jar" );
217 connector.addBlackListPattern( "**/*.war" );
219 // Perform Test w/no pattern value.
220 preRequest( action );
221 String status = action.removeBlackListPattern();
222 assertEquals( Action.INPUT, status );
224 // Should have returned an error, with no blacklist pattern removed.
225 assertHasErrors( action );
226 assertEquals( 2, connector.getBlackListPatterns().size() );
228 // Perform test w/invalid (non-existant) pattern value to remove.
229 preRequest( action );
230 action.setPattern( "**/*oops*" );
231 status = action.removeBlackListPattern();
232 assertEquals( Action.INPUT, status );
234 // Should have returned an error, with no blacklist pattern removed.
235 assertHasErrors( action );
236 assertEquals( 2, connector.getBlackListPatterns().size() );
238 // Try again, but now with a valid pattern to remove.
239 action.setPattern( "**/*-javadoc.jar" );
240 preRequest( action );
241 status = action.removeBlackListPattern();
242 assertEquals( Action.INPUT, status );
244 // Should have no error, and 1 blacklist pattern left.
245 assertNoErrors( action );
246 assertEquals( 1, connector.getBlackListPatterns().size() );
247 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
250 public void testRemoveProperty()
253 expectConfigurationRequests( 7 );
254 archivaConfigurationControl.replay();
258 ProxyConnectorConfiguration connector = action.getConnector();
259 populateProxyConnector( connector );
261 // Add some arbitrary properties.
262 connector.addProperty( "username", "general-tso" );
263 connector.addProperty( "password", "chicken" );
265 // Perform Test w/no property key.
266 preRequest( action );
267 String status = action.removeProperty();
268 assertEquals( Action.INPUT, status );
270 // Should have returned an error, with no properties removed.
271 assertHasErrors( action );
272 assertEquals( 2, connector.getProperties().size() );
274 // Perform test w/invalid (non-existant) property key to remove.
275 preRequest( action );
276 action.setPropertyKey( "slurm" );
277 status = action.removeProperty();
278 assertEquals( Action.INPUT, status );
280 // Should have returned an error, with no properties removed.
281 assertHasErrors( action );
282 assertEquals( 2, connector.getProperties().size() );
284 // Try again, but now with a valid property to remove.
285 preRequest( action );
286 action.setPropertyKey( "password" );
287 status = action.removeProperty();
288 assertEquals( Action.INPUT, status );
290 // Should have no error, and 1 property left.
291 assertNoErrors( action );
292 assertEquals( 1, connector.getProperties().size() );
293 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
296 public void testRemoveWhiteListPattern()
299 expectConfigurationRequests( 7 );
300 archivaConfigurationControl.replay();
304 ProxyConnectorConfiguration connector = action.getConnector();
305 populateProxyConnector( connector );
307 // Add some arbitrary whitelist patterns.
308 connector.addWhiteListPattern( "javax/**/*" );
309 connector.addWhiteListPattern( "com/sun/**/*" );
311 // Perform Test w/no pattern value.
312 preRequest( action );
313 String status = action.removeWhiteListPattern();
314 assertEquals( Action.INPUT, status );
316 // Should have returned an error, with no whitelist pattern removed.
317 assertHasErrors( action );
318 assertEquals( 2, connector.getWhiteListPatterns().size() );
320 // Perform test w/invalid (non-existant) pattern value to remove.
321 preRequest( action );
322 action.setPattern( "**/*oops*" );
323 status = action.removeWhiteListPattern();
324 assertEquals( Action.INPUT, status );
326 // Should have returned an error, with no whitelist pattern removed.
327 assertHasErrors( action );
328 assertEquals( 2, connector.getWhiteListPatterns().size() );
330 // Try again, but now with a valid pattern to remove.
331 action.setPattern( "com/sun/**/*" );
332 preRequest( action );
333 status = action.removeWhiteListPattern();
334 assertEquals( Action.INPUT, status );
336 // Should have no error, and 1 whitelist pattern left.
337 assertNoErrors( action );
338 assertEquals( 1, connector.getWhiteListPatterns().size() );
339 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
342 public void testSecureActionBundle()
345 expectConfigurationRequests( 3 );
346 archivaConfigurationControl.replay();
349 SecureActionBundle bundle = action.getSecureActionBundle();
350 assertTrue( bundle.requiresAuthentication() );
351 assertEquals( 1, bundle.getAuthorizationTuples().size() );
354 private Configuration createInitialConfiguration()
356 Configuration config = new Configuration();
358 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
359 managedRepo.setId( "corporate" );
360 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
361 managedRepo.setReleases( true );
363 config.addManagedRepository( managedRepo );
365 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
366 remoteRepo.setId( "central" );
367 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
369 config.addRemoteRepository( remoteRepo );
374 private void expectConfigurationRequests( int requestConfigCount )
375 throws RegistryException, IndeterminateConfigurationException
377 Configuration config = createInitialConfiguration();
379 for ( int i = 0; i < requestConfigCount; i++ )
381 archivaConfiguration.getConfiguration();
382 archivaConfigurationControl.setReturnValue( config );
385 archivaConfiguration.save( config );
388 private void populateProxyConnector( ProxyConnectorConfiguration connector )
390 connector.setProxyId( AbstractProxyConnectorFormAction.DIRECT_CONNECTION );
391 connector.setSourceRepoId( "corporate" );
392 connector.setTargetRepoId( "central" );
394 // TODO: Set these options programatically via list of available policies.
395 Map<String, String> policies = connector.getPolicies();
396 policies.put( "releases", new ReleasesPolicy().getDefaultOption() );
397 policies.put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
398 policies.put( "checksum", new ChecksumPolicy().getDefaultOption() );
399 policies.put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
400 policies.put( "propagate-errors", new PropagateErrorsDownloadPolicy().getDefaultOption() );
401 policies.put( "propagate-errors-on-update", new PropagateErrorsOnUpdateDownloadPolicy().getDefaultOption() );
405 protected void setUp()
410 action = (AddProxyConnectorAction) lookup( Action.class.getName(), "addProxyConnectorAction" );
412 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
413 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
414 action.setArchivaConfiguration( archivaConfiguration );