]> source.dussan.org Git - archiva.git/blob
084a38cea32f463fd727cd79ff636183a628c274
[archiva.git] /
1 package org.apache.archiva.remotedownload;
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 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;
36
37 import java.io.IOException;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.util.Locale;
41
42 import static org.assertj.core.api.Assertions.assertThat;
43
44 /**
45  * @author Olivier Lamy
46  */
47 public class RemoteRepositoryConnectivityCheckTest
48     extends AbstractDownloadTest
49 {
50
51     private static Path appServerBase;
52
53     @BeforeClass
54     public static void setAppServerBase()
55         throws IOException
56     {
57         previousAppServerBase = System.getProperty( "appserver.base" );
58         appServerBase = Files.createTempDirectory( "archiva-common-web_appsrv6_" ).toAbsolutePath( );
59         System.setProperty( "appserver.base", appServerBase.toString( ) );
60     }
61
62     @AfterClass
63     public static void resetAppServerBase()
64     {
65         if (Files.exists(appServerBase)) {
66             FileUtils.deleteQuietly( appServerBase.toFile() );
67         }
68         System.setProperty( "appserver.base", previousAppServerBase );
69     }
70
71     @Override
72     protected String getSpringConfigLocation()
73     {
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";
76     }
77
78     @Test
79     public void checkRemoteConnectivity()
80         throws Exception
81     {
82         String id = Long.toString( System.currentTimeMillis() );
83
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 );
88
89
90         Server repoServer =
91             buildStaticServer( testRep );
92
93         ServerConnector serverConnector = new ServerConnector( repoServer, new HttpConnectionFactory());
94         repoServer.addConnector( serverConnector );
95         repoServer.start();
96
97         RemoteRepositoriesService service = getRemoteRepositoriesService();
98
99         WebClient.client( service ).header( "Authorization", authorizationHeader );
100
101         try
102         {
103
104             int repoServerPort = serverConnector.getLocalPort();
105
106             RemoteRepository repo = getRemoteRepository();
107
108             repo.setUrl( "http://localhost:" + repoServerPort );
109
110             service.addRemoteRepository( repo );
111
112             assertThat( service.checkRemoteConnectivity( repo.getId() ).isSuccess() ).isTrue();
113         }
114         finally
115         {
116             service.deleteRemoteRepository( "id-new" );
117             repoServer.stop();
118         }
119     }
120
121     @Test
122     public void checkRemoteConnectivityEmptyRemote()
123         throws Exception
124     {
125
126         Path tmpDir = Files.createTempDirectory( "test" );
127         Server repoServer = buildStaticServer( tmpDir );
128         ServerConnector serverConnector = new ServerConnector( repoServer, new HttpConnectionFactory());
129         repoServer.addConnector( serverConnector );
130         repoServer.start();
131
132         RemoteRepositoriesService service = getRemoteRepositoriesService();
133
134         WebClient.client( service ).header( "Authorization", authorizationHeader );
135
136         try
137         {
138
139             int repoServerPort = serverConnector.getLocalPort();
140
141             RemoteRepository repo = getRemoteRepository();
142
143             repo.setUrl( "http://localhost:" + repoServerPort );
144
145             service.addRemoteRepository( repo );
146
147             assertThat( service.checkRemoteConnectivity( repo.getId() ).isSuccess() ).isTrue();
148         }
149         finally
150         {
151             service.deleteRemoteRepository( "id-new" );
152             org.apache.archiva.common.utils.FileUtils.deleteQuietly( tmpDir );
153             repoServer.stop();
154         }
155     }
156
157     @Test
158     public void checkRemoteConnectivityFail()
159         throws Exception
160     {
161
162         RemoteRepositoriesService service = getRemoteRepositoriesService();
163
164         WebClient.client( service ).header( "Authorization", authorizationHeader );
165
166         try
167         {
168
169             RemoteRepository repo = getRemoteRepository();
170
171             repo.setUrl( "http://localhost:8956" );
172
173             service.addRemoteRepository( repo );
174
175             assertThat( service.checkRemoteConnectivity( repo.getId() ).isSuccess() ).isFalse();
176         }
177         finally
178         {
179             service.deleteRemoteRepository( "id-new" );
180
181         }
182     }
183
184     protected Server buildStaticServer( Path path )
185     {
186         Server repoServer = new Server(  );
187
188         ResourceHandler resourceHandler = new ResourceHandler();
189         resourceHandler.setDirectoriesListed( true );
190         resourceHandler.setWelcomeFiles( new String[]{ "index.html" } );
191         resourceHandler.setResourceBase( path.toAbsolutePath().toString() );
192
193         HandlerList handlers = new HandlerList();
194         handlers.setHandlers( new Handler[]{ resourceHandler, new DefaultHandler() } );
195         repoServer.setHandler( handlers );
196
197         return repoServer;
198     }
199
200
201     RemoteRepository getRemoteRepository()
202     {
203         return new RemoteRepository( Locale.getDefault( ), "id-new", "new one", "http://foo.com", "default", "foo", "foopassword", 120,
204                                      "cool repo" );
205     }
206
207 }