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.*;
27 import org.apache.archiva.security.common.ArchivaRoleConstants;
28 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
32 import org.apache.cxf.jaxrs.client.WebClient;
33 import org.junit.Assume;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.runner.RunWith;
37 import org.slf4j.LoggerFactory;
39 import javax.ws.rs.core.MediaType;
40 import java.io.IOException;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.util.Collections;
45 import java.util.Date;
46 import java.util.Locale;
49 * @author Olivier Lamy
51 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
52 public abstract class AbstractArchivaRestTest
53 extends AbstractRestServicesTest
56 // START SNIPPET: authz-header
57 // guest with an empty password
58 public static String guestAuthzHeader =
59 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
61 // with an other login/password
62 //public String authzHeader =
63 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
65 // END SNIPPET: authz-header
69 public static void chekRepo()
71 Assume.assumeTrue( !System.getProperty( "appserver.base" ).contains( " " ) );
72 LoggerFactory.getLogger( AbstractArchivaRestTest.class.getName() ).
73 error( "Rest services unit test must be run in a folder with no space" );
74 // skygo: was not possible to fix path in this particular module
75 // Skip test if not in proper folder , otherwise test are not fair coz repository
76 // cannot have space in their name.
81 public void startServer()
84 Path appServerBase = Paths.get( System.getProperty( "appserver.base" ) );
86 removeAppsubFolder( appServerBase, "jcr" );
87 removeAppsubFolder( appServerBase, "conf" );
88 removeAppsubFolder( appServerBase, "data" );
94 private void removeAppsubFolder( Path appServerBase, String folder )
97 Path directory = appServerBase.resolve( folder );
98 if ( Files.exists(directory) )
100 org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
105 protected String getSpringConfigLocation()
107 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
111 protected String getRestServicesPath()
113 return "restServices";
116 protected RepositoriesService getRepositoriesService()
118 return getRepositoriesService( null );
121 protected <T> T getService( Class<T> clazz, String authzHeader )
123 T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz,
124 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
126 if ( authzHeader != null )
128 WebClient.client( service ).header( "Authorization", authzHeader );
130 WebClient.client(service).header("Referer","http://localhost:"+port);
131 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
132 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
133 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
137 protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader )
139 return getService( ProxyConnectorRuleService.class, authzHeader );
142 protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
144 return getService( MergeRepositoriesService.class, authzHeader );
147 protected RepositoriesService getRepositoriesService( String authzHeader )
149 return getService( RepositoriesService.class, authzHeader );
153 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
155 return getService( ManagedRepositoriesService.class, authzHeader );
158 protected PingService getPingService()
160 return getService( PingService.class, null );
163 protected PluginsService getPluginsService()
165 PluginsService service = getService( PluginsService.class, null );
166 WebClient.client( service ).accept( MediaType.TEXT_PLAIN );
167 WebClient.client( service ).type( MediaType.TEXT_PLAIN );
171 protected RemoteRepositoriesService getRemoteRepositoriesService()
173 return getService( RemoteRepositoriesService.class, null );
178 protected RepositoryGroupService getRepositoryGroupService()
180 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
181 RepositoryGroupService.class,
182 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
185 protected ProxyConnectorService getProxyConnectorService()
187 ProxyConnectorService service =
188 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
189 ProxyConnectorService.class,
190 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
192 WebClient.client( service ).header( "Authorization", authorizationHeader );
193 WebClient.client(service).header("Referer","http://localhost:"+port);
194 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
195 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
196 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
200 protected NetworkProxyService getNetworkProxyService()
202 NetworkProxyService service =
203 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
204 NetworkProxyService.class,
205 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
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 );
215 protected ArchivaAdministrationService getArchivaAdministrationService()
217 ArchivaAdministrationService service =
218 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
219 ArchivaAdministrationService.class,
220 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
222 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
223 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
225 WebClient.client( service ).header( "Authorization", authorizationHeader );
226 WebClient.client(service).header("Referer","http://localhost:"+port);
228 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
232 protected RedbackRuntimeConfigurationService getRedbackRuntimeConfigurationService()
234 RedbackRuntimeConfigurationService service =
235 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
236 RedbackRuntimeConfigurationService.class,
237 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
239 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
240 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
242 WebClient.client( service ).header( "Authorization", authorizationHeader );
243 WebClient.client(service).header("Referer","http://localhost:"+port);
244 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
248 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
250 // START SNIPPET: cxf-browseservice-creation
251 BrowseService service =
252 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
254 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
255 // to add authentification
256 if ( authzHeader != null )
258 WebClient.client( service ).header( "Authorization", authzHeader );
260 // Set the Referer header to your archiva server url
261 WebClient.client(service).header("Referer","http://localhost:"+port);
263 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
266 WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
267 WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
271 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
272 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
275 // END SNIPPET: cxf-browseservice-creation
279 protected SearchService getSearchService( String authzHeader )
281 // START SNIPPET: cxf-searchservice-creation
282 SearchService service =
283 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
285 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
286 // to add authentification
287 if ( authzHeader != null )
289 WebClient.client( service ).header( "Authorization", authzHeader );
291 // Set the Referer header to your archiva server url
292 WebClient.client(service).header("Referer","http://localhost:"+port);
293 // to configure read timeout
294 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
295 // if you want to use json as exchange format xml is supported too
296 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
297 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
299 // END SNIPPET: cxf-searchservice-creation
303 protected CommonServices getCommonServices( String authzHeader )
305 CommonServices service =
306 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
307 CommonServices.class,
308 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
310 if ( authzHeader != null )
312 WebClient.client( service ).header( "Authorization", authzHeader );
314 WebClient.client(service).header("Referer","http://localhost:"+port);
315 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
316 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
317 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
321 protected ManagedRepository getTestManagedRepository()
323 String location = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repo" ).toAbsolutePath().toString();
324 return new ManagedRepository( Locale.getDefault(), "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
325 false, 2, 3, true, false, "my nice repo", false );
329 protected String getBaseUrl()
331 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
332 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
335 //-----------------------------------------------------
336 // utilities to create repos for testing
337 //-----------------------------------------------------
339 static final String TARGET_REPO_ID = "test-copy-target";
341 static final String SOURCE_REPO_ID = "test-origin-repo";
343 protected void initSourceTargetRepo()
346 Path targetRepo = Paths.get( "target/test-repo-copy" );
347 if ( Files.exists(targetRepo) )
349 org.apache.archiva.common.utils.FileUtils.deleteDirectory( targetRepo );
351 assertFalse( Files.exists(targetRepo) );
352 Files.createDirectories( targetRepo );
354 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
356 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
357 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
359 ManagedRepository managedRepository = getTestManagedRepository();
360 managedRepository.setId( TARGET_REPO_ID );
361 managedRepository.setLocation( targetRepo.toAbsolutePath().toString() );
362 managedRepository.setCronExpression( "* * * * * ?" );
363 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
364 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
366 Path originRepo = Paths.get( "target/test-origin-repo" );
367 if ( Files.exists(originRepo) )
369 org.apache.archiva.common.utils.FileUtils.deleteDirectory( originRepo );
371 assertFalse( Files.exists(originRepo) );
372 FileUtils.copyDirectory( Paths.get( "src/test/repo-with-osgi" ).toAbsolutePath().toFile(), originRepo.toAbsolutePath().toFile() );
374 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
376 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
377 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
380 managedRepository = getTestManagedRepository();
381 managedRepository.setId( SOURCE_REPO_ID );
382 managedRepository.setLocation( originRepo.toAbsolutePath().toString() );
384 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
385 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
387 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
388 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
392 protected void cleanRepos()
396 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
400 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
402 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
404 catch ( Exception e )
406 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
409 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
413 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
415 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
417 catch ( Exception e )
419 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
425 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean stageNeeded )
426 throws ArchivaRestServiceException, IOException, RedbackServiceException
428 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
430 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
433 ManagedRepository managedRepository = new ManagedRepository(Locale.getDefault());
434 managedRepository.setId( testRepoId );
435 managedRepository.setName( "test repo" );
437 Path badContent = Paths.get( repoPath, "target" );
438 if ( Files.exists(badContent) )
440 org.apache.archiva.common.utils.FileUtils.deleteDirectory( badContent );
443 Path file = Paths.get( repoPath );
444 if ( !file.isAbsolute() )
446 repoPath = getBasedir() + "/" + repoPath;
449 managedRepository.setLocation( Paths.get( repoPath ).toString() );
450 managedRepository.setIndexDirectory(
451 System.getProperty( "java.io.tmpdir" ) + "/.index-" + Long.toString( new Date().getTime() ) );
453 managedRepository.setStageRepoNeeded( stageNeeded );
454 managedRepository.setSnapshots( true );
456 //managedRepository.setScanned( scanned );
458 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
459 service.addManagedRepository( managedRepository );
461 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
462 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
464 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
465 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
468 protected void scanRepo( String testRepoId )
469 throws ArchivaRestServiceException
471 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
474 protected void createAndIndexRepo( String testRepoId, String repoPath )
477 createAndIndexRepo( testRepoId, repoPath, false );
478 scanRepo( testRepoId );
481 protected void createStagedNeededRepo( String testRepoId, String repoPath, boolean scan )
484 createAndIndexRepo( testRepoId, repoPath, true );
487 scanRepo( testRepoId );
490 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
491 repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
494 repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
495 repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
500 protected void deleteTestRepo( String id )
503 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
505 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
509 public Path getBasedir()
511 return Paths.get(System.getProperty( "basedir" ));
514 protected void waitForScanToComplete( String repoId )
515 throws ArchivaRestServiceException, InterruptedException
517 while ( getRepositoriesService( authorizationHeader ).alreadyScanning( repoId ) ) {
518 // Would be better to cancel, if we had that capacity