You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractArchivaRestTest.java 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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 com.fasterxml.jackson.jaxrs.xml.JacksonJaxbXMLProvider;
  22. import org.apache.archiva.admin.model.beans.ManagedRepository;
  23. import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
  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.ArchivaRestServiceException;
  27. import org.apache.archiva.rest.api.services.BrowseService;
  28. import org.apache.archiva.rest.api.services.CommonServices;
  29. import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
  30. import org.apache.archiva.rest.api.services.MergeRepositoriesService;
  31. import org.apache.archiva.rest.api.services.NetworkProxyService;
  32. import org.apache.archiva.rest.api.services.PingService;
  33. import org.apache.archiva.rest.api.services.PluginsService;
  34. import org.apache.archiva.rest.api.services.ProxyConnectorRuleService;
  35. import org.apache.archiva.rest.api.services.ProxyConnectorService;
  36. import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
  37. import org.apache.archiva.rest.api.services.RemoteRepositoriesService;
  38. import org.apache.archiva.rest.api.services.RepositoriesService;
  39. import org.apache.archiva.rest.api.services.RepositoryGroupService;
  40. import org.apache.archiva.rest.api.services.SearchService;
  41. import org.apache.archiva.security.common.ArchivaRoleConstants;
  42. import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
  43. import org.apache.commons.io.FileUtils;
  44. import org.apache.commons.lang3.StringUtils;
  45. import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
  46. import org.apache.cxf.jaxrs.client.WebClient;
  47. import org.junit.Assume;
  48. import org.junit.Before;
  49. import org.junit.BeforeClass;
  50. import org.junit.runner.RunWith;
  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.time.LocalTime;
  57. import java.util.Arrays;
  58. import java.util.Date;
  59. import java.util.Locale;
  60. import java.util.concurrent.atomic.AtomicReference;
  61. /**
  62. * @author Olivier Lamy
  63. */
  64. @RunWith(ArchivaBlockJUnit4ClassRunner.class)
  65. public abstract class AbstractArchivaRestTest
  66. extends AbstractRestServicesTest
  67. {
  68. private AtomicReference<Path> projectDir = new AtomicReference<>();
  69. private AtomicReference<Path> appServerBase = new AtomicReference<>( );
  70. private AtomicReference<Path> basePath = new AtomicReference<>( );
  71. /*
  72. * Used by tryAssert to allow to throw exceptions in the lambda expression.
  73. */
  74. @FunctionalInterface
  75. protected interface AssertFunction
  76. {
  77. void accept( ) throws Exception;
  78. }
  79. protected void tryAssert( AssertFunction func ) throws Exception
  80. {
  81. tryAssert( func, 10, 500 );
  82. }
  83. /*
  84. * Runs the assert method until the assert is successful or the number of retries
  85. * is reached. This is needed because the JCR Oak index update is asynchronous, so updates
  86. * may not be visible immediately after the modification.
  87. */
  88. private void tryAssert( AssertFunction func, int retries, int sleepMillis ) throws Exception
  89. {
  90. Throwable t = null;
  91. int retry = retries;
  92. while ( retry-- > 0 )
  93. {
  94. try
  95. {
  96. func.accept( );
  97. return;
  98. }
  99. catch ( Exception | AssertionError e )
  100. {
  101. t = e;
  102. Thread.currentThread( ).sleep( sleepMillis );
  103. log.warn( "Retrying assert {}: {}", retry, e.getMessage( ) );
  104. }
  105. }
  106. log.warn( "Retries: {}, Exception: {}", retry, t.getMessage( ) );
  107. if ( retry <= 0 && t != null )
  108. {
  109. if ( t instanceof RuntimeException )
  110. {
  111. throw (RuntimeException) t;
  112. }
  113. else if ( t instanceof Exception )
  114. {
  115. throw (Exception) t;
  116. }
  117. else if ( t instanceof Error )
  118. {
  119. throw (Error) t;
  120. }
  121. }
  122. }
  123. // START SNIPPET: authz-header
  124. // guest with an empty password
  125. public static String guestAuthzHeader =
  126. "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );
  127. // with an other login/password
  128. //public String authzHeader =
  129. // "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );
  130. // END SNIPPET: authz-header
  131. Path getAppserverBase() {
  132. if (appServerBase.get()==null) {
  133. String basePath = System.getProperty( "appserver.base" );
  134. final Path appserverPath;
  135. if (StringUtils.isNotEmpty( basePath )) {
  136. appserverPath = Paths.get( basePath ).toAbsolutePath( );
  137. } else {
  138. appserverPath = getBasedir( ).resolve( "target" ).resolve( "appserver-base-" + LocalTime.now( ).toSecondOfDay( ) );
  139. }
  140. appServerBase.compareAndSet( null, appserverPath );
  141. }
  142. return appServerBase.get();
  143. }
  144. @BeforeClass
  145. public static void checkRepo()
  146. {
  147. Assume.assumeFalse("Test is ignored, because path to appserver contains whitespace characters!", System.getProperty( "appserver.base" ).contains( " " ) );
  148. // skygo: was not possible to fix path in this particular module
  149. // Skip test if not in proper folder , otherwise test are not fair coz repository
  150. // cannot have space in their name.
  151. }
  152. @Override
  153. @Before
  154. public void startServer()
  155. throws Exception
  156. {
  157. Path appServerBase = getAppserverBase();
  158. removeAppsubFolder(appServerBase, "jcr");
  159. removeAppsubFolder(appServerBase, "conf");
  160. removeAppsubFolder(appServerBase, "data");
  161. super.startServer();
  162. }
  163. private void removeAppsubFolder( Path appServerBase, String folder )
  164. throws Exception
  165. {
  166. Path directory = appServerBase.resolve( folder );
  167. if ( Files.exists(directory) )
  168. {
  169. org.apache.archiva.common.utils.FileUtils.deleteDirectory( directory );
  170. }
  171. }
  172. @Override
  173. protected String getSpringConfigLocation()
  174. {
  175. return "classpath*:META-INF/spring-context.xml,classpath:META-INF/spring-context-test.xml";
  176. }
  177. @Override
  178. protected String getRestServicesPath()
  179. {
  180. return "restServices";
  181. }
  182. protected RepositoriesService getRepositoriesService()
  183. {
  184. return getRepositoriesService( null );
  185. }
  186. protected <T> T getService( Class<T> clazz, String authzHeader )
  187. {
  188. T service = JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/", clazz,
  189. Arrays.asList( new JacksonJaxbJsonProvider( ), new JacksonJaxbXMLProvider( ) ) );
  190. if ( authzHeader != null )
  191. {
  192. WebClient.client( service ).header( "Authorization", authzHeader );
  193. }
  194. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  195. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
  196. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  197. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  198. return service;
  199. }
  200. protected ProxyConnectorRuleService getProxyConnectorRuleService( String authzHeader )
  201. {
  202. return getService( ProxyConnectorRuleService.class, authzHeader );
  203. }
  204. protected MergeRepositoriesService getMergeRepositoriesService( String authzHeader )
  205. {
  206. return getService( MergeRepositoriesService.class, authzHeader );
  207. }
  208. protected RepositoriesService getRepositoriesService( String authzHeader )
  209. {
  210. return getService( RepositoriesService.class, authzHeader );
  211. }
  212. protected ManagedRepositoriesService getManagedRepositoriesService( String authzHeader )
  213. {
  214. return getService( ManagedRepositoriesService.class, authzHeader );
  215. }
  216. protected PingService getPingService()
  217. {
  218. return getService( PingService.class, null );
  219. }
  220. protected PluginsService getPluginsService()
  221. {
  222. PluginsService service = getService( PluginsService.class, null );
  223. WebClient.client( service ).accept( MediaType.TEXT_PLAIN );
  224. WebClient.client( service ).type( MediaType.TEXT_PLAIN );
  225. return service;
  226. }
  227. protected RemoteRepositoriesService getRemoteRepositoriesService()
  228. {
  229. return getService( RemoteRepositoriesService.class, null );
  230. }
  231. protected RepositoryGroupService getRepositoryGroupService()
  232. {
  233. return JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  234. RepositoryGroupService.class,
  235. Arrays.asList( new JacksonJaxbJsonProvider( ), new JacksonJaxbXMLProvider( ) ) );
  236. }
  237. protected ProxyConnectorService getProxyConnectorService()
  238. {
  239. ProxyConnectorService service =
  240. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  241. ProxyConnectorService.class,
  242. Arrays.asList( new JacksonJaxbJsonProvider( ), new JacksonJaxbXMLProvider( ) ) );
  243. WebClient.client( service ).header( "Authorization", authorizationHeader );
  244. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  245. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
  246. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  247. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  248. return service;
  249. }
  250. protected NetworkProxyService getNetworkProxyService()
  251. {
  252. NetworkProxyService service =
  253. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  254. NetworkProxyService.class,
  255. Arrays.asList( new JacksonJaxbJsonProvider( ), new JacksonJaxbXMLProvider( ) ) );
  256. WebClient.client( service ).header( "Authorization", authorizationHeader );
  257. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  258. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
  259. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  260. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  261. return service;
  262. }
  263. protected ArchivaAdministrationService getArchivaAdministrationService()
  264. {
  265. ArchivaAdministrationService service =
  266. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  267. ArchivaAdministrationService.class,
  268. Arrays.asList( new JacksonJaxbJsonProvider( ), new JacksonJaxbXMLProvider( ) ) );
  269. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  270. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  271. WebClient.client( service ).header( "Authorization", authorizationHeader );
  272. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  273. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
  274. return service;
  275. }
  276. protected RedbackRuntimeConfigurationService getRedbackRuntimeConfigurationService()
  277. {
  278. RedbackRuntimeConfigurationService service =
  279. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  280. RedbackRuntimeConfigurationService.class,
  281. Arrays.asList( new JacksonJaxbJsonProvider( ), new JacksonJaxbXMLProvider( ) ) );
  282. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  283. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  284. WebClient.client( service ).header( "Authorization", authorizationHeader );
  285. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  286. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 300000 );
  287. return service;
  288. }
  289. protected BrowseService getBrowseService( String authzHeader, boolean useXml )
  290. {
  291. // START SNIPPET: cxf-browseservice-creation
  292. BrowseService service =
  293. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  294. BrowseService.class,
  295. Arrays.asList( new JacksonJaxbJsonProvider( ), new JacksonJaxbXMLProvider( ) ) );
  296. // to add authentification
  297. if ( authzHeader != null )
  298. {
  299. WebClient.client( service ).header( "Authorization", authzHeader );
  300. }
  301. // Set the Referer header to your archiva server url
  302. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  303. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
  304. if ( useXml )
  305. {
  306. WebClient.client( service ).accept( MediaType.APPLICATION_XML_TYPE );
  307. WebClient.client( service ).type( MediaType.APPLICATION_XML_TYPE );
  308. }
  309. else
  310. {
  311. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  312. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  313. }
  314. return service;
  315. // END SNIPPET: cxf-browseservice-creation
  316. }
  317. protected SearchService getSearchService( String authzHeader )
  318. {
  319. // START SNIPPET: cxf-searchservice-creation
  320. SearchService service =
  321. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  322. SearchService.class,
  323. Arrays.asList( new JacksonJaxbJsonProvider( ), new JacksonJaxbXMLProvider( ) ) );
  324. // to add authentification
  325. if ( authzHeader != null )
  326. {
  327. WebClient.client( service ).header( "Authorization", authzHeader );
  328. }
  329. // Set the Referer header to your archiva server url
  330. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  331. // to configure read timeout
  332. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
  333. // if you want to use json as exchange format xml is supported too
  334. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  335. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  336. return service;
  337. // END SNIPPET: cxf-searchservice-creation
  338. }
  339. protected CommonServices getCommonServices( String authzHeader )
  340. {
  341. CommonServices service =
  342. JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
  343. CommonServices.class,
  344. Arrays.asList( new JacksonJaxbJsonProvider( ), new JacksonJaxbXMLProvider( ) ) );
  345. if ( authzHeader != null )
  346. {
  347. WebClient.client( service ).header( "Authorization", authzHeader );
  348. }
  349. WebClient.client(service).header("Referer","http://localhost:"+getServerPort());
  350. WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
  351. WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
  352. WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
  353. return service;
  354. }
  355. protected ManagedRepository getTestManagedRepository()
  356. {
  357. String location = getAppserverBase().resolve( "data/repositories/test-repo" ).toAbsolutePath().toString();
  358. return new ManagedRepository( Locale.getDefault(), "TEST", "test", location, "default", true, true, false, "2 * * * * ?", null,
  359. false, 2, 3, true, false, "my nice repo", false );
  360. }
  361. protected String getBaseUrl()
  362. {
  363. String baseUrlSysProps = System.getProperty( "archiva.baseRestUrl" );
  364. return StringUtils.isBlank( baseUrlSysProps ) ? "http://localhost:" + getServerPort() : baseUrlSysProps;
  365. }
  366. protected Path getProjectDirectory() {
  367. if ( projectDir.get()==null) {
  368. String propVal = System.getProperty("mvn.project.base.dir");
  369. Path newVal;
  370. if (StringUtils.isEmpty(propVal)) {
  371. newVal = Paths.get("").toAbsolutePath();
  372. } else {
  373. newVal = Paths.get(propVal).toAbsolutePath();
  374. }
  375. projectDir.compareAndSet(null, newVal);
  376. }
  377. return projectDir.get();
  378. }
  379. //-----------------------------------------------------
  380. // utilities to create repos for testing
  381. //-----------------------------------------------------
  382. static final String TARGET_REPO_ID = "test-copy-target";
  383. static final String SOURCE_REPO_ID = "test-origin-repo";
  384. protected void initSourceTargetRepo()
  385. throws Exception
  386. {
  387. Path targetRepo = getAppserverBase().resolve("data/repositories/test-repo-copy" );
  388. if ( Files.exists(targetRepo) )
  389. {
  390. org.apache.archiva.common.utils.FileUtils.deleteDirectory( targetRepo );
  391. }
  392. assertFalse( Files.exists(targetRepo) );
  393. Files.createDirectories( targetRepo );
  394. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
  395. {
  396. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
  397. assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
  398. }
  399. ManagedRepository managedRepository = getTestManagedRepository();
  400. managedRepository.setId( TARGET_REPO_ID );
  401. managedRepository.setLocation( targetRepo.toAbsolutePath().toString() );
  402. managedRepository.setCronExpression( "* * * * * ?" );
  403. getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
  404. assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
  405. Path originRepo = getAppserverBase().resolve( "data/repositories/test-origin-repo" );
  406. if ( Files.exists(originRepo) )
  407. {
  408. org.apache.archiva.common.utils.FileUtils.deleteDirectory( originRepo );
  409. }
  410. assertFalse( Files.exists(originRepo) );
  411. FileUtils.copyDirectory( getProjectDirectory().resolve("src/test/repo-with-osgi" ).toAbsolutePath().toFile(), originRepo.toAbsolutePath().toFile() );
  412. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
  413. {
  414. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
  415. assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
  416. }
  417. managedRepository = getTestManagedRepository();
  418. managedRepository.setId( SOURCE_REPO_ID );
  419. managedRepository.setLocation( originRepo.toAbsolutePath().toString() );
  420. getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
  421. assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
  422. getArchivaAdministrationService().enabledKnownContentConsumer( "create-missing-checksums" );
  423. getArchivaAdministrationService().enabledKnownContentConsumer( "metadata-updater" );
  424. }
  425. protected void cleanRepos()
  426. throws Exception
  427. {
  428. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) != null )
  429. {
  430. try
  431. {
  432. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( TARGET_REPO_ID, true );
  433. assertNull(
  434. getManagedRepositoriesService( authorizationHeader ).getManagedRepository( TARGET_REPO_ID ) );
  435. }
  436. catch ( Exception e )
  437. {
  438. log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
  439. }
  440. }
  441. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) != null )
  442. {
  443. try
  444. {
  445. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SOURCE_REPO_ID, true );
  446. assertNull(
  447. getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SOURCE_REPO_ID ) );
  448. }
  449. catch ( Exception e )
  450. {
  451. log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
  452. }
  453. }
  454. }
  455. protected void createAndIndexRepo( String testRepoId, Path srcRepoPath, Path stagedSrcRepoPath, boolean stageNeeded )
  456. throws ArchivaRestServiceException, IOException, RedbackServiceException
  457. {
  458. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( testRepoId ) != null )
  459. {
  460. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( testRepoId, false );
  461. }
  462. ManagedRepository managedRepository = new ManagedRepository(Locale.getDefault());
  463. managedRepository.setId( testRepoId );
  464. managedRepository.setName( "test repo" );
  465. Path badContent = srcRepoPath.resolve( "target" );
  466. if ( Files.exists(badContent) )
  467. {
  468. org.apache.archiva.common.utils.FileUtils.deleteDirectory( badContent );
  469. }
  470. Path repoPath = getAppserverBase().resolve( "data" ).resolve( "repositories" ).resolve( testRepoId);
  471. Path stagedRepoPath = getAppserverBase().resolve( "data" ).resolve( "repositories" ).resolve( testRepoId + "-stage");
  472. FileUtils.deleteQuietly(repoPath.toFile());
  473. FileUtils.copyDirectory(srcRepoPath.toFile(), repoPath.toFile());
  474. if (stagedSrcRepoPath!=null) {
  475. FileUtils.deleteQuietly(stagedRepoPath.toFile());
  476. FileUtils.copyDirectory(stagedSrcRepoPath.toFile(), stagedRepoPath.toFile());
  477. }
  478. managedRepository.setLocation( repoPath.toAbsolutePath().toString() );
  479. String suffix = Long.toString( new Date().getTime() );
  480. Path baseDir = Files.createTempDirectory( "archiva-test-index" ).toAbsolutePath();
  481. managedRepository.setIndexDirectory(
  482. baseDir.resolve( ".indexer-" + suffix ).toString());
  483. managedRepository.setPackedIndexDirectory(baseDir.resolve(".index-" + suffix).toString());
  484. managedRepository.setStageRepoNeeded( stageNeeded );
  485. managedRepository.setSnapshots( true );
  486. //managedRepository.setScanned( scanned );
  487. ManagedRepositoriesService service = getManagedRepositoriesService( authorizationHeader );
  488. service.addManagedRepository( managedRepository );
  489. getRoleManagementService( authorizationHeader ).assignTemplatedRole(
  490. ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "admin" );
  491. getRoleManagementService( authorizationHeader ).assignTemplatedRole(
  492. ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, testRepoId, "guest" );
  493. }
  494. protected void scanRepo( String testRepoId )
  495. throws ArchivaRestServiceException
  496. {
  497. getRepositoriesService( authorizationHeader ).scanRepositoryNow( testRepoId, true );
  498. }
  499. protected void createAndIndexRepo( String testRepoId, Path srcRepoPath )
  500. throws Exception
  501. {
  502. createAndIndexRepo( testRepoId, srcRepoPath, null, false );
  503. scanRepo( testRepoId );
  504. }
  505. protected void createStagedNeededRepo( String testRepoId, Path srcRepoPath, Path stagedSrcRepoPath, boolean scan )
  506. throws Exception
  507. {
  508. createAndIndexRepo( testRepoId, srcRepoPath, stagedSrcRepoPath, true );
  509. if ( scan )
  510. {
  511. scanRepo( testRepoId );
  512. }
  513. RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
  514. repositoriesService.scanRepositoryDirectoriesNow( testRepoId );
  515. if ( scan )
  516. {
  517. repositoriesService.scanRepositoryNow( testRepoId + "-stage", true );
  518. repositoriesService.scanRepositoryDirectoriesNow( testRepoId + "-stage" );
  519. }
  520. }
  521. protected void deleteTestRepo( String id )
  522. throws Exception
  523. {
  524. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( id ) != null )
  525. {
  526. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, false );
  527. }
  528. }
  529. public Path getBasedir()
  530. {
  531. if (basePath.get()==null) {
  532. String baseDir = System.getProperty( "basedir" );
  533. final Path baseDirPath;
  534. if (StringUtils.isNotEmpty( baseDir )) {
  535. baseDirPath = Paths.get( baseDir );
  536. } else {
  537. baseDirPath = getProjectDirectory( );
  538. }
  539. basePath.compareAndSet( null, baseDirPath );
  540. }
  541. return basePath.get( );
  542. }
  543. protected void waitForScanToComplete( String repoId )
  544. throws ArchivaRestServiceException, InterruptedException
  545. {
  546. while ( getRepositoriesService( authorizationHeader ).getScanStatus( repoId ).isAlreadyScanning() ) {
  547. // Would be better to cancel, if we had that capacity
  548. Thread.sleep( 100 );
  549. }
  550. }
  551. }