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;
48 * @author Olivier Lamy
50 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
51 public abstract class AbstractArchivaRestTest
52 extends AbstractRestServicesTest
55 // START SNIPPET: authz-header
56 // guest with an empty password
57 public static String guestAuthzHeader =
58 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
60 // with an other login/password
61 //public String authzHeader =
62 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
64 // END SNIPPET: authz-header
68 public static void chekRepo()
70 Assume.assumeTrue( !System.getProperty( "appserver.base" ).contains( " " ) );
71 LoggerFactory.getLogger( AbstractArchivaRestTest.class.getName() ).
72 error( "Rest services unit test must be run in a folder with no space" );
73 // skygo: was not possible to fix path in this particular module
74 // Skip test if not in proper folder , otherwise test are not fair coz repository
75 // cannot have space in their name.
80 public void startServer()
83 Path appServerBase = Paths.get( System.getProperty( "appserver.base" ) );
85 removeAppsubFolder( appServerBase, "jcr" );
86 removeAppsubFolder( appServerBase, "conf" );
87 removeAppsubFolder( appServerBase, "data" );
93 private void removeAppsubFolder( Path appServerBase, String folder )
96 Path directory = appServerBase.resolve( folder );
97 if ( Files.exists(directory) )
99 org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
104 protected String getSpringConfigLocation()
106 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
110 protected String getRestServicesPath()
112 return "restServices";
115 protected RepositoriesService getRepositoriesService()
117 return getRepositoriesService( null );
120 protected <T> T getService( Class<T> clazz, String authzHeader )
122 T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz,
123 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
125 if ( authzHeader != null )
127 WebClient.client( service ).header( "Authorization", authzHeader );
129 WebClient.client(service).header("Referer","http://localhost:"+port);
130 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
131 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
132 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
136 protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader )
138 return getService( ProxyConnectorRuleService.class, authzHeader );
141 protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
143 return getService( MergeRepositoriesService.class, authzHeader );
146 protected RepositoriesService getRepositoriesService( String authzHeader )
148 return getService( RepositoriesService.class, authzHeader );
152 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
154 return getService( ManagedRepositoriesService.class, authzHeader );
157 protected PingService getPingService()
159 return getService( PingService.class, null );
162 protected PluginsService getPluginsService()
164 PluginsService service = getService( PluginsService.class, null );
165 WebClient.client( service ).accept( MediaType.TEXT_PLAIN );
166 WebClient.client( service ).type( MediaType.TEXT_PLAIN );
170 protected RemoteRepositoriesService getRemoteRepositoriesService()
172 return getService( RemoteRepositoriesService.class, null );
177 protected RepositoryGroupService getRepositoryGroupService()
179 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
180 RepositoryGroupService.class,
181 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
184 protected ProxyConnectorService getProxyConnectorService()
186 ProxyConnectorService service =
187 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
188 ProxyConnectorService.class,
189 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
191 WebClient.client( service ).header( "Authorization", authorizationHeader );
192 WebClient.client(service).header("Referer","http://localhost:"+port);
193 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
194 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
195 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
199 protected NetworkProxyService getNetworkProxyService()
201 NetworkProxyService service =
202 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
203 NetworkProxyService.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 ArchivaAdministrationService getArchivaAdministrationService()
216 ArchivaAdministrationService service =
217 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
218 ArchivaAdministrationService.class,
219 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
221 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
222 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
224 WebClient.client( service ).header( "Authorization", authorizationHeader );
225 WebClient.client(service).header("Referer","http://localhost:"+port);
227 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
231 protected RedbackRuntimeConfigurationService getRedbackRuntimeConfigurationService()
233 RedbackRuntimeConfigurationService service =
234 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
235 RedbackRuntimeConfigurationService.class,
236 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
238 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
239 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
241 WebClient.client( service ).header( "Authorization", authorizationHeader );
242 WebClient.client(service).header("Referer","http://localhost:"+port);
243 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
247 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
249 // START SNIPPET: cxf-browseservice-creation
250 BrowseService service =
251 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
253 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
254 // to add authentification
255 if ( authzHeader != null )
257 WebClient.client( service ).header( "Authorization", authzHeader );
259 // Set the Referer header to your archiva server url
260 WebClient.client(service).header("Referer","http://localhost:"+port);
262 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
265 WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
266 WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
270 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
271 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
274 // END SNIPPET: cxf-browseservice-creation
278 protected SearchService getSearchService( String authzHeader )
280 // START SNIPPET: cxf-searchservice-creation
281 SearchService service =
282 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
284 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
285 // to add authentification
286 if ( authzHeader != null )
288 WebClient.client( service ).header( "Authorization", authzHeader );
290 // Set the Referer header to your archiva server url
291 WebClient.client(service).header("Referer","http://localhost:"+port);
292 // to configure read timeout
293 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
294 // if you want to use json as exchange format xml is supported too
295 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
296 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
298 // END SNIPPET: cxf-searchservice-creation
302 protected CommonServices getCommonServices( String authzHeader )
304 CommonServices service =
305 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
306 CommonServices.class,
307 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
309 if ( authzHeader != null )
311 WebClient.client( service ).header( "Authorization", authzHeader );
313 WebClient.client(service).header("Referer","http://localhost:"+port);
314 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
315 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
316 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
320 protected ManagedRepository getTestManagedRepository()
322 String location = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repo" ).toAbsolutePath().toString();
323 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
324 false, 2, 3, true, false, "my nice repo", false );
328 protected String getBaseUrl()
330 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
331 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
334 //-----------------------------------------------------
335 // utilities to create repos for testing
336 //-----------------------------------------------------
338 static final String TARGET_REPO_ID = "test-copy-target";
340 static final String SOURCE_REPO_ID = "test-origin-repo";
342 protected void initSourceTargetRepo()
345 Path targetRepo = Paths.get( "target/test-repo-copy" );
346 if ( Files.exists(targetRepo) )
348 org.apache.archiva.common.utils.FileUtils.deleteDirectory( targetRepo );
350 assertFalse( Files.exists(targetRepo) );
351 Files.createDirectories( targetRepo );
353 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
355 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
356 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
358 ManagedRepository managedRepository = getTestManagedRepository();
359 managedRepository.setId( TARGET_REPO_ID );
360 managedRepository.setLocation( targetRepo.toAbsolutePath().toString() );
361 managedRepository.setCronExpression( "* * * * * ?" );
362 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
363 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
365 Path originRepo = Paths.get( "target/test-origin-repo" );
366 if ( Files.exists(originRepo) )
368 org.apache.archiva.common.utils.FileUtils.deleteDirectory( originRepo );
370 assertFalse( Files.exists(originRepo) );
371 FileUtils.copyDirectory( Paths.get( "src/test/repo-with-osgi" ).toAbsolutePath().toFile(), originRepo.toAbsolutePath().toFile() );
373 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
375 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
376 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
379 managedRepository = getTestManagedRepository();
380 managedRepository.setId( SOURCE_REPO_ID );
381 managedRepository.setLocation( originRepo.toAbsolutePath().toString() );
383 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
384 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
386 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
387 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
391 protected void cleanRepos()
395 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
399 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
401 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
403 catch ( Exception e )
405 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
408 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
412 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
414 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
416 catch ( Exception e )
418 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
424 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean stageNeeded )
425 throws ArchivaRestServiceException, IOException, RedbackServiceException
427 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
429 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
432 ManagedRepository managedRepository = new ManagedRepository();
433 managedRepository.setId( testRepoId );
434 managedRepository.setName( "test repo" );
436 Path badContent = Paths.get( repoPath, "target" );
437 if ( Files.exists(badContent) )
439 org.apache.archiva.common.utils.FileUtils.deleteDirectory( badContent );
442 Path file = Paths.get( repoPath );
443 if ( !file.isAbsolute() )
445 repoPath = getBasedir() + "/" + repoPath;
448 managedRepository.setLocation( Paths.get( repoPath ).toString() );
449 managedRepository.setIndexDirectory(
450 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
452 managedRepository.setStageRepoNeeded( stageNeeded );
453 managedRepository.setSnapshots( true );
455 //managedRepository.setScanned( scanned );
457 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
458 service.addManagedRepository( managedRepository );
460 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
461 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
463 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
464 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
467 protected void scanRepo( String testRepoId )
468 throws ArchivaRestServiceException
470 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
473 protected void createAndIndexRepo( String testRepoId, String repoPath )
476 createAndIndexRepo( testRepoId, repoPath, false );
477 scanRepo( testRepoId );
480 protected void createStagedNeededRepo( String testRepoId, String repoPath, boolean scan )
483 createAndIndexRepo( testRepoId, repoPath, true );
486 scanRepo( testRepoId );
489 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
490 repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
493 repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
494 repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
499 protected void deleteTestRepo( String id )
502 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
504 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
508 public Path getBasedir()
510 return Paths.get(System.getProperty( "basedir" ));
513 protected void waitForScanToComplete( String repoId )
514 throws ArchivaRestServiceException, InterruptedException
516 while ( getRepositoriesService( authorizationHeader ).alreadyScanning( repoId ) ) {
517 // Would be better to cancel, if we had that capacity