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