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;
34 public class EnableProxyConnectorActionTest
35 extends AbstractWebworkTestCase
37 private static final String TEST_TARGET_ID = "central";
39 private static final String TEST_SOURCE_ID = "corporate";
41 private EnableProxyConnectorAction action;
43 private MockControl archivaConfigurationControl;
45 private ArchivaConfiguration archivaConfiguration;
48 protected void setUp()
52 action = (EnableProxyConnectorAction) getActionProxy( "/admin/enableProxyConnector.action" ).getAction();
53 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
54 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
55 ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
56 archivaConfiguration );
57 ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
58 archivaConfiguration );
59 ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration( archivaConfiguration );
62 private void expectConfigurationRequests( int requestConfigCount )
63 throws org.apache.archiva.redback.components.registry.RegistryException, IndeterminateConfigurationException
65 Configuration config = createInitialConfiguration();
67 archivaConfiguration.getConfiguration();
68 archivaConfigurationControl.setReturnValue( config, requestConfigCount );
69 archivaConfiguration.save( config );
72 public void testConfirmDeleteBadSourceOrTarget()
75 expectConfigurationRequests( 4 );
76 archivaConfigurationControl.replay();
78 // Attempt to show the confirm enable screen, but provide
79 // a bad source id or target id to actually enable
82 action.setSource( "bad-source" ); // id doesn't exist.
83 action.setTarget( "bad-target" ); // id doesn't exist.
84 String status = action.confirmEnable();
85 // Should have resulted in an error.
86 assertEquals( Action.ERROR, status );
87 assertHasErrors( action );
90 action.setSource( "bad" ); // Bad doesn't exist.
91 action.setTarget( TEST_TARGET_ID );
92 status = action.confirmEnable();
93 // Should have resulted in an error.
94 assertEquals( Action.ERROR, status );
95 assertHasErrors( action );
98 action.setSource( TEST_SOURCE_ID );
99 action.setTarget( "bad" ); // Bad doesn't exist.
100 status = action.confirmEnable();
101 // Should have resulted in an error.
102 assertEquals( Action.ERROR, status );
103 assertHasErrors( action );
106 public void testConfirmEnableNoSourceOrTarget()
109 expectConfigurationRequests( 1 );
110 archivaConfigurationControl.replay();
112 // Attempt to show the confirm enable screen, but don't provide
113 // the source id or target id to actually delete
115 preRequest( action );
116 action.setSource( null ); // No source Id.
117 action.setTarget( null ); // No target Id.
118 String status = action.confirmEnable();
119 // Should have resulted in an error.
120 assertEquals( Action.ERROR, status );
121 assertHasErrors( action );
123 preRequest( action );
124 action.setSource( TEST_SOURCE_ID );
125 action.setTarget( null ); // No target Id.
126 status = action.confirmEnable();
127 // Should have resulted in an error.
128 assertEquals( Action.ERROR, status );
129 assertHasErrors( action );
131 preRequest( action );
132 action.setSource( null ); // No source Id.
133 action.setTarget( TEST_TARGET_ID );
134 status = action.confirmEnable();
135 // Should have resulted in an error.
136 assertEquals( Action.ERROR, status );
137 assertHasErrors( action );
140 public void testEnable()
143 expectConfigurationRequests( 5 );
144 archivaConfigurationControl.replay();
146 // Show the confirm the enable of proxy connector screen.
147 preRequest( action );
148 action.setSource( TEST_SOURCE_ID );
149 action.setTarget( TEST_TARGET_ID );
150 String status = action.confirmEnable();
151 assertEquals( Action.INPUT, status );
152 assertNoErrors( action );
154 // Perform the delete.
155 preRequest( action );
156 status = action.enable();
157 assertEquals( Action.SUCCESS, status );
158 assertNoErrors( action );
159 assertHasMessages( action );
161 // Test the configuration.
162 assertEquals( 1, archivaConfiguration.getConfiguration().getProxyConnectors().size() );
163 ProxyConnectorConfiguration config =
164 (ProxyConnectorConfiguration) archivaConfiguration.getConfiguration().getProxyConnectors().get( 0 );
165 assertFalse( config.isDisabled() );
168 public void testSecureActionBundle()
171 expectConfigurationRequests( 1 );
172 archivaConfigurationControl.replay();
174 SecureActionBundle bundle = action.getSecureActionBundle();
175 assertTrue( bundle.requiresAuthentication() );
176 assertEquals( 1, bundle.getAuthorizationTuples().size() );
179 public void testConfirmEnable()
182 expectConfigurationRequests( 1 );
183 archivaConfigurationControl.replay();
185 // Show the confirm the enable of proxy connector screen.
186 preRequest( action );
187 action.setSource( TEST_SOURCE_ID );
188 action.setTarget( TEST_TARGET_ID );
189 String status = action.confirmEnable();
190 assertEquals( Action.INPUT, status );
191 assertNoErrors( action );
194 private Configuration createInitialConfiguration()
196 Configuration config = new Configuration();
198 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
199 managedRepo.setId( TEST_SOURCE_ID );
200 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
201 managedRepo.setReleases( true );
203 config.addManagedRepository( managedRepo );
205 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
206 remoteRepo.setId( TEST_TARGET_ID );
207 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
209 config.addRemoteRepository( remoteRepo );
211 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
212 connector.setSourceRepoId( TEST_SOURCE_ID );
213 connector.setTargetRepoId( TEST_TARGET_ID );
214 connector.setDisabled( true );
216 config.addProxyConnector( connector );