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.BrowseService;
29 import org.apache.archiva.rest.api.services.CommonServices;
30 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
31 import org.apache.archiva.rest.api.services.MergeRepositoriesService;
32 import org.apache.archiva.rest.api.services.NetworkProxyService;
33 import org.apache.archiva.rest.api.services.PingService;
34 import org.apache.archiva.rest.api.services.PluginsService;
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.RedbackRuntimeConfigurationService;
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;
55 import java.io.IOException;
56 import java.nio.file.Files;
57 import java.nio.file.Path;
58 import java.nio.file.Paths;
59 import java.util.Collections;
60 import java.util.Date;
61 import java.util.Locale;
64 * @author Olivier Lamy
66 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
67 public abstract class AbstractArchivaRestTest
68 extends AbstractRestServicesTest
71 // START SNIPPET: authz-header
72 // guest with an empty password
73 public static String guestAuthzHeader =
74 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
76 // with an other login/password
77 //public String authzHeader =
78 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
80 // END SNIPPET: authz-header
84 public static void chekRepo()
86 Assume.assumeTrue( !System.getProperty( "appserver.base" ).contains( " " ) );
87 LoggerFactory.getLogger( AbstractArchivaRestTest.class.getName() ).
88 error( "Rest services unit test must be run in a folder with no space" );
89 // skygo: was not possible to fix path in this particular module
90 // Skip test if not in proper folder , otherwise test are not fair coz repository
91 // cannot have space in their name.
96 public void startServer()
99 Path appServerBase = Paths.get( System.getProperty( "appserver.base" ) );
101 removeAppsubFolder( appServerBase, "jcr" );
102 removeAppsubFolder( appServerBase, "conf" );
103 removeAppsubFolder( appServerBase, "data" );
109 private void removeAppsubFolder( Path appServerBase, String folder )
112 Path directory = appServerBase.resolve( folder );
113 if ( Files.exists(directory) )
115 org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
120 protected String getSpringConfigLocation()
122 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
126 protected String getRestServicesPath()
128 return "restServices";
131 protected RepositoriesService getRepositoriesService()
133 return getRepositoriesService( null );
136 protected <T> T getService( Class<T> clazz, String authzHeader )
138 T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz,
139 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
141 if ( authzHeader != null )
143 WebClient.client( service ).header( "Authorization", authzHeader );
145 WebClient.client(service).header("Referer","http://localhost:"+port);
146 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
147 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
148 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
152 protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader )
154 return getService( ProxyConnectorRuleService.class, authzHeader );
157 protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
159 return getService( MergeRepositoriesService.class, authzHeader );
162 protected RepositoriesService getRepositoriesService( String authzHeader )
164 return getService( RepositoriesService.class, authzHeader );
168 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
170 return getService( ManagedRepositoriesService.class, authzHeader );
173 protected PingService getPingService()
175 return getService( PingService.class, null );
178 protected PluginsService getPluginsService()
180 PluginsService service = getService( PluginsService.class, null );
181 WebClient.client( service ).accept( MediaType.TEXT_PLAIN );
182 WebClient.client( service ).type( MediaType.TEXT_PLAIN );
186 protected RemoteRepositoriesService getRemoteRepositoriesService()
188 return getService( RemoteRepositoriesService.class, null );
193 protected RepositoryGroupService getRepositoryGroupService()
195 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
196 RepositoryGroupService.class,
197 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
200 protected ProxyConnectorService getProxyConnectorService()
202 ProxyConnectorService service =
203 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
204 ProxyConnectorService.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 NetworkProxyService getNetworkProxyService()
217 NetworkProxyService service =
218 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
219 NetworkProxyService.class,
220 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
222 WebClient.client( service ).header( "Authorization", authorizationHeader );
223 WebClient.client(service).header("Referer","http://localhost:"+port);
224 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
225 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
226 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
230 protected ArchivaAdministrationService getArchivaAdministrationService()
232 ArchivaAdministrationService service =
233 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
234 ArchivaAdministrationService.class,
235 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
237 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
238 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
240 WebClient.client( service ).header( "Authorization", authorizationHeader );
241 WebClient.client(service).header("Referer","http://localhost:"+port);
243 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
247 protected RedbackRuntimeConfigurationService getRedbackRuntimeConfigurationService()
249 RedbackRuntimeConfigurationService service =
250 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
251 RedbackRuntimeConfigurationService.class,
252 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
254 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
255 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
257 WebClient.client( service ).header( "Authorization", authorizationHeader );
258 WebClient.client(service).header("Referer","http://localhost:"+port);
259 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
263 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
265 // START SNIPPET: cxf-browseservice-creation
266 BrowseService service =
267 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
269 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
270 // to add authentification
271 if ( authzHeader != null )
273 WebClient.client( service ).header( "Authorization", authzHeader );
275 // Set the Referer header to your archiva server url
276 WebClient.client(service).header("Referer","http://localhost:"+port);
278 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
281 WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
282 WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
286 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
287 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
290 // END SNIPPET: cxf-browseservice-creation
294 protected SearchService getSearchService( String authzHeader )
296 // START SNIPPET: cxf-searchservice-creation
297 SearchService service =
298 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
300 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
301 // to add authentification
302 if ( authzHeader != null )
304 WebClient.client( service ).header( "Authorization", authzHeader );
306 // Set the Referer header to your archiva server url
307 WebClient.client(service).header("Referer","http://localhost:"+port);
308 // to configure read timeout
309 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
310 // if you want to use json as exchange format xml is supported too
311 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
312 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
314 // END SNIPPET: cxf-searchservice-creation
318 protected CommonServices getCommonServices( String authzHeader )
320 CommonServices service =
321 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
322 CommonServices.class,
323 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
325 if ( authzHeader != null )
327 WebClient.client( service ).header( "Authorization", authzHeader );
329 WebClient.client(service).header("Referer","http://localhost:"+port);
330 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
331 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
332 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
336 protected ManagedRepository getTestManagedRepository()
338 String location = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repo" ).toAbsolutePath().toString();
339 return new ManagedRepository( Locale.getDefault(), "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
340 false, 2, 3, true, false, "my nice repo", false );
344 protected String getBaseUrl()
346 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
347 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
350 //-----------------------------------------------------
351 // utilities to create repos for testing
352 //-----------------------------------------------------
354 static final String TARGET_REPO_ID = "test-copy-target";
356 static final String SOURCE_REPO_ID = "test-origin-repo";
358 protected void initSourceTargetRepo()
361 Path targetRepo = Paths.get( "target/test-repo-copy" );
362 if ( Files.exists(targetRepo) )
364 org.apache.archiva.common.utils.FileUtils.deleteDirectory( targetRepo );
366 assertFalse( Files.exists(targetRepo) );
367 Files.createDirectories( targetRepo );
369 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
371 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
372 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
374 ManagedRepository managedRepository = getTestManagedRepository();
375 managedRepository.setId( TARGET_REPO_ID );
376 managedRepository.setLocation( targetRepo.toAbsolutePath().toString() );
377 managedRepository.setCronExpression( "* * * * * ?" );
378 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
379 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
381 Path originRepo = Paths.get( "target/test-origin-repo" );
382 if ( Files.exists(originRepo) )
384 org.apache.archiva.common.utils.FileUtils.deleteDirectory( originRepo );
386 assertFalse( Files.exists(originRepo) );
387 FileUtils.copyDirectory( Paths.get( "src/test/repo-with-osgi" ).toAbsolutePath().toFile(), originRepo.toAbsolutePath().toFile() );
389 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
391 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
392 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
395 managedRepository = getTestManagedRepository();
396 managedRepository.setId( SOURCE_REPO_ID );
397 managedRepository.setLocation( originRepo.toAbsolutePath().toString() );
399 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
400 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
402 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
403 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
407 protected void cleanRepos()
411 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
415 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
417 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
419 catch ( Exception e )
421 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
424 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
428 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
430 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
432 catch ( Exception e )
434 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
440 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean stageNeeded )
441 throws ArchivaRestServiceException, IOException, RedbackServiceException
443 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
445 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
448 ManagedRepository managedRepository = new ManagedRepository(Locale.getDefault());
449 managedRepository.setId( testRepoId );
450 managedRepository.setName( "test repo" );
452 Path badContent = Paths.get( repoPath, "target" );
453 if ( Files.exists(badContent) )
455 org.apache.archiva.common.utils.FileUtils.deleteDirectory( badContent );
458 Path file = Paths.get( repoPath );
459 if ( !file.isAbsolute() )
461 repoPath = getBasedir() + "/" + repoPath;
464 managedRepository.setLocation( Paths.get( repoPath ).toString() );
465 String suffix = Long.toString( new Date().getTime() );
466 String baseDir = System.getProperty("java.io.tmpdir");
467 managedRepository.setIndexDirectory(
468 baseDir + "/.indexer-" + suffix );
469 managedRepository.setPackedIndexDirectory(baseDir + "/.index-" + suffix);
471 managedRepository.setStageRepoNeeded( stageNeeded );
472 managedRepository.setSnapshots( true );
474 //managedRepository.setScanned( scanned );
476 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
477 service.addManagedRepository( managedRepository );
479 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
480 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
482 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
483 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
486 protected void scanRepo( String testRepoId )
487 throws ArchivaRestServiceException
489 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
492 protected void createAndIndexRepo( String testRepoId, String repoPath )
495 createAndIndexRepo( testRepoId, repoPath, false );
496 scanRepo( testRepoId );
499 protected void createStagedNeededRepo( String testRepoId, String repoPath, boolean scan )
502 createAndIndexRepo( testRepoId, repoPath, true );
505 scanRepo( testRepoId );
508 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
509 repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
512 repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
513 repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
518 protected void deleteTestRepo( String id )
521 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
523 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
527 public Path getBasedir()
529 return Paths.get(System.getProperty( "basedir" ));
532 protected void waitForScanToComplete( String repoId )
533 throws ArchivaRestServiceException, InterruptedException
535 while ( getRepositoriesService( authorizationHeader ).alreadyScanning( repoId ) ) {
536 // Would be better to cancel, if we had that capacity