]> source.dussan.org Git - archiva.git/blob
227da18bc20ea19024de800ecae21f2338c2baa0
[archiva.git] /
1 package org.apache.archiva.remotedownload;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20
21 import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
22 import junit.framework.TestCase;
23 import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
24 import org.apache.archiva.redback.rest.api.model.User;
25 import org.apache.archiva.redback.rest.api.services.RoleManagementService;
26 import org.apache.archiva.redback.rest.api.services.UserService;
27 import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
28 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
29 import org.apache.archiva.rest.api.services.ProxyConnectorService;
30 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
31 import org.apache.archiva.rest.api.services.RepositoriesService;
32 import org.apache.archiva.rest.api.services.RepositoryGroupService;
33 import org.apache.archiva.rest.api.services.SearchService;
34 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
35 import org.apache.archiva.webdav.RepositoryServlet;
36 import org.apache.catalina.Context;
37 import org.apache.catalina.deploy.ApplicationListener;
38 import org.apache.catalina.deploy.ApplicationParameter;
39 import org.apache.catalina.startup.Tomcat;
40 import org.apache.commons.lang.StringUtils;
41 import org.apache.cxf.common.util.Base64Utility;
42 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
43 import org.apache.cxf.jaxrs.client.WebClient;
44 import org.apache.cxf.transport.servlet.CXFServlet;
45 import org.eclipse.jetty.server.Server;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.junit.runner.RunWith;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.web.context.ContextLoaderListener;
52
53 import java.util.ArrayList;
54 import java.util.Collections;
55 import java.util.Enumeration;
56 import java.util.List;
57 import java.util.zip.ZipEntry;
58 import java.util.zip.ZipFile;
59
60 /**
61  * @author Olivier Lamy
62  */
63 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
64 public abstract class AbstractDownloadTest
65     extends TestCase
66 {
67
68     protected final Logger log = LoggerFactory.getLogger( getClass() );
69
70     protected static String previousAppServerBase;
71
72     public String authorizationHeader = getAdminAuthzHeader();
73
74     public Server server = null;
75
76     public Tomcat tomcat;
77
78     public int port;
79
80     public static String encode( String uid, String password )
81     {
82         return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
83     }
84
85     public static String getAdminAuthzHeader()
86     {
87         String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
88         if ( StringUtils.isBlank( adminPwdSysProps ) )
89         {
90             return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, FakeCreateAdminService.ADMIN_TEST_PWD );
91         }
92         return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
93     }
94
95
96     protected abstract String getSpringConfigLocation();
97
98
99     protected String getRestServicesPath()
100     {
101         return "restServices";
102     }
103
104
105     @Before
106     public void startServer()
107         throws Exception
108     {
109
110         System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
111
112         tomcat = new Tomcat();
113         tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
114         tomcat.setPort( 0 );
115
116         Context context = tomcat.addContext( "", System.getProperty( "java.io.tmpdir" ) );
117
118         ApplicationParameter applicationParameter = new ApplicationParameter();
119         applicationParameter.setName( "contextConfigLocation" );
120         applicationParameter.setValue( getSpringConfigLocation() );
121         context.addApplicationParameter( applicationParameter );
122
123         context.addApplicationListener( new ApplicationListener( ContextLoaderListener.class.getName(), false ) );
124
125         tomcat.addServlet( context, "cxf", new CXFServlet() );
126         context.addServletMapping( "/" + getRestServicesPath() + "/*" , "cxf" );
127
128         tomcat.addServlet( context, "archivarepo", new RepositoryServlet() );
129         context.addServletMapping( "/repository/*" , "archivarepo" );
130
131         tomcat.start();
132
133         port = tomcat.getConnector().getLocalPort();
134
135
136         log.info( "start server on port {}", this.port );
137
138         User user = new User();
139         user.setEmail( "toto@toto.fr" );
140         user.setFullName( "the root user" );
141         user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
142         user.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
143
144         getUserService( null ).createAdminUser( user );
145
146
147     }
148
149
150     @After
151     @Override
152     public void tearDown()
153         throws Exception
154     {
155         System.clearProperty( "redback.admin.creation.file" );
156         super.tearDown();
157         if ( this.server != null )
158         {
159             this.server.stop();
160         }
161         if (this.tomcat != null)
162         {
163             this.tomcat.stop();
164         }
165     }
166
167
168     protected ProxyConnectorService getProxyConnectorService()
169     {
170         ProxyConnectorService service =
171             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
172                                        ProxyConnectorService.class,
173                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
174
175         WebClient.client( service ).header( "Authorization", authorizationHeader );
176         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
177         return service;
178     }
179
180     protected RemoteRepositoriesService getRemoteRepositoriesService()
181     {
182         RemoteRepositoriesService service =
183             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
184                                        RemoteRepositoriesService.class,
185                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
186
187         WebClient.client( service ).header( "Authorization", authorizationHeader );
188         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
189         return service;
190     }
191
192     protected ManagedRepositoriesService getManagedRepositoriesService()
193     {
194         ManagedRepositoriesService service =
195             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
196                                        ManagedRepositoriesService.class,
197                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
198
199         WebClient.client( service ).header( "Authorization", authorizationHeader );
200         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
201         return service;
202     }
203
204
205     protected RepositoryGroupService getRepositoryGroupService()
206     {
207         RepositoryGroupService service =
208             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
209                                        RepositoryGroupService.class,
210                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
211
212         WebClient.client( service ).header( "Authorization", authorizationHeader );
213         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
214         return service;
215     }
216
217     protected RepositoriesService getRepositoriesService()
218     {
219         RepositoriesService service =
220             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
221                                        RepositoriesService.class,
222                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
223
224         WebClient.client( service ).header( "Authorization", authorizationHeader );
225         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
226         return service;
227     }
228
229     protected SearchService getSearchService()
230     {
231         SearchService service =
232             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
233                                        SearchService.class,
234                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
235
236         WebClient.client( service ).header( "Authorization", authorizationHeader );
237         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
238         return service;
239     }
240
241     protected String getBaseUrl()
242     {
243         String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
244         return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
245     }
246
247
248     protected RoleManagementService getRoleManagementService( String authzHeader )
249     {
250         RoleManagementService service =
251             JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
252                                        RoleManagementService.class,
253                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
254
255         // for debuging purpose
256         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
257
258         if ( authzHeader != null )
259         {
260             WebClient.client( service ).header( "Authorization", authzHeader );
261         }
262         return service;
263     }
264
265     protected UserService getUserService( String authzHeader )
266     {
267         UserService service =
268             JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
269                                        UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
270
271         // for debuging purpose
272         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
273
274         if ( authzHeader != null )
275         {
276             WebClient.client( service ).header( "Authorization", authzHeader );
277         }
278         return service;
279     }
280
281     protected FakeCreateAdminService getFakeCreateAdminService()
282     {
283         return JAXRSClientFactory.create(
284             "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
285             FakeCreateAdminService.class );
286     }
287
288
289     protected List<String> getZipEntriesNames( ZipFile zipFile )
290     {
291         try
292         {
293             List<String> entriesNames = new ArrayList<>();
294             Enumeration<? extends ZipEntry> entries = zipFile.entries();
295             while ( entries.hasMoreElements() )
296             {
297                 entriesNames.add( entries.nextElement().getName() );
298             }
299             return entriesNames;
300         }
301         catch ( Throwable e )
302         {
303             log.info( "fail to get zipEntries {}", e.getMessage(), e );
304         }
305         return Collections.emptyList();
306     }
307 }