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