VSE: Delete a JSON Object
authorRishi Chail <rishi.chail@est.tech>
Thu, 15 Oct 2020 07:40:23 +0000 (08:40 +0100)
committerRishi Chail <rishi.chail@est.tech>
Fri, 16 Oct 2020 05:13:05 +0000 (06:13 +0100)
Issue-ID: CCSDK-2760
https://jira.onap.org/browse/CCSDK-2760

Signed-off-by: Rishi Chail <rishi.chail@est.tech>
Change-Id: Ie6b3235c3eb17ce30b00533ea2b8a25a9823ef2c

cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java
cps/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java
cps/cps-service/src/main/java/org/onap/cps/api/CpService.java
cps/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java
cps/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java
cps/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy

index 68d101f..e86d329 100644 (file)
@@ -25,6 +25,7 @@ import java.io.File;
 import java.io.IOException;
 import javax.persistence.PersistenceException;
 import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
@@ -38,6 +39,7 @@ import org.onap.cps.api.CpService;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.EmptyResultDataAccessException;
 
 
 
@@ -111,6 +113,25 @@ public class RestController {
         }
     }
 
+    /**
+     * Delete a JSON Object using the object identifier.
+     *
+     * @param jsonObjectId the JSON object identifier.
+     * @return a HTTP response.
+     */
+    @DELETE
+    @Path("json-object/{id}")
+    public final Response deleteJsonObjectById(@PathParam("id") int jsonObjectId) {
+        try {
+            cpService.deleteJsonById(jsonObjectId);
+            return Response.status(Status.OK).entity(Status.OK.toString()).build();
+        } catch (final EmptyResultDataAccessException e) {
+            return Response.status(Status.NOT_FOUND).entity(Status.NOT_FOUND.toString()).build();
+        } catch (final Exception e) {
+            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
+        }
+    }
+
     private static final void validateJsonStructure(final String jsonFile) {
         final Gson gson = new Gson();
         gson.fromJson(jsonFile, Object.class);
index 44d38f9..2b4f135 100644 (file)
@@ -38,19 +38,32 @@ public class DataPersistencyServiceImpl implements DataPersistencyService {
      * @param jsonStructure the JSON data structure.
      * @return the entity identifier.
      */
+    @Override
     public final Integer storeJsonStructure(final String jsonStructure) {
         final JsonDataEntity jsonDataEntity = new JsonDataEntity(jsonStructure);
         dataRepository.save(jsonDataEntity);
         return jsonDataEntity.getId();
     }
 
-    /**
+    /*
      * Return the JSON structure from the database using the object identifier.
      *
      * @param jsonStructureId the JSON object identifier.
+     *
      * @return the JSON structure from the database as a string.
      */
+    @Override
     public final String getJsonById(final int jsonStructureId) {
         return dataRepository.getOne(jsonStructureId).getJsonStructure();
     }
+
+    /**
+     * Delete the JSON structure from the database using the object identifier.
+     *
+     * @param jsonStructureId the JSON object identifier.
+     */
+    @Override
+    public void deleteJsonById(int jsonStructureId) {
+        dataRepository.deleteById(jsonStructureId);
+    }
 }
index 2a8b216..177fc04 100644 (file)
@@ -67,4 +67,11 @@ public interface CpService {
      * @return the JSON structure.
      */
     String getJsonById(final int jsonObjectId);
+
+    /**
+     * Delete a JSON Object using the object identifier.
+     *
+     * @param jsonObjectId the JSON object identifier.
+     */
+    void deleteJsonById(final int jsonObjectId);
 }
index 4113fbc..73138cc 100644 (file)
@@ -51,7 +51,7 @@ public class CpServiceImpl implements CpService {
 
     @Override
     public final SchemaContext parseAndValidateModel(final String yangModelContent) throws IOException,
-            YangParserException {
+        YangParserException {
         final File tempFile = File.createTempFile("yang", ".yang");
         try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) {
             writer.write(yangModelContent);
@@ -74,6 +74,11 @@ public class CpServiceImpl implements CpService {
         return dataPersistencyService.getJsonById(jsonObjectId);
     }
 
+    @Override
+    public void deleteJsonById(int jsonObjectId) {
+        dataPersistencyService.deleteJsonById(jsonObjectId);;
+    }
+
     @Override
     public final void storeSchemaContext(final SchemaContext schemaContext) {
         for (final Module module : schemaContext.getModules()) {
index 11f9ed0..ce51f40 100644 (file)
@@ -39,4 +39,11 @@ public interface DataPersistencyService {
      * @return a JSON Structure.
      */
     String getJsonById(int jsonStructureId);
-}
+
+    /**
+     * Delete the JSON structure from the database using the entity identifier.
+     *
+     * @param jsonStructureId the json entity identifier.
+     */
+    void deleteJsonById(int jsonStructureId);
+}
\ No newline at end of file
index f112fa2..a2f9b35 100644 (file)
@@ -96,4 +96,20 @@ class CpServiceImplSpec extends Specification {
         then: 'the same exception is thrown by CPS'
             thrown(IllegalStateException)
     }
-}
+
+    def 'Delete a JSON object with a valid identifier'(){
+        given: 'that the data persistence service can delete a JSON structure for identifier 1'
+            mockDataPersistencyService.deleteJsonById(1)
+        expect: 'No exception is thrown when we delete a JSON structure with identifier 1'
+            objectUnderTest.deleteJsonById(1)
+    }
+
+    def 'Delete a JSON object with an identifier that does not exist'(){
+        given: 'that the data persistence service throws an exception'
+            mockDataPersistencyService.deleteJsonById(_) >> {throw new IllegalStateException()}
+        when: 'we try to delete a JSON structure'
+            objectUnderTest.deleteJsonById(100);
+        then: 'the same exception is thrown by CPS'
+            thrown(IllegalStateException)
+    }
+}
\ No newline at end of file