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.rest.api.services.ArchivaAdministrationService;
25 import org.apache.archiva.rest.api.services.BrowseService;
26 import org.apache.archiva.rest.api.services.CommonServices;
27 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
28 import org.apache.archiva.rest.api.services.NetworkProxyService;
29 import org.apache.archiva.rest.api.services.PingService;
30 import org.apache.archiva.rest.api.services.ProxyConnectorService;
31 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
32 import org.apache.archiva.rest.api.services.RepositoriesService;
33 import org.apache.archiva.rest.api.services.RepositoryGroupService;
34 import org.apache.archiva.rest.api.services.SearchService;
35 import org.apache.archiva.security.common.ArchivaRoleConstants;
36 import org.apache.commons.io.FileUtils;
37 import org.apache.commons.lang.StringUtils;
38 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
39 import org.apache.cxf.jaxrs.client.WebClient;
40 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
41 import org.apache.archiva.redback.rest.services.AbstractRestServicesTest;
42 import org.junit.Before;
44 import javax.ws.rs.core.MediaType;
46 import java.util.Collections;
47 import java.util.Date;
50 * @author Olivier Lamy
52 public abstract class AbstractArchivaRestTest
53 extends AbstractRestServicesTest
56 // START SNIPPET: authz-header
57 // guest with an empty password
58 public static String guestAuthzHeader =
59 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
61 // with an other login/password
62 //public String authzHeader =
63 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
65 // END SNIPPET: authz-header
70 public void startServer()
73 File appServerBase = new File( System.getProperty( "appserver.base" ) );
75 File jcrDirectory = new File( appServerBase, "jcr" );
77 if ( jcrDirectory.exists() )
79 FileUtils.deleteDirectory( jcrDirectory );
86 protected String getSpringConfigLocation()
88 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
91 protected String getRestServicesPath()
93 return "restServices";
96 protected RepositoriesService getRepositoriesService()
98 return getRepositoriesService( null );
102 protected RepositoriesService getRepositoriesService( String authzHeader )
104 RepositoriesService service =
105 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
106 RepositoriesService.class,
107 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
109 if ( authzHeader != null )
111 WebClient.client( service ).header( "Authorization", authzHeader );
113 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
114 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
115 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
120 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
122 ManagedRepositoriesService service =
123 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
124 ManagedRepositoriesService.class,
125 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
127 if ( authzHeader != null )
129 WebClient.client( service ).header( "Authorization", authzHeader );
131 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
132 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
133 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
138 protected PingService getPingService()
140 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
142 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
145 protected RemoteRepositoriesService getRemoteRepositoriesService()
147 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
148 RemoteRepositoriesService.class,
149 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
154 protected RepositoryGroupService getRepositoryGroupService()
156 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
157 RepositoryGroupService.class,
158 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
161 protected ProxyConnectorService getProxyConnectorService()
163 ProxyConnectorService service =
164 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
165 ProxyConnectorService.class,
166 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
168 WebClient.client( service ).header( "Authorization", authorizationHeader );
169 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
170 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
171 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
175 protected NetworkProxyService getNetworkProxyService()
177 NetworkProxyService service =
178 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
179 NetworkProxyService.class,
180 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
182 WebClient.client( service ).header( "Authorization", authorizationHeader );
183 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
184 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
185 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
189 protected ArchivaAdministrationService getArchivaAdministrationService()
191 ArchivaAdministrationService service =
192 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
193 ArchivaAdministrationService.class,
194 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
196 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
197 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
199 WebClient.client( service ).header( "Authorization", authorizationHeader );
200 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
204 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
206 BrowseService service =
207 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
209 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
210 // to add authentification
211 if ( authzHeader != null )
213 WebClient.client( service ).header( "Authorization", authzHeader );
216 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
219 WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
220 WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
224 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
225 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
231 protected SearchService getSearchService( String authzHeader )
233 // START SNIPPET: cxf-searchservice-creation
234 SearchService service =
235 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
237 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
238 // to add authentification
239 if ( authzHeader != null )
241 WebClient.client( service ).header( "Authorization", authzHeader );
243 // to configure read timeout
244 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
245 // if you want to use json as exchange format xml is supported too
246 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
247 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
249 // END SNIPPET: cxf-searchservice-creation
253 protected CommonServices getCommonServices( String authzHeader )
255 CommonServices service =
256 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
257 CommonServices.class,
258 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
260 if ( authzHeader != null )
262 WebClient.client( service ).header( "Authorization", authzHeader );
264 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
265 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
266 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
270 protected ManagedRepository getTestManagedRepository()
272 String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
273 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
274 false, 2, 3, true, false );
278 protected String getBaseUrl()
280 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
281 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
284 //-----------------------------------------------------
285 // utilities to create repos for testing
286 //-----------------------------------------------------
288 static final String TARGET_REPO_ID = "test-copy-target";
290 static final String SOURCE_REPO_ID = "test-origin-repo";
292 protected void initSourceTargetRepo()
295 File targetRepo = new File( "target/test-repo-copy" );
296 if ( targetRepo.exists() )
298 FileUtils.deleteDirectory( targetRepo );
300 assertFalse( targetRepo.exists() );
303 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
305 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
306 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
308 ManagedRepository managedRepository = getTestManagedRepository();
309 managedRepository.setId( TARGET_REPO_ID );
310 managedRepository.setLocation( targetRepo.getCanonicalPath() );
311 managedRepository.setCronExpression( "* * * * * ?" );
312 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
313 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
315 File originRepo = new File( "target/test-origin-repo" );
316 if ( originRepo.exists() )
318 FileUtils.deleteDirectory( originRepo );
320 assertFalse( originRepo.exists() );
321 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
323 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
325 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
326 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
329 managedRepository = getTestManagedRepository();
330 managedRepository.setId( SOURCE_REPO_ID );
331 managedRepository.setLocation( originRepo.getCanonicalPath() );
333 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
334 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
336 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
337 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
341 protected void cleanRepos()
345 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
347 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
348 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
350 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
352 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
353 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
358 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean scan )
361 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
363 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
366 ManagedRepository managedRepository = new ManagedRepository();
367 managedRepository.setId( testRepoId );
368 managedRepository.setName( "test repo" );
370 File badContent = new File( repoPath, "target" );
371 if ( badContent.exists() )
373 FileUtils.deleteDirectory( badContent );
376 managedRepository.setLocation( new File( repoPath ).getPath() );
377 managedRepository.setIndexDirectory(
378 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
380 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
381 service.addManagedRepository( managedRepository );
383 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
384 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
386 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
387 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
390 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
395 protected void createAndIndexRepo( String testRepoId, String repoPath )
398 createAndIndexRepo( testRepoId, repoPath, true );
401 protected void deleteTestRepo( String id )
404 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
406 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
411 public String getBasedir()
413 return System.getProperty( "basedir" );