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.Assume;
49 import org.junit.Before;
50 import org.junit.BeforeClass;
51 import org.junit.runner.RunWith;
52 import org.slf4j.LoggerFactory;
54 import javax.ws.rs.core.MediaType;
56 import java.io.IOException;
57 import java.util.Collections;
58 import java.util.Date;
59 import org.apache.archiva.rest.api.services.PluginsService;
62 * @author Olivier Lamy
64 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
65 public abstract class AbstractArchivaRestTest
66 extends AbstractRestServicesTest
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() );
74 // with an other login/password
75 //public String authzHeader =
76 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
78 // END SNIPPET: authz-header
81 public static void chekRepo()
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.
93 public void startServer()
96 File appServerBase = new File( System.getProperty( "appserver.base" ) );
98 removeAppsubFolder( appServerBase, "jcr" );
99 removeAppsubFolder( appServerBase, "conf" );
100 removeAppsubFolder( appServerBase, "data" );
105 private void removeAppsubFolder( File appServerBase, String folder )
108 File directory = new File( appServerBase, folder );
109 if ( directory.exists() )
111 FileUtils.deleteDirectory( directory );
116 protected String getSpringConfigLocation()
118 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
122 protected String getRestServicesPath()
124 return "restServices";
127 protected RepositoriesService getRepositoriesService()
129 return getRepositoriesService( null );
132 protected <T> T getService( Class<T> clazz, String authzHeader )
134 T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz,
135 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
137 if ( authzHeader != null )
139 WebClient.client( service ).header( "Authorization", authzHeader );
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 );
148 protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader )
150 return getService( ProxyConnectorRuleService.class, authzHeader );
153 protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
155 return getService( MergeRepositoriesService.class, authzHeader );
158 protected RepositoriesService getRepositoriesService( String authzHeader )
160 return getService( RepositoriesService.class, authzHeader );
164 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
166 return getService( ManagedRepositoriesService.class, authzHeader );
169 protected PingService getPingService()
171 return getService( PingService.class, null );
174 protected PluginsService getPluginsService()
176 PluginsService service = getService( PluginsService.class, null );
177 WebClient.client( service ).accept( MediaType.TEXT_PLAIN );
178 WebClient.client( service ).type( MediaType.TEXT_PLAIN );
182 protected RemoteRepositoriesService getRemoteRepositoriesService()
184 return getService( RemoteRepositoriesService.class, null );
189 protected RepositoryGroupService getRepositoryGroupService()
191 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
192 RepositoryGroupService.class,
193 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
196 protected ProxyConnectorService getProxyConnectorService()
198 ProxyConnectorService service =
199 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
200 ProxyConnectorService.class,
201 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
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 );
211 protected NetworkProxyService getNetworkProxyService()
213 NetworkProxyService service =
214 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
215 NetworkProxyService.class,
216 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
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 );
226 protected ArchivaAdministrationService getArchivaAdministrationService()
228 ArchivaAdministrationService service =
229 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
230 ArchivaAdministrationService.class,
231 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
233 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
234 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
236 WebClient.client( service ).header( "Authorization", authorizationHeader );
237 WebClient.client(service).header("Referer","http://localhost:"+port);
239 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
243 protected RedbackRuntimeConfigurationService getRedbackRuntimeConfigurationService()
245 RedbackRuntimeConfigurationService service =
246 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
247 RedbackRuntimeConfigurationService.class,
248 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
250 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
251 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
253 WebClient.client( service ).header( "Authorization", authorizationHeader );
254 WebClient.client(service).header("Referer","http://localhost:"+port);
255 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
259 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
261 // START SNIPPET: cxf-browseservice-creation
262 BrowseService service =
263 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
265 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
266 // to add authentification
267 if ( authzHeader != null )
269 WebClient.client( service ).header( "Authorization", authzHeader );
271 // Set the Referer header to your archiva server url
272 WebClient.client(service).header("Referer","http://localhost:"+port);
274 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
277 WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
278 WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
282 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
283 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
286 // END SNIPPET: cxf-browseservice-creation
290 protected SearchService getSearchService( String authzHeader )
292 // START SNIPPET: cxf-searchservice-creation
293 SearchService service =
294 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
296 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
297 // to add authentification
298 if ( authzHeader != null )
300 WebClient.client( service ).header( "Authorization", authzHeader );
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 );
310 // END SNIPPET: cxf-searchservice-creation
314 protected CommonServices getCommonServices( String authzHeader )
316 CommonServices service =
317 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
318 CommonServices.class,
319 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
321 if ( authzHeader != null )
323 WebClient.client( service ).header( "Authorization", authzHeader );
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 );
332 protected ManagedRepository getTestManagedRepository()
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 );
340 protected String getBaseUrl()
342 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
343 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
346 //-----------------------------------------------------
347 // utilities to create repos for testing
348 //-----------------------------------------------------
350 static final String TARGET_REPO_ID = "test-copy-target";
352 static final String SOURCE_REPO_ID = "test-origin-repo";
354 protected void initSourceTargetRepo()
357 File targetRepo = new File( "target/test-repo-copy" );
358 if ( targetRepo.exists() )
360 FileUtils.deleteDirectory( targetRepo );
362 assertFalse( targetRepo.exists() );
365 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
367 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
368 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
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 ) );
377 File originRepo = new File( "target/test-origin-repo" );
378 if ( originRepo.exists() )
380 FileUtils.deleteDirectory( originRepo );
382 assertFalse( originRepo.exists() );
383 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
385 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
387 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
388 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
391 managedRepository = getTestManagedRepository();
392 managedRepository.setId( SOURCE_REPO_ID );
393 managedRepository.setLocation( originRepo.getCanonicalPath() );
395 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
396 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
398 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
399 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
403 protected void cleanRepos()
407 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
411 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
413 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
415 catch ( Exception e )
417 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
420 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
424 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
426 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
428 catch ( Exception e )
430 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
436 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean stageNeeded )
437 throws ArchivaRestServiceException, IOException, RedbackServiceException
439 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
441 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
444 ManagedRepository managedRepository = new ManagedRepository();
445 managedRepository.setId( testRepoId );
446 managedRepository.setName( "test repo" );
448 File badContent = new File( repoPath, "target" );
449 if ( badContent.exists() )
451 FileUtils.deleteDirectory( badContent );
454 File file = new File( repoPath );
455 if ( !file.isAbsolute() )
457 repoPath = getBasedir() + "/" + repoPath;
460 managedRepository.setLocation( new File( repoPath ).getPath() );
461 managedRepository.setIndexDirectory(
462 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
464 managedRepository.setStageRepoNeeded( stageNeeded );
465 managedRepository.setSnapshots( true );
467 //managedRepository.setScanned( scanned );
469 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
470 service.addManagedRepository( managedRepository );
472 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
473 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
475 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
476 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
479 protected void scanRepo( String testRepoId )
480 throws ArchivaRestServiceException
482 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
485 protected void createAndIndexRepo( String testRepoId, String repoPath )
488 createAndIndexRepo( testRepoId, repoPath, false );
489 scanRepo( testRepoId );
492 protected void createStagedNeededRepo( String testRepoId, String repoPath, boolean scan )
495 createAndIndexRepo( testRepoId, repoPath, true );
498 scanRepo( testRepoId );
501 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
502 repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
505 repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
506 repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
511 protected void deleteTestRepo( String id )
514 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
516 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
520 public String getBasedir()
522 return System.getProperty( "basedir" );
525 protected void waitForScanToComplete( String repoId )
526 throws ArchivaRestServiceException, InterruptedException
528 while ( getRepositoriesService( authorizationHeader ).alreadyScanning( repoId ) ) {
529 // Would be better to cancel, if we had that capacity