]> source.dussan.org Git - archiva.git/blob
5cf32d8df8bbba97ba2fb302e953c2bfd05074ca
[archiva.git] /
1 package org.apache.archiva;
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 junit.framework.TestCase;
22 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
23 import org.apache.archiva.rest.api.services.ProxyConnectorService;
24 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
25 import org.apache.archiva.rest.api.services.RepositoriesService;
26 import org.apache.archiva.rest.api.services.RepositoryGroupService;
27 import org.apache.archiva.rest.api.services.SearchService;
28 import org.apache.archiva.webdav.RepositoryServlet;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.cxf.common.util.Base64Utility;
31 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
32 import org.apache.cxf.jaxrs.client.WebClient;
33 import org.apache.cxf.transport.servlet.CXFServlet;
34 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
35 import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
36 import org.apache.archiva.redback.rest.api.model.User;
37 import org.apache.archiva.redback.rest.api.services.RoleManagementService;
38 import org.apache.archiva.redback.rest.api.services.UserService;
39 import org.apache.archiva.redback.rest.services.FakeCreateAdminService;
40 import org.eclipse.jetty.server.Connector;
41 import org.eclipse.jetty.server.Server;
42 import org.eclipse.jetty.server.session.SessionHandler;
43 import org.eclipse.jetty.servlet.ServletContextHandler;
44 import org.eclipse.jetty.servlet.ServletHolder;
45 import org.junit.After;
46 import org.junit.Before;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.web.context.ContextLoaderListener;
50
51 import java.util.Collections;
52 import org.apache.archiva.test.ArchivaBlockJUnit4ClassRunner;
53 import org.junit.runner.RunWith;
54
55 /**
56  * @author Olivier Lamy
57  */
58 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
59 public abstract class AbstractDownloadTest
60     extends TestCase
61 {
62
63     protected Logger log = LoggerFactory.getLogger( getClass() );
64
65     static String previousAppServerBase;
66
67     public String authorizationHeader = getAdminAuthzHeader();
68
69     public Server server = null;
70
71     public int port;
72
73     public static String encode( String uid, String password )
74     {
75         return "Basic " + Base64Utility.encode( ( uid + ":" + password ).getBytes() );
76     }
77
78     public static String getAdminAuthzHeader()
79     {
80         String adminPwdSysProps = System.getProperty( "rest.admin.pwd" );
81         if ( StringUtils.isBlank( adminPwdSysProps ) )
82         {
83             return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, FakeCreateAdminService.ADMIN_TEST_PWD );
84         }
85         return encode( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME, adminPwdSysProps );
86     }
87
88
89     protected abstract String getSpringConfigLocation();
90
91
92     protected String getRestServicesPath()
93     {
94         return "restServices";
95     }
96
97
98     @Before
99     public void startServer()
100         throws Exception
101     {
102
103         System.setProperty( "redback.admin.creation.file", "target/auto-admin-creation.properties" );
104         this.server = new Server( 0 );
105
106         ServletContextHandler context = new ServletContextHandler();
107
108         context.setContextPath( "/" );
109
110         context.setInitParameter( "contextConfigLocation", getSpringConfigLocation() );
111
112         ContextLoaderListener contextLoaderListener = new ContextLoaderListener();
113
114         context.addEventListener( contextLoaderListener );
115
116         ServletHolder sh = new ServletHolder( CXFServlet.class );
117
118         SessionHandler sessionHandler = new SessionHandler();
119
120         context.setSessionHandler( sessionHandler );
121
122         context.addServlet( sh, "/" + getRestServicesPath() + "/*" );
123
124         ServletHolder repoSh = new ServletHolder( RepositoryServlet.class );
125         context.addServlet( repoSh, "/repository/*" );
126
127         server.setHandler( context );
128         this.server.start();
129         Connector connector = this.server.getConnectors()[0];
130         this.port = connector.getLocalPort();
131         log.info( "start server on port " + this.port );
132
133         User user = new User();
134         user.setEmail( "toto@toto.fr" );
135         user.setFullName( "the root user" );
136         user.setUsername( RedbackRoleConstants.ADMINISTRATOR_ACCOUNT_NAME );
137         user.setPassword( FakeCreateAdminService.ADMIN_TEST_PWD );
138
139         getUserService( null ).createAdminUser( user );
140
141
142     }
143
144
145     @After
146     public void tearDown()
147         throws Exception
148     {
149         System.clearProperty( "redback.admin.creation.file" );
150         super.tearDown();
151         if ( this.server != null )
152         {
153             this.server.stop();
154         }
155     }
156
157
158     protected ProxyConnectorService getProxyConnectorService()
159     {
160         ProxyConnectorService service =
161             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
162                                        ProxyConnectorService.class,
163                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
164
165         WebClient.client( service ).header( "Authorization", authorizationHeader );
166         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
167         return service;
168     }
169
170     protected RemoteRepositoriesService getRemoteRepositoriesService()
171     {
172         RemoteRepositoriesService service =
173             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
174                                        RemoteRepositoriesService.class,
175                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
176
177         WebClient.client( service ).header( "Authorization", authorizationHeader );
178         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
179         return service;
180     }
181
182     protected ManagedRepositoriesService getManagedRepositoriesService()
183     {
184         ManagedRepositoriesService service =
185             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
186                                        ManagedRepositoriesService.class,
187                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
188
189         WebClient.client( service ).header( "Authorization", authorizationHeader );
190         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
191         return service;
192     }
193
194
195     protected RepositoryGroupService getRepositoryGroupService()
196     {
197         RepositoryGroupService service =
198             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
199                                        RepositoryGroupService.class,
200                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
201
202         WebClient.client( service ).header( "Authorization", authorizationHeader );
203         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
204         return service;
205     }
206
207     protected RepositoriesService getRepositoriesService()
208     {
209         RepositoriesService service =
210             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
211                                        RepositoriesService.class,
212                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
213
214         WebClient.client( service ).header( "Authorization", authorizationHeader );
215         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
216         return service;
217     }
218
219     protected SearchService getSearchService()
220     {
221         SearchService service =
222             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
223                                        SearchService.class,
224                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
225
226         WebClient.client( service ).header( "Authorization", authorizationHeader );
227         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000L );
228         return service;
229     }
230
231     protected String getBaseUrl()
232     {
233         String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
234         return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
235     }
236
237
238     protected RoleManagementService getRoleManagementService( String authzHeader )
239     {
240         RoleManagementService service =
241             JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
242                                        RoleManagementService.class,
243                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
244
245         // for debuging purpose
246         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
247
248         if ( authzHeader != null )
249         {
250             WebClient.client( service ).header( "Authorization", authzHeader );
251         }
252         return service;
253     }
254
255     protected UserService getUserService( String authzHeader )
256     {
257         UserService service =
258             JAXRSClientFactory.create( "http://localhost:" + port + "/" + getRestServicesPath() + "/redbackServices/",
259                                        UserService.class, Collections.singletonList( new JacksonJaxbJsonProvider() ) );
260
261         // for debuging purpose
262         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 3000000L );
263
264         if ( authzHeader != null )
265         {
266             WebClient.client( service ).header( "Authorization", authzHeader );
267         }
268         return service;
269     }
270
271     protected FakeCreateAdminService getFakeCreateAdminService()
272     {
273         return JAXRSClientFactory.create(
274             "http://localhost:" + port + "/" + getRestServicesPath() + "/fakeCreateAdminService/",
275             FakeCreateAdminService.class );
276     }
277
278 }