]> source.dussan.org Git - archiva.git/blob
d2304c508f74bb7cd254a232595236752674ecbc
[archiva.git] /
1 package org.apache.archiva.rest.services;
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
22 import org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.utils.FileUtil;
24 import org.apache.archiva.rest.api.services.ArchivaAdministrationService;
25 import org.apache.archiva.rest.api.services.CommonServices;
26 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
27 import org.apache.archiva.rest.api.services.NetworkProxyService;
28 import org.apache.archiva.rest.api.services.PingService;
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.security.common.ArchivaRoleConstants;
35 import org.apache.commons.io.FileUtils;
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
38 import org.apache.cxf.jaxrs.client.WebClient;
39 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
40 import org.codehaus.redback.rest.services.AbstractRestServicesTest;
41 import org.junit.Before;
42
43 import javax.ws.rs.core.MediaType;
44 import java.io.File;
45 import java.util.Collections;
46 import java.util.Date;
47
48 /**
49  * @author Olivier Lamy
50  */
51 public abstract class AbstractArchivaRestTest
52     extends AbstractRestServicesTest
53 {
54
55     // START SNIPPET: authz-header
56     // guest with an empty password
57     public String guestAuthzHeader =
58         "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
59
60     // with an other login/password
61     //public String authzHeader =
62     //    "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
63
64     // END SNIPPET: authz-header
65
66
67     @Override
68     @Before
69     public void startServer()
70         throws Exception
71     {
72         File appServerBase = new File( System.getProperty( "appserver.base" ) );
73
74         File jcrDirectory = new File( appServerBase, "jcr" );
75
76         if ( jcrDirectory.exists() )
77         {
78             FileUtils.deleteDirectory( jcrDirectory );
79         }
80
81         super.startServer();
82     }
83
84     @Override
85     protected String getSpringConfigLocation()
86     {
87         return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
88     }
89
90     protected String getRestServicesPath()
91     {
92         return "restServices";
93     }
94
95     protected RepositoriesService getRepositoriesService()
96     {
97         return getRepositoriesService( null );
98     }
99
100
101     protected RepositoriesService getRepositoriesService( String authzHeader )
102     {
103         RepositoriesService service =
104             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
105                                        RepositoriesService.class,
106                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
107
108         if ( authzHeader != null )
109         {
110             WebClient.client( service ).header( "Authorization", authzHeader );
111         }
112         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
113         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
114         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
115         return service;
116
117     }
118
119     protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
120     {
121         ManagedRepositoriesService service =
122             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
123                                        ManagedRepositoriesService.class,
124                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
125
126         if ( authzHeader != null )
127         {
128             WebClient.client( service ).header( "Authorization", authzHeader );
129         }
130         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
131         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
132         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
133         return service;
134
135     }
136
137     protected PingService getPingService()
138     {
139         return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
140                                           PingService.class,
141                                           Collections.singletonList( new JacksonJaxbJsonProvider() ) );
142     }
143
144     protected RemoteRepositoriesService getRemoteRepositoriesService()
145     {
146         return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
147                                           RemoteRepositoriesService.class,
148                                           Collections.singletonList( new JacksonJaxbJsonProvider() ) );
149
150
151     }
152
153     protected RepositoryGroupService getRepositoryGroupService()
154     {
155         return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
156                                           RepositoryGroupService.class,
157                                           Collections.singletonList( new JacksonJaxbJsonProvider() ) );
158     }
159
160     protected ProxyConnectorService getProxyConnectorService()
161     {
162         ProxyConnectorService service =
163             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
164                                        ProxyConnectorService.class,
165                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
166
167         WebClient.client( service ).header( "Authorization", authorizationHeader );
168         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
169         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
170         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
171         return service;
172     }
173
174     protected NetworkProxyService getNetworkProxyService()
175     {
176         NetworkProxyService service =
177             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
178                                        NetworkProxyService.class,
179                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
180
181         WebClient.client( service ).header( "Authorization", authorizationHeader );
182         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
183         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
184         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
185         return service;
186     }
187
188     protected ArchivaAdministrationService getArchivaAdministrationService()
189     {
190         ArchivaAdministrationService service =
191             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
192                                        ArchivaAdministrationService.class,
193                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
194
195         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
196         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
197
198         WebClient.client( service ).header( "Authorization", authorizationHeader );
199         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
200         return service;
201     }
202
203     protected SearchService getSearchService( String authzHeader )
204     {
205         // START SNIPPET: cxf-searchservice-creation
206         SearchService service =
207             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
208                                        SearchService.class,
209                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
210         // to add authentification
211         if ( authzHeader != null )
212         {
213             WebClient.client( service ).header( "Authorization", authzHeader );
214         }
215         // to configure read timeout
216         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
217         // if you want to use json as exchange format xml is supported too
218         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
219         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
220         return service;
221         // END SNIPPET: cxf-searchservice-creation
222
223     }
224
225     protected CommonServices getCommonServices( String authzHeader )
226     {
227         CommonServices service =
228             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
229                                        CommonServices.class,
230                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
231
232         if ( authzHeader != null )
233         {
234             WebClient.client( service ).header( "Authorization", authzHeader );
235         }
236         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
237         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
238         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
239         return service;
240     }
241
242     protected ManagedRepository getTestManagedRepository()
243     {
244         String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
245         return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
246                                       false, 2, 3, true, false );
247
248     }
249
250     protected String getBaseUrl()
251     {
252         String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
253         return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
254     }
255
256     //-----------------------------------------------------
257     // utilities to create repos for testing
258     //-----------------------------------------------------
259
260     static final String TARGET_REPO_ID = "test-copy-target";
261
262     static final String SOURCE_REPO_ID = "test-origin-repo";
263
264     protected void initSourceTargetRepo()
265         throws Exception
266     {
267         File targetRepo = new File( "target/test-repo-copy" );
268         if ( targetRepo.exists() )
269         {
270             FileUtils.deleteDirectory( targetRepo );
271         }
272         assertFalse( targetRepo.exists() );
273         targetRepo.mkdirs();
274
275         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
276         {
277             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
278             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
279         }
280         ManagedRepository managedRepository = getTestManagedRepository();
281         managedRepository.setId( TARGET_REPO_ID );
282         managedRepository.setLocation( targetRepo.getCanonicalPath() );
283         managedRepository.setCronExpression( "* * * * * ?" );
284         getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
285         assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
286
287         File originRepo = new File( "target/test-origin-repo" );
288         if ( originRepo.exists() )
289         {
290             FileUtils.deleteDirectory( originRepo );
291         }
292         assertFalse( originRepo.exists() );
293         FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
294
295         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
296         {
297             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
298             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
299         }
300
301         managedRepository = getTestManagedRepository();
302         managedRepository.setId( SOURCE_REPO_ID );
303         managedRepository.setLocation( originRepo.getCanonicalPath() );
304
305         getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
306         assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
307
308         getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
309         getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
310
311     }
312
313     protected void cleanRepos()
314         throws Exception
315     {
316
317         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
318         {
319             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
320             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
321         }
322         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
323         {
324             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
325             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
326         }
327
328     }
329
330     protected void createAndIndexRepo( String testRepoId, String repoPath )
331         throws Exception
332     {
333         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
334         {
335             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
336         }
337
338         ManagedRepository managedRepository = new ManagedRepository();
339         managedRepository.setId( testRepoId );
340         managedRepository.setName( "test repo" );
341
342         File badContent = new File( repoPath, "target" );
343         if ( badContent.exists() )
344         {
345             FileUtils.deleteDirectory( badContent );
346         }
347
348         managedRepository.setLocation( new File( repoPath ).getPath() );
349         managedRepository.setIndexDirectory(
350             System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
351
352         ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
353         service.addManagedRepository( managedRepository );
354
355         getRoleManagementService( authorizationHeader ).assignTemplatedRole(
356             ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
357
358         getRoleManagementService( authorizationHeader ).assignTemplatedRole(
359             ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
360
361         getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
362
363     }
364
365     protected void deleteTestRepo( String id )
366         throws Exception
367     {
368         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
369         {
370             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
371         }
372
373     }
374 }