1 package org.apache.archiva.remotedownload;
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.google.common.io.Files;
23 import org.apache.archiva.admin.model.beans.RemoteRepository;
24 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.cxf.jaxrs.client.WebClient;
27 import org.eclipse.jetty.server.Handler;
28 import org.eclipse.jetty.server.Server;
29 import org.eclipse.jetty.server.handler.DefaultHandler;
30 import org.eclipse.jetty.server.handler.HandlerList;
31 import org.eclipse.jetty.server.handler.ResourceHandler;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
38 import static org.assertj.core.api.Assertions.assertThat;
41 * @author Olivier Lamy
43 public class RemoteRepositoryConnectivityCheckTest
44 extends AbstractDownloadTest
48 public static void setAppServerBase()
50 previousAppServerBase = System.getProperty( "appserver.base" );
51 System.setProperty( "appserver.base", "target/" + RemoteRepositoryConnectivityCheckTest.class.getName() );
56 public static void resetAppServerBase()
58 System.setProperty( "appserver.base", previousAppServerBase );
62 protected String getSpringConfigLocation()
64 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
68 public void checkRemoteConnectivity()
73 buildStaticServer( new File( System.getProperty( "basedir" ) + "/src/test/repositories/test-repo" ) );
76 RemoteRepositoriesService service = getRemoteRepositoriesService();
78 WebClient.client( service ).header( "Authorization", authorizationHeader );
83 int repoServerPort = repoServer.getConnectors()[0].getLocalPort();
85 RemoteRepository repo = getRemoteRepository();
87 repo.setUrl( "http://localhost:" + repoServerPort );
89 service.addRemoteRepository( repo );
91 assertThat( service.checkRemoteConnectivity( repo.getId() ) ).isTrue();
95 service.deleteRemoteRepository( "id-new" );
101 public void checkRemoteConnectivityEmptyRemote()
105 File tmpDir = Files.createTempDir();
106 Server repoServer = buildStaticServer( tmpDir );
109 RemoteRepositoriesService service = getRemoteRepositoriesService();
111 WebClient.client( service ).header( "Authorization", authorizationHeader );
116 int repoServerPort = repoServer.getConnectors()[0].getLocalPort();
118 RemoteRepository repo = getRemoteRepository();
120 repo.setUrl( "http://localhost:" + repoServerPort );
122 service.addRemoteRepository( repo );
124 assertThat( service.checkRemoteConnectivity( repo.getId() ) ).isTrue();
128 service.deleteRemoteRepository( "id-new" );
129 FileUtils.deleteQuietly( tmpDir );
135 public void checkRemoteConnectivityFail()
139 RemoteRepositoriesService service = getRemoteRepositoriesService();
141 WebClient.client( service ).header( "Authorization", authorizationHeader );
146 RemoteRepository repo = getRemoteRepository();
148 repo.setUrl( "http://localhost:8956" );
150 service.addRemoteRepository( repo );
152 assertThat( service.checkRemoteConnectivity( repo.getId() ) ).isFalse();
156 service.deleteRemoteRepository( "id-new" );
161 protected Server buildStaticServer( File path )
163 Server repoServer = new Server( 0 );
165 ResourceHandler resourceHandler = new ResourceHandler();
166 resourceHandler.setDirectoriesListed( true );
167 resourceHandler.setWelcomeFiles( new String[]{ "index.html" } );
168 resourceHandler.setResourceBase( path.getAbsolutePath() );
170 HandlerList handlers = new HandlerList();
171 handlers.setHandlers( new Handler[]{ resourceHandler, new DefaultHandler() } );
172 repoServer.setHandler( handlers );
178 RemoteRepository getRemoteRepository()
180 return new RemoteRepository( "id-new", "new one", "http://foo.com", "default", "foo", "foopassword", 120,