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.services.AbstractRestServicesTest;
26 import org.apache.archiva.rest.api.services.ArchivaAdministrationService;
27 import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
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.ProxyConnectorRuleService;
35 import org.apache.archiva.rest.api.services.ProxyConnectorService;
36 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
37 import org.apache.archiva.rest.api.services.RepositoriesService;
38 import org.apache.archiva.rest.api.services.RepositoryGroupService;
39 import org.apache.archiva.rest.api.services.SearchService;
40 import org.apache.archiva.security.common.ArchivaRoleConstants;
41 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
42 import org.apache.commons.io.FileUtils;
43 import org.apache.commons.lang.StringUtils;
44 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
45 import org.apache.cxf.jaxrs.client.WebClient;
46 import org.junit.Assume;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.runner.RunWith;
50 import org.slf4j.LoggerFactory;
52 import javax.ws.rs.core.MediaType;
54 import java.util.Collections;
55 import java.util.Date;
56 import org.apache.archiva.rest.api.services.PluginsService;
59 * @author Olivier Lamy
61 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
62 public abstract class AbstractArchivaRestTest
63 extends AbstractRestServicesTest
66 // START SNIPPET: authz-header
67 // guest with an empty password
68 public static String guestAuthzHeader =
69 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
71 // with an other login/password
72 //public String authzHeader =
73 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
75 // END SNIPPET: authz-header
78 public static void chekRepo()
80 Assume.assumeTrue( !System.getProperty( "appserver.base" ).contains( " " ) );
81 LoggerFactory.getLogger( AbstractArchivaRestTest.class.getName() ).
82 error( "Rest services unit test must be run in a folder with no space" );
83 // skygo: was not possible to fix path in this particular module
84 // Skip test if not in proper folder , otherwise test are not fair coz repository
85 // cannot have space in their name.
90 public void startServer()
93 File appServerBase = new File( System.getProperty( "appserver.base" ) );
95 removeAppsubFolder( appServerBase, "jcr" );
96 removeAppsubFolder( appServerBase, "conf" );
97 removeAppsubFolder( appServerBase, "data" );
102 private void removeAppsubFolder( File appServerBase, String folder )
105 File directory = new File( appServerBase, folder );
106 if ( directory.exists() )
108 FileUtils.deleteDirectory( directory );
113 protected String getSpringConfigLocation()
115 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
119 protected String getRestServicesPath()
121 return "restServices";
124 protected RepositoriesService getRepositoriesService()
126 return getRepositoriesService( null );
129 protected <T> T getService( Class<T> clazz, String authzHeader )
131 T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz,
132 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
134 if ( authzHeader != null )
136 WebClient.client( service ).header( "Authorization", authzHeader );
138 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
139 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
140 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
144 protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader )
146 return getService( ProxyConnectorRuleService.class, authzHeader );
149 protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
151 return getService( MergeRepositoriesService.class, authzHeader );
154 protected RepositoriesService getRepositoriesService( String authzHeader )
156 return getService( RepositoriesService.class, authzHeader );
160 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
162 return getService( ManagedRepositoriesService.class, authzHeader );
165 protected PingService getPingService()
167 return getService( PingService.class, null );
170 protected PluginsService getPluginsService()
172 PluginsService service = getService( PluginsService.class, null );
173 WebClient.client( service ).accept( MediaType.TEXT_PLAIN );
174 WebClient.client( service ).type( MediaType.TEXT_PLAIN );
178 protected RemoteRepositoriesService getRemoteRepositoriesService()
180 return getService( RemoteRepositoriesService.class, null );
185 protected RepositoryGroupService getRepositoryGroupService()
187 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
188 RepositoryGroupService.class,
189 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
192 protected ProxyConnectorService getProxyConnectorService()
194 ProxyConnectorService service =
195 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
196 ProxyConnectorService.class,
197 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
199 WebClient.client( service ).header( "Authorization", authorizationHeader );
200 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
201 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
202 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
206 protected NetworkProxyService getNetworkProxyService()
208 NetworkProxyService service =
209 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
210 NetworkProxyService.class,
211 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
213 WebClient.client( service ).header( "Authorization", authorizationHeader );
214 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
215 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
216 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
220 protected ArchivaAdministrationService getArchivaAdministrationService()
222 ArchivaAdministrationService service =
223 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
224 ArchivaAdministrationService.class,
225 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
227 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
228 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
230 WebClient.client( service ).header( "Authorization", authorizationHeader );
231 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
235 protected RedbackRuntimeConfigurationService getRedbackRuntimeConfigurationService()
237 RedbackRuntimeConfigurationService service =
238 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
239 RedbackRuntimeConfigurationService.class,
240 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
242 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
243 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
245 WebClient.client( service ).header( "Authorization", authorizationHeader );
246 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
250 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
252 BrowseService service =
253 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
255 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
256 // to add authentification
257 if ( authzHeader != null )
259 WebClient.client( service ).header( "Authorization", authzHeader );
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 );
277 protected SearchService getSearchService( String authzHeader )
279 // START SNIPPET: cxf-searchservice-creation
280 SearchService service =
281 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
283 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
284 // to add authentification
285 if ( authzHeader != null )
287 WebClient.client( service ).header( "Authorization", authzHeader );
289 // to configure read timeout
290 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
291 // if you want to use json as exchange format xml is supported too
292 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
293 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
295 // END SNIPPET: cxf-searchservice-creation
299 protected CommonServices getCommonServices( String authzHeader )
301 CommonServices service =
302 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
303 CommonServices.class,
304 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
306 if ( authzHeader != null )
308 WebClient.client( service ).header( "Authorization", authzHeader );
310 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
311 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
312 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
316 protected ManagedRepository getTestManagedRepository()
318 String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
319 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
320 false, 2, 3, true, false, "my nice repo", false );
324 protected String getBaseUrl()
326 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
327 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
330 //-----------------------------------------------------
331 // utilities to create repos for testing
332 //-----------------------------------------------------
334 static final String TARGET_REPO_ID = "test-copy-target";
336 static final String SOURCE_REPO_ID = "test-origin-repo";
338 protected void initSourceTargetRepo()
341 File targetRepo = new File( "target/test-repo-copy" );
342 if ( targetRepo.exists() )
344 FileUtils.deleteDirectory( targetRepo );
346 assertFalse( targetRepo.exists() );
349 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
351 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
352 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
354 ManagedRepository managedRepository = getTestManagedRepository();
355 managedRepository.setId( TARGET_REPO_ID );
356 managedRepository.setLocation( targetRepo.getCanonicalPath() );
357 managedRepository.setCronExpression( "* * * * * ?" );
358 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
359 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
361 File originRepo = new File( "target/test-origin-repo" );
362 if ( originRepo.exists() )
364 FileUtils.deleteDirectory( originRepo );
366 assertFalse( originRepo.exists() );
367 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
369 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
371 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
372 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
375 managedRepository = getTestManagedRepository();
376 managedRepository.setId( SOURCE_REPO_ID );
377 managedRepository.setLocation( originRepo.getCanonicalPath() );
379 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
380 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
382 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
383 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
387 protected void cleanRepos()
391 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
395 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
397 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
399 catch ( Exception e )
401 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
404 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
408 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
410 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
412 catch ( Exception e )
414 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
420 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean scan )
423 createAndIndexRepo( testRepoId, repoPath, scan, false );
426 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean scan, boolean stageNeeded )
429 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
431 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
434 ManagedRepository managedRepository = new ManagedRepository();
435 managedRepository.setId( testRepoId );
436 managedRepository.setName( "test repo" );
438 File badContent = new File( repoPath, "target" );
439 if ( badContent.exists() )
441 FileUtils.deleteDirectory( badContent );
444 File file = new File( repoPath );
445 if ( !file.isAbsolute() )
447 repoPath = getBasedir() + "/" + repoPath;
450 managedRepository.setLocation( new File( repoPath ).getPath() );
451 managedRepository.setIndexDirectory(
452 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
454 managedRepository.setStageRepoNeeded( stageNeeded );
455 managedRepository.setSnapshots( true );
457 //managedRepository.setScanned( scanned );
459 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
460 service.addManagedRepository( managedRepository );
462 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
463 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
465 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
466 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
469 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
474 protected void createAndIndexRepo( String testRepoId, String repoPath )
477 createAndIndexRepo( testRepoId, repoPath, true, false );
480 protected void createStagedNeededRepo( String testRepoId, String repoPath, boolean scan )
483 createAndIndexRepo( testRepoId, repoPath, scan, true );
484 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
485 repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
488 repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
489 repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
494 protected void deleteTestRepo( String id )
499 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
501 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
504 catch ( Exception e )
506 log.warn( "skip error deleting repo {}", id, e );
510 public String getBasedir()
512 return System.getProperty( "basedir" );