1 package org.apache.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.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;
39 import java.util.Collections;
40 import java.util.List;
41 import org.junit.Before;
42 import org.junit.Test;
45 * SortProxyConnectorsActionTest
49 public class SortProxyConnectorsActionTest
50 extends AbstractWebworkTestCase
52 private static final String JAVAX = "javax";
54 private static final String CENTRAL = "central";
56 private static final String CORPORATE = "corporate";
58 private static final String CODEHAUS = "codehaus";
60 private SortProxyConnectorsAction action;
62 private MockControl archivaConfigurationControl;
64 private ArchivaConfiguration archivaConfiguration;
73 action = (SortProxyConnectorsAction) getActionProxy( "/admin/sortUpProxyConnector.action" ).getAction();
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 );
85 private void expectConfigurationRequests( int requestConfigCount )
86 throws RegistryException, IndeterminateConfigurationException
88 Configuration config = createInitialConfiguration();
90 archivaConfiguration.getConfiguration();
91 archivaConfigurationControl.setReturnValue( config, requestConfigCount );
93 archivaConfiguration.save( config );
97 public void testSecureActionBundle()
100 expectConfigurationRequests( 1 );
101 archivaConfigurationControl.replay();
103 SecureActionBundle bundle = action.getSecureActionBundle();
104 assertTrue( bundle.requiresAuthentication() );
105 assertEquals( 1, bundle.getAuthorizationTuples().size() );
109 public void testSortDown()
112 expectConfigurationRequests( 7 );
113 archivaConfigurationControl.replay();
115 action.setSource( CORPORATE );
116 action.setTarget( CENTRAL );
117 String status = action.sortDown();
118 assertEquals( Action.SUCCESS, status );
120 assertOrder( new String[]{ JAVAX, CENTRAL, CODEHAUS } );
124 public void testSortDownPastEnd()
127 expectConfigurationRequests( 7 );
128 archivaConfigurationControl.replay();
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 );
137 assertOrder( new String[]{ CENTRAL, JAVAX, CODEHAUS } );
141 public void testSortUp()
144 expectConfigurationRequests( 7 );
145 archivaConfigurationControl.replay();
147 action.setSource( CORPORATE );
148 action.setTarget( CODEHAUS );
149 String status = action.sortUp();
150 assertEquals( Action.SUCCESS, status );
152 assertOrder( new String[]{ CENTRAL, CODEHAUS, JAVAX } );
156 public void testSortUpPastBeginning()
159 expectConfigurationRequests( 7 );
160 archivaConfigurationControl.replay();
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 );
169 assertOrder( new String[]{ CENTRAL, JAVAX, CODEHAUS } );
172 private void assertOrder( String[] targetRepoOrder )
174 List<ProxyConnectorConfiguration> connectors = archivaConfiguration.getConfiguration().getProxyConnectors();
175 Collections.sort( connectors, ProxyConnectorConfigurationOrderComparator.getInstance() );
177 for ( ProxyConnectorConfiguration connector : connectors )
179 assertEquals( "All connectors in list should have the same source id (in this test)", CORPORATE,
180 connector.getSourceRepoId() );
183 assertEquals( targetRepoOrder.length, connectors.size() );
185 int orderFailedAt = ( -1 );
186 for ( int i = 0; i < targetRepoOrder.length; i++ )
188 if ( !StringUtils.equals( targetRepoOrder[i], connectors.get( i ).getTargetRepoId() ) )
195 if ( orderFailedAt >= 0 )
197 StringBuffer msg = new StringBuffer();
199 msg.append( "Failed expected order of the proxy connectors <" );
200 msg.append( StringUtils.join( targetRepoOrder, ", " ) );
201 msg.append( ">, actual <" );
203 boolean needsComma = false;
204 for ( ProxyConnectorConfiguration proxy : connectors )
210 msg.append( proxy.getTargetRepoId() );
213 msg.append( "> failure at index <" ).append( orderFailedAt ).append( ">." );
215 fail( msg.toString() );
219 private Configuration createInitialConfiguration()
221 Configuration config = new Configuration();
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 );
229 RemoteRepositoryConfiguration remoteRepo = new RemoteRepositoryConfiguration();
230 remoteRepo.setId( CENTRAL );
231 remoteRepo.setUrl( "http://repo1.maven.org/maven2/" );
232 config.addRemoteRepository( remoteRepo );
234 remoteRepo = new RemoteRepositoryConfiguration();
235 remoteRepo.setId( JAVAX );
236 remoteRepo.setUrl( "http://download.java.net/maven/2/" );
237 config.addRemoteRepository( remoteRepo );
239 remoteRepo = new RemoteRepositoryConfiguration();
240 remoteRepo.setId( CODEHAUS );
241 remoteRepo.setUrl( "http://repository.codehaus.org/" );
242 config.addRemoteRepository( remoteRepo );
244 ProxyConnectorConfiguration connector = new ProxyConnectorConfiguration();
245 connector.setSourceRepoId( CORPORATE );
246 connector.setTargetRepoId( CENTRAL );
247 connector.setOrder( 1 );
248 config.addProxyConnector( connector );
250 connector = new ProxyConnectorConfiguration();
251 connector.setSourceRepoId( CORPORATE );
252 connector.setTargetRepoId( JAVAX );
253 connector.setOrder( 2 );
254 config.addProxyConnector( connector );
256 connector = new ProxyConnectorConfiguration();
257 connector.setSourceRepoId( CORPORATE );
258 connector.setTargetRepoId( CODEHAUS );
259 connector.setOrder( 3 );
260 config.addProxyConnector( connector );