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.MergeRepositoriesService;
30 import org.apache.archiva.rest.api.services.NetworkProxyService;
31 import org.apache.archiva.rest.api.services.PingService;
32 import org.apache.archiva.rest.api.services.ProxyConnectorService;
33 import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
34 import org.apache.archiva.rest.api.services.RepositoriesService;
35 import org.apache.archiva.rest.api.services.RepositoryGroupService;
36 import org.apache.archiva.rest.api.services.SearchService;
37 import org.apache.archiva.security.common.ArchivaRoleConstants;
38 import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
39 import org.apache.commons.io.FileUtils;
40 import org.apache.commons.lang.StringUtils;
41 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
42 import org.apache.cxf.jaxrs.client.WebClient;
43 import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;
44 import org.junit.Assume;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.runner.RunWith;
48 import org.slf4j.LoggerFactory;
50 import javax.ws.rs.core.MediaType;
52 import java.util.Collections;
53 import java.util.Date;
56 * @author Olivier Lamy
58 @RunWith (ArchivaBlockJUnit4ClassRunner.class)
59 public abstract class AbstractArchivaRestTest
60 extends AbstractRestServicesTest
63 // START SNIPPET: authz-header
64 // guest with an empty password
65 public static String guestAuthzHeader =
66 "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
68 // with an other login/password
69 //public String authzHeader =
70 // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
72 // END SNIPPET: authz-header
75 public static void chekRepo()
77 Assume.assumeTrue( !System.getProperty( "appserver.base" ).contains( " " ) );
78 LoggerFactory.getLogger( AbstractArchivaRestTest.class.getName() ).
79 error( "Rest services unit test must be run in a folder with no space" );
80 // skygo: was not possible to fix path in this particular module
81 // Skip test if not in proper folder , otherwise test are not fair coz repository
82 // cannot have space in their name.
87 public void startServer()
90 File appServerBase = new File( System.getProperty( "appserver.base" ) );
92 removeAppsubFolder( appServerBase, "jcr" );
93 removeAppsubFolder( appServerBase, "conf" );
94 removeAppsubFolder( appServerBase, "data" );
99 private void removeAppsubFolder( File appServerBase, String folder )
102 File directory = new File( appServerBase, folder );
103 if ( directory.exists() )
105 FileUtils.deleteDirectory( directory );
110 protected String getSpringConfigLocation()
112 return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
116 protected String getRestServicesPath()
118 return "restServices";
121 protected RepositoriesService getRepositoriesService()
123 return getRepositoriesService( null );
126 protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
128 MergeRepositoriesService service =
129 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
130 MergeRepositoriesService.class,
131 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
133 if ( authzHeader != null )
135 WebClient.client( service ).header( "Authorization", authzHeader );
137 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
138 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
139 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
143 protected RepositoriesService getRepositoriesService( String authzHeader )
145 RepositoriesService service =
146 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
147 RepositoriesService.class,
148 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
150 if ( authzHeader != null )
152 WebClient.client( service ).header( "Authorization", authzHeader );
154 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
155 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
156 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
161 protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
163 ManagedRepositoriesService service =
164 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
165 ManagedRepositoriesService.class,
166 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
168 if ( authzHeader != null )
170 WebClient.client( service ).header( "Authorization", authzHeader );
172 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
173 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
174 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
179 protected PingService getPingService()
181 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
183 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
186 protected RemoteRepositoriesService getRemoteRepositoriesService()
188 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
189 RemoteRepositoriesService.class,
190 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
195 protected RepositoryGroupService getRepositoryGroupService()
197 return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
198 RepositoryGroupService.class,
199 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
202 protected ProxyConnectorService getProxyConnectorService()
204 ProxyConnectorService service =
205 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
206 ProxyConnectorService.class,
207 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
209 WebClient.client( service ).header( "Authorization", authorizationHeader );
210 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
211 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
212 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
216 protected NetworkProxyService getNetworkProxyService()
218 NetworkProxyService service =
219 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
220 NetworkProxyService.class,
221 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
223 WebClient.client( service ).header( "Authorization", authorizationHeader );
224 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
225 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
226 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
230 protected ArchivaAdministrationService getArchivaAdministrationService()
232 ArchivaAdministrationService service =
233 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
234 ArchivaAdministrationService.class,
235 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
237 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
238 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
240 WebClient.client( service ).header( "Authorization", authorizationHeader );
241 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
245 protected BrowseService getBrowseService( String authzHeader, boolean useXml )
247 BrowseService service =
248 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
250 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
251 // to add authentification
252 if ( authzHeader != null )
254 WebClient.client( service ).header( "Authorization", authzHeader );
257 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
260 WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
261 WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
265 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
266 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
272 protected SearchService getSearchService( String authzHeader )
274 // START SNIPPET: cxf-searchservice-creation
275 SearchService service =
276 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
278 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
279 // to add authentification
280 if ( authzHeader != null )
282 WebClient.client( service ).header( "Authorization", authzHeader );
284 // to configure read timeout
285 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
286 // if you want to use json as exchange format xml is supported too
287 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
288 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
290 // END SNIPPET: cxf-searchservice-creation
294 protected CommonServices getCommonServices( String authzHeader )
296 CommonServices service =
297 JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
298 CommonServices.class,
299 Collections.singletonList( new JacksonJaxbJsonProvider() ) );
301 if ( authzHeader != null )
303 WebClient.client( service ).header( "Authorization", authzHeader );
305 WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
306 WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
307 WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
311 protected ManagedRepository getTestManagedRepository()
313 String location = new File( FileUtil.getBasedir(), "target/test-repo" ).getAbsolutePath();
314 return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
315 false, 2, 3, true, false, "my nice repo", false );
319 protected String getBaseUrl()
321 String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
322 return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
325 //-----------------------------------------------------
326 // utilities to create repos for testing
327 //-----------------------------------------------------
329 static final String TARGET_REPO_ID = "test-copy-target";
331 static final String SOURCE_REPO_ID = "test-origin-repo";
333 protected void initSourceTargetRepo()
336 File targetRepo = new File( "target/test-repo-copy" );
337 if ( targetRepo.exists() )
339 FileUtils.deleteDirectory( targetRepo );
341 assertFalse( targetRepo.exists() );
344 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
346 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
347 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
349 ManagedRepository managedRepository = getTestManagedRepository();
350 managedRepository.setId( TARGET_REPO_ID );
351 managedRepository.setLocation( targetRepo.getCanonicalPath() );
352 managedRepository.setCronExpression( "* * * * * ?" );
353 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
354 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
356 File originRepo = new File( "target/test-origin-repo" );
357 if ( originRepo.exists() )
359 FileUtils.deleteDirectory( originRepo );
361 assertFalse( originRepo.exists() );
362 FileUtils.copyDirectory( new File( "src/test/repo-with-osgi" ), originRepo );
364 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
366 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
367 assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
370 managedRepository = getTestManagedRepository();
371 managedRepository.setId( SOURCE_REPO_ID );
372 managedRepository.setLocation( originRepo.getCanonicalPath() );
374 getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
375 assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
377 getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
378 getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
382 protected void cleanRepos()
386 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
390 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
392 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
394 catch ( Exception e )
396 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
399 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
403 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
405 getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
407 catch ( Exception e )
409 log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
415 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean scan )
418 createAndIndexRepo( testRepoId, repoPath, scan, false );
421 protected void createAndIndexRepo( String testRepoId, String repoPath, boolean scan, boolean stageNeeded )
424 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
426 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
429 ManagedRepository managedRepository = new ManagedRepository();
430 managedRepository.setId( testRepoId );
431 managedRepository.setName( "test repo" );
433 File badContent = new File( repoPath, "target" );
434 if ( badContent.exists() )
436 FileUtils.deleteDirectory( badContent );
439 managedRepository.setLocation( new File( repoPath ).getPath() );
440 managedRepository.setIndexDirectory(
441 System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
443 managedRepository.setStageRepoNeeded( stageNeeded );
444 managedRepository.setSnapshots( true );
446 ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
447 service.addManagedRepository( managedRepository );
449 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
450 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
452 getRoleManagementService( authorizationHeader ).assignTemplatedRole(
453 ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
456 getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
461 protected void createAndIndexRepo( String testRepoId, String repoPath )
464 createAndIndexRepo( testRepoId, repoPath, true, false );
467 protected void createStagedNeededRepo( String testRepoId, String repoPath, boolean scan )
470 createAndIndexRepo( testRepoId, repoPath, scan, true );
471 RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
472 repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
475 repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
476 repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
481 protected void deleteTestRepo( String id )
486 if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
488 getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
491 catch ( Exception e )
493 log.warn( "skip error deleting repo {}", id, e );
497 public String getBasedir()
499 return System.getProperty( "basedir" );