]> source.dussan.org Git - archiva.git/blob
310448015b604b940a855217f87769d3743fc28c
[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 com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.common.utils.FileUtil;
25 import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
26 import org.apache.archiva.redback.rest.services.AbstractRestServicesTest;
27 import org.apache.archiva.rest.api.services.ArchivaAdministrationService;
28 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
29 import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
30 import org.apache.archiva.rest.api.services.BrowseService;
31 import org.apache.archiva.rest.api.services.CommonServices;
32 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
33 import org.apache.archiva.rest.api.services.MergeRepositoriesService;
34 import org.apache.archiva.rest.api.services.NetworkProxyService;
35 import org.apache.archiva.rest.api.services.PingService;
36 import org.apache.archiva.rest.api.services.ProxyConnectorRuleService;
37 import org.apache.archiva.rest.api.services.ProxyConnectorService;
38 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
39 import org.apache.archiva.rest.api.services.RepositoriesService;
40 import org.apache.archiva.rest.api.services.RepositoryGroupService;
41 import org.apache.archiva.rest.api.services.SearchService;
42 import org.apache.archiva.security.common.ArchivaRoleConstants;
43 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
44 import org.apache.commons.io.FileUtils;
45 import org.apache.commons.lang.StringUtils;
46 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
47 import org.apache.cxf.jaxrs.client.WebClient;
48 import org.junit.Assume;
49 import org.junit.Before;
50 import org.junit.BeforeClass;
51 import org.junit.runner.RunWith;
52 import org.slf4j.LoggerFactory;
53
54 import javax.ws.rs.core.MediaType;
55 import java.io.File;
56 import java.io.IOException;
57 import java.util.Collections;
58 import java.util.Date;
59 import org.apache.archiva.rest.api.services.PluginsService;
60
61 /**
62  * @author Olivier Lamy
63  */
64 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
65 public abstract class AbstractArchivaRestTest
66     extends AbstractRestServicesTest
67 {
68
69     // START SNIPPET: authz-header
70     // guest with an empty password
71     public static String guestAuthzHeader =
72         "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
73
74     // with an other login/password
75     //public String authzHeader =
76     //    "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
77
78     // END SNIPPET: authz-header
79
80     @BeforeClass
81     public static void chekRepo()
82     {
83         Assume.assumeTrue( !System.getProperty( "appserver.base" ).contains( " " ) );
84         LoggerFactory.getLogger( AbstractArchivaRestTest.class.getName() ).
85             error( "Rest services unit test must be run in a folder with no space" );
86         // skygo: was not possible to fix path in this particular module
87         // Skip test if not in proper folder , otherwise test are not fair coz repository
88         // cannot have space in their name.
89     }
90
91     @Override
92     @Before
93     public void startServer()
94         throws Exception
95     {
96         File appServerBase = new File( System.getProperty( "appserver.base" ) );
97
98         removeAppsubFolder( appServerBase, "jcr" );
99         removeAppsubFolder( appServerBase, "conf" );
100         removeAppsubFolder( appServerBase, "data" );
101
102         super.startServer();
103     }
104
105     private void removeAppsubFolder( File appServerBase, String folder )
106         throws Exception
107     {
108         File directory = new File( appServerBase, folder );
109         if ( directory.exists() )
110         {
111             FileUtils.deleteDirectory( directory );
112         }
113     }
114
115     @Override
116     protected String getSpringConfigLocation()
117     {
118         return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
119     }
120
121     @Override
122     protected String getRestServicesPath()
123     {
124         return "restServices";
125     }
126
127     protected RepositoriesService getRepositoriesService()
128     {
129         return getRepositoriesService( null );
130     }
131
132     protected <T> T getService( Class<T> clazz, String authzHeader )
133     {
134         T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz,
135                                                Collections.singletonList( new JacksonJaxbJsonProvider() ) );
136
137         if ( authzHeader != null )
138         {
139             WebClient.client( service ).header( "Authorization", authzHeader );
140         }
141         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
142         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
143         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
144         return service;
145     }
146
147     protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader )
148     {
149         return getService( ProxyConnectorRuleService.class, authzHeader );
150     }
151
152     protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
153     {
154         return getService( MergeRepositoriesService.class, authzHeader );
155     }
156
157     protected RepositoriesService getRepositoriesService( String authzHeader )
158     {
159         return getService( RepositoriesService.class, authzHeader );
160
161     }
162
163     protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
164     {
165         return getService( ManagedRepositoriesService.class, authzHeader );
166     }
167
168     protected PingService getPingService()
169     {
170         return getService( PingService.class, null );
171     }
172     
173     protected PluginsService getPluginsService()
174     {
175         PluginsService service = getService( PluginsService.class, null );
176         WebClient.client( service ).accept( MediaType.TEXT_PLAIN );
177         WebClient.client( service ).type( MediaType.TEXT_PLAIN );
178         return service;
179     }
180
181     protected RemoteRepositoriesService getRemoteRepositoriesService()
182     {
183         return getService( RemoteRepositoriesService.class, null );
184
185
186     }
187
188     protected RepositoryGroupService getRepositoryGroupService()
189     {
190         return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
191                                           RepositoryGroupService.class,
192                                           Collections.singletonList( new JacksonJaxbJsonProvider() ) );
193     }
194
195     protected ProxyConnectorService getProxyConnectorService()
196     {
197         ProxyConnectorService service =
198             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
199                                        ProxyConnectorService.class,
200                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
201
202         WebClient.client( service ).header( "Authorization", authorizationHeader );
203         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
204         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
205         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
206         return service;
207     }
208
209     protected NetworkProxyService getNetworkProxyService()
210     {
211         NetworkProxyService service =
212             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
213                                        NetworkProxyService.class,
214                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
215
216         WebClient.client( service ).header( "Authorization", authorizationHeader );
217         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
218         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
219         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
220         return service;
221     }
222
223     protected ArchivaAdministrationService getArchivaAdministrationService()
224     {
225         ArchivaAdministrationService service =
226             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
227                                        ArchivaAdministrationService.class,
228                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
229
230         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
231         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
232
233         WebClient.client( service ).header( "Authorization", authorizationHeader );
234         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
235         return service;
236     }
237
238     protected RedbackRuntimeConfigurationService getRedbackRuntimeConfigurationService()
239     {
240         RedbackRuntimeConfigurationService service =
241             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
242                                        RedbackRuntimeConfigurationService.class,
243                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
244
245         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
246         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
247
248         WebClient.client( service ).header( "Authorization", authorizationHeader );
249         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
250         return service;
251     }
252
253     protected BrowseService getBrowseService( String authzHeader, boolean useXml )
254     {
255         BrowseService service =
256             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
257                                        BrowseService.class,
258                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
259         // to add authentification
260         if ( authzHeader != null )
261         {
262             WebClient.client( service ).header( "Authorization", authzHeader );
263         }
264
265         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
266         if ( useXml )
267         {
268             WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
269             WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
270         }
271         else
272         {
273             WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
274             WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
275         }
276         return service;
277
278     }
279
280     protected SearchService getSearchService( String authzHeader )
281     {
282         // START SNIPPET: cxf-searchservice-creation        
283         SearchService service =
284             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
285                                        SearchService.class,
286                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
287         // to add authentification
288         if ( authzHeader != null )
289         {
290             WebClient.client( service ).header( "Authorization", authzHeader );
291         }
292         // to configure read timeout
293         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
294         // if you want to use json as exchange format xml is supported too
295         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
296         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
297         return service;
298         // END SNIPPET: cxf-searchservice-creation
299
300     }
301
302     protected CommonServices getCommonServices( String authzHeader )
303     {
304         CommonServices service =
305             JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
306                                        CommonServices.class,
307                                        Collections.singletonList( new JacksonJaxbJsonProvider() ) );
308
309         if ( authzHeader != null )
310         {
311             WebClient.client( service ).header( "Authorization", authzHeader );
312         }
313         WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
314         WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
315         WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
316         return service;
317     }
318
319     protected ManagedRepository getTestManagedRepository()
320     {
321         String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
322         return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
323                                       false, 2, 3, true, false, "my nice repo", false );
324
325     }
326
327     protected String getBaseUrl()
328     {
329         String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
330         return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
331     }
332
333     //-----------------------------------------------------
334     // utilities to create repos for testing
335     //-----------------------------------------------------
336
337     static final String TARGET_REPO_ID = "test-copy-target";
338
339     static final String SOURCE_REPO_ID = "test-origin-repo";
340
341     protected void initSourceTargetRepo()
342         throws Exception
343     {
344         File targetRepo = new File( "target/test-repo-copy" );
345         if ( targetRepo.exists() )
346         {
347             FileUtils.deleteDirectory( targetRepo );
348         }
349         assertFalse( targetRepo.exists() );
350         targetRepo.mkdirs();
351
352         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
353         {
354             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
355             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
356         }
357         ManagedRepository managedRepository = getTestManagedRepository();
358         managedRepository.setId( TARGET_REPO_ID );
359         managedRepository.setLocation( targetRepo.getCanonicalPath() );
360         managedRepository.setCronExpression( "* * * * * ?" );
361         getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
362         assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
363
364         File originRepo = new File( "target/test-origin-repo" );
365         if ( originRepo.exists() )
366         {
367             FileUtils.deleteDirectory( originRepo );
368         }
369         assertFalse( originRepo.exists() );
370         FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
371
372         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
373         {
374             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
375             assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
376         }
377
378         managedRepository = getTestManagedRepository();
379         managedRepository.setId( SOURCE_REPO_ID );
380         managedRepository.setLocation( originRepo.getCanonicalPath() );
381
382         getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
383         assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
384
385         getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
386         getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
387
388     }
389
390     protected void cleanRepos()
391         throws Exception
392     {
393
394         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
395         {
396             try
397             {
398                 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
399                 assertNull(
400                     getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
401             }
402             catch ( Exception e )
403             {
404                 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
405             }
406         }
407         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
408         {
409             try
410             {
411                 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
412                 assertNull(
413                     getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
414             }
415             catch ( Exception e )
416             {
417                 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
418             }
419         }
420
421     }
422
423     protected void createAndIndexRepo( String testRepoId, String repoPath, boolean stageNeeded )
424         throws ArchivaRestServiceException, IOException, RedbackServiceException
425     {
426         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
427         {
428             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
429         }
430
431         ManagedRepository managedRepository = new ManagedRepository();
432         managedRepository.setId( testRepoId );
433         managedRepository.setName( "test repo" );
434
435         File badContent = new File( repoPath, "target" );
436         if ( badContent.exists() )
437         {
438             FileUtils.deleteDirectory( badContent );
439         }
440
441         File file = new File( repoPath );
442         if ( !file.isAbsolute() )
443         {
444             repoPath = getBasedir() + "/" + repoPath;
445         }
446
447         managedRepository.setLocation( new File( repoPath ).getPath() );
448         managedRepository.setIndexDirectory(
449             System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
450
451         managedRepository.setStageRepoNeeded( stageNeeded );
452         managedRepository.setSnapshots( true );
453
454         //managedRepository.setScanned( scanned );
455
456         ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
457         service.addManagedRepository( managedRepository );
458
459         getRoleManagementService( authorizationHeader ).assignTemplatedRole(
460             ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
461
462         getRoleManagementService( authorizationHeader ).assignTemplatedRole(
463             ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
464     }
465
466     protected void scanRepo( String testRepoId )
467         throws ArchivaRestServiceException
468     {
469         getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
470     }
471
472     protected void createAndIndexRepo( String testRepoId, String repoPath )
473         throws Exception
474     {
475         createAndIndexRepo( testRepoId, repoPath, false );
476         scanRepo( testRepoId );
477     }
478
479     protected void createStagedNeededRepo( String testRepoId, String repoPath, boolean scan )
480         throws Exception
481     {
482         createAndIndexRepo( testRepoId, repoPath, true );
483         if ( scan )
484         {
485             scanRepo( testRepoId );
486         }
487
488         RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
489         repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
490         if ( scan )
491         {
492             repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
493             repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
494         }
495     }
496
497
498     protected void deleteTestRepo( String id )
499         throws Exception
500     {
501         if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
502         {
503             getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
504         }
505     }
506
507     public String getBasedir()
508     {
509         return System.getProperty( "basedir" );
510     }
511
512     protected void waitForScanToComplete( String repoId )
513         throws ArchivaRestServiceException, InterruptedException
514     {
515         while ( getRepositoriesService( authorizationHeader ).alreadyScanning( repoId ) ) {
516             // Would be better to cancel, if we had that capacity
517             Thread.sleep( 100 );
518         }
519     }
520 }