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.xwork.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.ReleasesPolicy;
32 import org.apache.maven.archiva.policies.SnapshotsPolicy;
33 import org.apache.maven.archiva.web.action.AbstractWebworkTestCase;
34 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
35 import org.codehaus.plexus.registry.RegistryException;
36 import org.easymock.MockControl;
38 import java.util.List;
41 * AddProxyConnectorActionTest
43 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
46 public class AddProxyConnectorActionTest
47 extends AbstractWebworkTestCase
49 private AddProxyConnectorAction action;
51 private MockControl archivaConfigurationControl;
53 private ArchivaConfiguration archivaConfiguration;
55 public void testAddBlackListPattern()
58 expectConfigurationRequests( 7 );
59 archivaConfigurationControl.replay();
63 ProxyConnectorConfiguration connector = action.getConnector();
64 populateProxyConnector( connector );
66 // Perform Test w/no values.
68 String status = action.addBlackListPattern();
69 assertEquals( Action.INPUT, status );
71 // Should have returned an error, with no blacklist pattern added.
72 assertHasErrors( action );
73 assertEquals( 0, connector.getBlackListPatterns().size() );
75 // Try again, but now with a pattern to add.
76 action.setBlackListPattern( "**/*-javadoc.jar" );
78 status = action.addBlackListPattern();
79 assertEquals( Action.INPUT, status );
81 // Should have no error, and 1 blacklist pattern added.
82 assertNoErrors( action );
83 assertEquals( 1, connector.getBlackListPatterns().size() );
86 public void testAddProperty()
89 expectConfigurationRequests( 7 );
90 archivaConfigurationControl.replay();
94 ProxyConnectorConfiguration connector = action.getConnector();
95 populateProxyConnector( connector );
97 // Perform Test w/no values.
99 String status = action.addProperty();
100 assertEquals( Action.INPUT, status );
102 // Should have returned an error, with no property pattern added.
103 assertHasErrors( action );
104 assertEquals( 0, connector.getProperties().size() );
106 // Try again, but now with a property key/value to add.
107 action.setPropertyKey( "eat-a" );
108 action.setPropertyValue( "gramov-a-bits" );
109 preRequest( action );
110 status = action.addProperty();
111 assertEquals( Action.INPUT, status );
113 // Should have no error, and 1 property added.
114 assertNoErrors( action );
115 assertEquals( 1, connector.getProperties().size() );
118 public void testAddProxyConnectorCommit()
121 expectConfigurationRequests( 7 );
122 archivaConfigurationControl.replay();
126 ProxyConnectorConfiguration connector = action.getConnector();
127 populateProxyConnector( connector );
128 // forms will use an array
129 connector.getProperties().put( "eat-a", new String[] { "gramov-a-bits" } );
131 // Create the input screen.
132 assertRequestStatus( action, Action.SUCCESS, "commit" );
133 assertNoErrors( action );
135 // Test configuration.
136 List<ProxyConnectorConfiguration> proxyConfigs = archivaConfiguration.getConfiguration().getProxyConnectors();
137 assertNotNull( proxyConfigs );
138 assertEquals( 1, proxyConfigs.size() );
140 ProxyConnectorConfiguration actualConnector = proxyConfigs.get( 0 );
142 assertNotNull( actualConnector );
143 // The use of "(direct connection)" should result in a proxyId which is <null>.
144 assertNull( actualConnector.getProxyId() );
145 assertEquals( "corporate", actualConnector.getSourceRepoId() );
146 assertEquals( "central", actualConnector.getTargetRepoId() );
147 assertEquals( "gramov-a-bits", actualConnector.getProperties().get( "eat-a" ) );
150 public void testAddProxyConnectorInitialPage()
153 expectConfigurationRequests( 3 );
154 archivaConfigurationControl.replay();
157 ProxyConnectorConfiguration configuration = action.getConnector();
158 assertNotNull( configuration );
159 assertNull( configuration.getProxyId() );
160 assertNull( configuration.getSourceRepoId() );
161 assertNull( configuration.getTargetRepoId() );
162 assertTrue( configuration.getPolicies().isEmpty() );
163 assertTrue( configuration.getProperties().isEmpty() );
164 assertTrue( configuration.getBlackListPatterns().isEmpty() );
165 assertTrue( configuration.getWhiteListPatterns().isEmpty() );
167 String status = action.input();
168 assertEquals( Action.INPUT, status );
171 public void testAddWhiteListPattern()
174 expectConfigurationRequests( 7 );
175 archivaConfigurationControl.replay();
179 ProxyConnectorConfiguration connector = action.getConnector();
180 populateProxyConnector( connector );
182 // Perform Test w/no values.
183 preRequest( action );
184 String status = action.addWhiteListPattern();
185 assertEquals( Action.INPUT, status );
187 // Should have returned an error, with no whitelist pattern added.
188 assertHasErrors( action );
189 assertEquals( 0, connector.getWhiteListPatterns().size() );
191 // Try again, but now with a pattern to add.
192 action.setWhiteListPattern( "**/*.jar" );
193 preRequest( action );
194 status = action.addWhiteListPattern();
195 assertEquals( Action.INPUT, status );
197 // Should have no error, and 1 whitelist pattern added.
198 assertNoErrors( action );
199 assertEquals( 1, connector.getWhiteListPatterns().size() );
202 public void testRemoveBlackListPattern()
205 expectConfigurationRequests( 7 );
206 archivaConfigurationControl.replay();
210 ProxyConnectorConfiguration connector = action.getConnector();
211 populateProxyConnector( connector );
213 // Add some arbitrary blacklist patterns.
214 connector.addBlackListPattern( "**/*-javadoc.jar" );
215 connector.addBlackListPattern( "**/*.war" );
217 // Perform Test w/no pattern value.
218 preRequest( action );
219 String status = action.removeBlackListPattern();
220 assertEquals( Action.INPUT, status );
222 // Should have returned an error, with no blacklist pattern removed.
223 assertHasErrors( action );
224 assertEquals( 2, connector.getBlackListPatterns().size() );
226 // Perform test w/invalid (non-existant) pattern value to remove.
227 preRequest( action );
228 action.setPattern( "**/*oops*" );
229 status = action.removeBlackListPattern();
230 assertEquals( Action.INPUT, status );
232 // Should have returned an error, with no blacklist pattern removed.
233 assertHasErrors( action );
234 assertEquals( 2, connector.getBlackListPatterns().size() );
236 // Try again, but now with a valid pattern to remove.
237 action.setPattern( "**/*-javadoc.jar" );
238 preRequest( action );
239 status = action.removeBlackListPattern();
240 assertEquals( Action.INPUT, status );
242 // Should have no error, and 1 blacklist pattern left.
243 assertNoErrors( action );
244 assertEquals( 1, connector.getBlackListPatterns().size() );
245 assertEquals( "Should have left 1 blacklist pattern", "**/*.war", connector.getBlackListPatterns().get( 0 ) );
248 public void testRemoveProperty()
251 expectConfigurationRequests( 7 );
252 archivaConfigurationControl.replay();
256 ProxyConnectorConfiguration connector = action.getConnector();
257 populateProxyConnector( connector );
259 // Add some arbitrary properties.
260 connector.addProperty( "username", "general-tso" );
261 connector.addProperty( "password", "chicken" );
263 // Perform Test w/no property key.
264 preRequest( action );
265 String status = action.removeProperty();
266 assertEquals( Action.INPUT, status );
268 // Should have returned an error, with no properties removed.
269 assertHasErrors( action );
270 assertEquals( 2, connector.getProperties().size() );
272 // Perform test w/invalid (non-existant) property key to remove.
273 preRequest( action );
274 action.setPropertyKey( "slurm" );
275 status = action.removeProperty();
276 assertEquals( Action.INPUT, status );
278 // Should have returned an error, with no properties removed.
279 assertHasErrors( action );
280 assertEquals( 2, connector.getProperties().size() );
282 // Try again, but now with a valid property to remove.
283 preRequest( action );
284 action.setPropertyKey( "password" );
285 status = action.removeProperty();
286 assertEquals( Action.INPUT, status );
288 // Should have no error, and 1 property left.
289 assertNoErrors( action );
290 assertEquals( 1, connector.getProperties().size() );
291 assertEquals( "Should have left 1 property", "general-tso", connector.getProperties().get( "username" ) );
294 public void testRemoveWhiteListPattern()
297 expectConfigurationRequests( 7 );
298 archivaConfigurationControl.replay();
302 ProxyConnectorConfiguration connector = action.getConnector();
303 populateProxyConnector( connector );
305 // Add some arbitrary whitelist patterns.
306 connector.addWhiteListPattern( "javax/**/*" );
307 connector.addWhiteListPattern( "com/sun/**/*" );
309 // Perform Test w/no pattern value.
310 preRequest( action );
311 String status = action.removeWhiteListPattern();
312 assertEquals( Action.INPUT, status );
314 // Should have returned an error, with no whitelist pattern removed.
315 assertHasErrors( action );
316 assertEquals( 2, connector.getWhiteListPatterns().size() );
318 // Perform test w/invalid (non-existant) pattern value to remove.
319 preRequest( action );
320 action.setPattern( "**/*oops*" );
321 status = action.removeWhiteListPattern();
322 assertEquals( Action.INPUT, status );
324 // Should have returned an error, with no whitelist pattern removed.
325 assertHasErrors( action );
326 assertEquals( 2, connector.getWhiteListPatterns().size() );
328 // Try again, but now with a valid pattern to remove.
329 action.setPattern( "com/sun/**/*" );
330 preRequest( action );
331 status = action.removeWhiteListPattern();
332 assertEquals( Action.INPUT, status );
334 // Should have no error, and 1 whitelist pattern left.
335 assertNoErrors( action );
336 assertEquals( 1, connector.getWhiteListPatterns().size() );
337 assertEquals( "Should have left 1 whitelist pattern", "javax/**/*", connector.getWhiteListPatterns().get( 0 ) );
340 public void testSecureActionBundle()
343 expectConfigurationRequests( 3 );
344 archivaConfigurationControl.replay();
347 SecureActionBundle bundle = action.getSecureActionBundle();
348 assertTrue( bundle.requiresAuthentication() );
349 assertEquals( 1, bundle.getAuthorizationTuples().size() );
352 private Configuration createInitialConfiguration()
354 Configuration config = new Configuration();
356 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
357 managedRepo.setId( "corporate" );
358 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
359 managedRepo.setReleases( true );
361 config.addManagedRepository( managedRepo );
363 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
364 remoteRepo.setId( "central" );
365 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
367 config.addRemoteRepository( remoteRepo );
372 private void expectConfigurationRequests( int requestConfigCount )
373 throws RegistryException, IndeterminateConfigurationException
375 Configuration config = createInitialConfiguration();
377 for ( int i = 0; i < requestConfigCount; i++ )
379 archivaConfiguration.getConfiguration();
380 archivaConfigurationControl.setReturnValue( config );
383 archivaConfiguration.save( config );
386 private void populateProxyConnector( ProxyConnectorConfiguration connector )
388 connector.setProxyId( AbstractProxyConnectorFormAction.DIRECT_CONNECTION );
389 connector.setSourceRepoId( "corporate" );
390 connector.setTargetRepoId( "central" );
392 // TODO: Set these options programatically via list of available policies.
393 connector.getPolicies().put( "releases", new ReleasesPolicy().getDefaultOption() );
394 connector.getPolicies().put( "snapshots", new SnapshotsPolicy().getDefaultOption() );
395 connector.getPolicies().put( "checksum", new ChecksumPolicy().getDefaultOption() );
396 connector.getPolicies().put( "cache-failures", new CachedFailuresPolicy().getDefaultOption() );
400 protected void setUp()
405 action = (AddProxyConnectorAction) lookup( Action.class.getName(), "addProxyConnectorAction" );
407 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
408 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
409 action.setArchivaConfiguration( archivaConfiguration );