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 28KB

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