]> source.dussan.org Git - archiva.git/blob
3a5601f78950b1829ec2af09a3ccdee01aecaa70
[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             Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
96                 .when( )
97                 .body( jsonAsMap )
98                 .post( "" )
99                 .prettyPeek()
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             Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
130                 .when( )
131                 .body( jsonAsMap )
132                 .post( "" )
133                 .then( ).statusCode( 201 ).extract( ).response( );
134             assertNotNull( response );
135             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
136                 .when( )
137                 .redirects().follow( false )
138                 .body( jsonAsMap )
139                 .post( "" )
140                 .prettyPeek()
141                 .then( ).statusCode( 303 )
142                 .assertThat()
143                 .header( "Location", endsWith("group_001") ).extract( ).response( );
144         } finally
145         {
146             given( ).spec( getRequestSpec( token ) ).contentType( JSON )
147                 .when( )
148                 .delete( "group_001" )
149                 .then( ).statusCode( 200 );
150         }
151     }
152
153
154     @Test
155     void testAddMultipleGroups( )
156     {
157         String token = getAdminToken( );
158         List<String> groups = new ArrayList<>( );
159         try
160         {
161             for ( int i=0; i<10; i++)
162             {
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 )
168                     .when( )
169                     .body( jsonAsMap )
170                     .post( "" )
171                     .then( ).statusCode( 201 ).extract( ).response( );
172                 assertNotNull( response );
173                 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
174                 assertNotNull( result );
175             }
176             Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
177                 .when( )
178                 .get( "" )
179                 .then( ).statusCode( 200 ).extract( ).response( );
180             assertNotNull( response );
181             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
182             assertEquals( 10, resultList.getPagination( ).getTotalCount( ) );
183         } finally
184         {
185             for (String groupName : groups)
186             {
187                 given( ).spec( getRequestSpec( token ) ).contentType( JSON )
188                     .when( )
189                     .delete( groupName )
190                     .then( ).statusCode( 200 );
191             }
192         }
193     }
194
195     @Test
196     void testRemoveRepositoryGroup( )
197     {
198         String token = getAdminToken( );
199         List<String> groups = new ArrayList<>( );
200         try
201         {
202             for ( int i=0; i<10; i++)
203             {
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 )
209                     .when( )
210                     .body( jsonAsMap )
211                     .post( "" )
212                     .then( ).statusCode( 201 ).extract( ).response( );
213                 assertNotNull( response );
214                 RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
215                 assertNotNull( result );
216             }
217             Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
218                 .when( )
219                 .delete( "group_001" )
220                 .then( ).statusCode( 200 ).extract( ).response( );
221             assertNotNull( response );
222
223             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
224                 .when( )
225                 .get( "" )
226                 .then( ).statusCode( 200 ).extract( ).response( );
227             assertNotNull( response );
228             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
229             assertEquals( 9, resultList.getPagination( ).getTotalCount( ) );
230
231
232             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
233                 .when( )
234                 .delete( "group_005" )
235                 .then( ).statusCode( 200 ).extract( ).response( );
236             assertNotNull( response );
237
238             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
239                 .when( )
240                 .get( "" )
241                 .then( ).statusCode( 200 ).extract( ).response( );
242             assertNotNull( response );
243             resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
244             assertEquals( 8, resultList.getPagination( ).getTotalCount( ) );
245
246         } finally
247         {
248             for (String groupName : groups)
249             {
250                 if (!("group_001".equals(groupName) || "group_005".equals(groupName) ) )
251                 {
252                     given( ).spec( getRequestSpec( token ) ).contentType( JSON )
253                         .when( )
254                         .delete( groupName )
255                         .then( ).statusCode( 200 );
256                 }
257             }
258         }
259     }
260
261
262     @Test
263     void testAddRepositoryToGroup( )
264     {
265         String token = getAdminToken( );
266         try
267         {
268             Map<String, Object> jsonAsMap = new HashMap<>( );
269             jsonAsMap.put( "id", "group_001" );
270             Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
271                 .when( )
272                 .body( jsonAsMap )
273                 .post( "" )
274                 .prettyPeek()
275                 .then( ).statusCode( 201 ).extract( ).response( );
276             assertNotNull( response );
277             RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
278             assertNotNull( result );
279
280             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
281                 .when( )
282                 .get( "" )
283                 .then( ).statusCode( 200 ).extract( ).response( );
284             assertNotNull( response );
285             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
286             assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
287
288             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
289                 .when( )
290                 .body( jsonAsMap )
291                 .put( "group_001/repositories/internal" )
292                 .prettyPeek()
293                 .then( ).statusCode( 200 ).extract( ).response( );
294
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" ) );
300
301         } finally
302         {
303             given( ).spec( getRequestSpec( token ) ).contentType( JSON )
304                 .when( )
305                 .delete( "group_001" )
306                 .then( ).statusCode( 200 );
307         }
308     }
309
310     @Test
311     void testAddRepositoryToGroupIdempotency( )
312     {
313         String token = getAdminToken( );
314         try
315         {
316             Map<String, Object> jsonAsMap = new HashMap<>( );
317             jsonAsMap.put( "id", "group_001" );
318             Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
319                 .when( )
320                 .body( jsonAsMap )
321                 .post( "" )
322                 .prettyPeek()
323                 .then( ).statusCode( 201 ).extract( ).response( );
324             assertNotNull( response );
325             RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
326             assertNotNull( result );
327
328             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
329                 .when( )
330                 .get( "" )
331                 .then( ).statusCode( 200 ).extract( ).response( );
332             assertNotNull( response );
333             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
334             assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
335
336             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
337                 .when( )
338                 .body( jsonAsMap )
339                 .put( "group_001/repositories/internal" )
340                 .prettyPeek()
341                 .then( ).statusCode( 200 ).extract( ).response( );
342
343             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
344                 .when( )
345                 .body( jsonAsMap )
346                 .put( "group_001/repositories/internal" )
347                 .prettyPeek()
348                 .then( ).statusCode( 200 ).extract( ).response( );
349
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" ) );
355
356         } finally
357         {
358             given( ).spec( getRequestSpec( token ) ).contentType( JSON )
359                 .when( )
360                 .delete( "group_001" )
361                 .then( ).statusCode( 200 );
362         }
363     }
364
365
366     @Test
367     void testRemoveRepositoryFromGroup( )
368     {
369         String token = getAdminToken( );
370         try
371         {
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 )
376                 .when( )
377                 .body( jsonAsMap )
378                 .post( "" )
379                 .prettyPeek()
380                 .then( ).statusCode( 201 ).extract( ).response( );
381             assertNotNull( response );
382             RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
383             assertNotNull( result );
384
385             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
386                 .when( )
387                 .get( "" )
388                 .then( ).statusCode( 200 ).extract( ).response( );
389             assertNotNull( response );
390             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
391             assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
392
393             assertNotNull( result.getRepositories( ) );
394             assertEquals( 1, result.getRepositories( ).size( ) );
395             assertTrue( result.getRepositories( ).contains( "internal" ) );
396
397             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
398                 .when( )
399                 .body( jsonAsMap )
400                 .delete( "group_001/repositories/internal" )
401                 .prettyPeek()
402                 .then( ).statusCode( 200 ).extract( ).response( );
403
404             assertNotNull( response );
405             result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
406             assertNotNull( result );
407             assertEquals( 0, result.getRepositories( ).size( ) );
408
409         } finally
410         {
411             given( ).spec( getRequestSpec( token ) ).contentType( JSON )
412                 .when( )
413                 .delete( "group_001" )
414                 .then( ).statusCode( 200 );
415         }
416     }
417
418     @Test
419     void testRemoveRepositoryFromGroup404( )
420     {
421         String token = getAdminToken( );
422         try
423         {
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 )
428                 .when( )
429                 .body( jsonAsMap )
430                 .post( "" )
431                 .prettyPeek()
432                 .then( ).statusCode( 201 ).extract( ).response( );
433             assertNotNull( response );
434             RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
435             assertNotNull( result );
436
437             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
438                 .when( )
439                 .get( "" )
440                 .then( ).statusCode( 200 ).extract( ).response( );
441             assertNotNull( response );
442             PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
443             assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
444
445             assertNotNull( result.getRepositories( ) );
446             assertEquals( 1, result.getRepositories( ).size( ) );
447             assertTrue( result.getRepositories( ).contains( "internal" ) );
448
449             response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
450                 .when( )
451                 .body( jsonAsMap )
452                 .delete( "group_001/repositories/internalxx" )
453                 .prettyPeek()
454                 .then( ).statusCode( 404 ).extract( ).response( );
455
456             assertNotNull( response );
457             ArchivaRestError error = response.getBody( ).jsonPath( ).getObject( "", ArchivaRestError.class );
458             assertNotNull( error );
459         } finally
460         {
461             given( ).spec( getRequestSpec( token ) ).contentType( JSON )
462                 .when( )
463                 .delete( "group_001" )
464                 .then( ).statusCode( 200 );
465         }
466     }
467
468 }