From 084b9efccf81b64e66ba2049794b7a287ca2fa5c Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Wed, 30 Jun 2021 20:21:12 +0200 Subject: [PATCH] Additional tests for groups and fix for repository remove action --- .../services/v2/RepositoryGroupService.java | 2 +- .../v2/DefaultRepositoryGroupService.java | 10 +- .../v2/NativeRepositoryGroupServiceTest.java | 118 ++++++++++++++++++ 3 files changed, 125 insertions(+), 5 deletions(-) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/v2/RepositoryGroupService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/v2/RepositoryGroupService.java index 1c0b0595e..3da674307 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/v2/RepositoryGroupService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-api/src/main/java/org/apache/archiva/rest/api/services/v2/RepositoryGroupService.java @@ -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 ) ) ), } ) diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/v2/DefaultRepositoryGroupService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/v2/DefaultRepositoryGroupService.java index 146632011..2d73a53f2 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/v2/DefaultRepositoryGroupService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/v2/DefaultRepositoryGroupService.java @@ -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 ); diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/v2/NativeRepositoryGroupServiceTest.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/v2/NativeRepositoryGroupServiceTest.java index 594b79c15..3a5601f78 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/v2/NativeRepositoryGroupServiceTest.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/test/java/org/apache/archiva/rest/services/v2/NativeRepositoryGroupServiceTest.java @@ -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 groups = new ArrayList<>( ); + try + { + for ( int i=0; i<10; i++) + { + String groupName = String.format( "group_%03d", i ); + groups.add( groupName ); + Map 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 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 ); + } + } + } -- 2.39.5