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.archiva.web.action.admin.connectors.proxy;
20 import com.opensymphony.xwork2.Action;
21 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
22 import org.apache.archiva.admin.repository.proxyconnector.DefaultProxyConnectorAdmin;
23 import org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin;
24 import org.apache.archiva.configuration.ArchivaConfiguration;
25 import org.apache.archiva.configuration.Configuration;
26 import org.apache.archiva.configuration.IndeterminateConfigurationException;
27 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
29 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
30 import org.apache.archiva.web.action.AbstractWebworkTestCase;
31 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
32 import org.easymock.MockControl;
33 import org.junit.Before;
34 import org.junit.Test;
36 public class EnableProxyConnectorActionTest
37 extends AbstractWebworkTestCase
39 private static final String TEST_TARGET_ID = "central";
41 private static final String TEST_SOURCE_ID = "corporate";
43 private EnableProxyConnectorAction action;
45 private MockControl archivaConfigurationControl;
47 private ArchivaConfiguration archivaConfiguration;
55 action = (EnableProxyConnectorAction) getActionProxy( "/admin/enableProxyConnector.action" ).getAction();
56 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
57 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
58 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
59 archivaConfiguration );
60 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
61 archivaConfiguration );
62 ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration( archivaConfiguration );
65 private void expectConfigurationRequests( int requestConfigCount )
66 throws org.apache.archiva.redback.components.registry.RegistryException, IndeterminateConfigurationException
68 Configuration config = createInitialConfiguration();
70 archivaConfiguration.getConfiguration();
71 archivaConfigurationControl.setReturnValue( config, requestConfigCount );
72 archivaConfiguration.save( config );
76 public void testConfirmDeleteBadSourceOrTarget()
79 expectConfigurationRequests( 4 );
80 archivaConfigurationControl.replay();
82 // Attempt to show the confirm enable screen, but provide
83 // a bad source id or target id to actually enable
86 action.setSource( "bad-source" ); // id doesn't exist.
87 action.setTarget( "bad-target" ); // id doesn't exist.
88 String status = action.confirmEnable();
89 // Should have resulted in an error.
90 assertEquals( Action.ERROR, status );
91 assertHasErrors( action );
94 action.setSource( "bad" ); // Bad doesn't exist.
95 action.setTarget( TEST_TARGET_ID );
96 status = action.confirmEnable();
97 // Should have resulted in an error.
98 assertEquals( Action.ERROR, status );
99 assertHasErrors( action );
101 preRequest( action );
102 action.setSource( TEST_SOURCE_ID );
103 action.setTarget( "bad" ); // Bad doesn't exist.
104 status = action.confirmEnable();
105 // Should have resulted in an error.
106 assertEquals( Action.ERROR, status );
107 assertHasErrors( action );
111 public void testConfirmEnableNoSourceOrTarget()
114 expectConfigurationRequests( 1 );
115 archivaConfigurationControl.replay();
117 // Attempt to show the confirm enable screen, but don't provide
118 // the source id or target id to actually delete
120 preRequest( action );
121 action.setSource( null ); // No source Id.
122 action.setTarget( null ); // No target Id.
123 String status = action.confirmEnable();
124 // Should have resulted in an error.
125 assertEquals( Action.ERROR, status );
126 assertHasErrors( action );
128 preRequest( action );
129 action.setSource( TEST_SOURCE_ID );
130 action.setTarget( null ); // No target Id.
131 status = action.confirmEnable();
132 // Should have resulted in an error.
133 assertEquals( Action.ERROR, status );
134 assertHasErrors( action );
136 preRequest( action );
137 action.setSource( null ); // No source Id.
138 action.setTarget( TEST_TARGET_ID );
139 status = action.confirmEnable();
140 // Should have resulted in an error.
141 assertEquals( Action.ERROR, status );
142 assertHasErrors( action );
146 public void testEnable()
149 expectConfigurationRequests( 5 );
150 archivaConfigurationControl.replay();
152 // Show the confirm the enable of proxy connector screen.
153 preRequest( action );
154 action.setSource( TEST_SOURCE_ID );
155 action.setTarget( TEST_TARGET_ID );
156 String status = action.confirmEnable();
157 assertEquals( Action.INPUT, status );
158 assertNoErrors( action );
160 // Perform the delete.
161 preRequest( action );
162 status = action.enable();
163 assertEquals( Action.SUCCESS, status );
164 assertNoErrors( action );
165 assertHasMessages( action );
167 // Test the configuration.
168 assertEquals( 1, archivaConfiguration.getConfiguration().getProxyConnectors().size() );
169 ProxyConnectorConfiguration config =
170 (ProxyConnectorConfiguration) archivaConfiguration.getConfiguration().getProxyConnectors().get( 0 );
171 assertFalse( config.isDisabled() );
175 public void testSecureActionBundle()
178 expectConfigurationRequests( 1 );
179 archivaConfigurationControl.replay();
181 SecureActionBundle bundle = action.getSecureActionBundle();
182 assertTrue( bundle.requiresAuthentication() );
183 assertEquals( 1, bundle.getAuthorizationTuples().size() );
187 public void testConfirmEnable()
190 expectConfigurationRequests( 1 );
191 archivaConfigurationControl.replay();
193 // Show the confirm the enable of proxy connector screen.
194 preRequest( action );
195 action.setSource( TEST_SOURCE_ID );
196 action.setTarget( TEST_TARGET_ID );
197 String status = action.confirmEnable();
198 assertEquals( Action.INPUT, status );
199 assertNoErrors( action );
202 private Configuration createInitialConfiguration()
204 Configuration config = new Configuration();
206 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
207 managedRepo.setId( TEST_SOURCE_ID );
208 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
209 managedRepo.setReleases( true );
211 config.addManagedRepository( managedRepo );
213 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
214 remoteRepo.setId( TEST_TARGET_ID );
215 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
217 config.addRemoteRepository( remoteRepo );
219 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
220 connector.setSourceRepoId( TEST_SOURCE_ID );
221 connector.setTargetRepoId( TEST_TARGET_ID );
222 connector.setDisabled( true );
224 config.addProxyConnector( connector );