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.CommonServices;
26 import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
27 import org.apache.archiva.rest.api.services.NetworkProxyService;
28 import org.apache.archiva.rest.api.services.PingService;
29 import org.apache.archiva.rest.api.services.ProxyConnectorService;
30 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
31 import org.apache.archiva.rest.api.services.RepositoriesService;
32 import org.apache.archiva.rest.api.services.RepositoryGroupService;
33 import org.apache.archiva.rest.api.services.SearchService;
34 import org.apache.archiva.security.common.ArchivaRoleConstants;
35 import org.apache.commons.io.FileUtils;
36 import org.apache.commons.lang.StringUtils;
37 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
38 import org.apache.cxf.jaxrs.client.WebClient;
39 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
40 import org.codehaus.redback.rest.services.AbstractRestServicesTest;
41 import org.junit.Before;
43 import javax.ws.rs.core.MediaType;
45 import java.util.Collections;
46 import java.util.Date;
49 * @author Olivier Lamy
51 public abstract class AbstractArchivaRestTest
52 extends AbstractRestServicesTest
55 // START SNIPPET: authz-header
56 // guest with an empty password
57 public String guestAuthzHeader =
58 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
60 // with an other login/password
61 //public String authzHeader =
62 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
64 // END SNIPPET: authz-header
69 public void startServer()
72 File appServerBase = new File( System.getProperty( "appserver.base" ) );
74 File jcrDirectory = new File( appServerBase, "jcr" );
76 if ( jcrDirectory.exists() )
78 FileUtils.deleteDirectory( jcrDirectory );
85 protected String getSpringConfigLocation()
87 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
90 protected String getRestServicesPath()
92 return "restServices";
95 protected RepositoriesService getRepositoriesService()
97 return getRepositoriesService( null );
101 protected RepositoriesService getRepositoriesService( String authzHeader )
103 RepositoriesService service =
104 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
105 RepositoriesService.class,
106 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
108 if ( authzHeader != null )
110 WebClient.client( service ).header( "Authorization", authzHeader );
112 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
113 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
114 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
119 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
121 ManagedRepositoriesService service =
122 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
123 ManagedRepositoriesService.class,
124 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
126 if ( authzHeader != null )
128 WebClient.client( service ).header( "Authorization", authzHeader );
130 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
131 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
132 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
137 protected PingService getPingService()
139 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
141 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
144 protected RemoteRepositoriesService getRemoteRepositoriesService()
146 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
147 RemoteRepositoriesService.class,
148 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
153 protected RepositoryGroupService getRepositoryGroupService()
155 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
156 RepositoryGroupService.class,
157 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
160 protected ProxyConnectorService getProxyConnectorService()
162 ProxyConnectorService service =
163 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
164 ProxyConnectorService.class,
165 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
167 WebClient.client( service ).header( "Authorization", authorizationHeader );
168 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
169 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
170 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
174 protected NetworkProxyService getNetworkProxyService()
176 NetworkProxyService service =
177 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
178 NetworkProxyService.class,
179 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
181 WebClient.client( service ).header( "Authorization", authorizationHeader );
182 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
183 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
184 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
188 protected ArchivaAdministrationService getArchivaAdministrationService()
190 ArchivaAdministrationService service =
191 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
192 ArchivaAdministrationService.class,
193 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
195 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
196 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
198 WebClient.client( service ).header( "Authorization", authorizationHeader );
199 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
203 protected SearchService getSearchService( String authzHeader )
205 // START SNIPPET: cxf-searchservice-creation
206 SearchService 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 );
215 // to configure read timeout
216 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
217 // if you want to use json as exchange format xml is supported too
218 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
219 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
221 // END SNIPPET: cxf-searchservice-creation
225 protected CommonServices getCommonServices( String authzHeader )
227 CommonServices service =
228 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
229 CommonServices.class,
230 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
232 if ( authzHeader != null )
234 WebClient.client( service ).header( "Authorization", authzHeader );
236 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
237 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
238 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
242 protected ManagedRepository getTestManagedRepository()
244 String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
245 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
246 false, 2, 3, true, false );
250 protected String getBaseUrl()
252 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
253 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
256 //-----------------------------------------------------
257 // utilities to create repos for testing
258 //-----------------------------------------------------
260 static final String TARGET_REPO_ID = "test-copy-target";
262 static final String SOURCE_REPO_ID = "test-origin-repo";
264 protected void initSourceTargetRepo()
267 File targetRepo = new File( "target/test-repo-copy" );
268 if ( targetRepo.exists() )
270 FileUtils.deleteDirectory( targetRepo );
272 assertFalse( targetRepo.exists() );
275 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
277 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
278 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
280 ManagedRepository managedRepository = getTestManagedRepository();
281 managedRepository.setId( TARGET_REPO_ID );
282 managedRepository.setLocation( targetRepo.getCanonicalPath() );
283 managedRepository.setCronExpression( "* * * * * ?" );
284 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
285 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
287 File originRepo = new File( "target/test-origin-repo" );
288 if ( originRepo.exists() )
290 FileUtils.deleteDirectory( originRepo );
292 assertFalse( originRepo.exists() );
293 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
295 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
297 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
298 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
301 managedRepository = getTestManagedRepository();
302 managedRepository.setId( SOURCE_REPO_ID );
303 managedRepository.setLocation( originRepo.getCanonicalPath() );
305 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
306 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
308 getArchivaAdministrationService().addKnownContentConsumer( "create-missing-checksums" );
309 getArchivaAdministrationService().addKnownContentConsumer( "metadata-updater" );
313 protected void cleanRepos()
317 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
319 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
320 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
322 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
324 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
325 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
330 protected void createAndIndexRepo( String testRepoId, String repoPath )
333 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
335 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
338 ManagedRepository managedRepository = new ManagedRepository();
339 managedRepository.setId( testRepoId );
340 managedRepository.setName( "test repo" );
342 File badContent = new File( repoPath, "target" );
343 if ( badContent.exists() )
345 FileUtils.deleteDirectory( badContent );
348 managedRepository.setLocation( new File( repoPath ).getPath() );
349 managedRepository.setIndexDirectory(
350 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
352 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
353 service.addManagedRepository( managedRepository );
355 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
356 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
358 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
359 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
361 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
365 protected void deleteTestRepo( String id )
368 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
370 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );