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.

RepositoriesServiceTest.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 org.apache.archiva.admin.model.beans.ManagedRepository;
  21. import org.apache.archiva.maven2.model.Artifact;
  22. import org.apache.archiva.rest.api.model.BrowseResult;
  23. import org.apache.archiva.rest.api.model.BrowseResultEntry;
  24. import org.apache.archiva.rest.api.model.VersionsList;
  25. import org.apache.archiva.rest.api.services.BrowseService;
  26. import org.apache.archiva.rest.api.services.ManagedRepositoriesService;
  27. import org.apache.archiva.rest.api.services.RepositoriesService;
  28. import org.apache.commons.io.FileUtils;
  29. import org.junit.Test;
  30. import javax.ws.rs.BadRequestException;
  31. import javax.ws.rs.ForbiddenException;
  32. import javax.ws.rs.core.Response;
  33. import java.nio.file.Files;
  34. import java.nio.file.Path;
  35. import java.nio.file.Paths;
  36. import java.util.List;
  37. import java.util.Locale;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. /**
  40. * @author Olivier Lamy
  41. */
  42. public class RepositoriesServiceTest
  43. extends AbstractArchivaRestTest
  44. {
  45. @Test( expected = ForbiddenException.class )
  46. public void scanRepoKarmaFailed()
  47. throws Exception
  48. {
  49. RepositoriesService service = getRepositoriesService();
  50. try
  51. {
  52. service.scanRepository( "id", true );
  53. }
  54. catch ( ForbiddenException e )
  55. {
  56. assertEquals( 403, e.getResponse().getStatus() );
  57. throw e;
  58. }
  59. }
  60. @Test
  61. public void scanRepo()
  62. throws Exception
  63. {
  64. RepositoriesService service = getRepositoriesService( authorizationHeader );
  65. ManagedRepositoriesService managedRepositoriesService = getManagedRepositoriesService( authorizationHeader );
  66. String repoId = managedRepositoriesService.getManagedRepositories().get( 0 ).getId();
  67. int timeout = 20000;
  68. while ( timeout > 0 && service.alreadyScanning( repoId ) )
  69. {
  70. Thread.sleep( 500 );
  71. timeout -= 500;
  72. }
  73. assertTrue( service.scanRepository( repoId, true ) );
  74. }
  75. @Test( expected = ForbiddenException.class )
  76. public void deleteArtifactKarmaFailed()
  77. throws Exception
  78. {
  79. try
  80. {
  81. Artifact artifact = new Artifact();
  82. artifact.setGroupId( "commons-logging" );
  83. artifact.setArtifactId( "commons-logging" );
  84. artifact.setVersion( "1.0.1" );
  85. artifact.setPackaging( "jar" );
  86. artifact.setContext( SOURCE_REPO_ID );
  87. RepositoriesService repositoriesService = getRepositoriesService( null );
  88. repositoriesService.deleteArtifact( artifact );
  89. }
  90. catch ( ForbiddenException e )
  91. {
  92. assertEquals( 403, e.getResponse().getStatus() );
  93. throw e;
  94. }
  95. }
  96. @Test( expected = BadRequestException.class )
  97. public void deleteWithRepoNull()
  98. throws Exception
  99. {
  100. try
  101. {
  102. RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
  103. Artifact artifact = new Artifact();
  104. artifact.setGroupId( "commons-logging" );
  105. artifact.setArtifactId( "commons-logging" );
  106. artifact.setVersion( "1.0.1" );
  107. artifact.setPackaging( "jar" );
  108. repositoriesService.deleteArtifact( artifact );
  109. }
  110. catch ( BadRequestException e )
  111. {
  112. assertEquals( "not http " + Response.Status.BAD_REQUEST.getStatusCode() + " status",
  113. Response.Status.BAD_REQUEST.getStatusCode(), e.getResponse().getStatus() );
  114. throw e;
  115. }
  116. }
  117. /**
  118. * delete a version of an artifact without packaging
  119. *
  120. * @throws Exception
  121. */
  122. @Test
  123. public void deleteArtifactVersion()
  124. throws Exception
  125. {
  126. initSourceTargetRepo();
  127. BrowseService browseService = getBrowseService( authorizationHeader, false );
  128. List<Artifact> artifacts =
  129. browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
  130. "2.2.2", SOURCE_REPO_ID );
  131. log.info( "artifacts: {}", artifacts );
  132. assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
  133. VersionsList versionsList =
  134. browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
  135. SOURCE_REPO_ID );
  136. assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
  137. log.info( "artifacts.size: {}", artifacts.size() );
  138. try
  139. {
  140. Path artifactFile = getBasedir().resolve(
  141. "target/repositories/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" );
  142. assertTrue( "artifact not exists:" + artifactFile, Files.exists(artifactFile) );
  143. Artifact artifact = new Artifact();
  144. artifact.setGroupId( "org.apache.karaf.features" );
  145. artifact.setArtifactId( "org.apache.karaf.features.core" );
  146. artifact.setVersion( "2.2.2" );
  147. artifact.setContext( SOURCE_REPO_ID );
  148. RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
  149. repositoriesService.deleteArtifact( artifact );
  150. assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) );
  151. artifacts =
  152. browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
  153. "2.2.2", SOURCE_REPO_ID );
  154. assertThat( artifacts ).isNotNull().isEmpty();
  155. versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
  156. SOURCE_REPO_ID );
  157. assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
  158. }
  159. finally
  160. {
  161. cleanRepos();
  162. }
  163. }
  164. @Test
  165. public void deleteArtifact()
  166. throws Exception
  167. {
  168. initSourceTargetRepo();
  169. BrowseService browseService = getBrowseService( authorizationHeader, false );
  170. List<Artifact> artifacts =
  171. browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
  172. "2.2.2", SOURCE_REPO_ID );
  173. log.info( "artifacts: {}", artifacts );
  174. assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
  175. VersionsList versionsList =
  176. browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
  177. SOURCE_REPO_ID );
  178. assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 2 );
  179. log.info( "artifacts.size: {}", artifacts.size() );
  180. try
  181. {
  182. Path artifactFile = getBasedir().resolve(
  183. "target/repositories/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.core/2.2.2/org.apache.karaf.features.core-2.2.2.jar" );
  184. assertTrue( "artifact not exists:" + artifactFile.toString(), Files.exists(artifactFile) );
  185. Artifact artifact = new Artifact();
  186. artifact.setGroupId( "org.apache.karaf.features" );
  187. artifact.setArtifactId( "org.apache.karaf.features.core" );
  188. artifact.setVersion( "2.2.2" );
  189. artifact.setPackaging( "jar" );
  190. artifact.setContext( SOURCE_REPO_ID );
  191. RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
  192. repositoriesService.deleteArtifact( artifact );
  193. assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) );
  194. artifacts =
  195. browseService.getArtifactDownloadInfos( "org.apache.karaf.features", "org.apache.karaf.features.core",
  196. "2.2.2", SOURCE_REPO_ID );
  197. assertThat( artifacts ).isNotNull().isEmpty();
  198. versionsList = browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
  199. SOURCE_REPO_ID );
  200. assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 1 );
  201. }
  202. finally
  203. {
  204. cleanRepos();
  205. }
  206. }
  207. @Test
  208. public void deleteArtifactWithClassifier()
  209. throws Exception
  210. {
  211. initSourceTargetRepo();
  212. BrowseService browseService = getBrowseService( authorizationHeader, false );
  213. List<Artifact> artifacts =
  214. browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
  215. assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 3 );
  216. VersionsList versionsList =
  217. browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
  218. assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
  219. log.info( "artifacts.size: {}", artifacts.size() );
  220. try
  221. {
  222. Path artifactFile = getBasedir().resolve(
  223. "target/repositories/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar" );
  224. Path artifactFilemd5 = getBasedir().resolve(
  225. "target/repositories/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.md5" );
  226. Path artifactFilesha1 = getBasedir().resolve(
  227. "target/repositories/test-origin-repo/commons-logging/commons-logging/1.0.1/commons-logging-1.0.1-javadoc.jar.sha1" );
  228. assertTrue( "artifact not exists:" + artifactFile, Files.exists(artifactFile) );
  229. assertTrue( "md5 not exists:" + artifactFilemd5, Files.exists(artifactFilemd5) );
  230. assertTrue( "sha1 not exists:" + artifactFilesha1, Files.exists(artifactFilesha1) );
  231. Artifact artifact = new Artifact();
  232. artifact.setGroupId( "commons-logging" );
  233. artifact.setArtifactId( "commons-logging" );
  234. artifact.setVersion( "1.0.1" );
  235. artifact.setClassifier( "javadoc" );
  236. artifact.setPackaging( "jar" );
  237. artifact.setContext( SOURCE_REPO_ID );
  238. RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
  239. repositoriesService.deleteArtifact( artifact );
  240. assertFalse( "artifact not deleted exists:" + artifactFile, Files.exists(artifactFile) );
  241. assertFalse( "md5 still exists:" + artifactFilemd5, Files.exists(artifactFilemd5) );
  242. assertFalse( "sha1 still exists:" + artifactFilesha1, Files.exists(artifactFilesha1) );
  243. artifacts =
  244. browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.0.1", SOURCE_REPO_ID );
  245. log.info( "artifact: {}", artifacts );
  246. assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 2 );
  247. versionsList = browseService.getVersionsList( "commons-logging", "commons-logging", SOURCE_REPO_ID );
  248. log.info( "versionsList: {}", versionsList );
  249. assertThat( versionsList.getVersions() ).isNotNull().isNotEmpty().hasSize( 6 );
  250. }
  251. finally
  252. {
  253. cleanRepos();
  254. }
  255. }
  256. @Test
  257. public void deleteGroupId()
  258. throws Exception
  259. {
  260. initSourceTargetRepo();
  261. try
  262. {
  263. BrowseService browseService = getBrowseService( authorizationHeader, false );
  264. BrowseResult browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
  265. assertNotNull( browseResult );
  266. log.info( "browseResult: {}", browseResult );
  267. assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isNotEmpty().contains(
  268. new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.command", true ),
  269. new BrowseResultEntry( "org.apache.karaf.features.org.apache.karaf.features.core", true ) );
  270. Path directory =
  271. getBasedir().resolve( "target/repositories/test-origin-repo/org/apache/karaf/features/org.apache.karaf.features.command" );
  272. assertTrue( "directory not exists", Files.exists(directory) );
  273. RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
  274. repositoriesService.deleteGroupId( "org.apache.karaf", SOURCE_REPO_ID );
  275. assertFalse( "directory not exists", Files.exists(directory) );
  276. browseResult = browseService.browseGroupId( "org.apache.karaf.features", SOURCE_REPO_ID );
  277. assertNotNull( browseResult );
  278. assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
  279. browseResult = browseService.browseGroupId( "org.apache.karaf", SOURCE_REPO_ID );
  280. assertNotNull( browseResult );
  281. assertThat( browseResult.getBrowseResultEntries() ).isNotNull().isEmpty();
  282. log.info( "browseResult empty: {}", browseResult );
  283. }
  284. finally
  285. {
  286. cleanRepos();
  287. }
  288. }
  289. @Test
  290. public void authorizedToDeleteArtifacts()
  291. throws Exception
  292. {
  293. ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
  294. try
  295. {
  296. getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
  297. RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
  298. assertTrue( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
  299. }
  300. finally
  301. {
  302. cleanQuietlyRepo( managedRepository.getId() );
  303. }
  304. }
  305. @Test
  306. public void notAuthorizedToDeleteArtifacts()
  307. throws Exception
  308. {
  309. ManagedRepository managedRepository = getTestManagedRepository( "SOURCE_REPO_ID", "SOURCE_REPO_ID" );
  310. try
  311. {
  312. getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
  313. RepositoriesService repositoriesService = getRepositoriesService( guestAuthzHeader );
  314. assertFalse( repositoriesService.isAuthorizedToDeleteArtifacts( managedRepository.getId() ) );
  315. }
  316. finally
  317. {
  318. cleanQuietlyRepo( managedRepository.getId() );
  319. }
  320. }
  321. protected void cleanQuietlyRepo( String id )
  322. {
  323. try
  324. {
  325. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( id, true );
  326. }
  327. catch ( Exception e )
  328. {
  329. log.info( "ignore issue deleting test repo: {}", e.getMessage() );
  330. }
  331. }
  332. @Test
  333. public void deleteSnapshot()
  334. throws Exception
  335. {
  336. Path targetRepo = initSnapshotRepo();
  337. try
  338. {
  339. RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
  340. //repositoriesService.scanRepositoryDirectoriesNow( SNAPSHOT_REPO_ID );
  341. BrowseService browseService = getBrowseService( authorizationHeader, false );
  342. List<Artifact> artifacts =
  343. browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
  344. "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
  345. log.info( "artifacts: {}", artifacts );
  346. assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 10 );
  347. Path artifactFile = targetRepo.resolve(
  348. "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar" );
  349. Path artifactFilemd5 = targetRepo.resolve(
  350. "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.jar.md5" );
  351. Path artifactFilepom = targetRepo.resolve(
  352. "org/apache/archiva/redback/components/spring-quartz/2.0-SNAPSHOT/spring-quartz-2.0-20120618.214127-1.pom" );
  353. assertTrue( Files.exists(artifactFile) );
  354. assertTrue( Files.exists(artifactFilemd5) );
  355. assertTrue( Files.exists(artifactFilepom ));
  356. // we delete only one snapshot
  357. Artifact artifact =
  358. new Artifact( "org.apache.archiva.redback.components", "spring-quartz", "2.0-20120618.214127-1" );
  359. artifact.setPackaging( "jar" );
  360. artifact.setRepositoryId( SNAPSHOT_REPO_ID );
  361. artifact.setContext( SNAPSHOT_REPO_ID );
  362. repositoriesService.deleteArtifact( artifact );
  363. artifacts =
  364. browseService.getArtifactDownloadInfos( "org.apache.archiva.redback.components", "spring-quartz",
  365. "2.0-SNAPSHOT", SNAPSHOT_REPO_ID );
  366. log.info( "artifacts: {}", artifacts );
  367. assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 8 );
  368. assertFalse( Files.exists(artifactFile) );
  369. assertFalse( Files.exists(artifactFilemd5 ));
  370. assertFalse( Files.exists(artifactFilepom ));
  371. }
  372. catch ( Exception e )
  373. {
  374. log.error( e.getMessage(), e );
  375. throw e;
  376. }
  377. finally
  378. {
  379. cleanSnapshotRepo();
  380. }
  381. }
  382. protected Path initSnapshotRepo()
  383. throws Exception
  384. {
  385. Path targetRepo = getBasedir().resolve( "target/repositories/repo-with-snapshots" );
  386. if ( Files.exists(targetRepo) )
  387. {
  388. org.apache.archiva.common.utils.FileUtils.deleteDirectory( targetRepo );
  389. }
  390. assertFalse( Files.exists(targetRepo) );
  391. FileUtils.copyDirectoryToDirectory( getBasedir().resolve( "src/test/repo-with-snapshots" ).toFile(),
  392. targetRepo.getParent().toFile() );
  393. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
  394. {
  395. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
  396. assertNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
  397. }
  398. ManagedRepository managedRepository = getTestManagedRepository( SNAPSHOT_REPO_ID, "repo-with-snapshots" );
  399. /*managedRepository.setId( SNAPSHOT_REPO_ID );
  400. managedRepository.setLocation( );
  401. managedRepository.setCronExpression( "* * * * * ?" );*/
  402. getManagedRepositoriesService( authorizationHeader ).addManagedRepository( managedRepository );
  403. assertNotNull( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
  404. return targetRepo;
  405. }
  406. protected void cleanSnapshotRepo()
  407. throws Exception
  408. {
  409. if ( getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) != null )
  410. {
  411. try
  412. {
  413. getManagedRepositoriesService( authorizationHeader ).deleteManagedRepository( SNAPSHOT_REPO_ID, true );
  414. assertNull(
  415. getManagedRepositoriesService( authorizationHeader ).getManagedRepository( SNAPSHOT_REPO_ID ) );
  416. }
  417. catch ( Exception e )
  418. {
  419. log.warn( "skip issue while cleaning test repository: this can cause test failure", e );
  420. }
  421. }
  422. }
  423. protected ManagedRepository getTestManagedRepository( String id, String path )
  424. {
  425. String location = getBasedir().resolve("target/repositories/" + path ).toAbsolutePath().toString();
  426. return new ManagedRepository( Locale.getDefault(), id, id, location, "default", true, true, true, "2 * * * * ?", null, false, 80, 80,
  427. true, false );
  428. }
  429. @Override
  430. protected ManagedRepository getTestManagedRepository()
  431. {
  432. return getTestManagedRepository( "TEST", "test-repo" );
  433. }
  434. static final String SNAPSHOT_REPO_ID = "snapshot-repo";
  435. }