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.ManagedRepositoriesService;
26 import org.apache.archiva.rest.api.services.NetworkProxyService;
27 import org.apache.archiva.rest.api.services.PingService;
28 import org.apache.archiva.rest.api.services.ProxyConnectorService;
29 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
30 import org.apache.archiva.rest.api.services.RepositoriesService;
31 import org.apache.archiva.rest.api.services.RepositoryGroupService;
32 import org.apache.archiva.rest.api.services.SearchService;
33 import org.apache.archiva.security.common.ArchivaRoleConstants;
34 import org.apache.commons.io.FileUtils;
35 import org.apache.commons.lang.StringUtils;
36 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
37 import org.apache.cxf.jaxrs.client.WebClient;
38 import org.codehaus.redback.rest.services.AbstractRestServicesTest;
39 import org.junit.Before;
41 import javax.ws.rs.core.MediaType;
43 import java.util.Date;
46 * @author Olivier Lamy
48 public abstract class AbstractArchivaRestTest
49 extends AbstractRestServicesTest
52 // START SNIPPET: authz-header
53 // guest with an empty password
54 public String guestAuthzHeader =
55 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
57 // with an other login/password
58 //public String authzHeader =
59 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
61 // END SNIPPET: authz-header
66 public void startServer()
69 File appServerBase = new File( System.getProperty( "appserver.base" ) );
71 File jcrDirectory = new File( appServerBase, "jcr" );
73 if ( jcrDirectory.exists() )
75 FileUtils.deleteDirectory( jcrDirectory );
82 protected String getSpringConfigLocation()
84 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
87 protected String getRestServicesPath()
89 return "restServices";
92 protected RepositoriesService getRepositoriesService()
94 return getRepositoriesService( null );
98 protected RepositoriesService getRepositoriesService( String authzHeader )
100 RepositoriesService service =
101 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
102 RepositoriesService.class );
104 if ( authzHeader != null )
106 WebClient.client( service ).header( "Authorization", authzHeader );
108 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
113 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
115 ManagedRepositoriesService service =
116 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
117 ManagedRepositoriesService.class );
119 if ( authzHeader != null )
121 WebClient.client( service ).header( "Authorization", authzHeader );
123 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
128 protected PingService getPingService()
130 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
134 protected RemoteRepositoriesService getRemoteRepositoriesService()
136 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
137 RemoteRepositoriesService.class );
142 protected RepositoryGroupService getRepositoryGroupService()
144 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
145 RepositoryGroupService.class );
148 protected ProxyConnectorService getProxyConnectorService()
150 ProxyConnectorService service =
151 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
152 ProxyConnectorService.class );
154 WebClient.client( service ).header( "Authorization", authorizationHeader );
155 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
159 protected NetworkProxyService getNetworkProxyService()
161 NetworkProxyService service =
162 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
163 NetworkProxyService.class );
165 WebClient.client( service ).header( "Authorization", authorizationHeader );
166 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
170 protected ArchivaAdministrationService getArchivaAdministrationService()
172 ArchivaAdministrationService service =
173 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
174 ArchivaAdministrationService.class );
176 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
177 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
179 WebClient.client( service ).header( "Authorization", authorizationHeader );
180 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
184 protected SearchService getSearchService( String authzHeader )
186 // START SNIPPET: cxf-searchservice-creation
187 SearchService service =
188 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
189 SearchService.class );
191 if ( authzHeader != null )
193 WebClient.client( service ).header( "Authorization", authzHeader );
195 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
197 // END SNIPPET: cxf-searchservice-creation
201 protected ManagedRepository getTestManagedRepository()
203 String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
204 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
205 false, 2, 3, true, false );
209 protected String getBaseUrl()
211 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
212 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
215 //-----------------------------------------------------
216 // utilities to create repos for testing
217 //-----------------------------------------------------
219 static final String TARGET_REPO_ID = "test-copy-target";
221 static final String SOURCE_REPO_ID = "test-origin-repo";
223 protected void initSourceTargetRepo()
226 File targetRepo = new File( "target/test-repo-copy" );
227 if ( targetRepo.exists() )
229 FileUtils.deleteDirectory( targetRepo );
231 assertFalse( targetRepo.exists() );
234 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
236 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
237 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
239 ManagedRepository managedRepository = getTestManagedRepository();
240 managedRepository.setId( TARGET_REPO_ID );
241 managedRepository.setLocation( targetRepo.getCanonicalPath() );
242 managedRepository.setCronExpression( "* * * * * ?" );
243 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
244 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
246 File originRepo = new File( "target/test-origin-repo" );
247 if ( originRepo.exists() )
249 FileUtils.deleteDirectory( originRepo );
251 assertFalse( originRepo.exists() );
252 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
254 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
256 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
257 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
260 managedRepository = getTestManagedRepository();
261 managedRepository.setId( SOURCE_REPO_ID );
262 managedRepository.setLocation( originRepo.getCanonicalPath() );
264 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
265 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
267 getArchivaAdministrationService().addKnownContentConsumer( "create-missing-checksums" );
268 getArchivaAdministrationService().addKnownContentConsumer( "metadata-updater" );
272 protected void cleanRepos()
276 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
278 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
279 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
281 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
283 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
284 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
289 protected void createAndIndexRepo( String testRepoId, String repoPath )
292 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
294 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
297 ManagedRepository managedRepository = new ManagedRepository();
298 managedRepository.setId( testRepoId );
299 managedRepository.setName( "test repo" );
301 File badContent = new File( repoPath, "target" );
302 if ( badContent.exists() )
304 FileUtils.deleteDirectory( badContent );
307 managedRepository.setLocation( new File( repoPath ).getPath() );
308 managedRepository.setIndexDirectory(
309 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
311 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
312 service.addManagedRepository( managedRepository );
314 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
315 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
317 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
321 protected void deleteTestRepo( String id )
324 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
326 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );