]> source.dussan.org Git - archiva.git/blob
033a4a060a9c10095887563881f2fff2ed2b71d3
[archiva.git] /
1 package org.apache.archiva.rest.services.v2;
2
3 /*
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
11  *
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
18  * under the License.
19  */
20
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;
33
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
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.*;
45
46 /**
47  * @author Martin Stockhammer <martin_s@apache.org>
48  */
49 @TestInstance( TestInstance.Lifecycle.PER_CLASS )
50 @Tag( "rest-native" )
51 @TestMethodOrder( MethodOrderer.Random.class )
52 @DisplayName( "Native REST tests for V2 RepositoryGroupService" )
53 public class NativeRepositoryGroupServiceTest extends AbstractNativeRestServices
54 {
55     @Override
56     protected String getServicePath( )
57     {
58         return "/repository_groups";
59     }
60
61     @BeforeAll
62     void setup( ) throws Exception
63     {
64         super.setupNative( );
65     }
66
67     @AfterAll
68     void destroy( ) throws Exception
69     {
70         super.shutdownNative( );
71     }
72
73     @Test
74     void testGetEmptyList( )
75     {
76         String token = getAdminToken( );
77         Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
78             .when( )
79             .get( "" )
80             .then( ).statusCode( 200 ).extract( ).response( );
81         assertNotNull( response );
82         PagedResult result = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
83         assertEquals( 0, result.getPagination( ).getTotalCount( ) );
84
85     }
86
87     @Test
88     void testAddGroup( )
89     {
90         String token = getAdminToken( );
91         try
92         {
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 )
97                 .when( )
98                 .body( jsonAsMap )
99                 .post( "" )
100                 .then( ).statusCode( 201 ).extract( ).response( );
101             assertNotNull( response );
102             RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
103             assertNotNull( result );
104
105             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
106                 .when( )
107                 .get( "" )
108                 .then( ).statusCode( 200 ).extract( ).response( );
109             assertNotNull( response );
110             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
111             assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
112         } finally
113         {
114             given( ).spec( getRequestSpec( token ) ).contentType( JSON )
115                 .when( )
116                 .delete( "group_001" )
117                 .then( ).statusCode( 200 );
118         }
119     }
120
121     @Test
122     void testAddExistingGroup( )
123     {
124         String token = getAdminToken( );
125         try
126         {
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 )
131                 .when( )
132                 .body( jsonAsMap )
133                 .post( "" )
134                 .prettyPeek()
135                 .then( ).statusCode( 201 ).extract( ).response( );
136             assertNotNull( response );
137             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
138                 .when( )
139                 .redirects().follow( false )
140                 .body( jsonAsMap )
141                 .post( "" )
142                 .prettyPeek()
143                 .then( ).statusCode( 303 )
144                 .assertThat()
145                 .header( "Location", endsWith("group_001") ).extract( ).response( );
146         } finally
147         {
148             given( ).spec( getRequestSpec( token ) ).contentType( JSON )
149                 .when( )
150                 .delete( "group_001" );
151         }
152     }
153
154
155     @Test
156     void testAddMultipleGroups( )
157     {
158         String token = getAdminToken( );
159         List<String> groups = new ArrayList<>( );
160         try
161         {
162             for ( int i=0; i<10; i++)
163             {
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 )
170                     .when( )
171                     .body( jsonAsMap )
172                     .post( "" )
173                     .then( ).statusCode( 201 ).extract( ).response( );
174                 assertNotNull( response );
175                 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
176                 assertNotNull( result );
177             }
178             Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
179                 .when( )
180                 .get( "" )
181                 .then( ).statusCode( 200 ).extract( ).response( );
182             assertNotNull( response );
183             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
184             assertEquals( 10, resultList.getPagination( ).getTotalCount( ) );
185         } finally
186         {
187             for (String groupName : groups)
188             {
189                 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
190                     .when( )
191                     .delete( groupName );
192             }
193         }
194     }
195
196     @Test
197     void testRemoveRepositoryGroup( )
198     {
199         String token = getAdminToken( );
200         List<String> groups = new ArrayList<>( );
201         try
202         {
203             for ( int i=0; i<10; i++)
204             {
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 )
211                     .when( )
212                     .body( jsonAsMap )
213                     .post( "" )
214                     .then( ).statusCode( 201 ).extract( ).response( );
215                 assertNotNull( response );
216                 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
217                 assertNotNull( result );
218             }
219             Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
220                 .when( )
221                 .delete( "group_001" )
222                 .then( ).statusCode( 200 ).extract( ).response( );
223             assertNotNull( response );
224
225             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
226                 .when( )
227                 .get( "" )
228                 .then( ).statusCode( 200 ).extract( ).response( );
229             assertNotNull( response );
230             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
231             assertEquals( 9, resultList.getPagination( ).getTotalCount( ) );
232
233
234             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
235                 .when( )
236                 .delete( "group_005" )
237                 .then( ).statusCode( 200 ).extract( ).response( );
238             assertNotNull( response );
239
240             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
241                 .when( )
242                 .get( "" )
243                 .then( ).statusCode( 200 ).extract( ).response( );
244             assertNotNull( response );
245             resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
246             assertEquals( 8, resultList.getPagination( ).getTotalCount( ) );
247
248         } finally
249         {
250             for (String groupName : groups)
251             {
252                 if (!("group_001".equals(groupName) || "group_005".equals(groupName) ) )
253                 {
254                     given( ).spec( getRequestSpec( token ) ).contentType( JSON )
255                         .when( )
256                         .delete( groupName );
257                 }
258             }
259         }
260     }
261
262
263     @Test
264     void testAddRepositoryToGroup( )
265     {
266         String token = getAdminToken( );
267         try
268         {
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 )
273                 .when( )
274                 .body( jsonAsMap )
275                 .post( "" )
276                 .prettyPeek()
277                 .then( ).statusCode( 201 ).extract( ).response( );
278             assertNotNull( response );
279             RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
280             assertNotNull( result );
281
282             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
283                 .when( )
284                 .get( "" )
285                 .then( ).statusCode( 200 ).extract( ).response( );
286             assertNotNull( response );
287             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
288             assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
289
290             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
291                 .when( )
292                 .body( jsonAsMap )
293                 .put( "group_001/repositories/internal" )
294                 .prettyPeek()
295                 .then( ).statusCode( 200 ).extract( ).response( );
296
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" ) );
302
303         } finally
304         {
305             given( ).spec( getRequestSpec( token ) ).contentType( JSON )
306                 .when( )
307                 .delete( "group_001" );
308         }
309     }
310
311     @Test
312     void testAddRepositoryToGroupIdempotency( )
313     {
314         String token = getAdminToken( );
315         try
316         {
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 )
321                 .when( )
322                 .body( jsonAsMap )
323                 .post( "" )
324                 .prettyPeek()
325                 .then( ).statusCode( 201 ).extract( ).response( );
326             assertNotNull( response );
327             RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
328             assertNotNull( result );
329
330             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
331                 .when( )
332                 .get( "" )
333                 .then( ).statusCode( 200 ).extract( ).response( );
334             assertNotNull( response );
335             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
336             assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
337
338             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
339                 .when( )
340                 .body( jsonAsMap )
341                 .put( "group_001/repositories/internal" )
342                 .prettyPeek()
343                 .then( ).statusCode( 200 ).extract( ).response( );
344
345             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
346                 .when( )
347                 .body( jsonAsMap )
348                 .put( "group_001/repositories/internal" )
349                 .prettyPeek()
350                 .then( ).statusCode( 200 ).extract( ).response( );
351
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" ) );
357
358         } finally
359         {
360             given( ).spec( getRequestSpec( token ) ).contentType( JSON )
361                 .when( )
362                 .delete( "group_001" )
363                 .then( ).statusCode( 200 );
364         }
365     }
366
367
368     @Test
369     void testRemoveRepositoryFromGroup( )
370     {
371         String token = getAdminToken( );
372         try
373         {
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 )
379                 .when( )
380                 .body( jsonAsMap )
381                 .post( "" )
382                 .prettyPeek()
383                 .then( ).statusCode( 201 ).extract( ).response( );
384             assertNotNull( response );
385             RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
386             assertNotNull( result );
387
388             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
389                 .when( )
390                 .get( "" )
391                 .then( ).statusCode( 200 ).extract( ).response( );
392             assertNotNull( response );
393             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
394             assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
395
396             assertNotNull( result.getRepositories( ) );
397             assertEquals( 1, result.getRepositories( ).size( ) );
398             assertTrue( result.getRepositories( ).contains( "internal" ) );
399
400             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
401                 .when( )
402                 .body( jsonAsMap )
403                 .delete( "group_001/repositories/internal" )
404                 .prettyPeek()
405                 .then( ).statusCode( 200 ).extract( ).response( );
406
407             assertNotNull( response );
408             result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
409             assertNotNull( result );
410             assertEquals( 0, result.getRepositories( ).size( ) );
411
412         } finally
413         {
414             given( ).spec( getRequestSpec( token ) ).contentType( JSON )
415                 .when( )
416                 .delete( "group_001" )
417                 .then( ).statusCode( 200 );
418         }
419     }
420
421     @Test
422     void testRemoveRepositoryFromGroup404( )
423     {
424         String token = getAdminToken( );
425         try
426         {
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 )
432                 .when( )
433                 .body( jsonAsMap )
434                 .post( "" )
435                 .prettyPeek()
436                 .then( ).statusCode( 201 ).extract( ).response( );
437             assertNotNull( response );
438             RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
439             assertNotNull( result );
440
441             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
442                 .when( )
443                 .get( "" )
444                 .then( ).statusCode( 200 ).extract( ).response( );
445             assertNotNull( response );
446             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
447             assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
448
449             assertNotNull( result.getRepositories( ) );
450             assertEquals( 1, result.getRepositories( ).size( ) );
451             assertTrue( result.getRepositories( ).contains( "internal" ) );
452
453             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
454                 .when( )
455                 .body( jsonAsMap )
456                 .delete( "group_001/repositories/internalxx" )
457                 .prettyPeek()
458                 .then( ).statusCode( 404 ).extract( ).response( );
459
460             assertNotNull( response );
461             ArchivaRestError error = response.getBody( ).jsonPath( ).getObject( "", ArchivaRestError.class );
462             assertNotNull( error );
463         } finally
464         {
465             given( ).spec( getRequestSpec( token ) ).contentType( JSON )
466                 .when( )
467                 .delete( "group_001" );
468         }
469     }
470
471 }