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