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