From 1d8e556c8cea9c0c8b78381e1e4ce3c36b965dc9 Mon Sep 17 00:00:00 2001 From: niamhcore Date: Thu, 8 Oct 2020 14:19:23 +0100 Subject: [PATCH] Abandoned Review because of git issues; https://gerrit.nordix.org/#/c/onap/ccsdk/features/+/6170/ Change-Id: I63d0d460e120b12851e0b3b460639ee4c34c4e0c --- .../onap/cps/rest/controller/RestController.java | 21 +++++++++++++ cps/cps-rest/src/main/resources/application.yml | 34 ++++++++++++---------- .../cps/spi/impl/DataPersistencyServiceImpl.java | 12 +++++++- .../src/main/java/org/onap/cps/api/CpService.java | 8 +++++ .../java/org/onap/cps/api/impl/CpServiceImpl.java | 6 +++- .../org/onap/cps/spi/DataPersistencyService.java | 8 +++++ .../org/onap/cps/api/impl/CpServiceImplSpec.groovy | 17 +++++++++++ 7 files changed, 89 insertions(+), 17 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 a64cd6a04..2cac690b1 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 @@ -23,9 +23,12 @@ import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; import java.io.File; import java.io.IOException; +import javax.persistence.PersistenceException; import javax.ws.rs.Consumes; +import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -89,6 +92,24 @@ public class RestController { } } + /** + * Read a JSON Object using the object identifier. + * + * @param jsonObjectId the JSON object identifier. + * @return a HTTP response. + */ + @GET + @Path("json-object/{id}") + public final Response getJsonObjectById(@PathParam("id") int jsonObjectId) { + try { + return Response.status(Status.OK).entity(cpService.getJsonById(jsonObjectId)).build(); + } catch (PersistenceException e) { + return Response.status(Status.NOT_FOUND).entity(e.getMessage()).build(); + } catch (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-rest/src/main/resources/application.yml b/cps/cps-rest/src/main/resources/application.yml index bc726694c..a8a4690f6 100644 --- a/cps/cps-rest/src/main/resources/application.yml +++ b/cps/cps-rest/src/main/resources/application.yml @@ -1,20 +1,24 @@ server: - port: 8080 + port: 8080 spring: - main: - banner-mode: "off" - # for POC only, later this should move to cpi-ri module - jpa: - hibernate: - ddl-auto: create - datasource: - url: jdbc:mariadb://${DB_HOST}:3306/${DB_NAME} - username: ${USERNAME} - password: ${PWD} - driverClassName: org.mariadb.jdbc.Driver + main: + banner-mode: "off" + # for POC only, later this should move to cpi-ri module + jpa: + hibernate: + ddl-auto: create + open-in-view: false + properties: + hibernate: + enable_lazy_load_no_trans: true + datasource: + url: jdbc:mariadb://${DB_HOST}:3306/${DB_NAME} + username: ${USERNAME} + password: ${PWD} + driverClassName: org.mariadb.jdbc.Driver logging: - level: - org: - springframework: INFO \ No newline at end of file + level: + org: + springframework: INFO \ No newline at end of file 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 93746e06f..44d38f91d 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 @@ -36,11 +36,21 @@ public class DataPersistencyServiceImpl implements DataPersistencyService { * Method to store a JSON data structure in the database. * * @param jsonStructure the JSON data structure. - * @return + * @return the entity identifier. */ 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. + */ + public final String getJsonById(final int jsonStructureId) { + return dataRepository.getOne(jsonStructureId).getJsonStructure(); + } } 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 cdd1c4dca..2a8b2165b 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 @@ -59,4 +59,12 @@ public interface CpService { * @return entity ID. */ Integer storeJsonStructure(final String jsonStructure); + + /** + * Read a JSON Object using the object identifier. + * + * @param jsonObjectId the JSON object identifier. + * @return the JSON structure. + */ + String getJsonById(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 001a47438..4113fbc64 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 @@ -31,7 +31,6 @@ import org.onap.cps.utils.YangUtils; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.parser.api.YangParserException; -import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -70,6 +69,11 @@ public class CpServiceImpl implements CpService { return dataPersistencyService.storeJsonStructure(jsonStructure); } + @Override + public final String getJsonById(final int jsonObjectId) { + return dataPersistencyService.getJsonById(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 4dcd31d95..11f9ed0db 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 @@ -31,4 +31,12 @@ public interface DataPersistencyService { * @return jsonEntityID the ID of the JSON entity. */ Integer storeJsonStructure(final String jsonStructure); + + /** + * Get the JSON structure from the database using the entity identifier. + * + * @param jsonStructureId the json entity identifier. + * @return a JSON Structure. + */ + String getJsonById(int jsonStructureId); } 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 67bbab3c6..f112fa210 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 @@ -79,4 +79,21 @@ class CpServiceImplSpec extends Specification { expect: 'No exception to be thrown when a valid model (schema) is stored' objectUnderTest.storeSchemaContext(Stub(SchemaContext.class)) } + + def 'Read a JSON object with a valid identifier'(){ + given: 'that the data persistence service returns a JSON structure for identifier 1' + mockDataPersistencyService.getJsonById(1) >> '{name : hello}' + expect: 'that the same JSON structure is returned by CPS' + objectUnderTest.getJsonById(1) == '{name : hello}' + } + + def 'Read a JSON object with an identifier that does not exist'(){ + given: 'that the data persistence service throws an exception' + def exceptionThrownByPersistenceService = new IllegalStateException() + mockDataPersistencyService.getJsonById(_) >> {throw exceptionThrownByPersistenceService} + when: 'we try to get the JSON structure' + objectUnderTest.getJsonById(1); + then: 'the same exception is thrown by CPS' + thrown(IllegalStateException) + } } -- 2.16.6