2 * Copyright 2008 jdumay.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 package org.apache.maven.archiva.web.action.admin.connectors.proxy;
20 import com.opensymphony.xwork2.Action;
21 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
22 import org.apache.maven.archiva.configuration.Configuration;
23 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
24 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
25 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
26 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
27 import org.apache.maven.archiva.web.action.AbstractWebworkTestCase;
28 import org.codehaus.plexus.registry.RegistryException;
29 import org.codehaus.redback.integration.interceptor.SecureActionBundle;
30 import org.easymock.MockControl;
32 public class EnableProxyConnectorActionTest extends AbstractWebworkTestCase
34 private static final String TEST_TARGET_ID = "central";
36 private static final String TEST_SOURCE_ID = "corporate";
38 private EnableProxyConnectorAction action;
40 private MockControl archivaConfigurationControl;
42 private ArchivaConfiguration archivaConfiguration;
44 public void testConfirmDeleteBadSourceOrTarget()
47 expectConfigurationRequests( 4 );
48 archivaConfigurationControl.replay();
50 // Attempt to show the confirm enable screen, but provide
51 // a bad source id or target id to actually enable
54 action.setSource( "bad-source" ); // id doesn't exist.
55 action.setTarget( "bad-target" ); // id doesn't exist.
56 String status = action.confirmEnable();
57 // Should have resulted in an error.
58 assertEquals( Action.ERROR, status );
59 assertHasErrors( action );
62 action.setSource( "bad" ); // Bad doesn't exist.
63 action.setTarget( TEST_TARGET_ID );
64 status = action.confirmEnable();
65 // Should have resulted in an error.
66 assertEquals( Action.ERROR, status );
67 assertHasErrors( action );
70 action.setSource( TEST_SOURCE_ID );
71 action.setTarget( "bad" ); // Bad doesn't exist.
72 status = action.confirmEnable();
73 // Should have resulted in an error.
74 assertEquals( Action.ERROR, status );
75 assertHasErrors( action );
78 public void testConfirmEnableNoSourceOrTarget()
81 expectConfigurationRequests( 1 );
82 archivaConfigurationControl.replay();
84 // Attempt to show the confirm enable screen, but don't provide
85 // the source id or target id to actually delete
88 action.setSource( null ); // No source Id.
89 action.setTarget( null ); // No target Id.
90 String status = action.confirmEnable();
91 // Should have resulted in an error.
92 assertEquals( Action.ERROR, status );
93 assertHasErrors( action );
96 action.setSource( TEST_SOURCE_ID );
97 action.setTarget( null ); // No target Id.
98 status = action.confirmEnable();
99 // Should have resulted in an error.
100 assertEquals( Action.ERROR, status );
101 assertHasErrors( action );
103 preRequest( action );
104 action.setSource( null ); // No source Id.
105 action.setTarget( TEST_TARGET_ID );
106 status = action.confirmEnable();
107 // Should have resulted in an error.
108 assertEquals( Action.ERROR, status );
109 assertHasErrors( action );
112 public void testEnable()
115 expectConfigurationRequests( 5 );
116 archivaConfigurationControl.replay();
118 // Show the confirm the enable of proxy connector screen.
119 preRequest( action );
120 action.setSource( TEST_SOURCE_ID );
121 action.setTarget( TEST_TARGET_ID );
122 String status = action.confirmEnable();
123 assertEquals( Action.INPUT, status );
124 assertNoErrors( action );
126 // Perform the delete.
127 preRequest( action );
128 status = action.enable();
129 assertEquals( Action.SUCCESS, status );
130 assertNoErrors( action );
131 assertHasMessages( action );
133 // Test the configuration.
134 assertEquals( 1, archivaConfiguration.getConfiguration().getProxyConnectors().size() );
135 ProxyConnectorConfiguration config = (ProxyConnectorConfiguration)archivaConfiguration.getConfiguration().getProxyConnectors().get(0);
136 assertFalse(config.isDisabled());
139 public void testSecureActionBundle()
142 expectConfigurationRequests( 1 );
143 archivaConfigurationControl.replay();
145 SecureActionBundle bundle = action.getSecureActionBundle();
146 assertTrue( bundle.requiresAuthentication() );
147 assertEquals( 1, bundle.getAuthorizationTuples().size() );
150 public void testConfirmEnable()
153 expectConfigurationRequests( 1 );
154 archivaConfigurationControl.replay();
156 // Show the confirm the enable of proxy connector screen.
157 preRequest( action );
158 action.setSource( TEST_SOURCE_ID );
159 action.setTarget( TEST_TARGET_ID );
160 String status = action.confirmEnable();
161 assertEquals( Action.INPUT, status );
162 assertNoErrors( action );
165 private Configuration createInitialConfiguration()
167 Configuration config = new Configuration();
169 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
170 managedRepo.setId( TEST_SOURCE_ID );
171 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
172 managedRepo.setReleases( true );
174 config.addManagedRepository( managedRepo );
176 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
177 remoteRepo.setId( TEST_TARGET_ID );
178 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
180 config.addRemoteRepository( remoteRepo );
182 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
183 connector.setSourceRepoId( TEST_SOURCE_ID );
184 connector.setTargetRepoId( TEST_TARGET_ID );
185 connector.setDisabled(true);
187 config.addProxyConnector( connector );
192 private void expectConfigurationRequests( int requestConfigCount )
193 throws RegistryException, IndeterminateConfigurationException
195 Configuration config = createInitialConfiguration();
197 for ( int i = 0; i < requestConfigCount; i++ )
199 archivaConfiguration.getConfiguration();
200 archivaConfigurationControl.setReturnValue( config );
203 archivaConfiguration.save( config );
207 protected void setUp()
212 //action = (EnableProxyConnectorAction) lookup( Action.class.getName(), "enableProxyConnectorAction" );
214 action = (EnableProxyConnectorAction) getActionProxy( "/admin/enableProxyConnector.action" ).getAction();
215 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
216 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
217 action.setArchivaConfiguration( archivaConfiguration );