]> source.dussan.org Git - archiva.git/blob
b59e2a4bde9b7207e88f0552b926e52447371522
[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.commons.lang.StringUtils;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.configuration.Configuration;
29 import org.apache.archiva.configuration.IndeterminateConfigurationException;
30 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
31 import org.apache.archiva.configuration.ProxyConnectorConfiguration;
32 import org.apache.archiva.configuration.RemoteRepositoryConfiguration;
33 import org.apache.archiva.configuration.functors.ProxyConnectorConfigurationOrderComparator;
34 import org.apache.archiva.web.action.AbstractWebworkTestCase;
35 import org.apache.archiva.redback.components.registry.RegistryException;
36 import org.apache.archiva.redback.integration.interceptor.SecureActionBundle;
37 import org.easymock.MockControl;
38
39 import java.util.Collections;
40 import java.util.List;
41 import org.junit.Before;
42 import org.junit.Test;
43
44 /**
45  * SortProxyConnectorsActionTest
46  *
47  *
48  */
49 public class SortProxyConnectorsActionTest
50     extends AbstractWebworkTestCase
51 {
52     private static final String JAVAX = "javax";
53
54     private static final String CENTRAL = "central";
55
56     private static final String CORPORATE = "corporate";
57
58     private static final String CODEHAUS = "codehaus";
59
60     private SortProxyConnectorsAction action;
61
62     private MockControl archivaConfigurationControl;
63
64     private ArchivaConfiguration archivaConfiguration;
65
66     @Override
67     @Before
68     public void setUp()
69         throws Exception
70     {
71         super.setUp();
72
73         action = (SortProxyConnectorsAction) getActionProxy( "/admin/sortUpProxyConnector.action" ).getAction();
74
75         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
76         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
77         ( (DefaultManagedRepositoryAdmin) action.getManagedRepositoryAdmin() ).setArchivaConfiguration(
78             archivaConfiguration );
79         ( (DefaultRemoteRepositoryAdmin) action.getRemoteRepositoryAdmin() ).setArchivaConfiguration(
80             archivaConfiguration );
81         ( (DefaultProxyConnectorAdmin) action.getProxyConnectorAdmin() ).setArchivaConfiguration(
82             archivaConfiguration );
83     }
84
85     private void expectConfigurationRequests( int requestConfigCount )
86         throws RegistryException, IndeterminateConfigurationException
87     {
88         Configuration config = createInitialConfiguration();
89
90         archivaConfiguration.getConfiguration();
91         archivaConfigurationControl.setReturnValue( config, requestConfigCount );
92
93         archivaConfiguration.save( config );
94     }
95
96     @Test
97     public void testSecureActionBundle()
98         throws Exception
99     {
100         expectConfigurationRequests( 1 );
101         archivaConfigurationControl.replay();
102
103         SecureActionBundle bundle = action.getSecureActionBundle();
104         assertTrue( bundle.requiresAuthentication() );
105         assertEquals( 1, bundle.getAuthorizationTuples().size() );
106     }
107
108     @Test
109     public void testSortDown()
110         throws Exception
111     {
112         expectConfigurationRequests( 7 );
113         archivaConfigurationControl.replay();
114
115         action.setSource( CORPORATE );
116         action.setTarget( CENTRAL );
117         String status = action.sortDown();
118         assertEquals( Action.SUCCESS, status );
119
120         assertOrder( new String[]{ JAVAX, CENTRAL, CODEHAUS } );
121     }
122
123     @Test
124     public void testSortDownPastEnd()
125         throws Exception
126     {
127         expectConfigurationRequests( 7 );
128         archivaConfigurationControl.replay();
129
130         // Ask the last connector to sort down (essentially a no-op)
131         action.setSource( CORPORATE );
132         action.setTarget( CODEHAUS );
133         String status = action.sortDown();
134         assertEquals( Action.SUCCESS, status );
135
136         // No order change.
137         assertOrder( new String[]{ CENTRAL, JAVAX, CODEHAUS } );
138     }
139
140     @Test
141     public void testSortUp()
142         throws Exception
143     {
144         expectConfigurationRequests( 7 );
145         archivaConfigurationControl.replay();
146
147         action.setSource( CORPORATE );
148         action.setTarget( CODEHAUS );
149         String status = action.sortUp();
150         assertEquals( Action.SUCCESS, status );
151
152         assertOrder( new String[]{ CENTRAL, CODEHAUS, JAVAX } );
153     }
154
155     @Test
156     public void testSortUpPastBeginning()
157         throws Exception
158     {
159         expectConfigurationRequests( 7 );
160         archivaConfigurationControl.replay();
161
162         // Ask the first connector to sort up (essentially a no-op)
163         action.setSource( CORPORATE );
164         action.setTarget( CENTRAL );
165         String status = action.sortUp();
166         assertEquals( Action.SUCCESS, status );
167
168         // No order change.
169         assertOrder( new String[]{ CENTRAL, JAVAX, CODEHAUS } );
170     }
171
172     private void assertOrder( String[] targetRepoOrder )
173     {
174         List<ProxyConnectorConfiguration> connectors = archivaConfiguration.getConfiguration().getProxyConnectors();
175         Collections.sort( connectors, ProxyConnectorConfigurationOrderComparator.getInstance() );
176
177         for ( ProxyConnectorConfiguration connector : connectors )
178         {
179             assertEquals( "All connectors in list should have the same source id (in this test)", CORPORATE,
180                           connector.getSourceRepoId() );
181         }
182
183         assertEquals( targetRepoOrder.length, connectors.size() );
184
185         int orderFailedAt = ( -1 );
186         for ( int i = 0; i < targetRepoOrder.length; i++ )
187         {
188             if ( !StringUtils.equals( targetRepoOrder[i], connectors.get( i ).getTargetRepoId() ) )
189             {
190                 orderFailedAt = i;
191                 break;
192             }
193         }
194
195         if ( orderFailedAt >= 0 )
196         {
197             StringBuilder msg = new StringBuilder();
198
199             msg.append( "Failed expected order of the proxy connectors <" );
200             msg.append( StringUtils.join( targetRepoOrder, ", " ) );
201             msg.append( ">, actual <" );
202
203             boolean needsComma = false;
204             for ( ProxyConnectorConfiguration proxy : connectors )
205             {
206                 if ( needsComma )
207                 {
208                     msg.append( ", " );
209                 }
210                 msg.append( proxy.getTargetRepoId() );
211                 needsComma = true;
212             }
213             msg.append( "> failure at index <" ).append( orderFailedAt ).append( ">." );
214
215             fail( msg.toString() );
216         }
217     }
218
219     private Configuration createInitialConfiguration()
220     {
221         Configuration config = new Configuration();
222
223         ManagedRepositoryConfiguration managedRepo = new ManagedRepositoryConfiguration();
224         managedRepo.setId( CORPORATE );
225         managedRepo.setLayout( "${java.io.tmpdir}/archiva-test/managed-repo" );
226         managedRepo.setReleases( true );
227         config.addManagedRepository( managedRepo );
228
229         RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
230         remoteRepo.setId( CENTRAL );
231         remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
232         config.addRemoteRepository( remoteRepo );
233
234         remoteRepo = new RemoteRepositoryConfiguration();
235         remoteRepo.setId( JAVAX );
236         remoteRepo.setUrl( "http://download.java.net/maven/2/" );
237         config.addRemoteRepository( remoteRepo );
238
239         remoteRepo = new RemoteRepositoryConfiguration();
240         remoteRepo.setId( CODEHAUS );
241         remoteRepo.setUrl( "http://repository.codehaus.org/" );
242         config.addRemoteRepository( remoteRepo );
243
244         ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
245         connector.setSourceRepoId( CORPORATE );
246         connector.setTargetRepoId( CENTRAL );
247         connector.setOrder( 1 );
248         config.addProxyConnector( connector );
249
250         connector = new ProxyConnectorConfiguration();
251         connector.setSourceRepoId( CORPORATE );
252         connector.setTargetRepoId( JAVAX );
253         connector.setOrder( 2 );
254         config.addProxyConnector( connector );
255
256         connector = new ProxyConnectorConfiguration();
257         connector.setSourceRepoId( CORPORATE );
258         connector.setTargetRepoId( CODEHAUS );
259         connector.setOrder( 3 );
260         config.addProxyConnector( connector );
261
262         return config;
263     }
264 }