diff options
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 f9d066e46b..80a685b092 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); @@ -75,6 +75,11 @@ public class CpServiceImpl implements CpService { } @Override + public void deleteJsonById(int jsonObjectId) { + dataPersistencyService.deleteJsonById(jsonObjectId);; + } + + @Override public final void storeSchemaContext(final SchemaContext schemaContext) { for (final Module module : schemaContext.getModules()) { modelPersistencyService.storeModule(module.getName(), module.toString(), module.getRevision().toString()); 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 |