1 package org.apache.maven.archiva.web.action.admin.connectors.proxy;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import com.opensymphony.xwork.Action;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.Configuration;
26 import org.apache.maven.archiva.configuration.IndeterminateConfigurationException;
27 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.maven.archiva.configuration.ProxyConnectorConfiguration;
29 import org.apache.maven.archiva.configuration.RemoteRepositoryConfiguration;
30 import org.apache.maven.archiva.web.action.AbstractWebworkTestCase;
31 import org.codehaus.plexus.redback.xwork.interceptor.SecureActionBundle;
32 import org.codehaus.plexus.registry.RegistryException;
33 import org.easymock.MockControl;
36 * DeleteProxyConnectorActionTest
38 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
41 public class DeleteProxyConnectorActionTest
42 extends AbstractWebworkTestCase
44 private static final String TEST_TARGET_ID = "central";
46 private static final String TEST_SOURCE_ID = "corporate";
48 private DeleteProxyConnectorAction action;
50 private MockControl archivaConfigurationControl;
52 private ArchivaConfiguration archivaConfiguration;
54 public void testConfirmDelete()
57 expectConfigurationRequests( 1 );
58 archivaConfigurationControl.replay();
60 // Show the confirm the delete of proxy connector screen.
62 action.setSource( TEST_SOURCE_ID );
63 action.setTarget( TEST_TARGET_ID );
64 String status = action.confirmDelete();
65 assertEquals( Action.INPUT, status );
66 assertNoErrors( action );
69 public void testConfirmDeleteBadSourceOrTarget()
72 expectConfigurationRequests( 4 );
73 archivaConfigurationControl.replay();
75 // Attempt to show the confirm delete screen, but provide
76 // a bad source id or target id to actually delete
79 action.setSource( "bad-source" ); // id doesn't exist.
80 action.setTarget( "bad-target" ); // id doesn't exist.
81 String status = action.confirmDelete();
82 // Should have resulted in an error.
83 assertEquals( Action.ERROR, status );
84 assertHasErrors( action );
87 action.setSource( "bad" ); // Bad doesn't exist.
88 action.setTarget( TEST_TARGET_ID );
89 status = action.confirmDelete();
90 // Should have resulted in an error.
91 assertEquals( Action.ERROR, status );
92 assertHasErrors( action );
95 action.setSource( TEST_SOURCE_ID );
96 action.setTarget( "bad" ); // Bad doesn't exist.
97 status = action.confirmDelete();
98 // Should have resulted in an error.
99 assertEquals( Action.ERROR, status );
100 assertHasErrors( action );
103 public void testConfirmDeleteNoSourceOrTarget()
106 expectConfigurationRequests( 1 );
107 archivaConfigurationControl.replay();
109 // Attempt to show the confirm delete screen, but don't provide
110 // the source id or target id to actually delete
112 preRequest( action );
113 action.setSource( null ); // No source Id.
114 action.setTarget( null ); // No target Id.
115 String status = action.confirmDelete();
116 // Should have resulted in an error.
117 assertEquals( Action.ERROR, status );
118 assertHasErrors( action );
120 preRequest( action );
121 action.setSource( TEST_SOURCE_ID );
122 action.setTarget( null ); // No target Id.
123 status = action.confirmDelete();
124 // Should have resulted in an error.
125 assertEquals( Action.ERROR, status );
126 assertHasErrors( action );
128 preRequest( action );
129 action.setSource( null ); // No source Id.
130 action.setTarget( TEST_TARGET_ID );
131 status = action.confirmDelete();
132 // Should have resulted in an error.
133 assertEquals( Action.ERROR, status );
134 assertHasErrors( action );
137 public void testDelete()
140 expectConfigurationRequests( 5 );
141 archivaConfigurationControl.replay();
143 // Show the confirm the delete of proxy connector screen.
144 preRequest( action );
145 action.setSource( TEST_SOURCE_ID );
146 action.setTarget( TEST_TARGET_ID );
147 String status = action.confirmDelete();
148 assertEquals( Action.INPUT, status );
149 assertNoErrors( action );
151 // Perform the delete.
152 preRequest( action );
153 status = action.delete();
154 assertEquals( Action.SUCCESS, status );
155 assertNoErrors( action );
156 assertHasMessages( action );
158 // Test the configuration.
159 assertEquals( 0, archivaConfiguration.getConfiguration().getProxyConnectors().size() );
162 public void testSecureActionBundle()
165 expectConfigurationRequests( 1 );
166 archivaConfigurationControl.replay();
168 SecureActionBundle bundle = action.getSecureActionBundle();
169 assertTrue( bundle.requiresAuthentication() );
170 assertEquals( 1, bundle.getAuthorizationTuples().size() );
173 private Configuration createInitialConfiguration()
175 Configuration config = new Configuration();
177 ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
178 managedRepo.setId( TEST_SOURCE_ID );
179 managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
180 managedRepo.setReleases( true );
182 config.addManagedRepository( managedRepo );
184 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
185 remoteRepo.setId( TEST_TARGET_ID );
186 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
188 config.addRemoteRepository( remoteRepo );
190 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
191 connector.setSourceRepoId( TEST_SOURCE_ID );
192 connector.setTargetRepoId( TEST_TARGET_ID );
194 config.addProxyConnector( connector );
199 private void expectConfigurationRequests( int requestConfigCount )
200 throws RegistryException, IndeterminateConfigurationException
202 Configuration config = createInitialConfiguration();
204 for ( int i = 0; i < requestConfigCount; i++ )
206 archivaConfiguration.getConfiguration();
207 archivaConfigurationControl.setReturnValue( config );
210 archivaConfiguration.save( config );
214 protected void setUp()
219 action = (DeleteProxyConnectorAction) lookup( Action.class.getName(), "deleteProxyConnectorAction" );
221 archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
222 archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
223 action.setArchivaConfiguration( archivaConfiguration );