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