1 package org.apache.archiva.rest.services;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.common.utils.FileUtil;
25 import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
26 import org.apache.archiva.redback.rest.services.AbstractRestServicesTest;
27 import org.apache.archiva.rest.api.services.ArchivaAdministrationService;
28 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
29 import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
30 import org.apache.archiva.rest.api.services.BrowseService;
31 import org.apache.archiva.rest.api.services.CommonServices;
32 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
33 import org.apache.archiva.rest.api.services.MergeRepositoriesService;
34 import org.apache.archiva.rest.api.services.NetworkProxyService;
35 import org.apache.archiva.rest.api.services.PingService;
36 import org.apache.archiva.rest.api.services.ProxyConnectorRuleService;
37 import org.apache.archiva.rest.api.services.ProxyConnectorService;
38 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
39 import org.apache.archiva.rest.api.services.RepositoriesService;
40 import org.apache.archiva.rest.api.services.RepositoryGroupService;
41 import org.apache.archiva.rest.api.services.SearchService;
42 import org.apache.archiva.security.common.ArchivaRoleConstants;
43 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
44 import org.apache.commons.io.FileUtils;
45 import org.apache.commons.lang.StringUtils;
46 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
47 import org.apache.cxf.jaxrs.client.WebClient;
48 import org.junit.After;
49 import org.junit.Assume;
50 import org.junit.Before;
51 import org.junit.BeforeClass;
52 import org.junit.runner.RunWith;
53 import org.slf4j.LoggerFactory;
55 import javax.ws.rs.core.MediaType;
57 import java.io.IOException;
58 import java.util.Collections;
59 import java.util.Date;
60 import org.apache.archiva.rest.api.services.PluginsService;
63 * @author Olivier Lamy
65 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
66 public abstract class AbstractArchivaRestTest
67 extends AbstractRestServicesTest
70 // START SNIPPET: authz-header
71 // guest with an empty password
72 public static String guestAuthzHeader =
73 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
75 // with an other login/password
76 //public String authzHeader =
77 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
79 // END SNIPPET: authz-header
83 public static void chekRepo()
85 Assume.assumeTrue( !System.getProperty( "appserver.base" ).contains( " " ) );
86 LoggerFactory.getLogger( AbstractArchivaRestTest.class.getName() ).
87 error( "Rest services unit test must be run in a folder with no space" );
88 // skygo: was not possible to fix path in this particular module
89 // Skip test if not in proper folder , otherwise test are not fair coz repository
90 // cannot have space in their name.
95 public void startServer()
98 File appServerBase = new File( System.getProperty( "appserver.base" ) );
100 removeAppsubFolder( appServerBase, "jcr" );
101 removeAppsubFolder( appServerBase, "conf" );
102 removeAppsubFolder( appServerBase, "data" );
108 private void removeAppsubFolder( File appServerBase, String folder )
111 File directory = new File( appServerBase, folder );
112 if ( directory.exists() )
114 FileUtils.deleteDirectory( directory );
119 protected String getSpringConfigLocation()
121 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
125 protected String getRestServicesPath()
127 return "restServices";
130 protected RepositoriesService getRepositoriesService()
132 return getRepositoriesService( null );
135 protected <T> T getService( Class<T> clazz, String authzHeader )
137 T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz,
138 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
140 if ( authzHeader != null )
142 WebClient.client( service ).header( "Authorization", authzHeader );
144 WebClient.client(service).header("Referer","http://localhost:"+port);
145 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
146 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
147 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
151 protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader )
153 return getService( ProxyConnectorRuleService.class, authzHeader );
156 protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
158 return getService( MergeRepositoriesService.class, authzHeader );
161 protected RepositoriesService getRepositoriesService( String authzHeader )
163 return getService( RepositoriesService.class, authzHeader );
167 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
169 return getService( ManagedRepositoriesService.class, authzHeader );
172 protected PingService getPingService()
174 return getService( PingService.class, null );
177 protected PluginsService getPluginsService()
179 PluginsService service = getService( PluginsService.class, null );
180 WebClient.client( service ).accept( MediaType.TEXT_PLAIN );
181 WebClient.client( service ).type( MediaType.TEXT_PLAIN );
185 protected RemoteRepositoriesService getRemoteRepositoriesService()
187 return getService( RemoteRepositoriesService.class, null );
192 protected RepositoryGroupService getRepositoryGroupService()
194 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
195 RepositoryGroupService.class,
196 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
199 protected ProxyConnectorService getProxyConnectorService()
201 ProxyConnectorService service =
202 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
203 ProxyConnectorService.class,
204 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
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 );
214 protected NetworkProxyService getNetworkProxyService()
216 NetworkProxyService service =
217 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
218 NetworkProxyService.class,
219 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
221 WebClient.client( service ).header( "Authorization", authorizationHeader );
222 WebClient.client(service).header("Referer","http://localhost:"+port);
223 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
224 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
225 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
229 protected ArchivaAdministrationService getArchivaAdministrationService()
231 ArchivaAdministrationService service =
232 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
233 ArchivaAdministrationService.class,
234 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
236 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
237 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
239 WebClient.client( service ).header( "Authorization", authorizationHeader );
240 WebClient.client(service).header("Referer","http://localhost:"+port);
242 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
246 protected RedbackRuntimeConfigurationService getRedbackRuntimeConfigurationService()
248 RedbackRuntimeConfigurationService service =
249 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
250 RedbackRuntimeConfigurationService.class,
251 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
253 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
254 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
256 WebClient.client( service ).header( "Authorization", authorizationHeader );
257 WebClient.client(service).header("Referer","http://localhost:"+port);
258 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
262 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
264 // START SNIPPET: cxf-browseservice-creation
265 BrowseService service =
266 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
268 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
269 // to add authentification
270 if ( authzHeader != null )
272 WebClient.client( service ).header( "Authorization", authzHeader );
274 // Set the Referer header to your archiva server url
275 WebClient.client(service).header("Referer","http://localhost:"+port);
277 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
280 WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
281 WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
285 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
286 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
289 // END SNIPPET: cxf-browseservice-creation
293 protected SearchService getSearchService( String authzHeader )
295 // START SNIPPET: cxf-searchservice-creation
296 SearchService service =
297 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
299 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
300 // to add authentification
301 if ( authzHeader != null )
303 WebClient.client( service ).header( "Authorization", authzHeader );
305 // Set the Referer header to your archiva server url
306 WebClient.client(service).header("Referer","http://localhost:"+port);
307 // to configure read timeout
308 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
309 // if you want to use json as exchange format xml is supported too
310 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
311 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
313 // END SNIPPET: cxf-searchservice-creation
317 protected CommonServices getCommonServices( String authzHeader )
319 CommonServices service =
320 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
321 CommonServices.class,
322 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
324 if ( authzHeader != null )
326 WebClient.client( service ).header( "Authorization", authzHeader );
328 WebClient.client(service).header("Referer","http://localhost:"+port);
329 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
330 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
331 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
335 protected ManagedRepository getTestManagedRepository()
337 String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
338 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
339 false, 2, 3, true, false, "my nice repo", false );
343 protected String getBaseUrl()
345 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
346 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
349 //-----------------------------------------------------
350 // utilities to create repos for testing
351 //-----------------------------------------------------
353 static final String TARGET_REPO_ID = "test-copy-target";
355 static final String SOURCE_REPO_ID = "test-origin-repo";
357 protected void initSourceTargetRepo()
360 File targetRepo = new File( "target/test-repo-copy" );
361 if ( targetRepo.exists() )
363 FileUtils.deleteDirectory( targetRepo );
365 assertFalse( targetRepo.exists() );
368 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
370 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
371 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
373 ManagedRepository managedRepository = getTestManagedRepository();
374 managedRepository.setId( TARGET_REPO_ID );
375 managedRepository.setLocation( targetRepo.getCanonicalPath() );
376 managedRepository.setCronExpression( "* * * * * ?" );
377 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
378 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
380 File originRepo = new File( "target/test-origin-repo" );
381 if ( originRepo.exists() )
383 FileUtils.deleteDirectory( originRepo );
385 assertFalse( originRepo.exists() );
386 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
388 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
390 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
391 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
394 managedRepository = getTestManagedRepository();
395 managedRepository.setId( SOURCE_REPO_ID );
396 managedRepository.setLocation( originRepo.getCanonicalPath() );
398 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
399 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
401 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
402 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
406 protected void cleanRepos()
410 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
414 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
416 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
418 catch ( Exception e )
420 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
423 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
427 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
429 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
431 catch ( Exception e )
433 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
439 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean stageNeeded )
440 throws ArchivaRestServiceException, IOException, RedbackServiceException
442 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
444 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
447 ManagedRepository managedRepository = new ManagedRepository();
448 managedRepository.setId( testRepoId );
449 managedRepository.setName( "test repo" );
451 File badContent = new File( repoPath, "target" );
452 if ( badContent.exists() )
454 FileUtils.deleteDirectory( badContent );
457 File file = new File( repoPath );
458 if ( !file.isAbsolute() )
460 repoPath = getBasedir() + "/" + repoPath;
463 managedRepository.setLocation( new File( repoPath ).getPath() );
464 managedRepository.setIndexDirectory(
465 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
467 managedRepository.setStageRepoNeeded( stageNeeded );
468 managedRepository.setSnapshots( true );
470 //managedRepository.setScanned( scanned );
472 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
473 service.addManagedRepository( managedRepository );
475 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
476 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
478 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
479 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
482 protected void scanRepo( String testRepoId )
483 throws ArchivaRestServiceException
485 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
488 protected void createAndIndexRepo( String testRepoId, String repoPath )
491 createAndIndexRepo( testRepoId, repoPath, false );
492 scanRepo( testRepoId );
495 protected void createStagedNeededRepo( String testRepoId, String repoPath, boolean scan )
498 createAndIndexRepo( testRepoId, repoPath, true );
501 scanRepo( testRepoId );
504 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
505 repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
508 repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
509 repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
514 protected void deleteTestRepo( String id )
517 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
519 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
523 public String getBasedir()
525 return System.getProperty( "basedir" );
528 protected void waitForScanToComplete( String repoId )
529 throws ArchivaRestServiceException, InterruptedException
531 while ( getRepositoriesService( authorizationHeader ).alreadyScanning( repoId ) ) {
532 // Would be better to cancel, if we had that capacity