From 4dbcd00b188506d76f7b5721191e713ded0ae726 Mon Sep 17 00:00:00 2001 From: Rishi Chail Date: Thu, 15 Oct 2020 08:40:23 +0100 Subject: [PATCH] VSE: Delete a JSON Object Issue-ID: CCSDK-2760 https://jira.onap.org/browse/CCSDK-2760 Signed-off-by: Rishi Chail Change-Id: Ie6b3235c3eb17ce30b00533ea2b8a25a9823ef2c --- .../onap/cps/rest/controller/RestController.java | 21 +++++++++++++++++++++ .../cps/spi/impl/DataPersistencyServiceImpl.java | 15 ++++++++++++++- .../src/main/java/org/onap/cps/api/CpService.java | 7 +++++++ .../java/org/onap/cps/api/impl/CpServiceImpl.java | 7 ++++++- .../org/onap/cps/spi/DataPersistencyService.java | 9 ++++++++- .../org/onap/cps/api/impl/CpServiceImplSpec.groovy | 18 +++++++++++++++++- 6 files changed, 73 insertions(+), 4 deletions(-) diff --git a/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java b/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java index 68d101f3ba..e86d329baa 100644 --- a/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java +++ b/cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java @@ -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); diff --git a/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java b/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java index 44d38f91d9..2b4f1357de 100644 --- a/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java +++ b/cps/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java @@ -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); + } } diff --git a/cps/cps-service/src/main/java/org/onap/cps/api/CpService.java b/cps/cps-service/src/main/java/org/onap/cps/api/CpService.java index 2a8b2165b5..177fc043a9 100644 --- a/cps/cps-service/src/main/java/org/onap/cps/api/CpService.java +++ b/cps/cps-service/src/main/java/org/onap/cps/api/CpService.java @@ -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); } diff --git a/cps/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java b/cps/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java index 4113fbc64f..73138ccc1b 100644 --- a/cps/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java +++ b/cps/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java @@ -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()) { diff --git a/cps/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java b/cps/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java index 11f9ed0db6..ce51f40ac6 100644 --- a/cps/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java +++ b/cps/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java @@ -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 diff --git a/cps/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy b/cps/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy index f112fa2100..a2f9b35437 100644 --- a/cps/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy +++ b/cps/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy @@ -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 -- 2.16.6