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.commons.io.FileUtils;
25 import org.apache.cxf.jaxrs.client.WebClient;
26 import org.eclipse.jetty.server.Handler;
27 import org.eclipse.jetty.server.HttpConnectionFactory;
28 import org.eclipse.jetty.server.Server;
29 import org.eclipse.jetty.server.ServerConnector;
30 import org.eclipse.jetty.server.handler.DefaultHandler;
31 import org.eclipse.jetty.server.handler.HandlerList;
32 import org.eclipse.jetty.server.handler.ResourceHandler;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
37 import java.io.IOException;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.util.Locale;
42 import static org.assertj.core.api.Assertions.assertThat;
45 * @author Olivier Lamy
47 public class RemoteRepositoryConnectivityCheckTest
48 extends AbstractDownloadTest
51 private static Path appServerBase;
54 public static void setAppServerBase()
57 previousAppServerBase = System.getProperty( "appserver.base" );
58 appServerBase = Files.createTempDirectory( "archiva-common-web_appsrv6_" ).toAbsolutePath( );
59 System.setProperty( "appserver.base", appServerBase.toString( ) );
63 public static void resetAppServerBase()
65 if (Files.exists(appServerBase)) {
66 FileUtils.deleteQuietly( appServerBase.toFile() );
68 System.setProperty( "appserver.base", previousAppServerBase );
72 protected String getSpringConfigLocation()
74 System.out.println( "AppserverBase: " + System.getProperty( "appserver.base" ) );
75 return "classpath*:META-INF/spring-context.xml classpath*:spring-context-test-common.xml classpath*:spring-context-artifacts-download.xml";
79 public void checkRemoteConnectivity()
82 String id = Long.toString( System.currentTimeMillis() );
84 Path srcRep = getProjectDirectory( ).resolve( "src/test/repositories/test-repo" );
85 Path testRep = getBasedir( ).resolve( "target" ).resolve( "test-repo-" + id ).toAbsolutePath();
86 FileUtils.copyDirectory( srcRep.toFile( ), testRep.toFile( ) );
87 createdPaths.add( testRep );
91 buildStaticServer( testRep );
93 ServerConnector serverConnector = new ServerConnector( repoServer, new HttpConnectionFactory());
94 repoServer.addConnector( serverConnector );
97 RemoteRepositoriesService service = getRemoteRepositoriesService();
99 WebClient.client( service ).header( "Authorization", authorizationHeader );
104 int repoServerPort = serverConnector.getLocalPort();
106 RemoteRepository repo = getRemoteRepository();
108 repo.setUrl( "http://localhost:" + repoServerPort );
110 service.addRemoteRepository( repo );
112 assertThat( service.checkRemoteConnectivity( repo.getId() ).isSuccess() ).isTrue();
116 service.deleteRemoteRepository( "id-new" );
122 public void checkRemoteConnectivityEmptyRemote()
126 Path tmpDir = Files.createTempDirectory( "test" );
127 Server repoServer = buildStaticServer( tmpDir );
128 ServerConnector serverConnector = new ServerConnector( repoServer, new HttpConnectionFactory());
129 repoServer.addConnector( serverConnector );
132 RemoteRepositoriesService service = getRemoteRepositoriesService();
134 WebClient.client( service ).header( "Authorization", authorizationHeader );
139 int repoServerPort = serverConnector.getLocalPort();
141 RemoteRepository repo = getRemoteRepository();
143 repo.setUrl( "http://localhost:" + repoServerPort );
145 service.addRemoteRepository( repo );
147 assertThat( service.checkRemoteConnectivity( repo.getId() ).isSuccess() ).isTrue();
151 service.deleteRemoteRepository( "id-new" );
152 org.apache.archiva.common.utils.FileUtils.deleteQuietly( tmpDir );
158 public void checkRemoteConnectivityFail()
162 RemoteRepositoriesService service = getRemoteRepositoriesService();
164 WebClient.client( service ).header( "Authorization", authorizationHeader );
169 RemoteRepository repo = getRemoteRepository();
171 repo.setUrl( "http://localhost:8956" );
173 service.addRemoteRepository( repo );
175 assertThat( service.checkRemoteConnectivity( repo.getId() ).isSuccess() ).isFalse();
179 service.deleteRemoteRepository( "id-new" );
184 protected Server buildStaticServer( Path path )
186 Server repoServer = new Server( );
188 ResourceHandler resourceHandler = new ResourceHandler();
189 resourceHandler.setDirectoriesListed( true );
190 resourceHandler.setWelcomeFiles( new String[]{ "index.html" } );
191 resourceHandler.setResourceBase( path.toAbsolutePath().toString() );
193 HandlerList handlers = new HandlerList();
194 handlers.setHandlers( new Handler[]{ resourceHandler, new DefaultHandler() } );
195 repoServer.setHandler( handlers );
201 RemoteRepository getRemoteRepository()
203 return new RemoteRepository( Locale.getDefault( ), "id-new", "new one", "http://foo.com", "default", "foo", "foopassword", 120,