]> source.dussan.org Git - archiva.git/blob
8616d47f5178bac795a6c34c1308fb7a00e04885
[archiva.git] /
1 package org.apache.archiva.web.action.admin.connectors.proxy;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import com.opensymphony.xwork2.Action;
23 import org.apache.archiva.admin.repository.managed.DefaultManagedRepositoryAdmin;
24 import org.apache.archiva.admin.repository.proxyconnector.DefaultProxyConnectorAdmin;
25 import org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin;
26 import org.apache.archiva.configuration.ArchivaConfiguration;
27 import org.apache.archiva.configuration.Configuration;
28 import org.apache.archiva.configuration.IndeterminateConfigurationException;
29 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
31 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
32 import org.apache.archiva.web.action.AbstractWebworkTestCase;
33 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
34 import org.easymock.MockControl;
35 import org.junit.Before;
36 import org.junit.Test;
37 /**
38  * DeleteProxyConnectorActionTest
39  *
40  *
41  */
42 public class DeleteProxyConnectorActionTest
43     extends AbstractWebworkTestCase
44 {
45     private static final String TEST_TARGET_ID = "central";
46
47     private static final String TEST_SOURCE_ID = "corporate";
48
49     private DeleteProxyConnectorAction action;
50
51     private MockControl archivaConfigurationControl;
52
53     private ArchivaConfiguration archivaConfiguration;
54
55     @Override
56     @Before
57     public void setUp()
58         throws Exception
59     {
60         super.setUp();
61
62         action = (DeleteProxyConnectorAction) getActionProxy( "/admin/deleteProxyConnector.action" ).getAction();
63
64         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
65         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
66         ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
67             archivaConfiguration );
68         ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
69             archivaConfiguration );
70         ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration(
71             archivaConfiguration );
72     }
73     
74     @Test
75     public void testConfirmDelete()
76         throws Exception
77     {
78         expectConfigurationRequests( 1 );
79         archivaConfigurationControl.replay();
80
81         // Show the confirm the delete of proxy connector screen.
82         preRequest( action );
83         action.setSource( TEST_SOURCE_ID );
84         action.setTarget( TEST_TARGET_ID );
85         String status = action.confirmDelete();
86         assertEquals( Action.INPUT, status );
87         assertNoErrors( action );
88     }
89
90     @Test
91     public void testConfirmDeleteBadSourceOrTarget()
92         throws Exception
93     {
94         expectConfigurationRequests( 4 );
95         archivaConfigurationControl.replay();
96
97         // Attempt to show the confirm delete screen, but provide
98         // a bad source id or target id to actually delete
99
100         preRequest( action );
101         action.setSource( "bad-source" ); // id doesn't exist.
102         action.setTarget( "bad-target" ); // id doesn't exist.
103         String status = action.confirmDelete();
104         // Should have resulted in an error.
105         assertEquals( Action.ERROR, status );
106         assertHasErrors( action );
107
108         preRequest( action );
109         action.setSource( "bad" ); // Bad doesn't exist.
110         action.setTarget( TEST_TARGET_ID );
111         status = action.confirmDelete();
112         // Should have resulted in an error.
113         assertEquals( Action.ERROR, status );
114         assertHasErrors( action );
115
116         preRequest( action );
117         action.setSource( TEST_SOURCE_ID );
118         action.setTarget( "bad" ); // Bad doesn't exist.
119         status = action.confirmDelete();
120         // Should have resulted in an error.
121         assertEquals( Action.ERROR, status );
122         assertHasErrors( action );
123     }
124
125     @Test
126     public void testConfirmDeleteNoSourceOrTarget()
127         throws Exception
128     {
129         expectConfigurationRequests( 1 );
130         archivaConfigurationControl.replay();
131
132         // Attempt to show the confirm delete screen, but don't provide
133         // the source id or target id to actually delete
134
135         preRequest( action );
136         action.setSource( null ); // No source Id.
137         action.setTarget( null ); // No target Id.
138         String status = action.confirmDelete();
139         // Should have resulted in an error.
140         assertEquals( Action.ERROR, status );
141         assertHasErrors( action );
142
143         preRequest( action );
144         action.setSource( TEST_SOURCE_ID );
145         action.setTarget( null ); // No target Id.
146         status = action.confirmDelete();
147         // Should have resulted in an error.
148         assertEquals( Action.ERROR, status );
149         assertHasErrors( action );
150
151         preRequest( action );
152         action.setSource( null ); // No source Id.
153         action.setTarget( TEST_TARGET_ID );
154         status = action.confirmDelete();
155         // Should have resulted in an error.
156         assertEquals( Action.ERROR, status );
157         assertHasErrors( action );
158     }
159
160     @Test
161     public void testDelete()
162         throws Exception
163     {
164         expectConfigurationRequests( 5 );
165         archivaConfigurationControl.replay();
166
167         // Show the confirm the delete of proxy connector screen.
168         preRequest( action );
169         action.setSource( TEST_SOURCE_ID );
170         action.setTarget( TEST_TARGET_ID );
171         String status = action.confirmDelete();
172         assertEquals( Action.INPUT, status );
173         assertNoErrors( action );
174
175         // Perform the delete.
176         preRequest( action );
177         status = action.delete();
178         assertEquals( Action.SUCCESS, status );
179         assertNoErrors( action );
180         assertHasMessages( action );
181
182         // Test the configuration.
183         assertEquals( 0, archivaConfiguration.getConfiguration().getProxyConnectors().size() );
184     }
185
186     @Test
187     public void testSecureActionBundle()
188         throws Exception
189     {
190         expectConfigurationRequests( 1 );
191         archivaConfigurationControl.replay();
192
193         SecureActionBundle bundle = action.getSecureActionBundle();
194         assertTrue( bundle.requiresAuthentication() );
195         assertEquals( 1, bundle.getAuthorizationTuples().size() );
196     }
197
198     private Configuration createInitialConfiguration()
199     {
200         Configuration config = new Configuration();
201
202         ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
203         managedRepo.setId( TEST_SOURCE_ID );
204         managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
205         managedRepo.setReleases( true );
206
207         config.addManagedRepository( managedRepo );
208
209         RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
210         remoteRepo.setId( TEST_TARGET_ID );
211         remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
212
213         config.addRemoteRepository( remoteRepo );
214
215         ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
216         connector.setSourceRepoId( TEST_SOURCE_ID );
217         connector.setTargetRepoId( TEST_TARGET_ID );
218
219         config.addProxyConnector( connector );
220
221         return config;
222     }
223
224     private void expectConfigurationRequests( int requestConfigCount )
225         throws org.apache.archiva.redback.components.registry.RegistryException, IndeterminateConfigurationException
226     {
227         Configuration config = createInitialConfiguration();
228
229         for ( int i = 0; i < requestConfigCount; i++ )
230         {
231             archivaConfiguration.getConfiguration();
232             archivaConfigurationControl.setReturnValue( config );
233         }
234
235         archivaConfiguration.save( config );
236     }
237
238 }