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.components.registry.RegistryException;
32 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
33 import org.easymock.MockControl;
35 public class DisableProxyConnectorActionTest
36 extends AbstractWebworkTestCase
38 private static final String TEST_TARGET_ID = "central";
40 private static final String TEST_SOURCE_ID = "corporate";
42 private DisableProxyConnectorAction action;
44 private MockControl archivaConfigurationControl;
46 private ArchivaConfiguration archivaConfiguration;
49 protected void setUp()
54 action = (DisableProxyConnectorAction) getActionProxy( "/admin/disableProxyConnector.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 public void testConfirmDisableBadSourceOrTarget()
68 expectConfigurationRequests( 4 );
69 archivaConfigurationControl.replay();
71 // Attempt to show the confirm disable screen, but provide
72 // a bad source id or target id to actually delete
75 action.setSource( "bad-source" ); // id doesn't exist.
76 action.setTarget( "bad-target" ); // id doesn't exist.
77 String status = action.confirmDisable();
78 // Should have resulted in an error.
79 assertEquals( Action.ERROR, status );
80 assertHasErrors( action );
83 action.setSource( "bad" ); // Bad doesn't exist.
84 action.setTarget( TEST_TARGET_ID );
85 status = action.confirmDisable();
86 // Should have resulted in an error.
87 assertEquals( Action.ERROR, status );
88 assertHasErrors( action );
91 action.setSource( TEST_SOURCE_ID );
92 action.setTarget( "bad" ); // Bad doesn't exist.
93 status = action.confirmDisable();
94 // Should have resulted in an error.
95 assertEquals( Action.ERROR, status );
96 assertHasErrors( action );
99 public void testConfirmDisableNoSourceOrTarget()
102 expectConfigurationRequests( 1 );
103 archivaConfigurationControl.replay();
105 // Attempt to show the confirm disable screen, but don't provide
106 // the source id or target id to actually delete
108 preRequest( action );
109 action.setSource( null ); // No source Id.
110 action.setTarget( null ); // No target Id.
111 String status = action.confirmDisable();
112 // Should have resulted in an error.
113 assertEquals( Action.ERROR, status );
114 assertHasErrors( action );
116 preRequest( action );
117 action.setSource( TEST_SOURCE_ID );
118 action.setTarget( null ); // No target Id.
119 status = action.confirmDisable();
120 // Should have resulted in an error.
121 assertEquals( Action.ERROR, status );
122 assertHasErrors( action );
124 preRequest( action );
125 action.setSource( null ); // No source Id.
126 action.setTarget( TEST_TARGET_ID );
127 status = action.confirmDisable();
128 // Should have resulted in an error.
129 assertEquals( Action.ERROR, status );
130 assertHasErrors( action );
133 public void testDelete()
136 expectConfigurationRequests( 5 );
137 archivaConfigurationControl.replay();
139 // Show the confirm the disable of proxy connector screen.
140 preRequest( action );
141 action.setSource( TEST_SOURCE_ID );
142 action.setTarget( TEST_TARGET_ID );
143 String status = action.confirmDisable();
144 assertEquals( Action.INPUT, status );
145 assertNoErrors( action );
147 // Perform the disable.
148 preRequest( action );
149 status = action.disable();
150 assertEquals( Action.SUCCESS, status );
151 assertNoErrors( action );
152 assertHasMessages( action );
154 // Test the configuration.
155 assertEquals( 1, archivaConfiguration.getConfiguration().getProxyConnectors().size() );
156 ProxyConnectorConfiguration config =
157 (ProxyConnectorConfiguration) archivaConfiguration.getConfiguration().getProxyConnectors().get( 0 );
158 assertTrue( config.isDisabled() );
161 public void testSecureActionBundle()
164 expectConfigurationRequests( 1 );
165 archivaConfigurationControl.replay();
167 SecureActionBundle bundle = action.getSecureActionBundle();
168 assertTrue( bundle.requiresAuthentication() );
169 assertEquals( 1, bundle.getAuthorizationTuples().size() );
172 public void testConfirmEnable()
175 expectConfigurationRequests( 1 );
176 archivaConfigurationControl.replay();
178 // Show the confirm the disable of proxy connector screen.
179 preRequest( action );
180 action.setSource( TEST_SOURCE_ID );
181 action.setTarget( TEST_TARGET_ID );
182 String status = action.confirmDisable();
183 assertEquals( Action.INPUT, status );
184 assertNoErrors( action );
187 private Configuration createInitialConfiguration()
189 Configuration config = new Configuration();
191 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
192 managedRepo.setId( TEST_SOURCE_ID );
193 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
194 managedRepo.setReleases( true );
196 config.addManagedRepository( managedRepo );
198 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
199 remoteRepo.setId( TEST_TARGET_ID );
200 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
202 config.addRemoteRepository( remoteRepo );
204 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
205 connector.setSourceRepoId( TEST_SOURCE_ID );
206 connector.setTargetRepoId( TEST_TARGET_ID );
208 connector.setDisabled( false );
210 config.addProxyConnector( connector );
215 private void expectConfigurationRequests( int requestConfigCount )
216 throws RegistryException, IndeterminateConfigurationException
218 Configuration config = createInitialConfiguration();
220 for ( int i = 0; i < requestConfigCount; i++ )
222 archivaConfiguration.getConfiguration();
223 archivaConfigurationControl.setReturnValue( config );
226 archivaConfiguration.save( config );