From dce4e316da66ede214977491c712b0a4ab457644 Mon Sep 17 00:00:00 2001 From: Ruslan Kashapov Date: Mon, 11 Jan 2021 15:34:10 +0200 Subject: Delete schema set - REST and service layers Issue-ID: CPS-121 Change-Id: I0fe885c79f98c994a8ac25a59b77b99eee4b3076 Signed-off-by: Ruslan Kashapov --- .../cps/rest/controller/AdminRestController.java | 7 ++++ .../rest/exceptions/CpsRestExceptionHandler.java | 6 ++++ .../rest/controller/AdminRestControllerSpec.groovy | 38 ++++++++++++++++++---- .../exceptions/CpsRestExceptionHandlerSpec.groovy | 18 ++++++++++ 4 files changed, 62 insertions(+), 7 deletions(-) (limited to 'cps-rest/src') diff --git a/cps-rest/src/main/java/org/onap/cps/rest/controller/AdminRestController.java b/cps-rest/src/main/java/org/onap/cps/rest/controller/AdminRestController.java index 4237846ee..08020eca2 100644 --- a/cps-rest/src/main/java/org/onap/cps/rest/controller/AdminRestController.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/controller/AdminRestController.java @@ -21,6 +21,7 @@ package org.onap.cps.rest.controller; import static org.onap.cps.rest.utils.MultipartFileUtil.extractYangResourcesMap; +import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED; import java.util.Collection; import org.modelmapper.ModelMapper; @@ -66,6 +67,12 @@ public class AdminRestController implements CpsAdminApi { return new ResponseEntity<>(schemaSet, HttpStatus.OK); } + @Override + public ResponseEntity deleteSchemaSet(final String dataspaceName, final String schemaSetName) { + cpsModuleService.deleteSchemaSet(dataspaceName, schemaSetName, CASCADE_DELETE_PROHIBITED); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + /** * Create a new anchor. * diff --git a/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java b/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java index 2b096240b..2f636630c 100644 --- a/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java +++ b/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java @@ -26,6 +26,7 @@ import org.onap.cps.rest.controller.DataRestController; import org.onap.cps.rest.model.ErrorMessage; import org.onap.cps.spi.exceptions.CpsAdminException; import org.onap.cps.spi.exceptions.CpsException; +import org.onap.cps.spi.exceptions.DataInUseException; import org.onap.cps.spi.exceptions.DataValidationException; import org.onap.cps.spi.exceptions.ModelValidationException; import org.onap.cps.spi.exceptions.NotFoundInDataspaceException; @@ -62,6 +63,11 @@ public class CpsRestExceptionHandler { return buildErrorResponse(HttpStatus.NOT_FOUND, exception.getMessage(), extractDetails(exception)); } + @ExceptionHandler({DataInUseException.class}) + public static ResponseEntity handleDataInUseException(final CpsException exception) { + return buildErrorResponse(HttpStatus.CONFLICT, exception.getMessage(), extractDetails(exception)); + } + @ExceptionHandler({CpsException.class}) public static ResponseEntity handleAnyOtherCpsExceptions(final CpsException exception) { return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, exception.getMessage(), extractDetails(exception)); diff --git a/cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy b/cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy index 0a61c7dd5..a95d606a3 100644 --- a/cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy +++ b/cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy @@ -23,8 +23,9 @@ package org.onap.cps.rest.controller import org.modelmapper.ModelMapper import org.onap.cps.api.CpsAdminService import org.onap.cps.api.CpsModuleService -import org.onap.cps.spi.model.Anchor import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException +import org.onap.cps.spi.exceptions.SchemaSetInUseException +import org.onap.cps.spi.model.Anchor import org.onap.cps.spi.model.SchemaSet import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired @@ -33,12 +34,14 @@ import org.springframework.http.HttpStatus import org.springframework.http.MediaType import org.springframework.mock.web.MockMultipartFile import org.springframework.test.web.servlet.MockMvc -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders import org.springframework.util.LinkedMultiValueMap import org.springframework.util.MultiValueMap import spock.lang.Specification +import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post @WebMvcTest @@ -105,11 +108,29 @@ class AdminRestControllerSpec extends Specification { response.status == HttpStatus.BAD_REQUEST.value() } + def 'Delete schema set.'() { + when: 'delete schema set endpoint is invoked' + def response = performDeleteRequest(schemaSetEndpoint) + then: 'associated service method is invoked with expected parameters' + 1 * mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED) + and: 'response code indicates success' + response.status == HttpStatus.NO_CONTENT.value() + } + + def 'Delete schema set which is in use.'() { + given: 'the service method throws an exception indicating the schema set is in use' + def thrownException = new SchemaSetInUseException('test-dataspace', 'my_schema_set') + mockCpsModuleService.deleteSchemaSet('test-dataspace', 'my_schema_set', CASCADE_DELETE_PROHIBITED) >> + { throw thrownException } + when: 'delete schema set endpoint is invoked' + def response = performDeleteRequest(schemaSetEndpoint) + then: 'schema set deletion fails with conflict response code' + response.status == HttpStatus.CONFLICT.value() + } + def performCreateDataspaceRequest(String dataspaceName) { return mvc.perform( - MockMvcRequestBuilders - .post('/v1/dataspaces') - .param('dataspace-name', dataspaceName) + post('/v1/dataspaces').param('dataspace-name', dataspaceName) ).andReturn().response } @@ -119,13 +140,16 @@ class AdminRestControllerSpec extends Specification { def performCreateSchemaSetRequest(multipartFile) { return mvc.perform( - MockMvcRequestBuilders - .multipart(schemaSetsEndpoint) + multipart(schemaSetsEndpoint) .file(multipartFile) .param('schema-set-name', 'test-schema-set') ).andReturn().response } + def performDeleteRequest(String uri) { + return mvc.perform(delete(uri)).andReturn().response + } + def 'Get existing schema set'() { given: mockCpsModuleService.getSchemaSet('test-dataspace', 'my_schema_set') >> diff --git a/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy b/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy index 99ffbfd05..d372d3c0b 100644 --- a/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy +++ b/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy @@ -25,10 +25,12 @@ import org.onap.cps.api.CpsAdminService import org.onap.cps.api.CpsModuleService import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException import org.onap.cps.spi.exceptions.CpsException +import org.onap.cps.spi.exceptions.DataInUseException import org.onap.cps.spi.exceptions.DataValidationException import org.onap.cps.spi.exceptions.ModelValidationException import org.onap.cps.spi.exceptions.NotFoundInDataspaceException import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException +import org.onap.cps.spi.exceptions.SchemaSetInUseException import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest @@ -38,6 +40,7 @@ import spock.lang.Specification import spock.lang.Unroll import static org.springframework.http.HttpStatus.BAD_REQUEST +import static org.springframework.http.HttpStatus.CONFLICT import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR import static org.springframework.http.HttpStatus.NOT_FOUND import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get @@ -133,6 +136,21 @@ class CpsRestExceptionHandlerSpec extends Specification { new DataValidationException(errorMessage, errorDetails, null)] } + @Unroll + def 'Delete request with a #exceptionThrown.class.simpleName returns HTTP Status Conflict'() { + + when: 'CPS validation exception is thrown by the service' + setupTestException(exceptionThrown) + def response = performTestRequest() + + then: 'an HTTP Conflict response is returned with correct message and details' + assertTestResponse(response, CONFLICT, exceptionThrown.getMessage(), exceptionThrown.getDetails()) + + where: 'the following exceptions are thrown' + exceptionThrown << [new DataInUseException(dataspaceName, existingObjectName), + new SchemaSetInUseException(dataspaceName, existingObjectName)] + } + /* * NB. The test uses 'get JSON by id' endpoint and associated service method invocation * to test the exception handling. The endpoint chosen is not a subject of test. -- cgit 1.2.3-korg