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.ArchivaBlockJUnit4ClassRunner;
49 import org.junit.runner.RunWith;
52 * @author Olivier Lamy
54 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
55 public abstract class AbstractArchivaRestTest
56 extends AbstractRestServicesTest
59 // START SNIPPET: authz-header
60 // guest with an empty password
61 public static String guestAuthzHeader =
62 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
64 // with an other login/password
65 //public String authzHeader =
66 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
68 // END SNIPPET: authz-header
73 public void startServer()
76 File appServerBase = new File( System.getProperty( "appserver.base" ) );
78 File jcrDirectory = new File( appServerBase, "jcr" );
80 if ( jcrDirectory.exists() )
82 FileUtils.deleteDirectory( jcrDirectory );
89 protected String getSpringConfigLocation()
91 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
94 protected String getRestServicesPath()
96 return "restServices";
99 protected RepositoriesService getRepositoriesService()
101 return getRepositoriesService( null );
105 protected RepositoriesService getRepositoriesService( String authzHeader )
107 RepositoriesService service =
108 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
109 RepositoriesService.class,
110 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
112 if ( authzHeader != null )
114 WebClient.client( service ).header( "Authorization", authzHeader );
116 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
117 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
118 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
123 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
125 ManagedRepositoriesService service =
126 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
127 ManagedRepositoriesService.class,
128 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
130 if ( authzHeader != null )
132 WebClient.client( service ).header( "Authorization", authzHeader );
134 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
135 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
136 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
141 protected PingService getPingService()
143 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
145 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
148 protected RemoteRepositoriesService getRemoteRepositoriesService()
150 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
151 RemoteRepositoriesService.class,
152 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
157 protected RepositoryGroupService getRepositoryGroupService()
159 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
160 RepositoryGroupService.class,
161 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
164 protected ProxyConnectorService getProxyConnectorService()
166 ProxyConnectorService service =
167 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
168 ProxyConnectorService.class,
169 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
171 WebClient.client( service ).header( "Authorization", authorizationHeader );
172 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
173 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
174 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
178 protected NetworkProxyService getNetworkProxyService()
180 NetworkProxyService service =
181 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
182 NetworkProxyService.class,
183 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
185 WebClient.client( service ).header( "Authorization", authorizationHeader );
186 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
187 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
188 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
192 protected ArchivaAdministrationService getArchivaAdministrationService()
194 ArchivaAdministrationService service =
195 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
196 ArchivaAdministrationService.class,
197 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
199 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
200 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
202 WebClient.client( service ).header( "Authorization", authorizationHeader );
203 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
207 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
209 BrowseService service =
210 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
212 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
213 // to add authentification
214 if ( authzHeader != null )
216 WebClient.client( service ).header( "Authorization", authzHeader );
219 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
222 WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
223 WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
227 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
228 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
234 protected SearchService getSearchService( String authzHeader )
236 // START SNIPPET: cxf-searchservice-creation
237 SearchService service =
238 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
240 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
241 // to add authentification
242 if ( authzHeader != null )
244 WebClient.client( service ).header( "Authorization", authzHeader );
246 // to configure read timeout
247 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
248 // if you want to use json as exchange format xml is supported too
249 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
250 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
252 // END SNIPPET: cxf-searchservice-creation
256 protected CommonServices getCommonServices( String authzHeader )
258 CommonServices service =
259 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
260 CommonServices.class,
261 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
263 if ( authzHeader != null )
265 WebClient.client( service ).header( "Authorization", authzHeader );
267 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
268 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
269 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
273 protected ManagedRepository getTestManagedRepository()
275 String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
276 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
277 false, 2, 3, true, false );
281 protected String getBaseUrl()
283 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
284 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
287 //-----------------------------------------------------
288 // utilities to create repos for testing
289 //-----------------------------------------------------
291 static final String TARGET_REPO_ID = "test-copy-target";
293 static final String SOURCE_REPO_ID = "test-origin-repo";
295 protected void initSourceTargetRepo()
298 File targetRepo = new File( "target/test-repo-copy" );
299 if ( targetRepo.exists() )
301 FileUtils.deleteDirectory( targetRepo );
303 assertFalse( targetRepo.exists() );
306 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
308 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
309 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
311 ManagedRepository managedRepository = getTestManagedRepository();
312 managedRepository.setId( TARGET_REPO_ID );
313 managedRepository.setLocation( targetRepo.getCanonicalPath() );
314 managedRepository.setCronExpression( "* * * * * ?" );
315 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
316 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
318 File originRepo = new File( "target/test-origin-repo" );
319 if ( originRepo.exists() )
321 FileUtils.deleteDirectory( originRepo );
323 assertFalse( originRepo.exists() );
324 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
326 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
328 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
329 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
332 managedRepository = getTestManagedRepository();
333 managedRepository.setId( SOURCE_REPO_ID );
334 managedRepository.setLocation( originRepo.getCanonicalPath() );
336 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
337 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
339 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
340 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
344 protected void cleanRepos()
348 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
352 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
354 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
356 catch ( Exception e )
358 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
361 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
365 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
367 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
369 catch ( Exception e )
371 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
377 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean scan )
380 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
382 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
385 ManagedRepository managedRepository = new ManagedRepository();
386 managedRepository.setId( testRepoId );
387 managedRepository.setName( "test repo" );
389 File badContent = new File( repoPath, "target" );
390 if ( badContent.exists() )
392 FileUtils.deleteDirectory( badContent );
395 managedRepository.setLocation( new File( repoPath ).getPath() );
396 managedRepository.setIndexDirectory(
397 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
399 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
400 service.addManagedRepository( managedRepository );
402 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
403 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
405 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
406 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
409 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
414 protected void createAndIndexRepo( String testRepoId, String repoPath )
417 createAndIndexRepo( testRepoId, repoPath, true );
420 protected void deleteTestRepo( String id )
423 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
425 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
430 public String getBasedir()
432 return System.getProperty( "basedir" );