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.redback.rest.services.AbstractRestServicesTest;
40 import org.junit.Before;
42 import javax.ws.rs.core.MediaType;
44 import java.util.Date;
47 * @author Olivier Lamy
49 public abstract class AbstractArchivaRestTest
50 extends AbstractRestServicesTest
53 // START SNIPPET: authz-header
54 // guest with an empty password
55 public String guestAuthzHeader =
56 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
58 // with an other login/password
59 //public String authzHeader =
60 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
62 // END SNIPPET: authz-header
67 public void startServer()
70 File appServerBase = new File( System.getProperty( "appserver.base" ) );
72 File jcrDirectory = new File( appServerBase, "jcr" );
74 if ( jcrDirectory.exists() )
76 FileUtils.deleteDirectory( jcrDirectory );
83 protected String getSpringConfigLocation()
85 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
88 protected String getRestServicesPath()
90 return "restServices";
93 protected RepositoriesService getRepositoriesService()
95 return getRepositoriesService( null );
99 protected RepositoriesService getRepositoriesService( String authzHeader )
101 RepositoriesService service =
102 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
103 RepositoriesService.class );
105 if ( authzHeader != null )
107 WebClient.client( service ).header( "Authorization", authzHeader );
109 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
114 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
116 ManagedRepositoriesService service =
117 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
118 ManagedRepositoriesService.class );
120 if ( authzHeader != null )
122 WebClient.client( service ).header( "Authorization", authzHeader );
124 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
129 protected PingService getPingService()
131 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
135 protected RemoteRepositoriesService getRemoteRepositoriesService()
137 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
138 RemoteRepositoriesService.class );
143 protected RepositoryGroupService getRepositoryGroupService()
145 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
146 RepositoryGroupService.class );
149 protected ProxyConnectorService getProxyConnectorService()
151 ProxyConnectorService service =
152 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
153 ProxyConnectorService.class );
155 WebClient.client( service ).header( "Authorization", authorizationHeader );
156 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
160 protected NetworkProxyService getNetworkProxyService()
162 NetworkProxyService service =
163 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
164 NetworkProxyService.class );
166 WebClient.client( service ).header( "Authorization", authorizationHeader );
167 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
171 protected ArchivaAdministrationService getArchivaAdministrationService()
173 ArchivaAdministrationService service =
174 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
175 ArchivaAdministrationService.class );
177 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
178 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
180 WebClient.client( service ).header( "Authorization", authorizationHeader );
181 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
185 protected SearchService getSearchService( String authzHeader )
187 // START SNIPPET: cxf-searchservice-creation
188 SearchService service =
189 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
190 SearchService.class );
192 if ( authzHeader != null )
194 WebClient.client( service ).header( "Authorization", authzHeader );
196 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
198 // END SNIPPET: cxf-searchservice-creation
202 protected CommonServices getCommonServices( String authzHeader )
204 CommonServices service =
205 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
206 CommonServices.class );
208 if ( authzHeader != null )
210 WebClient.client( service ).header( "Authorization", authzHeader );
212 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
216 protected ManagedRepository getTestManagedRepository()
218 String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
219 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
220 false, 2, 3, true, false );
224 protected String getBaseUrl()
226 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
227 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
230 //-----------------------------------------------------
231 // utilities to create repos for testing
232 //-----------------------------------------------------
234 static final String TARGET_REPO_ID = "test-copy-target";
236 static final String SOURCE_REPO_ID = "test-origin-repo";
238 protected void initSourceTargetRepo()
241 File targetRepo = new File( "target/test-repo-copy" );
242 if ( targetRepo.exists() )
244 FileUtils.deleteDirectory( targetRepo );
246 assertFalse( targetRepo.exists() );
249 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
251 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
252 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
254 ManagedRepository managedRepository = getTestManagedRepository();
255 managedRepository.setId( TARGET_REPO_ID );
256 managedRepository.setLocation( targetRepo.getCanonicalPath() );
257 managedRepository.setCronExpression( "* * * * * ?" );
258 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
259 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
261 File originRepo = new File( "target/test-origin-repo" );
262 if ( originRepo.exists() )
264 FileUtils.deleteDirectory( originRepo );
266 assertFalse( originRepo.exists() );
267 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
269 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
271 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
272 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
275 managedRepository = getTestManagedRepository();
276 managedRepository.setId( SOURCE_REPO_ID );
277 managedRepository.setLocation( originRepo.getCanonicalPath() );
279 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
280 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
282 getArchivaAdministrationService().addKnownContentConsumer( "create-missing-checksums" );
283 getArchivaAdministrationService().addKnownContentConsumer( "metadata-updater" );
287 protected void cleanRepos()
291 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
293 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
294 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
296 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
298 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
299 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
304 protected void createAndIndexRepo( String testRepoId, String repoPath )
307 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
309 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
312 ManagedRepository managedRepository = new ManagedRepository();
313 managedRepository.setId( testRepoId );
314 managedRepository.setName( "test repo" );
316 File badContent = new File( repoPath, "target" );
317 if ( badContent.exists() )
319 FileUtils.deleteDirectory( badContent );
322 managedRepository.setLocation( new File( repoPath ).getPath() );
323 managedRepository.setIndexDirectory(
324 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
326 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
327 service.addManagedRepository( managedRepository );
329 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
330 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
332 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
333 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
335 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
339 protected void deleteTestRepo( String id )
342 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
344 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );