]> source.dussan.org Git - archiva.git/commitdiff
Additional tests for groups and fix for repository remove action
authorMartin Stockhammer <martin_s@apache.org>
Wed, 30 Jun 2021 18:21:12 +0000 (20:21 +0200)
committerMartin Stockhammer <martin_s@apache.org>
Wed, 30 Jun 2021 18:21:12 +0000 (20:21 +0200)
archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/v2/RepositoryGroupService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/v2/DefaultRepositoryGroupService.java
archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/v2/NativeRepositoryGroupServiceTest.java

index 1c0b0595ee2dd560f4b7fb97cdd296c89f48c13d..3da674307fde055504102a74e14da322ae4b1474 100644 (file)
@@ -245,7 +245,7 @@ public interface RepositoryGroupService
             ),
             @ApiResponse( responseCode = "403", description = "Authenticated user is not permitted to delete the group",
                 content = @Content( mediaType = APPLICATION_JSON, schema = @Schema( implementation = ArchivaRestError.class ) ) ),
-            @ApiResponse( responseCode = "404", description = "The group with the given id does not exist, or the repository was not part of the group.",
+            @ApiResponse( responseCode = "404", description = "Either the group with the given id does not exist, or the repository was not part of the group.",
                 content = @Content( mediaType = APPLICATION_JSON, schema = @Schema( implementation = ArchivaRestError.class ) ) ),
         }
     )
index 146632011456d954aa91e3ba203c0bab59f34a91..2d73a53f2deee12cb56846bd2efafc7834a34fbc 100644 (file)
@@ -279,15 +279,15 @@ public class DefaultRepositoryGroupService implements RepositoryGroupService
     }
 
     @Override
-    public RepositoryGroup deleteRepositoryFromGroup( String repositoryGroupId, String repositoryId ) throws org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException
+    public RepositoryGroup deleteRepositoryFromGroup( final String repositoryGroupId, final String repositoryId ) throws org.apache.archiva.rest.api.services.v2.ArchivaRestServiceException
     {
         if ( StringUtils.isEmpty( repositoryGroupId ) )
         {
-            throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
+            throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, repositoryGroupId ), 404 );
         }
         if ( StringUtils.isEmpty( repositoryId ) )
         {
-            throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, "" ), 404 );
+            throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, repositoryId ), 404 );
         }
         try
         {
@@ -295,12 +295,14 @@ public class DefaultRepositoryGroupService implements RepositoryGroupService
             if (repositoryGroup==null) {
                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_NOT_FOUND, "" ), 404 );
             }
+            if (repositoryGroup.getRepositories().stream().noneMatch( r -> repositoryId.equals( r.getId() ) )) {
+                throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_NOT_FOUND, repositoryId ), 404 );
+            }
             if (!(repositoryGroup instanceof EditableRepositoryGroup)) {
                 log.error( "This group instance is not editable: {}", repositoryGroupId );
                 throw new ArchivaRestServiceException( ErrorMessage.of( ErrorKeys.REPOSITORY_GROUP_UPDATE_FAILED, "" ), 500 );
             }
             EditableRepositoryGroup editableRepositoryGroup = (EditableRepositoryGroup) repositoryGroup;
-
             editableRepositoryGroup.removeRepository( repositoryId );
             org.apache.archiva.repository.RepositoryGroup newGroup = repositoryRegistry.putRepositoryGroup( editableRepositoryGroup );
             return RepositoryGroup.of( newGroup );
index 594b79c15966c01d62744d3735f886e142c9f0f5..3a5601f78950b1829ec2af09a3ccdee01aecaa70 100644 (file)
@@ -21,6 +21,7 @@ package org.apache.archiva.rest.services.v2;
 import io.restassured.response.Response;
 import org.apache.archiva.components.rest.model.PagedResult;
 import org.apache.archiva.rest.api.model.v2.RepositoryGroup;
+import org.apache.archiva.rest.api.services.v2.ArchivaRestError;
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.DisplayName;
@@ -191,6 +192,73 @@ public class NativeRepositoryGroupServiceTest extends AbstractNativeRestServices
         }
     }
 
+    @Test
+    void testRemoveRepositoryGroup( )
+    {
+        String token = getAdminToken( );
+        List<String> groups = new ArrayList<>( );
+        try
+        {
+            for ( int i=0; i<10; i++)
+            {
+                String groupName = String.format( "group_%03d", i );
+                groups.add( groupName );
+                Map<String, Object> jsonAsMap = new HashMap<>( );
+                jsonAsMap.put( "id", groupName );
+                Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                    .when( )
+                    .body( jsonAsMap )
+                    .post( "" )
+                    .then( ).statusCode( 201 ).extract( ).response( );
+                assertNotNull( response );
+                RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
+                assertNotNull( result );
+            }
+            Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .delete( "group_001" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+
+            response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .get( "" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+            PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
+            assertEquals( 9, resultList.getPagination( ).getTotalCount( ) );
+
+
+            response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .delete( "group_005" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+
+            response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .get( "" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+            resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
+            assertEquals( 8, resultList.getPagination( ).getTotalCount( ) );
+
+        } finally
+        {
+            for (String groupName : groups)
+            {
+                if (!("group_001".equals(groupName) || "group_005".equals(groupName) ) )
+                {
+                    given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                        .when( )
+                        .delete( groupName )
+                        .then( ).statusCode( 200 );
+                }
+            }
+        }
+    }
+
+
     @Test
     void testAddRepositoryToGroup( )
     {
@@ -347,4 +415,54 @@ public class NativeRepositoryGroupServiceTest extends AbstractNativeRestServices
         }
     }
 
+    @Test
+    void testRemoveRepositoryFromGroup404( )
+    {
+        String token = getAdminToken( );
+        try
+        {
+            Map<String, Object> jsonAsMap = new HashMap<>( );
+            jsonAsMap.put( "id", "group_001" );
+            jsonAsMap.put( "repositories", Arrays.asList( "internal" ) );
+            Response response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .body( jsonAsMap )
+                .post( "" )
+                .prettyPeek()
+                .then( ).statusCode( 201 ).extract( ).response( );
+            assertNotNull( response );
+            RepositoryGroup result = response.getBody( ).jsonPath( ).getObject( "", RepositoryGroup.class );
+            assertNotNull( result );
+
+            response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .get( "" )
+                .then( ).statusCode( 200 ).extract( ).response( );
+            assertNotNull( response );
+            PagedResult resultList = response.getBody( ).jsonPath( ).getObject( "", PagedResult.class );
+            assertEquals( 1, resultList.getPagination( ).getTotalCount( ) );
+
+            assertNotNull( result.getRepositories( ) );
+            assertEquals( 1, result.getRepositories( ).size( ) );
+            assertTrue( result.getRepositories( ).contains( "internal" ) );
+
+            response = given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .body( jsonAsMap )
+                .delete( "group_001/repositories/internalxx" )
+                .prettyPeek()
+                .then( ).statusCode( 404 ).extract( ).response( );
+
+            assertNotNull( response );
+            ArchivaRestError error = response.getBody( ).jsonPath( ).getObject( "", ArchivaRestError.class );
+            assertNotNull( error );
+        } finally
+        {
+            given( ).spec( getRequestSpec( token ) ).contentType( JSON )
+                .when( )
+                .delete( "group_001" )
+                .then( ).statusCode( 200 );
+        }
+    }
+
 }