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.

BrowseServiceTest.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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.maven.model.Artifact;
  22. import org.apache.archiva.metadata.model.ProjectVersionMetadata;
  23. import org.apache.archiva.redback.rest.api.model.Role;
  24. import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
  25. import org.apache.archiva.rest.api.model.ArtifactContentEntry;
  26. import org.apache.archiva.rest.api.model.BrowseResult;
  27. import org.apache.archiva.rest.api.model.BrowseResultEntry;
  28. import org.apache.archiva.rest.api.model.Entry;
  29. import org.apache.archiva.rest.api.model.MetadataAddRequest;
  30. import org.apache.archiva.rest.api.model.VersionsList;
  31. import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
  32. import org.apache.archiva.rest.api.services.BrowseService;
  33. import org.apache.cxf.jaxrs.client.WebClient;
  34. import org.assertj.core.data.MapEntry;
  35. import org.junit.After;
  36. import org.junit.Before;
  37. import org.junit.Test;
  38. import javax.ws.rs.core.MediaType;
  39. import java.io.IOException;
  40. import java.util.ArrayList;
  41. import java.util.Arrays;
  42. import java.util.HashMap;
  43. import java.util.List;
  44. import java.util.Map;
  45. import static org.assertj.core.api.Assertions.assertThat;
  46. /**
  47. * @author Olivier Lamy
  48. */
  49. public class BrowseServiceTest
  50. extends AbstractArchivaRestTest
  51. {
  52. private static final String TEST_REPO_ID = "test-repo";
  53. Map<String, String> toMap( List<Entry> entries )
  54. {
  55. Map<String, String> map = new HashMap<>( entries.size() );
  56. for ( Entry entry : entries )
  57. {
  58. map.put( entry.getKey(), entry.getValue() );
  59. }
  60. return map;
  61. }
  62. @Test
  63. public void metadatagetthenadd()
  64. throws Exception
  65. {
  66. scanRepo( TEST_REPO_ID );
  67. waitForScanToComplete( TEST_REPO_ID );
  68. BrowseService browseService = getBrowseService( authorizationHeader, false );
  69. Map<String, String> metadatas =
  70. toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
  71. assertThat( metadatas ).isNotNull().isEmpty();
  72. browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
  73. metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
  74. assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
  75. }
  76. @Test
  77. public void metadatagetthenaddthendelete()
  78. throws Exception
  79. {
  80. try
  81. {
  82. scanRepo( TEST_REPO_ID );
  83. waitForScanToComplete( TEST_REPO_ID );
  84. BrowseService browseService = getBrowseService( authorizationHeader, false );
  85. Map<String, String> metadatas =
  86. toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
  87. assertThat( metadatas ).isNotNull().isEmpty();
  88. browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
  89. metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
  90. assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "wine", "bordeaux" ) );
  91. browseService.deleteMetadata( "commons-cli", "commons-cli", "1.0", "wine", TEST_REPO_ID );
  92. metadatas = toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
  93. assertThat( metadatas ).isNotNull().isEmpty();
  94. }
  95. catch ( ArchivaRestServiceException e )
  96. {
  97. log.error( e.getMessage(), e );
  98. throw e;
  99. }
  100. }
  101. @Test
  102. public void browserootGroups()
  103. throws Exception
  104. {
  105. BrowseService browseService = getBrowseService( authorizationHeader, false );
  106. BrowseResult browseResult = browseService.getRootGroups( TEST_REPO_ID );
  107. assertThat( browseResult ).isNotNull();
  108. assertThat( browseResult.getBrowseResultEntries() ) //
  109. .isNotNull() //
  110. .isNotEmpty() //
  111. .hasSize( 3 ) //
  112. .contains( new BrowseResultEntry( "commons-cli", false ), //
  113. new BrowseResultEntry( "commons-logging", false ), //
  114. new BrowseResultEntry( "org.apache", false ) );
  115. }
  116. @Test
  117. public void browsegroupId()
  118. throws Exception
  119. {
  120. BrowseService browseService = getBrowseService( authorizationHeader, false );
  121. BrowseResult browseResult = browseService.browseGroupId( "org.apache", TEST_REPO_ID );
  122. assertThat( browseResult ).isNotNull();
  123. assertThat( browseResult.getBrowseResultEntries() ) //
  124. .isNotNull() //
  125. .isNotEmpty() //
  126. .hasSize( 2 ) //
  127. .contains( new BrowseResultEntry( "org.apache.felix", false ), //
  128. new BrowseResultEntry( "org.apache.karaf.features", false ) );
  129. }
  130. @Test
  131. public void listUserRepositories()
  132. throws Exception
  133. {
  134. initSourceTargetRepo();
  135. BrowseService browseService = getBrowseService( authorizationHeader, false );
  136. List<ManagedRepository> browseResult = browseService.getUserRepositories();
  137. assertThat( browseResult )
  138. .isNotNull()
  139. .isNotEmpty()
  140. .hasSize(5);
  141. List<String> repIds = new ArrayList<>();
  142. for(ManagedRepository rep : browseResult) {
  143. repIds.add(rep.getId());
  144. }
  145. assertThat(repIds).contains("internal","snapshots","test-repo","test-copy-target","test-origin-repo");
  146. }
  147. @Test
  148. public void listUserManagableRepositories()
  149. throws Exception
  150. {
  151. initSourceTargetRepo();
  152. // Giving the guest user a manager role
  153. String name = "Repository Manager - internal";
  154. Role role = getRoleManagementService( authorizationHeader ).getRole( name );
  155. role.setUsers( Arrays.asList( getUserService( authorizationHeader ).getUser( "guest" ) ) );
  156. getRoleManagementService( authorizationHeader ).updateRoleUsers( role );
  157. // browseService with guest user
  158. BrowseService browseService = getBrowseService( "", false );
  159. List<ManagedRepository> browseResult = browseService.getUserManagableRepositories();
  160. assertThat( browseResult )
  161. .isNotNull()
  162. .isNotEmpty().hasSize(1);
  163. List<String> repIds = new ArrayList<>();
  164. for(ManagedRepository rep : browseResult) {
  165. repIds.add(rep.getId());
  166. }
  167. assertThat(repIds).contains("internal");
  168. }
  169. @Test
  170. public void browsegroupIdWithReleaseStartNumber()
  171. throws Exception
  172. {
  173. BrowseService browseService = getBrowseService( authorizationHeader, false );
  174. BrowseResult browseResult = browseService.browseGroupId( "commons-logging.commons-logging", TEST_REPO_ID );
  175. log.info( "browseResult: {}", browseResult );
  176. }
  177. @Test
  178. public void versionsList()
  179. throws Exception
  180. {
  181. BrowseService browseService = getBrowseService( authorizationHeader, false );
  182. VersionsList versions =
  183. browseService.getVersionsList( "org.apache.karaf.features", "org.apache.karaf.features.core",
  184. TEST_REPO_ID );
  185. assertThat( versions ).isNotNull();
  186. assertThat( versions.getVersions() ) //
  187. .isNotNull() //
  188. .isNotEmpty() //
  189. .hasSize( 2 ) //
  190. .contains( "2.2.1", "2.2.2" );
  191. }
  192. @Test
  193. public void getProjectVersionMetadata()
  194. throws Exception
  195. {
  196. BrowseService browseService = getBrowseService( authorizationHeader, true );
  197. ProjectVersionMetadata metadata =
  198. browseService.getProjectVersionMetadata( "org.apache.karaf.features", "org.apache.karaf.features.core",
  199. TEST_REPO_ID );
  200. assertThat( metadata ).isNotNull();
  201. }
  202. @Test
  203. public void readArtifactContentEntries()
  204. throws Exception
  205. {
  206. BrowseService browseService = getBrowseService( authorizationHeader, true );
  207. List<ArtifactContentEntry> artifactContentEntries =
  208. browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, null,
  209. TEST_REPO_ID );
  210. log.info( "artifactContentEntries: {}", artifactContentEntries );
  211. assertThat( artifactContentEntries ).isNotNull() //
  212. .isNotEmpty() //
  213. .hasSize( 2 ) //
  214. .contains( new ArtifactContentEntry( "org", false, 0, TEST_REPO_ID ), //
  215. new ArtifactContentEntry( "META-INF", false, 0, TEST_REPO_ID ) );
  216. }
  217. @Test
  218. public void readArtifactContentEntriesRootPath()
  219. throws Exception
  220. {
  221. BrowseService browseService = getBrowseService( authorizationHeader, true );
  222. List<ArtifactContentEntry> artifactContentEntries =
  223. browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null, "org/",
  224. TEST_REPO_ID );
  225. log.info( "artifactContentEntries: {}", artifactContentEntries );
  226. assertThat( artifactContentEntries ).isNotNull() //
  227. .isNotEmpty() //
  228. .hasSize( 1 ) //
  229. .contains( new ArtifactContentEntry( "org/apache", false, 1, TEST_REPO_ID ) );
  230. }
  231. @Test
  232. public void readArtifactContentEntriesFilesAndDirectories()
  233. throws Exception
  234. {
  235. BrowseService browseService = getBrowseService( authorizationHeader, true );
  236. List<ArtifactContentEntry> artifactContentEntries =
  237. browseService.getArtifactContentEntries( "commons-logging", "commons-logging", "1.1", null, null,
  238. "org/apache/commons/logging/", TEST_REPO_ID );
  239. log.info( "artifactContentEntries: {}", artifactContentEntries );
  240. assertThat( artifactContentEntries ).isNotNull().isNotEmpty().hasSize( 10 ).contains(
  241. new ArtifactContentEntry( "org/apache/commons/logging/impl", false, 4, TEST_REPO_ID ),
  242. new ArtifactContentEntry( "org/apache/commons/logging/LogSource.class", true, 4, TEST_REPO_ID ) );
  243. }
  244. @Test
  245. public void getArtifactDownloadInfos()
  246. throws Exception
  247. {
  248. BrowseService browseService = getBrowseService( authorizationHeader, true );
  249. List<Artifact> artifactDownloadInfos =
  250. browseService.getArtifactDownloadInfos( "commons-logging", "commons-logging", "1.1", TEST_REPO_ID );
  251. log.info( "artifactDownloadInfos {}", artifactDownloadInfos );
  252. assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 3 );
  253. }
  254. @Test
  255. public void getArtifactsByMetadata()
  256. throws Exception
  257. {
  258. // START SNIPPET: get-artifacts-by-metadata
  259. BrowseService browseService = getBrowseService( authorizationHeader, true );
  260. List<Artifact> artifactDownloadInfos = browseService.getArtifactsByMetadata( "type", "pom", TEST_REPO_ID );
  261. assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 11 );
  262. // END SNIPPET: get-artifacts-by-metadata
  263. }
  264. @Test
  265. public void getArtifactsByProjectVersionMetadata()
  266. throws Exception
  267. {
  268. // START SNIPPET: get-artifacts-by-project-version-metadata
  269. BrowseService browseService = getBrowseService( authorizationHeader, true );
  270. browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
  271. tryAssert( ( ) -> {
  272. List<Artifact> artifactDownloadInfos =
  273. browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", TEST_REPO_ID );
  274. assertThat( artifactDownloadInfos ).isNotNull( ).isNotEmpty( ).hasSize( 3 );
  275. // END SNIPPET: get-artifacts-by-project-version-metadata
  276. } );
  277. }
  278. @Test
  279. public void getArtifactsByProjectVersionMetadataWithNoRepository()
  280. throws Exception
  281. {
  282. final BrowseService browseService = getBrowseService( authorizationHeader, true );
  283. browseService.addMetadata( "commons-cli", "commons-cli", "1.0", "wine", "bordeaux", TEST_REPO_ID );
  284. tryAssert( ( ) -> {
  285. List<Artifact> artifactDownloadInfos =
  286. null;
  287. try
  288. {
  289. artifactDownloadInfos = browseService.getArtifactsByProjectVersionMetadata( "wine", "bordeaux", null );
  290. }
  291. catch ( ArchivaRestServiceException e )
  292. {
  293. throw new AssertionError( "ArchivaRestServiceException", e );
  294. }
  295. assertThat( artifactDownloadInfos ).isNotNull( ).isNotEmpty( ).hasSize( 3 );
  296. });
  297. }
  298. @Test
  299. public void getArtifactsByProperty()
  300. throws Exception
  301. {
  302. // START SNIPPET: get-artifacts-by-property
  303. BrowseService browseService = getBrowseService( authorizationHeader, true );
  304. tryAssert( ( ) -> {
  305. List<Artifact> artifactDownloadInfos =
  306. browseService.getArtifactsByProperty( "org.name", "The Apache Software Foundation", TEST_REPO_ID );
  307. assertThat( artifactDownloadInfos ).isNotNull( ).isNotEmpty( ).hasSize( 7 );
  308. // END SNIPPET: get-artifacts-by-property
  309. } );
  310. }
  311. @Test
  312. public void searchArtifacts()
  313. throws Exception
  314. {
  315. // START SNIPPET: search-artifacts
  316. BrowseService browseService = getBrowseService( authorizationHeader, true );
  317. tryAssert( ( ) -> {
  318. List<Artifact> artifactDownloadInfos =
  319. browseService.searchArtifacts( "The Apache Software Foundation", TEST_REPO_ID, true );
  320. assertThat( artifactDownloadInfos ).isNotNull( ).isNotEmpty( ).hasSize( 7 );
  321. } );
  322. // END SNIPPET: search-artifacts
  323. }
  324. @Test
  325. public void searchArtifactsByField()
  326. throws Exception
  327. {
  328. // START SNIPPET: search-artifacts-by-field
  329. BrowseService browseService = getBrowseService( authorizationHeader, true );
  330. List<Artifact> artifactDownloadInfos =
  331. browseService.searchArtifacts( "org.name", "The Apache Software Foundation", TEST_REPO_ID, true );
  332. assertThat( artifactDownloadInfos ).isNotNull().isNotEmpty().hasSize( 7 );
  333. // END SNIPPET: search-artifacts-by-field
  334. }
  335. @Test
  336. public void readArtifactContentText()
  337. throws Exception
  338. {
  339. BrowseService browseService = getBrowseService( authorizationHeader, true );
  340. WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
  341. String text =
  342. browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", "sources", null,
  343. "org/apache/commons/logging/LogSource.java",
  344. TEST_REPO_ID ).getContent();
  345. log.debug( "text: {}", text );
  346. assertThat( text ).contains( "package org.apache.commons.logging;" ).contains( "public class LogSource {" );
  347. }
  348. @Test
  349. public void readArtifactContentTextPom()
  350. throws Exception
  351. {
  352. BrowseService browseService = getBrowseService( authorizationHeader, true );
  353. WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
  354. String text =
  355. browseService.getArtifactContentText( "commons-logging", "commons-logging", "1.1", null, "pom", null,
  356. TEST_REPO_ID ).getContent();
  357. log.info( "text: {}", text );
  358. assertThat( text ).contains(
  359. "<url>http://jakarta.apache.org/commons/${pom.artifactId.substring(8)}/</url>" ).contains(
  360. "<subscribe>commons-dev-subscribe@jakarta.apache.org</subscribe>" );
  361. }
  362. @Test
  363. public void artifactsNumber()
  364. throws Exception
  365. {
  366. BrowseService browseService = getBrowseService( authorizationHeader, true );
  367. //WebClient.client( browseService ).accept( MediaType.TEXT_PLAIN );
  368. int number = browseService.getArtifacts( TEST_REPO_ID ).size();
  369. log.info( "getArtifactsNumber: {}", number );
  370. assertTrue( number > 1 );
  371. }
  372. @Test
  373. public void metadatainbatchmode()
  374. throws Exception
  375. {
  376. scanRepo( TEST_REPO_ID );
  377. waitForScanToComplete( TEST_REPO_ID );
  378. BrowseService browseService = getBrowseService( authorizationHeader, false );
  379. Map<String, String> inputMetadata = new HashMap<>( 3 );
  380. inputMetadata.put( "buildNumber", "1" );
  381. inputMetadata.put( "author", "alecharp" );
  382. inputMetadata.put( "jenkins_version", "1.486" );
  383. MetadataAddRequest metadataAddRequest = new MetadataAddRequest();
  384. metadataAddRequest.setGroupId( "commons-cli" );
  385. metadataAddRequest.setArtifactId( "commons-cli" );
  386. metadataAddRequest.setVersion( "1.0" );
  387. metadataAddRequest.setMetadatas( inputMetadata );
  388. browseService.importMetadata( metadataAddRequest, TEST_REPO_ID );
  389. Map<String, String> metadatas =
  390. toMap( browseService.getMetadatas( "commons-cli", "commons-cli", "1.0", TEST_REPO_ID ) );
  391. assertThat( metadatas ).isNotNull().isNotEmpty().contains( MapEntry.entry( "buildNumber", "1" ) ).contains(
  392. MapEntry.entry( "author", "alecharp" ) ).contains( MapEntry.entry( "jenkins_version", "1.486" ) );
  393. }
  394. @Before
  395. public void initialiseTestRepo()
  396. throws RedbackServiceException, ArchivaRestServiceException, IOException, InterruptedException
  397. {
  398. // force guest user creation if not exists
  399. if ( getUserService( authorizationHeader ).getGuestUser() == null )
  400. {
  401. assertNotNull( getUserService( authorizationHeader ).createGuestUser() );
  402. }
  403. createAndIndexRepo( TEST_REPO_ID, getProjectDirectory().resolve( "src/test/repo-with-osgi" ),
  404. null, false );
  405. waitForScanToComplete( TEST_REPO_ID );
  406. }
  407. @After
  408. public void deleteTestRepo()
  409. throws Exception
  410. {
  411. scanRepo( TEST_REPO_ID );
  412. waitForScanToComplete( TEST_REPO_ID );
  413. deleteTestRepo( TEST_REPO_ID );
  414. }
  415. }