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;
}
}
+ /**
+ * 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);
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
* 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();
+ }
}
* @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);
}
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;
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()) {
* @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);
}
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)
+ }
}