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 org.apache.archiva.admin.model.beans.RemoteRepository;
23 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
24 import org.apache.cxf.jaxrs.client.WebClient;
25 import org.eclipse.jetty.server.Handler;
26 import org.eclipse.jetty.server.HttpConnectionFactory;
27 import org.eclipse.jetty.server.Server;
28 import org.eclipse.jetty.server.ServerConnector;
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;
36 import java.nio.file.Files;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
40 import static org.assertj.core.api.Assertions.assertThat;
43 * @author Olivier Lamy
45 public class RemoteRepositoryConnectivityCheckTest
46 extends AbstractDownloadTest
50 public static void setAppServerBase()
52 previousAppServerBase = System.getProperty( "appserver.base" );
53 System.setProperty( "appserver.base", "target/" + RemoteRepositoryConnectivityCheckTest.class.getName() );
58 public static void resetAppServerBase()
60 System.setProperty( "appserver.base", previousAppServerBase );
64 protected String getSpringConfigLocation()
66 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
70 public void checkRemoteConnectivity()
75 buildStaticServer( Paths.get( System.getProperty( "basedir" ), "src/test/repositories/test-repo" ) );
77 ServerConnector serverConnector = new ServerConnector( repoServer, new HttpConnectionFactory());
78 repoServer.addConnector( serverConnector );
81 RemoteRepositoriesService service = getRemoteRepositoriesService();
83 WebClient.client( service ).header( "Authorization", authorizationHeader );
88 int repoServerPort = serverConnector.getLocalPort();
90 RemoteRepository repo = getRemoteRepository();
92 repo.setUrl( "http://localhost:" + repoServerPort );
94 service.addRemoteRepository( repo );
96 assertThat( service.checkRemoteConnectivity( repo.getId() ) ).isTrue();
100 service.deleteRemoteRepository( "id-new" );
106 public void checkRemoteConnectivityEmptyRemote()
110 Path tmpDir = Files.createTempDirectory( "test" );
111 Server repoServer = buildStaticServer( tmpDir );
112 ServerConnector serverConnector = new ServerConnector( repoServer, new HttpConnectionFactory());
113 repoServer.addConnector( serverConnector );
116 RemoteRepositoriesService service = getRemoteRepositoriesService();
118 WebClient.client( service ).header( "Authorization", authorizationHeader );
123 int repoServerPort = serverConnector.getLocalPort();
125 RemoteRepository repo = getRemoteRepository();
127 repo.setUrl( "http://localhost:" + repoServerPort );
129 service.addRemoteRepository( repo );
131 assertThat( service.checkRemoteConnectivity( repo.getId() ) ).isTrue();
135 service.deleteRemoteRepository( "id-new" );
136 org.apache.archiva.common.utils.FileUtils.deleteQuietly( tmpDir );
142 public void checkRemoteConnectivityFail()
146 RemoteRepositoriesService service = getRemoteRepositoriesService();
148 WebClient.client( service ).header( "Authorization", authorizationHeader );
153 RemoteRepository repo = getRemoteRepository();
155 repo.setUrl( "http://localhost:8956" );
157 service.addRemoteRepository( repo );
159 assertThat( service.checkRemoteConnectivity( repo.getId() ) ).isFalse();
163 service.deleteRemoteRepository( "id-new" );
168 protected Server buildStaticServer( Path path )
170 Server repoServer = new Server( );
172 ResourceHandler resourceHandler = new ResourceHandler();
173 resourceHandler.setDirectoriesListed( true );
174 resourceHandler.setWelcomeFiles( new String[]{ "index.html" } );
175 resourceHandler.setResourceBase( path.toAbsolutePath().toString() );
177 HandlerList handlers = new HandlerList();
178 handlers.setHandlers( new Handler[]{ resourceHandler, new DefaultHandler() } );
179 repoServer.setHandler( handlers );
185 RemoteRepository getRemoteRepository()
187 return new RemoteRepository( "id-new", "new one", "http://foo.com", "default", "foo", "foopassword", 120,