Delete schema set - REST and service layers 59/116959/5
authorRuslan Kashapov <ruslan.kashapov@pantheon.tech>
Mon, 11 Jan 2021 13:34:10 +0000 (15:34 +0200)
committerRuslan Kashapov <ruslan.kashapov@pantheon.tech>
Tue, 19 Jan 2021 12:08:37 +0000 (14:08 +0200)
Issue-ID: CPS-121
Change-Id: I0fe885c79f98c994a8ac25a59b77b99eee4b3076
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
cps-rest/docs/api/swagger/components.yaml
cps-rest/docs/api/swagger/cpsAdmin.yml
cps-rest/src/main/java/org/onap/cps/rest/controller/AdminRestController.java
cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java
cps-rest/src/test/groovy/org/onap/cps/rest/controller/AdminRestControllerSpec.groovy
cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy
cps-service/src/main/java/org/onap/cps/api/CpsModuleService.java
cps-service/src/main/java/org/onap/cps/api/impl/CpsModuleServiceImpl.java
cps-service/src/test/groovy/org/onap/cps/api/impl/CpsModuleServiceImplSpec.groovy

index ab964a9..3b36b8b 100644 (file)
@@ -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:
index 948c43b..d33c8e5 100644 (file)
@@ -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:
index 4237846..08020ec 100644 (file)
@@ -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.
      *
index 2b09624..2f63663 100644 (file)
@@ -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));
index 0a61c7d..a95d606 100644 (file)
@@ -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') >>
index 99ffbfd..d372d3c 100644 (file)
@@ -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.
index 5f63f97..75c6a78 100644 (file)
@@ -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);
 }
index 04a6fe1..eac28a9 100644 (file)
@@ -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);
+    }
 }
index ebe4fe7..f380d10 100644 (file)
 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
+    }
 }