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 DisableProxyConnectorActionTest
33 extends AbstractWebworkTestCase
35 private static final String TEST_TARGET_ID = "central";
37 private static final String TEST_SOURCE_ID = "corporate";
39 private DisableProxyConnectorAction action;
41 private MockControl archivaConfigurationControl;
43 private ArchivaConfiguration archivaConfiguration;
45 public void testConfirmDisableBadSourceOrTarget()
48 expectConfigurationRequests( 4 );
49 archivaConfigurationControl.replay();
51 // Attempt to show the confirm disable screen, but provide
52 // a bad source id or target id to actually delete
55 action.setSource( "bad-source" ); // id doesn't exist.
56 action.setTarget( "bad-target" ); // id doesn't exist.
57 String status = action.confirmDisable();
58 // Should have resulted in an error.
59 assertEquals( Action.ERROR, status );
60 assertHasErrors( action );
63 action.setSource( "bad" ); // Bad doesn't exist.
64 action.setTarget( TEST_TARGET_ID );
65 status = action.confirmDisable();
66 // Should have resulted in an error.
67 assertEquals( Action.ERROR, status );
68 assertHasErrors( action );
71 action.setSource( TEST_SOURCE_ID );
72 action.setTarget( "bad" ); // Bad doesn't exist.
73 status = action.confirmDisable();
74 // Should have resulted in an error.
75 assertEquals( Action.ERROR, status );
76 assertHasErrors( action );
79 public void testConfirmDisableNoSourceOrTarget()
82 expectConfigurationRequests( 1 );
83 archivaConfigurationControl.replay();
85 // Attempt to show the confirm disable screen, but don't provide
86 // the source id or target id to actually delete
89 action.setSource( null ); // No source Id.
90 action.setTarget( null ); // No target Id.
91 String status = action.confirmDisable();
92 // Should have resulted in an error.
93 assertEquals( Action.ERROR, status );
94 assertHasErrors( action );
97 action.setSource( TEST_SOURCE_ID );
98 action.setTarget( null ); // No target Id.
99 status = action.confirmDisable();
100 // Should have resulted in an error.
101 assertEquals( Action.ERROR, status );
102 assertHasErrors( action );
104 preRequest( action );
105 action.setSource( null ); // No source Id.
106 action.setTarget( TEST_TARGET_ID );
107 status = action.confirmDisable();
108 // Should have resulted in an error.
109 assertEquals( Action.ERROR, status );
110 assertHasErrors( action );
113 public void testDelete()
116 expectConfigurationRequests( 5 );
117 archivaConfigurationControl.replay();
119 // Show the confirm the disable of proxy connector screen.
120 preRequest( action );
121 action.setSource( TEST_SOURCE_ID );
122 action.setTarget( TEST_TARGET_ID );
123 String status = action.confirmDisable();
124 assertEquals( Action.INPUT, status );
125 assertNoErrors( action );
127 // Perform the disable.
128 preRequest( action );
129 status = action.disable();
130 assertEquals( Action.SUCCESS, status );
131 assertNoErrors( action );
132 assertHasMessages( action );
134 // Test the configuration.
135 assertEquals( 1, archivaConfiguration.getConfiguration().getProxyConnectors().size() );
136 ProxyConnectorConfiguration config =
137 (ProxyConnectorConfiguration) archivaConfiguration.getConfiguration().getProxyConnectors().get( 0 );
138 assertTrue( config.isDisabled() );
141 public void testSecureActionBundle()
144 expectConfigurationRequests( 1 );
145 archivaConfigurationControl.replay();
147 SecureActionBundle bundle = action.getSecureActionBundle();
148 assertTrue( bundle.requiresAuthentication() );
149 assertEquals( 1, bundle.getAuthorizationTuples().size() );
152 public void testConfirmEnable()
155 expectConfigurationRequests( 1 );
156 archivaConfigurationControl.replay();
158 // Show the confirm the disable of proxy connector screen.
159 preRequest( action );
160 action.setSource( TEST_SOURCE_ID );
161 action.setTarget( TEST_TARGET_ID );
162 String status = action.confirmDisable();
163 assertEquals( Action.INPUT, status );
164 assertNoErrors( action );
167 private Configuration createInitialConfiguration()
169 Configuration config = new Configuration();
171 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
172 managedRepo.setId( TEST_SOURCE_ID );
173 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
174 managedRepo.setReleases( true );
176 config.addManagedRepository( managedRepo );
178 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
179 remoteRepo.setId( TEST_TARGET_ID );
180 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
182 config.addRemoteRepository( remoteRepo );
184 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
185 connector.setSourceRepoId( TEST_SOURCE_ID );
186 connector.setTargetRepoId( TEST_TARGET_ID );
188 connector.setDisabled( false );
190 config.addProxyConnector( connector );
195 private void expectConfigurationRequests( int requestConfigCount )
196 throws RegistryException, IndeterminateConfigurationException
198 Configuration config = createInitialConfiguration();
200 for ( int i = 0; i < requestConfigCount; i++ )
202 archivaConfiguration.getConfiguration();
203 archivaConfigurationControl.setReturnValue( config );
206 archivaConfiguration.save( config );
210 protected void setUp()
215 //action = (DisableProxyConnectorAction) lookup( Action.class.getName(), "disableProxyConnectorAction" );
216 action = (DisableProxyConnectorAction) getActionProxy( "/admin/disableProxyConnector.action" ).getAction();
218 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
219 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
220 action.setArchivaConfiguration( archivaConfiguration );