Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AbstractArchivaRestTest.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. package org.apache.archiva.rest.services;
  2. /*
  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
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  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
  18. * under the License.
  19. */
  20. import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
  21. import org.apache.archiva.admin.model.beans.ManagedRepository;
  22. import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
  23. import org.apache.archiva.redback.rest.services.AbstractRestServicesTest;
  24. import org.apache.archiva.rest.api.services.ArchivaAdministrationService;
  25. import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
  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.PluginsService;
  33. import org.apache.archiva.rest.api.services.ProxyConnectorRuleService;
  34. import org.apache.archiva.rest.api.services.ProxyConnectorService;
  35. import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
  36. import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
  37. import org.apache.archiva.rest.api.services.RepositoriesService;
  38. import org.apache.archiva.rest.api.services.RepositoryGroupService;
  39. import org.apache.archiva.rest.api.services.SearchService;
  40. import org.apache.archiva.security.common.ArchivaRoleConstants;
  41. import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
  42. import org.apache.commons.io.FileUtils;
  43. import org.apache.commons.lang.StringUtils;
  44. import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
  45. import org.apache.cxf.jaxrs.client.WebClient;
  46. import org.junit.Assume;
  47. import org.junit.Before;
  48. import org.junit.BeforeClass;
  49. import org.junit.runner.RunWith;
  50. import org.slf4j.LoggerFactory;
  51. import javax.ws.rs.core.MediaType;
  52. import java.io.IOException;
  53. import java.nio.file.Files;
  54. import java.nio.file.Path;
  55. import java.nio.file.Paths;
  56. import java.util.Collections;
  57. import java.util.Date;
  58. /**
  59. * @author Olivier Lamy
  60. */
  61. @RunWith(ArchivaBlockJUnit4ClassRunner.class)
  62. public abstract class AbstractArchivaRestTest
  63. extends AbstractRestServicesTest
  64. {
  65. // START SNIPPET: authz-header
  66. // guest with an empty password
  67. public static String guestAuthzHeader =
  68. "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
  69. // with an other login/password
  70. //public String authzHeader =
  71. // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
  72. // END SNIPPET: authz-header
  73. @BeforeClass
  74. public static void chekRepo()
  75. {
  76. Assume.assumeTrue( !System.getProperty( "appserver.base" ).contains( " " ) );
  77. LoggerFactory.getLogger( AbstractArchivaRestTest.class.getName() ).
  78. error( "Rest services unit test must be run in a folder with no space" );
  79. // skygo: was not possible to fix path in this particular module
  80. // Skip test if not in proper folder , otherwise test are not fair coz repository
  81. // cannot have space in their name.
  82. }
  83. @Override
  84. @Before
  85. public void startServer()
  86. throws Exception
  87. {
  88. Path appServerBase = Paths.get( System.getProperty( "appserver.base" ) );
  89. removeAppsubFolder( appServerBase, "jcr" );
  90. removeAppsubFolder( appServerBase, "conf" );
  91. removeAppsubFolder( appServerBase, "data" );
  92. super.startServer();
  93. }
  94. private void removeAppsubFolder( Path appServerBase, String folder )
  95. throws Exception
  96. {
  97. Path directory = appServerBase.resolve( folder );
  98. if ( Files.exists(directory) )
  99. {
  100. org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
  101. }
  102. }
  103. @Override
  104. protected String getSpringConfigLocation()
  105. {
  106. return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
  107. }
  108. @Override
  109. protected String getRestServicesPath()
  110. {
  111. return "restServices";
  112. }
  113. protected RepositoriesService getRepositoriesService()
  114. {
  115. return getRepositoriesService( null );
  116. }
  117. protected <T> T getService( Class<T> clazz, String authzHeader )
  118. {
  119. T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz,
  120. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  121. if ( authzHeader != null )
  122. {
  123. WebClient.client( service ).header( "Authorization", authzHeader );
  124. }
  125. WebClient.client(service).header("Referer","http://localhost:"+port);
  126. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
  127. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  128. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  129. return service;
  130. }
  131. protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader )
  132. {
  133. return getService( ProxyConnectorRuleService.class, authzHeader );
  134. }
  135. protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
  136. {
  137. return getService( MergeRepositoriesService.class, authzHeader );
  138. }
  139. protected RepositoriesService getRepositoriesService( String authzHeader )
  140. {
  141. return getService( RepositoriesService.class, authzHeader );
  142. }
  143. protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
  144. {
  145. return getService( ManagedRepositoriesService.class, authzHeader );
  146. }
  147. protected PingService getPingService()
  148. {
  149. return getService( PingService.class, null );
  150. }
  151. protected PluginsService getPluginsService()
  152. {
  153. PluginsService service = getService( PluginsService.class, null );
  154. WebClient.client( service ).accept( MediaType.TEXT_PLAIN );
  155. WebClient.client( service ).type( MediaType.TEXT_PLAIN );
  156. return service;
  157. }
  158. protected RemoteRepositoriesService getRemoteRepositoriesService()
  159. {
  160. return getService( RemoteRepositoriesService.class, null );
  161. }
  162. protected RepositoryGroupService getRepositoryGroupService()
  163. {
  164. return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  165. RepositoryGroupService.class,
  166. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  167. }
  168. protected ProxyConnectorService getProxyConnectorService()
  169. {
  170. ProxyConnectorService service =
  171. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  172. ProxyConnectorService.class,
  173. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  174. WebClient.client( service ).header( "Authorization", authorizationHeader );
  175. WebClient.client(service).header("Referer","http://localhost:"+port);
  176. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
  177. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  178. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  179. return service;
  180. }
  181. protected NetworkProxyService getNetworkProxyService()
  182. {
  183. NetworkProxyService service =
  184. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  185. NetworkProxyService.class,
  186. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  187. WebClient.client( service ).header( "Authorization", authorizationHeader );
  188. WebClient.client(service).header("Referer","http://localhost:"+port);
  189. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
  190. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  191. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  192. return service;
  193. }
  194. protected ArchivaAdministrationService getArchivaAdministrationService()
  195. {
  196. ArchivaAdministrationService service =
  197. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  198. ArchivaAdministrationService.class,
  199. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  200. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  201. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  202. WebClient.client( service ).header( "Authorization", authorizationHeader );
  203. WebClient.client(service).header("Referer","http://localhost:"+port);
  204. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
  205. return service;
  206. }
  207. protected RedbackRuntimeConfigurationService getRedbackRuntimeConfigurationService()
  208. {
  209. RedbackRuntimeConfigurationService service =
  210. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  211. RedbackRuntimeConfigurationService.class,
  212. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  213. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  214. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  215. WebClient.client( service ).header( "Authorization", authorizationHeader );
  216. WebClient.client(service).header("Referer","http://localhost:"+port);
  217. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
  218. return service;
  219. }
  220. protected BrowseService getBrowseService( String authzHeader, boolean useXml )
  221. {
  222. // START SNIPPET: cxf-browseservice-creation
  223. BrowseService service =
  224. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  225. BrowseService.class,
  226. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  227. // to add authentification
  228. if ( authzHeader != null )
  229. {
  230. WebClient.client( service ).header( "Authorization", authzHeader );
  231. }
  232. // Set the Referer header to your archiva server url
  233. WebClient.client(service).header("Referer","http://localhost:"+port);
  234. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
  235. if ( useXml )
  236. {
  237. WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
  238. WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
  239. }
  240. else
  241. {
  242. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  243. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  244. }
  245. return service;
  246. // END SNIPPET: cxf-browseservice-creation
  247. }
  248. protected SearchService getSearchService( String authzHeader )
  249. {
  250. // START SNIPPET: cxf-searchservice-creation
  251. SearchService service =
  252. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  253. SearchService.class,
  254. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  255. // to add authentification
  256. if ( authzHeader != null )
  257. {
  258. WebClient.client( service ).header( "Authorization", authzHeader );
  259. }
  260. // Set the Referer header to your archiva server url
  261. WebClient.client(service).header("Referer","http://localhost:"+port);
  262. // to configure read timeout
  263. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
  264. // if you want to use json as exchange format xml is supported too
  265. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  266. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  267. return service;
  268. // END SNIPPET: cxf-searchservice-creation
  269. }
  270. protected CommonServices getCommonServices( String authzHeader )
  271. {
  272. CommonServices service =
  273. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  274. CommonServices.class,
  275. Collections.singletonList( new JacksonJaxbJsonProvider() ) );
  276. if ( authzHeader != null )
  277. {
  278. WebClient.client( service ).header( "Authorization", authzHeader );
  279. }
  280. WebClient.client(service).header("Referer","http://localhost:"+port);
  281. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
  282. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  283. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  284. return service;
  285. }
  286. protected ManagedRepository getTestManagedRepository()
  287. {
  288. String location = Paths.get( org.apache.archiva.common.utils.FileUtils.getBasedir(), "target/test-repo" ).toAbsolutePath().toString();
  289. return new ManagedRepository( "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
  290. false, 2, 3, true, false, "my nice repo", false );
  291. }
  292. protected String getBaseUrl()
  293. {
  294. String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
  295. return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + port : baseUrlSysProps;
  296. }
  297. //-----------------------------------------------------
  298. // utilities to create repos for testing
  299. //-----------------------------------------------------
  300. static final String TARGET_REPO_ID = "test-copy-target";
  301. static final String SOURCE_REPO_ID = "test-origin-repo";
  302. protected void initSourceTargetRepo()
  303. throws Exception
  304. {
  305. Path targetRepo = Paths.get( "target/test-repo-copy" );
  306. if ( Files.exists(targetRepo) )
  307. {
  308. org.apache.archiva.common.utils.FileUtils.deleteDirectory( targetRepo );
  309. }
  310. assertFalse( Files.exists(targetRepo) );
  311. Files.createDirectories( targetRepo );
  312. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
  313. {
  314. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
  315. assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
  316. }
  317. ManagedRepository managedRepository = getTestManagedRepository();
  318. managedRepository.setId( TARGET_REPO_ID );
  319. managedRepository.setLocation( targetRepo.toAbsolutePath().toString() );
  320. managedRepository.setCronExpression( "* * * * * ?" );
  321. getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
  322. assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
  323. Path originRepo = Paths.get( "target/test-origin-repo" );
  324. if ( Files.exists(originRepo) )
  325. {
  326. org.apache.archiva.common.utils.FileUtils.deleteDirectory( originRepo );
  327. }
  328. assertFalse( Files.exists(originRepo) );
  329. FileUtils.copyDirectory( Paths.get( "src/test/repo-with-osgi" ).toAbsolutePath().toFile(), originRepo.toAbsolutePath().toFile() );
  330. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
  331. {
  332. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
  333. assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
  334. }
  335. managedRepository = getTestManagedRepository();
  336. managedRepository.setId( SOURCE_REPO_ID );
  337. managedRepository.setLocation( originRepo.toAbsolutePath().toString() );
  338. getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
  339. assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
  340. getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
  341. getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
  342. }
  343. protected void cleanRepos()
  344. throws Exception
  345. {
  346. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
  347. {
  348. try
  349. {
  350. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
  351. assertNull(
  352. getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
  353. }
  354. catch ( Exception e )
  355. {
  356. log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
  357. }
  358. }
  359. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
  360. {
  361. try
  362. {
  363. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
  364. assertNull(
  365. getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
  366. }
  367. catch ( Exception e )
  368. {
  369. log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
  370. }
  371. }
  372. }
  373. protected void createAndIndexRepo( String testRepoId, String repoPath, boolean stageNeeded )
  374. throws ArchivaRestServiceException, IOException, RedbackServiceException
  375. {
  376. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
  377. {
  378. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
  379. }
  380. ManagedRepository managedRepository = new ManagedRepository();
  381. managedRepository.setId( testRepoId );
  382. managedRepository.setName( "test repo" );
  383. Path badContent = Paths.get( repoPath, "target" );
  384. if ( Files.exists(badContent) )
  385. {
  386. org.apache.archiva.common.utils.FileUtils.deleteDirectory( badContent );
  387. }
  388. Path file = Paths.get( repoPath );
  389. if ( !file.isAbsolute() )
  390. {
  391. repoPath = getBasedir() + "/" + repoPath;
  392. }
  393. managedRepository.setLocation( Paths.get( repoPath ).toString() );
  394. managedRepository.setIndexDirectory(
  395. System.getProperty( "java.io.tmpdir" ) + "/target/.index-" + Long.toString( new Date().getTime() ) );
  396. managedRepository.setStageRepoNeeded( stageNeeded );
  397. managedRepository.setSnapshots( true );
  398. //managedRepository.setScanned( scanned );
  399. ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
  400. service.addManagedRepository( managedRepository );
  401. getRoleManagementService( authorizationHeader ).assignTemplatedRole(
  402. ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
  403. getRoleManagementService( authorizationHeader ).assignTemplatedRole(
  404. ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
  405. }
  406. protected void scanRepo( String testRepoId )
  407. throws ArchivaRestServiceException
  408. {
  409. getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
  410. }
  411. protected void createAndIndexRepo( String testRepoId, String repoPath )
  412. throws Exception
  413. {
  414. createAndIndexRepo( testRepoId, repoPath, false );
  415. scanRepo( testRepoId );
  416. }
  417. protected void createStagedNeededRepo( String testRepoId, String repoPath, boolean scan )
  418. throws Exception
  419. {
  420. createAndIndexRepo( testRepoId, repoPath, true );
  421. if ( scan )
  422. {
  423. scanRepo( testRepoId );
  424. }
  425. RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
  426. repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
  427. if ( scan )
  428. {
  429. repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
  430. repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
  431. }
  432. }
  433. protected void deleteTestRepo( String id )
  434. throws Exception
  435. {
  436. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
  437. {
  438. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
  439. }
  440. }
  441. public Path getBasedir()
  442. {
  443. return Paths.get(System.getProperty( "basedir" ));
  444. }
  445. protected void waitForScanToComplete( String repoId )
  446. throws ArchivaRestServiceException, InterruptedException
  447. {
  448. while ( getRepositoriesService( authorizationHeader ).alreadyScanning( repoId ) ) {
  449. // Would be better to cancel, if we had that capacity
  450. Thread.sleep( 100 );
  451. }
  452. }
  453. }