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 org.apache.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.common.utils.FileUtil;
24 import org.apache.archiva.redback.rest.services.AbstractRestServicesTest;
25 import org.apache.archiva.rest.api.services.ArchivaAdministrationService;
26 import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
27 import org.apache.archiva.rest.api.services.BrowseService;
28 import org.apache.archiva.rest.api.services.CommonServices;
29 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
30 import org.apache.archiva.rest.api.services.MergeRepositoriesService;
31 import org.apache.archiva.rest.api.services.NetworkProxyService;
32 import org.apache.archiva.rest.api.services.PingService;
33 import org.apache.archiva.rest.api.services.ProxyConnectorRuleService;
34 import org.apache.archiva.rest.api.services.ProxyConnectorService;
35 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
36 import org.apache.archiva.rest.api.services.RepositoriesService;
37 import org.apache.archiva.rest.api.services.RepositoryGroupService;
38 import org.apache.archiva.rest.api.services.SearchService;
39 import org.apache.archiva.security.common.ArchivaRoleConstants;
40 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
41 import org.apache.commons.io.FileUtils;
42 import org.apache.commons.lang.StringUtils;
43 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
44 import org.apache.cxf.jaxrs.client.WebClient;
45 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
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;
58 * @author Olivier Lamy
60 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
61 public abstract class AbstractArchivaRestTest
62 extends AbstractRestServicesTest
65 // START SNIPPET: authz-header
66 // guest with an empty password
67 public static String guestAuthzHeader =
68 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
70 // with an other login/password
71 //public String authzHeader =
72 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
74 // END SNIPPET: authz-header
77 public static void chekRepo()
79 Assume.assumeTrue( !System.getProperty( "appserver.base" ).contains( " " ) );
80 LoggerFactory.getLogger( AbstractArchivaRestTest.class.getName() ).
81 error( "Rest services unit test must be run in a folder with no space" );
82 // skygo: was not possible to fix path in this particular module
83 // Skip test if not in proper folder , otherwise test are not fair coz repository
84 // cannot have space in their name.
89 public void startServer()
92 File appServerBase = new File( System.getProperty( "appserver.base" ) );
94 removeAppsubFolder( appServerBase, "jcr" );
95 removeAppsubFolder( appServerBase, "conf" );
96 removeAppsubFolder( appServerBase, "data" );
101 private void removeAppsubFolder( File appServerBase, String folder )
104 File directory = new File( appServerBase, folder );
105 if ( directory.exists() )
107 FileUtils.deleteDirectory( directory );
112 protected String getSpringConfigLocation()
114 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
118 protected String getRestServicesPath()
120 return "restServices";
123 protected RepositoriesService getRepositoriesService()
125 return getRepositoriesService( null );
128 protected <T> T getService( Class<T> clazz, String authzHeader )
130 T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz,
131 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
133 if ( authzHeader != null )
135 WebClient.client( service ).header( "Authorization", authzHeader );
137 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
138 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
139 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
143 protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader )
145 return getService( ProxyConnectorRuleService.class, authzHeader );
148 protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
150 return getService( MergeRepositoriesService.class, authzHeader );
153 protected RepositoriesService getRepositoriesService( String authzHeader )
155 return getService( RepositoriesService.class, authzHeader );
159 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
161 return getService( ManagedRepositoriesService.class, authzHeader );
164 protected PingService getPingService()
166 return getService( PingService.class, null );
169 protected RemoteRepositoriesService getRemoteRepositoriesService()
171 return getService( RemoteRepositoriesService.class, null );
176 protected RepositoryGroupService getRepositoryGroupService()
178 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
179 RepositoryGroupService.class,
180 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
183 protected ProxyConnectorService getProxyConnectorService()
185 ProxyConnectorService service =
186 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
187 ProxyConnectorService.class,
188 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
190 WebClient.client( service ).header( "Authorization", authorizationHeader );
191 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
192 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
193 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
197 protected NetworkProxyService getNetworkProxyService()
199 NetworkProxyService service =
200 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
201 NetworkProxyService.class,
202 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
204 WebClient.client( service ).header( "Authorization", authorizationHeader );
205 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
206 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
207 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
211 protected ArchivaAdministrationService getArchivaAdministrationService()
213 ArchivaAdministrationService service =
214 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
215 ArchivaAdministrationService.class,
216 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
218 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
219 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
221 WebClient.client( service ).header( "Authorization", authorizationHeader );
222 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
226 protected RedbackRuntimeConfigurationService getArchivaRuntimeConfigurationService()
228 RedbackRuntimeConfigurationService service =
229 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
230 RedbackRuntimeConfigurationService.class,
231 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
233 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
234 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
236 WebClient.client( service ).header( "Authorization", authorizationHeader );
237 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
241 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
243 BrowseService service =
244 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
246 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
247 // to add authentification
248 if ( authzHeader != null )
250 WebClient.client( service ).header( "Authorization", authzHeader );
253 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
256 WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
257 WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
261 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
262 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
268 protected SearchService getSearchService( String authzHeader )
270 // START SNIPPET: cxf-searchservice-creation
271 SearchService service =
272 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
274 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
275 // to add authentification
276 if ( authzHeader != null )
278 WebClient.client( service ).header( "Authorization", authzHeader );
280 // to configure read timeout
281 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
282 // if you want to use json as exchange format xml is supported too
283 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
284 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
286 // END SNIPPET: cxf-searchservice-creation
290 protected CommonServices getCommonServices( String authzHeader )
292 CommonServices service =
293 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
294 CommonServices.class,
295 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
297 if ( authzHeader != null )
299 WebClient.client( service ).header( "Authorization", authzHeader );
301 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
302 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
303 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
307 protected ManagedRepository getTestManagedRepository()
309 String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
310 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
311 false, 2, 3, true, false, "my nice repo", false );
315 protected String getBaseUrl()
317 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
318 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
321 //-----------------------------------------------------
322 // utilities to create repos for testing
323 //-----------------------------------------------------
325 static final String TARGET_REPO_ID = "test-copy-target";
327 static final String SOURCE_REPO_ID = "test-origin-repo";
329 protected void initSourceTargetRepo()
332 File targetRepo = new File( "target/test-repo-copy" );
333 if ( targetRepo.exists() )
335 FileUtils.deleteDirectory( targetRepo );
337 assertFalse( targetRepo.exists() );
340 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
342 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
343 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
345 ManagedRepository managedRepository = getTestManagedRepository();
346 managedRepository.setId( TARGET_REPO_ID );
347 managedRepository.setLocation( targetRepo.getCanonicalPath() );
348 managedRepository.setCronExpression( "* * * * * ?" );
349 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
350 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
352 File originRepo = new File( "target/test-origin-repo" );
353 if ( originRepo.exists() )
355 FileUtils.deleteDirectory( originRepo );
357 assertFalse( originRepo.exists() );
358 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
360 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
362 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
363 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
366 managedRepository = getTestManagedRepository();
367 managedRepository.setId( SOURCE_REPO_ID );
368 managedRepository.setLocation( originRepo.getCanonicalPath() );
370 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
371 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
373 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
374 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
378 protected void cleanRepos()
382 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
386 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
388 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
390 catch ( Exception e )
392 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
395 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
399 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
401 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
403 catch ( Exception e )
405 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
411 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean scan )
414 createAndIndexRepo( testRepoId, repoPath, scan, false );
417 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean scan, boolean stageNeeded )
420 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
422 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
425 ManagedRepository managedRepository = new ManagedRepository();
426 managedRepository.setId( testRepoId );
427 managedRepository.setName( "test repo" );
429 File badContent = new File( repoPath, "target" );
430 if ( badContent.exists() )
432 FileUtils.deleteDirectory( badContent );
435 File file = new File( repoPath );
436 if ( !file.isAbsolute() )
438 repoPath = getBasedir() + "/" + repoPath;
441 managedRepository.setLocation( new File( repoPath ).getPath() );
442 managedRepository.setIndexDirectory(
443 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
445 managedRepository.setStageRepoNeeded( stageNeeded );
446 managedRepository.setSnapshots( true );
448 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
449 service.addManagedRepository( managedRepository );
451 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
452 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
454 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
455 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
458 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
463 protected void createAndIndexRepo( String testRepoId, String repoPath )
466 createAndIndexRepo( testRepoId, repoPath, true, false );
469 protected void createStagedNeededRepo( String testRepoId, String repoPath, boolean scan )
472 createAndIndexRepo( testRepoId, repoPath, scan, true );
473 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
474 repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
477 repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
478 repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
483 protected void deleteTestRepo( String id )
488 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
490 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
493 catch ( Exception e )
495 log.warn( "skip error deleting repo {}", id, e );
499 public String getBasedir()
501 return System.getProperty( "basedir" );