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