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