1 package org.apache.archiva.rest.services.v2;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
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
21 import io.restassured.response.Response;
22 import org.apache.archiva.components.rest.model.PagedResult;
23 import org.apache.archiva.rest.api.model.v2.RepositoryGroup;
24 import org.apache.archiva.rest.api.services.v2.ArchivaRestError;
25 import org.junit.jupiter.api.AfterAll;
26 import org.junit.jupiter.api.BeforeAll;
27 import org.junit.jupiter.api.DisplayName;
28 import org.junit.jupiter.api.MethodOrderer;
29 import org.junit.jupiter.api.Tag;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.TestInstance;
32 import org.junit.jupiter.api.TestMethodOrder;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.List;
40 import static io.restassured.RestAssured.given;
41 import static io.restassured.http.ContentType.JSON;
42 import static org.easymock.EasyMock.contains;
43 import static org.hamcrest.Matchers.endsWith;
44 import static org.junit.jupiter.api.Assertions.*;
47 * @author Martin Stockhammer <martin_s@apache.org>
49 @TestInstance( TestInstance.Lifecycle.PER_CLASS )
51 @TestMethodOrder( MethodOrderer.Random.class )
52 @DisplayName( "Native REST tests for V2 RepositoryGroupService" )
53 public class NativeRepositoryGroupServiceTest extends AbstractNativeRestServices
56 protected String getServicePath( )
58 return "/repository_groups";
62 void setup( ) throws Exception
68 void destroy( ) throws Exception
70 super.shutdownNative( );
74 void testGetEmptyList( )
76 String token = getAdminToken( );
77 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
80 .then( ).statusCode( 200 ).extract( ).response( );
81 assertNotNull( response );
82 PagedResult result = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
83 assertEquals( 0, result.getPagination( ).getTotalCount( ) );
90 String token = getAdminToken( );
93 Map<String, Object> jsonAsMap = new HashMap<>( );
94 jsonAsMap.put( "id", "group_001" );
95 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
100 .then( ).statusCode( 201 ).extract( ).response( );
101 assertNotNull( response );
102 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
103 assertNotNull( result );
105 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
108 .then( ).statusCode( 200 ).extract( ).response( );
109 assertNotNull( response );
110 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
111 assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
114 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
116 .delete( "group_001" )
117 .then( ).statusCode( 200 );
122 void testAddExistingGroup( )
124 String token = getAdminToken( );
127 Map<String, Object> jsonAsMap = new HashMap<>( );
128 jsonAsMap.put( "id", "group_001" );
129 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
133 .then( ).statusCode( 201 ).extract( ).response( );
134 assertNotNull( response );
135 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
137 .redirects().follow( false )
141 .then( ).statusCode( 303 )
143 .header( "Location", endsWith("group_001") ).extract( ).response( );
146 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
148 .delete( "group_001" )
149 .then( ).statusCode( 200 );
155 void testAddMultipleGroups( )
157 String token = getAdminToken( );
158 List<String> groups = new ArrayList<>( );
161 for ( int i=0; i<10; i++)
163 String groupName = String.format( "group_%03d", i );
164 groups.add( groupName );
165 Map<String, Object> jsonAsMap = new HashMap<>( );
166 jsonAsMap.put( "id", groupName );
167 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
171 .then( ).statusCode( 201 ).extract( ).response( );
172 assertNotNull( response );
173 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
174 assertNotNull( result );
176 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
179 .then( ).statusCode( 200 ).extract( ).response( );
180 assertNotNull( response );
181 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
182 assertEquals( 10, resultList.getPagination( ).getTotalCount( ) );
185 for (String groupName : groups)
187 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
190 .then( ).statusCode( 200 );
196 void testRemoveRepositoryGroup( )
198 String token = getAdminToken( );
199 List<String> groups = new ArrayList<>( );
202 for ( int i=0; i<10; i++)
204 String groupName = String.format( "group_%03d", i );
205 groups.add( groupName );
206 Map<String, Object> jsonAsMap = new HashMap<>( );
207 jsonAsMap.put( "id", groupName );
208 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
212 .then( ).statusCode( 201 ).extract( ).response( );
213 assertNotNull( response );
214 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
215 assertNotNull( result );
217 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
219 .delete( "group_001" )
220 .then( ).statusCode( 200 ).extract( ).response( );
221 assertNotNull( response );
223 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
226 .then( ).statusCode( 200 ).extract( ).response( );
227 assertNotNull( response );
228 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
229 assertEquals( 9, resultList.getPagination( ).getTotalCount( ) );
232 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
234 .delete( "group_005" )
235 .then( ).statusCode( 200 ).extract( ).response( );
236 assertNotNull( response );
238 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
241 .then( ).statusCode( 200 ).extract( ).response( );
242 assertNotNull( response );
243 resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
244 assertEquals( 8, resultList.getPagination( ).getTotalCount( ) );
248 for (String groupName : groups)
250 if (!("group_001".equals(groupName) || "group_005".equals(groupName) ) )
252 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
255 .then( ).statusCode( 200 );
263 void testAddRepositoryToGroup( )
265 String token = getAdminToken( );
268 Map<String, Object> jsonAsMap = new HashMap<>( );
269 jsonAsMap.put( "id", "group_001" );
270 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
275 .then( ).statusCode( 201 ).extract( ).response( );
276 assertNotNull( response );
277 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
278 assertNotNull( result );
280 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
283 .then( ).statusCode( 200 ).extract( ).response( );
284 assertNotNull( response );
285 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
286 assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
288 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
291 .put( "group_001/repositories/internal" )
293 .then( ).statusCode( 200 ).extract( ).response( );
295 assertNotNull( response );
296 result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
297 assertNotNull( result );
298 assertEquals( 1, result.getRepositories( ).size( ) );
299 assertTrue( result.getRepositories( ).contains( "internal" ) );
303 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
305 .delete( "group_001" )
306 .then( ).statusCode( 200 );
311 void testAddRepositoryToGroupIdempotency( )
313 String token = getAdminToken( );
316 Map<String, Object> jsonAsMap = new HashMap<>( );
317 jsonAsMap.put( "id", "group_001" );
318 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
323 .then( ).statusCode( 201 ).extract( ).response( );
324 assertNotNull( response );
325 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
326 assertNotNull( result );
328 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
331 .then( ).statusCode( 200 ).extract( ).response( );
332 assertNotNull( response );
333 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
334 assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
336 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
339 .put( "group_001/repositories/internal" )
341 .then( ).statusCode( 200 ).extract( ).response( );
343 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
346 .put( "group_001/repositories/internal" )
348 .then( ).statusCode( 200 ).extract( ).response( );
350 assertNotNull( response );
351 result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
352 assertNotNull( result );
353 assertEquals( 1, result.getRepositories( ).size( ) );
354 assertTrue( result.getRepositories( ).contains( "internal" ) );
358 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
360 .delete( "group_001" )
361 .then( ).statusCode( 200 );
367 void testRemoveRepositoryFromGroup( )
369 String token = getAdminToken( );
372 Map<String, Object> jsonAsMap = new HashMap<>( );
373 jsonAsMap.put( "id", "group_001" );
374 jsonAsMap.put( "repositories", Arrays.asList( "internal" ) );
375 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
380 .then( ).statusCode( 201 ).extract( ).response( );
381 assertNotNull( response );
382 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
383 assertNotNull( result );
385 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
388 .then( ).statusCode( 200 ).extract( ).response( );
389 assertNotNull( response );
390 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
391 assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
393 assertNotNull( result.getRepositories( ) );
394 assertEquals( 1, result.getRepositories( ).size( ) );
395 assertTrue( result.getRepositories( ).contains( "internal" ) );
397 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
400 .delete( "group_001/repositories/internal" )
402 .then( ).statusCode( 200 ).extract( ).response( );
404 assertNotNull( response );
405 result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
406 assertNotNull( result );
407 assertEquals( 0, result.getRepositories( ).size( ) );
411 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
413 .delete( "group_001" )
414 .then( ).statusCode( 200 );
419 void testRemoveRepositoryFromGroup404( )
421 String token = getAdminToken( );
424 Map<String, Object> jsonAsMap = new HashMap<>( );
425 jsonAsMap.put( "id", "group_001" );
426 jsonAsMap.put( "repositories", Arrays.asList( "internal" ) );
427 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
432 .then( ).statusCode( 201 ).extract( ).response( );
433 assertNotNull( response );
434 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
435 assertNotNull( result );
437 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
440 .then( ).statusCode( 200 ).extract( ).response( );
441 assertNotNull( response );
442 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
443 assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
445 assertNotNull( result.getRepositories( ) );
446 assertEquals( 1, result.getRepositories( ).size( ) );
447 assertTrue( result.getRepositories( ).contains( "internal" ) );
449 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
452 .delete( "group_001/repositories/internalxx" )
454 .then( ).statusCode( 404 ).extract( ).response( );
456 assertNotNull( response );
457 ArchivaRestError error = response.getBody( ).jsonPath( ).getObject( "", ArchivaRestError.class );
458 assertNotNull( error );
461 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
463 .delete( "group_001" )
464 .then( ).statusCode( 200 );