diff options
9 files changed, 127 insertions, 8 deletions
diff --git a/cps-rest/docs/api/swagger/components.yaml b/cps-rest/docs/api/swagger/components.yaml index ab964a93a0..3b36b8b2fe 100644 --- a/cps-rest/docs/api/swagger/components.yaml +++ b/cps-rest/docs/api/swagger/components.yaml @@ -88,6 +88,12 @@ components: application/json: schema: $ref: '#/components/schemas/ErrorMessage' + Conflict: + description: Conflict + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorMessage' Ok: description: OK content: diff --git a/cps-rest/docs/api/swagger/cpsAdmin.yml b/cps-rest/docs/api/swagger/cpsAdmin.yml index 948c43b763..d33c8e55ce 100644 --- a/cps-rest/docs/api/swagger/cpsAdmin.yml +++ b/cps-rest/docs/api/swagger/cpsAdmin.yml @@ -83,6 +83,25 @@ schemaSetBySchemaSetName: $ref: 'components.yaml#/components/responses/Forbidden' 404: $ref: 'components.yaml#/components/responses/NotFound' + delete: + tags: + - cps-admin + summary: Delete schema set given a schema set and a dataspace + operationId: deleteSchemaSet + parameters: + - $ref: 'components.yaml#/components/parameters/dataspaceNameInPath' + - $ref: 'components.yaml#/components/parameters/schemaSetNameInPath' + responses: + 204: + $ref: 'components.yaml#/components/responses/NoContent' + 400: + $ref: 'components.yaml#/components/responses/BadRequest' + 401: + $ref: 'components.yaml#/components/responses/Unauthorized' + 403: + $ref: 'components.yaml#/components/responses/Forbidden' + 409: + $ref: 'components.yaml#/components/responses/Conflict' anchorsByDataspace: get: 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 4237846ee7..08020eca2a 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<Void> 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 2b096240bd..2f636630c4 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<Object> handleDataInUseException(final CpsException exception) { + return buildErrorResponse(HttpStatus.CONFLICT, exception.getMessage(), extractDetails(exception)); + } + @ExceptionHandler({CpsException.class}) public static ResponseEntity<Object> 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 0a61c7dd59..a95d606a35 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 99ffbfd05a..d372d3c0b7 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. diff --git a/cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java b/cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java index 5f63f97f1e..75c6a78637 100644 --- a/cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java +++ b/cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java @@ -21,6 +21,8 @@ package org.onap.cps.api; import java.util.Map; import org.checkerframework.checker.nullness.qual.NonNull; +import org.onap.cps.spi.CascadeDeleteAllowed; +import org.onap.cps.spi.exceptions.DataInUseException; import org.onap.cps.spi.model.SchemaSet; /** @@ -47,4 +49,16 @@ public interface CpsModuleService { * @return a SchemaSet */ SchemaSet getSchemaSet(@NonNull String dataspaceName, @NonNull String schemaSetName); + + /** + * Deletes Schema Set. + * + * @param dataspaceName dataspace name + * @param schemaSetName schema set name + * @param cascadeDeleteAllowed indicates the allowance to remove associated anchors and data if exist + * @throws DataInUseException if cascadeDeleteAllowed is set to CASCADE_DELETE_PROHIBITED and there + * is associated anchor record exists in database + */ + void deleteSchemaSet(@NonNull String dataspaceName, @NonNull String schemaSetName, + @NonNull CascadeDeleteAllowed cascadeDeleteAllowed); } diff --git a/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java b/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java index 04a6fe1cee..eac28a9f09 100644 --- a/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java +++ b/cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java @@ -20,7 +20,9 @@ package org.onap.cps.api.impl; import java.util.Map; +import org.checkerframework.checker.nullness.qual.NonNull; import org.onap.cps.api.CpsModuleService; +import org.onap.cps.spi.CascadeDeleteAllowed; import org.onap.cps.spi.CpsModulePersistenceService; import org.onap.cps.spi.model.SchemaSet; import org.onap.cps.yang.YangTextSchemaSourceSet; @@ -55,4 +57,10 @@ public class CpsModuleServiceImpl implements CpsModuleService { .moduleReferences(yangTextSchemaSourceSet.getModuleReferences()) .build(); } + + @Override + public void deleteSchemaSet(final String dataspaceName, final String schemaSetName, + final CascadeDeleteAllowed cascadeDeleteAllowed) { + cpsModulePersistenceService.deleteSchemaSet(dataspaceName, schemaSetName, cascadeDeleteAllowed); + } } diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy index ebe4fe7bce..f380d106c7 100644 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy @@ -21,10 +21,15 @@ package org.onap.cps.api.impl import org.onap.cps.TestUtils +import org.onap.cps.spi.CascadeDeleteAllowed import org.onap.cps.spi.CpsModulePersistenceService; import org.onap.cps.spi.exceptions.ModelValidationException import org.onap.cps.spi.model.ModuleReference import spock.lang.Specification +import spock.lang.Unroll + +import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED +import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_PROHIBITED class CpsModuleServiceImplSpec extends Specification { def mockModuleStoreService = Mock(CpsModulePersistenceService) @@ -52,7 +57,7 @@ class CpsModuleServiceImplSpec extends Specification { thrown(ModelValidationException.class) } - def 'Get schema set by name and namespace.'() { + def 'Get schema set by name and dataspace.'() { given: 'an already present schema set' def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang') mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> yangResourcesNameToContentMap @@ -63,4 +68,16 @@ class CpsModuleServiceImplSpec extends Specification { result.getDataspaceName().contains('someDataspace') result.getModuleReferences().contains(new ModuleReference('stores', 'org:onap:ccsdk:sample', '2020-09-15')) } + + @Unroll + def 'Delete set by name and dataspace with #cascadeDeleteOption.'(){ + when: 'schema set deletion is requested' + objectUnderTest.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption) + then: 'persistence service method is invoked with same parameters' + mockModuleStoreService.deleteSchemaSet(dataspaceName, schemaSetname, cascadeDeleteOption) + where: 'following parameters are used' + dataspaceName | schemaSetname | cascadeDeleteOption + 'dataspace-1' | 'schemas-set-1' | CASCADE_DELETE_ALLOWED + 'dataspace-2' | 'schemas-set-2' | CASCADE_DELETE_PROHIBITED + } } |