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.BrowseService;
27 import org.apache.archiva.rest.api.services.CommonServices;
28 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
29 import org.apache.archiva.rest.api.services.NetworkProxyService;
30 import org.apache.archiva.rest.api.services.PingService;
31 import org.apache.archiva.rest.api.services.ProxyConnectorService;
32 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
33 import org.apache.archiva.rest.api.services.RepositoriesService;
34 import org.apache.archiva.rest.api.services.RepositoryGroupService;
35 import org.apache.archiva.rest.api.services.SearchService;
36 import org.apache.archiva.security.common.ArchivaRoleConstants;
37 import org.apache.commons.io.FileUtils;
38 import org.apache.commons.lang.StringUtils;
39 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
40 import org.apache.cxf.jaxrs.client.WebClient;
41 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
42 import org.junit.Before;
44 import javax.ws.rs.core.MediaType;
46 import java.util.Collections;
47 import java.util.Date;
48 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
49 import org.junit.Assume;
50 import org.junit.BeforeClass;
51 import org.junit.runner.RunWith;
52 import org.slf4j.LoggerFactory;
55 * @author Olivier Lamy
57 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
58 public abstract class AbstractArchivaRestTest
59 extends AbstractRestServicesTest
62 // START SNIPPET: authz-header
63 // guest with an empty password
64 public static String guestAuthzHeader =
65 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
67 // with an other login/password
68 //public String authzHeader =
69 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
71 // END SNIPPET: authz-header
74 public static void chekRepo() {
75 Assume.assumeTrue( !System.getProperty( "appserver.base" ).contains(" ") );
76 LoggerFactory.getLogger( AbstractArchivaRestTest.class.getName() ).
77 error( "Rest services unit test must be run in a folder with no space" );
78 // skygo: was not possible to fix path in this particular module
79 // Skip test if not in proper folder , otherwise test are not fair coz repository
80 // cannot have space in their name.
85 public void startServer()
88 File appServerBase = new File( System.getProperty( "appserver.base" ) );
90 File jcrDirectory = new File( appServerBase, "jcr" );
92 if ( jcrDirectory.exists() )
94 FileUtils.deleteDirectory( jcrDirectory );
101 protected String getSpringConfigLocation()
103 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
107 protected String getRestServicesPath()
109 return "restServices";
112 protected RepositoriesService getRepositoriesService()
114 return getRepositoriesService( null );
118 protected RepositoriesService getRepositoriesService( String authzHeader )
120 RepositoriesService service =
121 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
122 RepositoriesService.class,
123 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
125 if ( authzHeader != null )
127 WebClient.client( service ).header( "Authorization", authzHeader );
129 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
130 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
131 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
136 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
138 ManagedRepositoriesService service =
139 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
140 ManagedRepositoriesService.class,
141 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
143 if ( authzHeader != null )
145 WebClient.client( service ).header( "Authorization", authzHeader );
147 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
148 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
149 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
154 protected PingService getPingService()
156 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
158 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
161 protected RemoteRepositoriesService getRemoteRepositoriesService()
163 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
164 RemoteRepositoriesService.class,
165 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
170 protected RepositoryGroupService getRepositoryGroupService()
172 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
173 RepositoryGroupService.class,
174 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
177 protected ProxyConnectorService getProxyConnectorService()
179 ProxyConnectorService service =
180 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
181 ProxyConnectorService.class,
182 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
184 WebClient.client( service ).header( "Authorization", authorizationHeader );
185 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
186 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
187 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
191 protected NetworkProxyService getNetworkProxyService()
193 NetworkProxyService service =
194 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
195 NetworkProxyService.class,
196 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
198 WebClient.client( service ).header( "Authorization", authorizationHeader );
199 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
200 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
201 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
205 protected ArchivaAdministrationService getArchivaAdministrationService()
207 ArchivaAdministrationService service =
208 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
209 ArchivaAdministrationService.class,
210 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
212 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
213 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
215 WebClient.client( service ).header( "Authorization", authorizationHeader );
216 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
220 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
222 BrowseService service =
223 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
225 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
226 // to add authentification
227 if ( authzHeader != null )
229 WebClient.client( service ).header( "Authorization", authzHeader );
232 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
235 WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
236 WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
240 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
241 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
247 protected SearchService getSearchService( String authzHeader )
249 // START SNIPPET: cxf-searchservice-creation
250 SearchService 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 // to configure read timeout
260 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
261 // if you want to use json as exchange format xml is supported too
262 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
263 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
265 // END SNIPPET: cxf-searchservice-creation
269 protected CommonServices getCommonServices( String authzHeader )
271 CommonServices service =
272 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
273 CommonServices.class,
274 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
276 if ( authzHeader != null )
278 WebClient.client( service ).header( "Authorization", authzHeader );
280 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
281 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
282 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
286 protected ManagedRepository getTestManagedRepository()
288 String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
289 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
290 false, 2, 3, true, false );
294 protected String getBaseUrl()
296 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
297 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
300 //-----------------------------------------------------
301 // utilities to create repos for testing
302 //-----------------------------------------------------
304 static final String TARGET_REPO_ID = "test-copy-target";
306 static final String SOURCE_REPO_ID = "test-origin-repo";
308 protected void initSourceTargetRepo()
311 File targetRepo = new File( "target/test-repo-copy" );
312 if ( targetRepo.exists() )
314 FileUtils.deleteDirectory( targetRepo );
316 assertFalse( targetRepo.exists() );
319 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
321 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
322 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
324 ManagedRepository managedRepository = getTestManagedRepository();
325 managedRepository.setId( TARGET_REPO_ID );
326 managedRepository.setLocation( targetRepo.getCanonicalPath() );
327 managedRepository.setCronExpression( "* * * * * ?" );
328 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
329 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
331 File originRepo = new File( "target/test-origin-repo" );
332 if ( originRepo.exists() )
334 FileUtils.deleteDirectory( originRepo );
336 assertFalse( originRepo.exists() );
337 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
339 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
341 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
342 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
345 managedRepository = getTestManagedRepository();
346 managedRepository.setId( SOURCE_REPO_ID );
347 managedRepository.setLocation( originRepo.getCanonicalPath() );
349 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
350 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
352 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
353 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
357 protected void cleanRepos()
361 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
365 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
367 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
369 catch ( Exception e )
371 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
374 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
378 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
380 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
382 catch ( Exception e )
384 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
390 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean scan )
393 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
395 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
398 ManagedRepository managedRepository = new ManagedRepository();
399 managedRepository.setId( testRepoId );
400 managedRepository.setName( "test repo" );
402 File badContent = new File( repoPath, "target" );
403 if ( badContent.exists() )
405 FileUtils.deleteDirectory( badContent );
408 managedRepository.setLocation( new File( repoPath ).getPath() );
409 managedRepository.setIndexDirectory(
410 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
412 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
413 service.addManagedRepository( managedRepository );
415 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
416 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
418 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
419 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
422 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
427 protected void createAndIndexRepo( String testRepoId, String repoPath )
430 createAndIndexRepo( testRepoId, repoPath, true );
433 protected void deleteTestRepo( String id )
436 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
438 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
443 public String getBasedir()
445 return System.getProperty( "basedir" );