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