summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorniamhcore <niamh.core@est.tech>2020-10-08 14:19:23 +0100
committerniamhcore <niamh.core@est.tech>2020-10-08 14:19:23 +0100
commit1d8e556c8cea9c0c8b78381e1e4ce3c36b965dc9 (patch)
tree406508ec8de9338d6e120c6a7cebb55190288332
parent75a05e296781f9427959355443bbebfc28ac804e (diff)
Abandoned Review because of git issues;
https://gerrit.nordix.org/#/c/onap/ccsdk/features/+/6170/ Change-Id: I63d0d460e120b12851e0b3b460639ee4c34c4e0c
-rw-r--r--cps/cps-rest/src/main/java/org/onap/cps/rest/controller/RestController.java21
-rw-r--r--cps/cps-rest/src/main/resources/application.yml34
-rw-r--r--cps/cps-ri/src/main/java/org/onap/cps/spi/impl/DataPersistencyServiceImpl.java12
-rw-r--r--cps/cps-service/src/main/java/org/onap/cps/api/CpService.java8
-rw-r--r--cps/cps-service/src/main/java/org/onap/cps/api/impl/CpServiceImpl.java6
-rw-r--r--cps/cps-service/src/main/java/org/onap/cps/spi/DataPersistencyService.java8
-rw-r--r--cps/cps-service/src/test/groovy/org/onap/cps/api/impl/CpServiceImplSpec.groovy17
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;
@@ -71,6 +70,11 @@ public class CpServiceImpl implements CpService {
}
@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()) {
modelPersistencyService.storeModule(module.getName(), module.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 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)
+ }
}