Abandoned Review because of git issues;
authorniamhcore <niamh.core@est.tech>
Thu, 8 Oct 2020 13:19:23 +0000 (14:19 +0100)
committerniamhcore <niamh.core@est.tech>
Thu, 8 Oct 2020 13:19:23 +0000 (14:19 +0100)
https://gerrit.nordix.org/#/c/onap/ccsdk/features/+/6170/

Change-Id: I63d0d460e120b12851e0b3b460639ee4c34c4e0c

cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java
cps/cps-rest/src/main/resources/application.yml
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 a64cd6a..2cac690 100644 (file)
@@ -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);
index bc72669..a8a4690 100644 (file)
@@ -1,20 +1,24 @@
 server:\r
-    port: 8080\r
+  port: 8080\r
 \r
 spring:\r
-    main:\r
-        banner-mode: "off"\r
-    # for POC only, later this should move to cpi-ri module\r
-    jpa:\r
-        hibernate:\r
-        ddl-auto: create\r
-    datasource:\r
-        url: jdbc:mariadb://${DB_HOST}:3306/${DB_NAME}\r
-        username: ${USERNAME}\r
-        password: ${PWD}\r
-        driverClassName: org.mariadb.jdbc.Driver\r
+  main:\r
+    banner-mode: "off"\r
+  # for POC only, later this should move to cpi-ri module\r
+  jpa:\r
+    hibernate:\r
+    ddl-auto: create\r
+    open-in-view: false\r
+    properties:\r
+      hibernate:\r
+        enable_lazy_load_no_trans: true\r
+  datasource:\r
+    url: jdbc:mariadb://${DB_HOST}:3306/${DB_NAME}\r
+    username: ${USERNAME}\r
+    password: ${PWD}\r
+    driverClassName: org.mariadb.jdbc.Driver\r
 \r
 logging:\r
-    level:\r
-        org:\r
-            springframework: INFO
\ No newline at end of file
+  level:\r
+    org:\r
+      springframework: INFO
\ No newline at end of file
index 93746e0..44d38f9 100644 (file)
@@ -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();
+    }
 }
index cdd1c4d..2a8b216 100644 (file)
@@ -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);
 }
index 001a474..4113fbc 100644 (file)
@@ -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()) {
index 4dcd31d..11f9ed0 100644 (file)
@@ -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);
 }
index 67bbab3..f112fa2 100644 (file)
@@ -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)
+    }
 }