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 jsonAsMap.put( "name", "group_001" );
96 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 jsonAsMap.put( "name", "group_001" );
130 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
135 .then( ).statusCode( 201 ).extract( ).response( );
136 assertNotNull( response );
137 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
139 .redirects().follow( false )
143 .then( ).statusCode( 303 )
145 .header( "Location", endsWith("group_001") ).extract( ).response( );
148 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
150 .delete( "group_001" );
156 void testAddMultipleGroups( )
158 String token = getAdminToken( );
159 List<String> groups = new ArrayList<>( );
162 for ( int i=0; i<10; i++)
164 String groupName = String.format( "group_%03d", i );
165 groups.add( groupName );
166 Map<String, Object> jsonAsMap = new HashMap<>( );
167 jsonAsMap.put( "id", groupName );
168 jsonAsMap.put( "name", groupName );
169 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
173 .then( ).statusCode( 201 ).extract( ).response( );
174 assertNotNull( response );
175 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
176 assertNotNull( result );
178 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
181 .then( ).statusCode( 200 ).extract( ).response( );
182 assertNotNull( response );
183 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
184 assertEquals( 10, resultList.getPagination( ).getTotalCount( ) );
187 for (String groupName : groups)
189 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
191 .delete( groupName );
197 void testRemoveRepositoryGroup( )
199 String token = getAdminToken( );
200 List<String> groups = new ArrayList<>( );
203 for ( int i=0; i<10; i++)
205 String groupName = String.format( "group_%03d", i );
206 groups.add( groupName );
207 Map<String, Object> jsonAsMap = new HashMap<>( );
208 jsonAsMap.put( "id", groupName );
209 jsonAsMap.put( "name", groupName );
210 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
214 .then( ).statusCode( 201 ).extract( ).response( );
215 assertNotNull( response );
216 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
217 assertNotNull( result );
219 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
221 .delete( "group_001" )
222 .then( ).statusCode( 200 ).extract( ).response( );
223 assertNotNull( response );
225 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
228 .then( ).statusCode( 200 ).extract( ).response( );
229 assertNotNull( response );
230 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
231 assertEquals( 9, resultList.getPagination( ).getTotalCount( ) );
234 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
236 .delete( "group_005" )
237 .then( ).statusCode( 200 ).extract( ).response( );
238 assertNotNull( response );
240 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
243 .then( ).statusCode( 200 ).extract( ).response( );
244 assertNotNull( response );
245 resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
246 assertEquals( 8, resultList.getPagination( ).getTotalCount( ) );
250 for (String groupName : groups)
252 if (!("group_001".equals(groupName) || "group_005".equals(groupName) ) )
254 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
256 .delete( groupName );
264 void testAddRepositoryToGroup( )
266 String token = getAdminToken( );
269 Map<String, Object> jsonAsMap = new HashMap<>( );
270 jsonAsMap.put( "id", "group_001" );
271 jsonAsMap.put( "name", "group_001" );
272 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
277 .then( ).statusCode( 201 ).extract( ).response( );
278 assertNotNull( response );
279 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
280 assertNotNull( result );
282 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
285 .then( ).statusCode( 200 ).extract( ).response( );
286 assertNotNull( response );
287 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
288 assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
290 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
293 .put( "group_001/repositories/internal" )
295 .then( ).statusCode( 200 ).extract( ).response( );
297 assertNotNull( response );
298 result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
299 assertNotNull( result );
300 assertEquals( 1, result.getRepositories( ).size( ) );
301 assertTrue( result.getRepositories( ).contains( "internal" ) );
305 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
307 .delete( "group_001" );
312 void testAddRepositoryToGroupIdempotency( )
314 String token = getAdminToken( );
317 Map<String, Object> jsonAsMap = new HashMap<>( );
318 jsonAsMap.put( "id", "group_001" );
319 jsonAsMap.put( "name", "group_001" );
320 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
325 .then( ).statusCode( 201 ).extract( ).response( );
326 assertNotNull( response );
327 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
328 assertNotNull( result );
330 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
333 .then( ).statusCode( 200 ).extract( ).response( );
334 assertNotNull( response );
335 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
336 assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
338 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
341 .put( "group_001/repositories/internal" )
343 .then( ).statusCode( 200 ).extract( ).response( );
345 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
348 .put( "group_001/repositories/internal" )
350 .then( ).statusCode( 200 ).extract( ).response( );
352 assertNotNull( response );
353 result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
354 assertNotNull( result );
355 assertEquals( 1, result.getRepositories( ).size( ) );
356 assertTrue( result.getRepositories( ).contains( "internal" ) );
360 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
362 .delete( "group_001" )
363 .then( ).statusCode( 200 );
369 void testRemoveRepositoryFromGroup( )
371 String token = getAdminToken( );
374 Map<String, Object> jsonAsMap = new HashMap<>( );
375 jsonAsMap.put( "id", "group_001" );
376 jsonAsMap.put( "name", "group_001" );
377 jsonAsMap.put( "repositories", Arrays.asList( "internal" ) );
378 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
383 .then( ).statusCode( 201 ).extract( ).response( );
384 assertNotNull( response );
385 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
386 assertNotNull( result );
388 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
391 .then( ).statusCode( 200 ).extract( ).response( );
392 assertNotNull( response );
393 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
394 assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
396 assertNotNull( result.getRepositories( ) );
397 assertEquals( 1, result.getRepositories( ).size( ) );
398 assertTrue( result.getRepositories( ).contains( "internal" ) );
400 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
403 .delete( "group_001/repositories/internal" )
405 .then( ).statusCode( 200 ).extract( ).response( );
407 assertNotNull( response );
408 result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
409 assertNotNull( result );
410 assertEquals( 0, result.getRepositories( ).size( ) );
414 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
416 .delete( "group_001" )
417 .then( ).statusCode( 200 );
422 void testRemoveRepositoryFromGroup404( )
424 String token = getAdminToken( );
427 Map<String, Object> jsonAsMap = new HashMap<>( );
428 jsonAsMap.put( "id", "group_001" );
429 jsonAsMap.put( "name", "group_001" );
430 jsonAsMap.put( "repositories", Arrays.asList( "internal" ) );
431 Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
436 .then( ).statusCode( 201 ).extract( ).response( );
437 assertNotNull( response );
438 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
439 assertNotNull( result );
441 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
444 .then( ).statusCode( 200 ).extract( ).response( );
445 assertNotNull( response );
446 PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
447 assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
449 assertNotNull( result.getRepositories( ) );
450 assertEquals( 1, result.getRepositories( ).size( ) );
451 assertTrue( result.getRepositories( ).contains( "internal" ) );
453 response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
456 .delete( "group_001/repositories/internalxx" )
458 .then( ).statusCode( 404 ).extract( ).response( );
460 assertNotNull( response );
461 ArchivaRestError error = response.getBody( ).jsonPath( ).getObject( "", ArchivaRestError.class );
462 assertNotNull( error );
465 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
467 .delete( "group_001" );