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.api.services.RedbackServiceException;
26 import org.apache.archiva.redback.rest.services.AbstractRestServicesTest;
27 import org.apache.archiva.rest.api.services.ArchivaAdministrationService;
28 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
29 import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
30 import org.apache.archiva.rest.api.services.BrowseService;
31 import org.apache.archiva.rest.api.services.CommonServices;
32 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
33 import org.apache.archiva.rest.api.services.MergeRepositoriesService;
34 import org.apache.archiva.rest.api.services.NetworkProxyService;
35 import org.apache.archiva.rest.api.services.PingService;
36 import org.apache.archiva.rest.api.services.ProxyConnectorRuleService;
37 import org.apache.archiva.rest.api.services.ProxyConnectorService;
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;
56 import java.io.IOException;
57 import java.util.Collections;
58 import java.util.Date;
59 import org.apache.archiva.rest.api.services.PluginsService;
62 * @author Olivier Lamy
64 @RunWith(ArchivaBlockJUnit4ClassRunner.class)
65 public abstract class AbstractArchivaRestTest
66 extends AbstractRestServicesTest
69 // START SNIPPET: authz-header
70 // guest with an empty password
71 public static String guestAuthzHeader =
72 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
74 // with an other login/password
75 //public String authzHeader =
76 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
78 // END SNIPPET: authz-header
81 public static void chekRepo()
83 Assume.assumeTrue( !System.getProperty( "appserver.base" ).contains( " " ) );
84 LoggerFactory.getLogger( AbstractArchivaRestTest.class.getName() ).
85 error( "Rest services unit test must be run in a folder with no space" );
86 // skygo: was not possible to fix path in this particular module
87 // Skip test if not in proper folder , otherwise test are not fair coz repository
88 // cannot have space in their name.
93 public void startServer()
96 File appServerBase = new File( System.getProperty( "appserver.base" ) );
98 removeAppsubFolder( appServerBase, "jcr" );
99 removeAppsubFolder( appServerBase, "conf" );
100 removeAppsubFolder( appServerBase, "data" );
105 private void removeAppsubFolder( File appServerBase, String folder )
108 File directory = new File( appServerBase, folder );
109 if ( directory.exists() )
111 FileUtils.deleteDirectory( directory );
116 protected String getSpringConfigLocation()
118 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
122 protected String getRestServicesPath()
124 return "restServices";
127 protected RepositoriesService getRepositoriesService()
129 return getRepositoriesService( null );
132 protected <T> T getService( Class<T> clazz, String authzHeader )
134 T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz,
135 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
137 if ( authzHeader != null )
139 WebClient.client( service ).header( "Authorization", authzHeader );
141 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
142 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
143 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
147 protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader )
149 return getService( ProxyConnectorRuleService.class, authzHeader );
152 protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
154 return getService( MergeRepositoriesService.class, authzHeader );
157 protected RepositoriesService getRepositoriesService( String authzHeader )
159 return getService( RepositoriesService.class, authzHeader );
163 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
165 return getService( ManagedRepositoriesService.class, authzHeader );
168 protected PingService getPingService()
170 return getService( PingService.class, null );
173 protected PluginsService getPluginsService()
175 PluginsService service = getService( PluginsService.class, null );
176 WebClient.client( service ).accept( MediaType.TEXT_PLAIN );
177 WebClient.client( service ).type( MediaType.TEXT_PLAIN );
181 protected RemoteRepositoriesService getRemoteRepositoriesService()
183 return getService( RemoteRepositoriesService.class, null );
188 protected RepositoryGroupService getRepositoryGroupService()
190 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
191 RepositoryGroupService.class,
192 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
195 protected ProxyConnectorService getProxyConnectorService()
197 ProxyConnectorService service =
198 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
199 ProxyConnectorService.class,
200 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
202 WebClient.client( service ).header( "Authorization", authorizationHeader );
203 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
204 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
205 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
209 protected NetworkProxyService getNetworkProxyService()
211 NetworkProxyService service =
212 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
213 NetworkProxyService.class,
214 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
216 WebClient.client( service ).header( "Authorization", authorizationHeader );
217 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
218 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
219 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
223 protected ArchivaAdministrationService getArchivaAdministrationService()
225 ArchivaAdministrationService service =
226 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
227 ArchivaAdministrationService.class,
228 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
230 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
231 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
233 WebClient.client( service ).header( "Authorization", authorizationHeader );
234 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
238 protected RedbackRuntimeConfigurationService getRedbackRuntimeConfigurationService()
240 RedbackRuntimeConfigurationService service =
241 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
242 RedbackRuntimeConfigurationService.class,
243 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
245 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
246 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
248 WebClient.client( service ).header( "Authorization", authorizationHeader );
249 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
253 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
255 BrowseService service =
256 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
258 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
259 // to add authentification
260 if ( authzHeader != null )
262 WebClient.client( service ).header( "Authorization", authzHeader );
265 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
268 WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
269 WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
273 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
274 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
280 protected SearchService getSearchService( String authzHeader )
282 // START SNIPPET: cxf-searchservice-creation
283 SearchService service =
284 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
286 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
287 // to add authentification
288 if ( authzHeader != null )
290 WebClient.client( service ).header( "Authorization", authzHeader );
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.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
314 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
315 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
319 protected ManagedRepository getTestManagedRepository()
321 String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
322 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
323 false, 2, 3, true, false, "my nice repo", false );
327 protected String getBaseUrl()
329 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
330 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
333 //-----------------------------------------------------
334 // utilities to create repos for testing
335 //-----------------------------------------------------
337 static final String TARGET_REPO_ID = "test-copy-target";
339 static final String SOURCE_REPO_ID = "test-origin-repo";
341 protected void initSourceTargetRepo()
344 File targetRepo = new File( "target/test-repo-copy" );
345 if ( targetRepo.exists() )
347 FileUtils.deleteDirectory( targetRepo );
349 assertFalse( targetRepo.exists() );
352 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
354 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
355 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
357 ManagedRepository managedRepository = getTestManagedRepository();
358 managedRepository.setId( TARGET_REPO_ID );
359 managedRepository.setLocation( targetRepo.getCanonicalPath() );
360 managedRepository.setCronExpression( "* * * * * ?" );
361 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
362 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
364 File originRepo = new File( "target/test-origin-repo" );
365 if ( originRepo.exists() )
367 FileUtils.deleteDirectory( originRepo );
369 assertFalse( originRepo.exists() );
370 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
372 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
374 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
375 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
378 managedRepository = getTestManagedRepository();
379 managedRepository.setId( SOURCE_REPO_ID );
380 managedRepository.setLocation( originRepo.getCanonicalPath() );
382 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
383 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
385 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
386 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
390 protected void cleanRepos()
394 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
398 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
400 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
402 catch ( Exception e )
404 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
407 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
411 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
413 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
415 catch ( Exception e )
417 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
423 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean stageNeeded )
424 throws ArchivaRestServiceException, IOException, RedbackServiceException
426 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
428 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
431 ManagedRepository managedRepository = new ManagedRepository();
432 managedRepository.setId( testRepoId );
433 managedRepository.setName( "test repo" );
435 File badContent = new File( repoPath, "target" );
436 if ( badContent.exists() )
438 FileUtils.deleteDirectory( badContent );
441 File file = new File( repoPath );
442 if ( !file.isAbsolute() )
444 repoPath = getBasedir() + "/" + repoPath;
447 managedRepository.setLocation( new File( repoPath ).getPath() );
448 managedRepository.setIndexDirectory(
449 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
451 managedRepository.setStageRepoNeeded( stageNeeded );
452 managedRepository.setSnapshots( true );
454 //managedRepository.setScanned( scanned );
456 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
457 service.addManagedRepository( managedRepository );
459 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
460 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
462 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
463 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
466 protected void scanRepo( String testRepoId )
467 throws ArchivaRestServiceException
469 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
472 protected void createAndIndexRepo( String testRepoId, String repoPath )
475 createAndIndexRepo( testRepoId, repoPath, false );
476 scanRepo( testRepoId );
479 protected void createStagedNeededRepo( String testRepoId, String repoPath, boolean scan )
482 createAndIndexRepo( testRepoId, repoPath, true );
485 scanRepo( testRepoId );
488 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
489 repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
492 repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
493 repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
498 protected void deleteTestRepo( String id )
501 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
503 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
507 public String getBasedir()
509 return System.getProperty( "basedir" );
512 protected void waitForScanToComplete( String repoId )
513 throws ArchivaRestServiceException, InterruptedException
515 while ( getRepositoriesService( authorizationHeader ).alreadyScanning( repoId ) ) {
516 // Would be better to cancel, if we had that capacity