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.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.RedbackRuntimeConfigurationService;
29 import org.apache.archiva.rest.api.services.BrowseService;
30 import org.apache.archiva.rest.api.services.CommonServices;
31 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
32 import org.apache.archiva.rest.api.services.MergeRepositoriesService;
33 import org.apache.archiva.rest.api.services.NetworkProxyService;
34 import org.apache.archiva.rest.api.services.PingService;
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.RemoteRepositoriesService;
38 import org.apache.archiva.rest.api.services.RepositoriesService;
39 import org.apache.archiva.rest.api.services.RepositoryGroupService;
40 import org.apache.archiva.rest.api.services.SearchService;
41 import org.apache.archiva.security.common.ArchivaRoleConstants;
42 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
43 import org.apache.commons.io.FileUtils;
44 import org.apache.commons.lang.StringUtils;
45 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
46 import org.apache.cxf.jaxrs.client.WebClient;
47 import org.junit.Assume;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.runner.RunWith;
51 import org.slf4j.LoggerFactory;
53 import javax.ws.rs.core.MediaType;
55 import java.io.IOException;
56 import java.util.Collections;
57 import java.util.Date;
58 import org.apache.archiva.rest.api.services.PluginsService;
61 * @author Olivier Lamy
63 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
64 public abstract class AbstractArchivaRestTest
65 extends AbstractRestServicesTest
68 // START SNIPPET: authz-header
69 // guest with an empty password
70 public static String guestAuthzHeader =
71 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
73 // with an other login/password
74 //public String authzHeader =
75 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
77 // 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" );
106 private void removeAppsubFolder( File appServerBase, String folder )
109 File directory = new File( appServerBase, folder );
110 if ( directory.exists() )
112 FileUtils.deleteDirectory( directory );
117 protected String getSpringConfigLocation()
119 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
123 protected String getRestServicesPath()
125 return "restServices";
128 protected RepositoriesService getRepositoriesService()
130 return getRepositoriesService( null );
133 protected <T> T getService( Class<T> clazz, String authzHeader )
135 T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz,
136 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
138 if ( authzHeader != null )
140 WebClient.client( service ).header( "Authorization", authzHeader );
142 WebClient.client(service).header("Referer","http://localhost:"+port);
143 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
144 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
145 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
149 protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader )
151 return getService( ProxyConnectorRuleService.class, authzHeader );
154 protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
156 return getService( MergeRepositoriesService.class, authzHeader );
159 protected RepositoriesService getRepositoriesService( String authzHeader )
161 return getService( RepositoriesService.class, authzHeader );
165 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
167 return getService( ManagedRepositoriesService.class, authzHeader );
170 protected PingService getPingService()
172 return getService( PingService.class, null );
175 protected PluginsService getPluginsService()
177 PluginsService service = getService( PluginsService.class, null );
178 WebClient.client( service ).accept( MediaType.TEXT_PLAIN );
179 WebClient.client( service ).type( MediaType.TEXT_PLAIN );
183 protected RemoteRepositoriesService getRemoteRepositoriesService()
185 return getService( RemoteRepositoriesService.class, null );
190 protected RepositoryGroupService getRepositoryGroupService()
192 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
193 RepositoryGroupService.class,
194 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
197 protected ProxyConnectorService getProxyConnectorService()
199 ProxyConnectorService service =
200 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
201 ProxyConnectorService.class,
202 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
204 WebClient.client( service ).header( "Authorization", authorizationHeader );
205 WebClient.client(service).header("Referer","http://localhost:"+port);
206 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
207 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
208 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
212 protected NetworkProxyService getNetworkProxyService()
214 NetworkProxyService service =
215 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
216 NetworkProxyService.class,
217 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
219 WebClient.client( service ).header( "Authorization", authorizationHeader );
220 WebClient.client(service).header("Referer","http://localhost:"+port);
221 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
222 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
223 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
227 protected ArchivaAdministrationService getArchivaAdministrationService()
229 ArchivaAdministrationService service =
230 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
231 ArchivaAdministrationService.class,
232 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
234 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
235 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
237 WebClient.client( service ).header( "Authorization", authorizationHeader );
238 WebClient.client(service).header("Referer","http://localhost:"+port);
240 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
244 protected RedbackRuntimeConfigurationService getRedbackRuntimeConfigurationService()
246 RedbackRuntimeConfigurationService service =
247 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
248 RedbackRuntimeConfigurationService.class,
249 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
251 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
252 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
254 WebClient.client( service ).header( "Authorization", authorizationHeader );
255 WebClient.client(service).header("Referer","http://localhost:"+port);
256 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
260 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
262 // START SNIPPET: cxf-browseservice-creation
263 BrowseService service =
264 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
266 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
267 // to add authentification
268 if ( authzHeader != null )
270 WebClient.client( service ).header( "Authorization", authzHeader );
272 // Set the Referer header to your archiva server url
273 WebClient.client(service).header("Referer","http://localhost:"+port);
275 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
278 WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
279 WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
283 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
284 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
287 // END SNIPPET: cxf-browseservice-creation
291 protected SearchService getSearchService( String authzHeader )
293 // START SNIPPET: cxf-searchservice-creation
294 SearchService service =
295 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
297 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
298 // to add authentification
299 if ( authzHeader != null )
301 WebClient.client( service ).header( "Authorization", authzHeader );
303 // Set the Referer header to your archiva server url
304 WebClient.client(service).header("Referer","http://localhost:"+port);
305 // to configure read timeout
306 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
307 // if you want to use json as exchange format xml is supported too
308 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
309 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
311 // END SNIPPET: cxf-searchservice-creation
315 protected CommonServices getCommonServices( String authzHeader )
317 CommonServices service =
318 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
319 CommonServices.class,
320 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
322 if ( authzHeader != null )
324 WebClient.client( service ).header( "Authorization", authzHeader );
326 WebClient.client(service).header("Referer","http://localhost:"+port);
327 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
328 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
329 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
333 protected ManagedRepository getTestManagedRepository()
335 String location = new File( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repo" ).getAbsolutePath();
336 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
337 false, 2, 3, true, false, "my nice repo", false );
341 protected String getBaseUrl()
343 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
344 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
347 //-----------------------------------------------------
348 // utilities to create repos for testing
349 //-----------------------------------------------------
351 static final String TARGET_REPO_ID = "test-copy-target";
353 static final String SOURCE_REPO_ID = "test-origin-repo";
355 protected void initSourceTargetRepo()
358 File targetRepo = new File( "target/test-repo-copy" );
359 if ( targetRepo.exists() )
361 FileUtils.deleteDirectory( targetRepo );
363 assertFalse( targetRepo.exists() );
366 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
368 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
369 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
371 ManagedRepository managedRepository = getTestManagedRepository();
372 managedRepository.setId( TARGET_REPO_ID );
373 managedRepository.setLocation( targetRepo.getCanonicalPath() );
374 managedRepository.setCronExpression( "* * * * * ?" );
375 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
376 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
378 File originRepo = new File( "target/test-origin-repo" );
379 if ( originRepo.exists() )
381 FileUtils.deleteDirectory( originRepo );
383 assertFalse( originRepo.exists() );
384 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
386 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
388 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
389 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
392 managedRepository = getTestManagedRepository();
393 managedRepository.setId( SOURCE_REPO_ID );
394 managedRepository.setLocation( originRepo.getCanonicalPath() );
396 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
397 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
399 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
400 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
404 protected void cleanRepos()
408 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
412 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
414 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
416 catch ( Exception e )
418 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
421 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
425 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
427 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
429 catch ( Exception e )
431 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
437 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean stageNeeded )
438 throws ArchivaRestServiceException, IOException, RedbackServiceException
440 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
442 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
445 ManagedRepository managedRepository = new ManagedRepository();
446 managedRepository.setId( testRepoId );
447 managedRepository.setName( "test repo" );
449 File badContent = new File( repoPath, "target" );
450 if ( badContent.exists() )
452 FileUtils.deleteDirectory( badContent );
455 File file = new File( repoPath );
456 if ( !file.isAbsolute() )
458 repoPath = getBasedir() + "/" + repoPath;
461 managedRepository.setLocation( new File( repoPath ).getPath() );
462 managedRepository.setIndexDirectory(
463 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
465 managedRepository.setStageRepoNeeded( stageNeeded );
466 managedRepository.setSnapshots( true );
468 //managedRepository.setScanned( scanned );
470 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
471 service.addManagedRepository( managedRepository );
473 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
474 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
476 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
477 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
480 protected void scanRepo( String testRepoId )
481 throws ArchivaRestServiceException
483 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
486 protected void createAndIndexRepo( String testRepoId, String repoPath )
489 createAndIndexRepo( testRepoId, repoPath, false );
490 scanRepo( testRepoId );
493 protected void createStagedNeededRepo( String testRepoId, String repoPath, boolean scan )
496 createAndIndexRepo( testRepoId, repoPath, true );
499 scanRepo( testRepoId );
502 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
503 repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
506 repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
507 repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
512 protected void deleteTestRepo( String id )
515 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
517 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
521 public String getBasedir()
523 return System.getProperty( "basedir" );
526 protected void waitForScanToComplete( String repoId )
527 throws ArchivaRestServiceException, InterruptedException
529 while ( getRepositoriesService( authorizationHeader ).alreadyScanning( repoId ) ) {
530 // Would be better to cancel, if we had that capacity