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.

NativeRepositoryGroupServiceTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. package org.apache.archiva.rest.services.v2;
  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. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import io.restassured.response.Response;
  20. import org.apache.archiva.components.rest.model.PagedResult;
  21. import org.apache.archiva.rest.api.model.v2.RepositoryGroup;
  22. import org.junit.jupiter.api.AfterAll;
  23. import org.junit.jupiter.api.BeforeAll;
  24. import org.junit.jupiter.api.DisplayName;
  25. import org.junit.jupiter.api.MethodOrderer;
  26. import org.junit.jupiter.api.Tag;
  27. import org.junit.jupiter.api.Test;
  28. import org.junit.jupiter.api.TestInstance;
  29. import org.junit.jupiter.api.TestMethodOrder;
  30. import java.util.ArrayList;
  31. import java.util.Arrays;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. import static io.restassured.RestAssured.given;
  36. import static io.restassured.http.ContentType.JSON;
  37. import static org.easymock.EasyMock.contains;
  38. import static org.hamcrest.Matchers.endsWith;
  39. import static org.junit.jupiter.api.Assertions.*;
  40. /**
  41. * @author Martin Stockhammer <martin_s@apache.org>
  42. */
  43. @TestInstance( TestInstance.Lifecycle.PER_CLASS )
  44. @Tag( "rest-native" )
  45. @TestMethodOrder( MethodOrderer.Random.class )
  46. @DisplayName( "Native REST tests for V2 RepositoryGroupService" )
  47. public class NativeRepositoryGroupServiceTest extends AbstractNativeRestServices
  48. {
  49. @Override
  50. protected String getServicePath( )
  51. {
  52. return "/repository_groups";
  53. }
  54. @BeforeAll
  55. void setup( ) throws Exception
  56. {
  57. super.setupNative( );
  58. }
  59. @AfterAll
  60. void destroy( ) throws Exception
  61. {
  62. super.shutdownNative( );
  63. }
  64. @Test
  65. void testGetEmptyList( )
  66. {
  67. String token = getAdminToken( );
  68. Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  69. .when( )
  70. .get( "" )
  71. .then( ).statusCode( 200 ).extract( ).response( );
  72. assertNotNull( response );
  73. PagedResult result = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
  74. assertEquals( 0, result.getPagination( ).getTotalCount( ) );
  75. }
  76. @Test
  77. void testAddGroup( )
  78. {
  79. String token = getAdminToken( );
  80. try
  81. {
  82. Map<String, Object> jsonAsMap = new HashMap<>( );
  83. jsonAsMap.put( "id", "group_001" );
  84. Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  85. .when( )
  86. .body( jsonAsMap )
  87. .post( "" )
  88. .prettyPeek()
  89. .then( ).statusCode( 201 ).extract( ).response( );
  90. assertNotNull( response );
  91. RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
  92. assertNotNull( result );
  93. response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  94. .when( )
  95. .get( "" )
  96. .then( ).statusCode( 200 ).extract( ).response( );
  97. assertNotNull( response );
  98. PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
  99. assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
  100. } finally
  101. {
  102. given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  103. .when( )
  104. .delete( "group_001" )
  105. .then( ).statusCode( 200 );
  106. }
  107. }
  108. @Test
  109. void testAddExistingGroup( )
  110. {
  111. String token = getAdminToken( );
  112. try
  113. {
  114. Map<String, Object> jsonAsMap = new HashMap<>( );
  115. jsonAsMap.put( "id", "group_001" );
  116. Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  117. .when( )
  118. .body( jsonAsMap )
  119. .post( "" )
  120. .then( ).statusCode( 201 ).extract( ).response( );
  121. assertNotNull( response );
  122. response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  123. .when( )
  124. .redirects().follow( false )
  125. .body( jsonAsMap )
  126. .post( "" )
  127. .prettyPeek()
  128. .then( ).statusCode( 303 )
  129. .assertThat()
  130. .header( "Location", endsWith("group_001") ).extract( ).response( );
  131. } finally
  132. {
  133. given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  134. .when( )
  135. .delete( "group_001" )
  136. .then( ).statusCode( 200 );
  137. }
  138. }
  139. @Test
  140. void testAddMultipleGroups( )
  141. {
  142. String token = getAdminToken( );
  143. List<String> groups = new ArrayList<>( );
  144. try
  145. {
  146. for ( int i=0; i<10; i++)
  147. {
  148. String groupName = String.format( "group_%03d", i );
  149. groups.add( groupName );
  150. Map<String, Object> jsonAsMap = new HashMap<>( );
  151. jsonAsMap.put( "id", groupName );
  152. Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  153. .when( )
  154. .body( jsonAsMap )
  155. .post( "" )
  156. .then( ).statusCode( 201 ).extract( ).response( );
  157. assertNotNull( response );
  158. RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
  159. assertNotNull( result );
  160. }
  161. Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  162. .when( )
  163. .get( "" )
  164. .then( ).statusCode( 200 ).extract( ).response( );
  165. assertNotNull( response );
  166. PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
  167. assertEquals( 10, resultList.getPagination( ).getTotalCount( ) );
  168. } finally
  169. {
  170. for (String groupName : groups)
  171. {
  172. given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  173. .when( )
  174. .delete( groupName )
  175. .then( ).statusCode( 200 );
  176. }
  177. }
  178. }
  179. @Test
  180. void testAddRepositoryToGroup( )
  181. {
  182. String token = getAdminToken( );
  183. try
  184. {
  185. Map<String, Object> jsonAsMap = new HashMap<>( );
  186. jsonAsMap.put( "id", "group_001" );
  187. Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  188. .when( )
  189. .body( jsonAsMap )
  190. .post( "" )
  191. .prettyPeek()
  192. .then( ).statusCode( 201 ).extract( ).response( );
  193. assertNotNull( response );
  194. RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
  195. assertNotNull( result );
  196. response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  197. .when( )
  198. .get( "" )
  199. .then( ).statusCode( 200 ).extract( ).response( );
  200. assertNotNull( response );
  201. PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
  202. assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
  203. response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  204. .when( )
  205. .body( jsonAsMap )
  206. .put( "group_001/repositories/internal" )
  207. .prettyPeek()
  208. .then( ).statusCode( 200 ).extract( ).response( );
  209. assertNotNull( response );
  210. result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
  211. assertNotNull( result );
  212. assertEquals( 1, result.getRepositories( ).size( ) );
  213. assertTrue( result.getRepositories( ).contains( "internal" ) );
  214. } finally
  215. {
  216. given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  217. .when( )
  218. .delete( "group_001" )
  219. .then( ).statusCode( 200 );
  220. }
  221. }
  222. @Test
  223. void testAddRepositoryToGroupIdempotency( )
  224. {
  225. String token = getAdminToken( );
  226. try
  227. {
  228. Map<String, Object> jsonAsMap = new HashMap<>( );
  229. jsonAsMap.put( "id", "group_001" );
  230. Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  231. .when( )
  232. .body( jsonAsMap )
  233. .post( "" )
  234. .prettyPeek()
  235. .then( ).statusCode( 201 ).extract( ).response( );
  236. assertNotNull( response );
  237. RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
  238. assertNotNull( result );
  239. response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  240. .when( )
  241. .get( "" )
  242. .then( ).statusCode( 200 ).extract( ).response( );
  243. assertNotNull( response );
  244. PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
  245. assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
  246. response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  247. .when( )
  248. .body( jsonAsMap )
  249. .put( "group_001/repositories/internal" )
  250. .prettyPeek()
  251. .then( ).statusCode( 200 ).extract( ).response( );
  252. response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  253. .when( )
  254. .body( jsonAsMap )
  255. .put( "group_001/repositories/internal" )
  256. .prettyPeek()
  257. .then( ).statusCode( 200 ).extract( ).response( );
  258. assertNotNull( response );
  259. result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
  260. assertNotNull( result );
  261. assertEquals( 1, result.getRepositories( ).size( ) );
  262. assertTrue( result.getRepositories( ).contains( "internal" ) );
  263. } finally
  264. {
  265. given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  266. .when( )
  267. .delete( "group_001" )
  268. .then( ).statusCode( 200 );
  269. }
  270. }
  271. @Test
  272. void testRemoveRepositoryFromGroup( )
  273. {
  274. String token = getAdminToken( );
  275. try
  276. {
  277. Map<String, Object> jsonAsMap = new HashMap<>( );
  278. jsonAsMap.put( "id", "group_001" );
  279. jsonAsMap.put( "repositories", Arrays.asList( "internal" ) );
  280. Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  281. .when( )
  282. .body( jsonAsMap )
  283. .post( "" )
  284. .prettyPeek()
  285. .then( ).statusCode( 201 ).extract( ).response( );
  286. assertNotNull( response );
  287. RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
  288. assertNotNull( result );
  289. response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  290. .when( )
  291. .get( "" )
  292. .then( ).statusCode( 200 ).extract( ).response( );
  293. assertNotNull( response );
  294. PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
  295. assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
  296. assertNotNull( result.getRepositories( ) );
  297. assertEquals( 1, result.getRepositories( ).size( ) );
  298. assertTrue( result.getRepositories( ).contains( "internal" ) );
  299. response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  300. .when( )
  301. .body( jsonAsMap )
  302. .delete( "group_001/repositories/internal" )
  303. .prettyPeek()
  304. .then( ).statusCode( 200 ).extract( ).response( );
  305. assertNotNull( response );
  306. result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
  307. assertNotNull( result );
  308. assertEquals( 0, result.getRepositories( ).size( ) );
  309. } finally
  310. {
  311. given( ).spec( getRequestSpec( token ) ).contentType( JSON )
  312. .when( )
  313. .delete( "group_001" )
  314. .then( ).statusCode( 200 );
  315. }
  316. }
  317. }