From ff1622c830ce1036c8e2789aace1a5bd3c125df1 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Tue, 18 Sep 2018 13:18:24 +0100 Subject: [PATCH] Added JSON test for Observation. --- backend/pom.xml | 11 ++++++ .../Observations/api/ObservationEntry.java | 29 ++++++++++++++++ .../neviyn/Observations/core/Observation.java | 25 +++++++++----- .../Observations/dao/ObservationDao.java | 4 +-- .../Observations/core/ObservationTest.java | 34 +++++++++++++++++++ .../test/resources/fixtures/Observation.json | 18 ++++++++++ 6 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 backend/src/main/java/uk/co/neviyn/Observations/api/ObservationEntry.java create mode 100644 backend/src/test/java/uk/co/neviyn/Observations/core/ObservationTest.java create mode 100644 backend/src/test/resources/fixtures/Observation.json diff --git a/backend/pom.xml b/backend/pom.xml index ae6bb4f..8246cb7 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -46,6 +46,17 @@ ${dropwizard.version} test + + org.mockito + mockito-core + 2.22.0 + test + + + org.apache.commons + commons-lang3 + 3.8 + com.h2database h2 diff --git a/backend/src/main/java/uk/co/neviyn/Observations/api/ObservationEntry.java b/backend/src/main/java/uk/co/neviyn/Observations/api/ObservationEntry.java new file mode 100644 index 0000000..060c1fc --- /dev/null +++ b/backend/src/main/java/uk/co/neviyn/Observations/api/ObservationEntry.java @@ -0,0 +1,29 @@ +package uk.co.neviyn.Observations.api; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.NonNull; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ObservationEntry { + + @NonNull + @JsonProperty + private String type; + + @NonNull + @JsonProperty + private int rating; + + @NonNull + @JsonProperty + private String strengths; + + @NonNull + @JsonProperty + private String improvements; +} diff --git a/backend/src/main/java/uk/co/neviyn/Observations/core/Observation.java b/backend/src/main/java/uk/co/neviyn/Observations/core/Observation.java index aaf3b30..4fffc4a 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/core/Observation.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/core/Observation.java @@ -1,17 +1,22 @@ package uk.co.neviyn.Observations.core; import com.fasterxml.jackson.annotation.JsonProperty; + +import java.io.Serializable; import java.sql.ResultSet; import java.sql.SQLException; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.NonNull; +import java.util.List; + +import lombok.*; +import org.apache.commons.lang3.SerializationUtils; import org.jdbi.v3.core.mapper.RowMapper; import org.jdbi.v3.core.statement.StatementContext; import org.joda.time.DateTime; +import uk.co.neviyn.Observations.api.ObservationEntry; @Data @NoArgsConstructor +@AllArgsConstructor public class Observation { @NonNull @@ -36,14 +41,14 @@ public class Observation { @NonNull @JsonProperty - private String rawData; + private List rawData; @NonNull @JsonProperty private DateTime date; public Observation(int siteId, String observed, int monitoring, int control, int conservatism, int teamwork, - int knowledge, String rawData) { + int knowledge, List rawData) { this.siteId = siteId; this.observed = observed; this.monitoring = monitoring; @@ -55,7 +60,7 @@ public class Observation { } private Observation(int id, int siteId, String observed, String type, int monitoring, int control, int conservatism, - int teamwork, int knowledge, String rawData, DateTime date) { + int teamwork, int knowledge, byte[] rawData, DateTime date) { this.id = id; this.siteId = siteId; this.observed = observed; @@ -65,16 +70,20 @@ public class Observation { this.conservatism = conservatism; this.teamwork = teamwork; this.knowledge = knowledge; - this.rawData = rawData; + this.rawData = SerializationUtils.deserialize(rawData); this.date = date; } + public byte[] serializeRawData(){ + return SerializationUtils.serialize((Serializable) rawData); + } + public static class Mapper implements RowMapper { public Observation map(ResultSet rs, StatementContext ctx) throws SQLException { return new Observation(rs.getInt("id"), rs.getInt("siteId"), rs.getString("observed"), rs.getString("type"), rs.getInt("monitoring"), rs.getInt("control"), rs.getInt("conservatism"), rs.getInt("teamwork"), - rs.getInt("knowledge"), rs.getString("rawData"), new DateTime(rs.getDate("date"))); + rs.getInt("knowledge"), rs.getBytes("rawData"), new DateTime(rs.getDate("date"))); } } } diff --git a/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java b/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java index 9901aab..d22feee 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/dao/ObservationDao.java @@ -17,7 +17,7 @@ public interface ObservationDao { @SqlUpdate("CREATE TABLE IF NOT EXISTS observations (id INTEGER PRIMARY KEY, siteId INTEGER, " + "observed TEXT, type TEXT, monitoring INTEGER, control INTEGER, conservatism INTEGER, teamwork INTEGER, " + - "knowledge INTEGER, rawData TEXT, date DATE)") + "knowledge INTEGER, rawData BLOB, date DATE)") void createObservationTable(); @SqlUpdate("CREATE TABLE IF NOT EXISTS observation_tutor (tutorId INT NOT NULL, observationId INT NOT NULL, " + @@ -26,7 +26,7 @@ public interface ObservationDao { @SqlUpdate("INSERT INTO observations (siteId, observed, type, monitoring, control, conservatism, teamwork, knowledge, " + "rawData, date) VALUES (:siteId, :observed, :type, :monitoring, :control, :conservatism, :teamwork, :knowledge, " + - ":rawData, :date)") + ":serializeRawData, :date)") @GetGeneratedKeys int addObservation(@BindBean NewObservation observation, @Bind("date") DateTime date); diff --git a/backend/src/test/java/uk/co/neviyn/Observations/core/ObservationTest.java b/backend/src/test/java/uk/co/neviyn/Observations/core/ObservationTest.java new file mode 100644 index 0000000..5490a5c --- /dev/null +++ b/backend/src/test/java/uk/co/neviyn/Observations/core/ObservationTest.java @@ -0,0 +1,34 @@ +package uk.co.neviyn.Observations.core; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.dropwizard.jackson.Jackson; +import lombok.extern.slf4j.Slf4j; +import org.joda.time.DateTime; +import org.junit.Test; +import uk.co.neviyn.Observations.api.ObservationEntry; + +import java.util.Collections; + +import static io.dropwizard.testing.FixtureHelpers.fixture; +import static org.junit.Assert.*; + +@Slf4j +public class ObservationTest { + private static final ObjectMapper mapper = Jackson.newObjectMapper(); + + private final Observation observation = new Observation(1, 1, "Just a test observation", TrainingType.INITIAL, 1, 2, 3, + 4, 5, Collections.singletonList(new ObservationEntry("MONITORING", 5, "some", "another sum")), DateTime.parse("2018-09-18T00:00:00.000Z") + ); + + @Test + public void serializesToJson() throws Exception { + final String expected = mapper.writeValueAsString(mapper.readValue(fixture("fixtures/Observation.json"), Observation.class)); + assertEquals(expected, mapper.writeValueAsString(observation)); + } + + @Test + public void deserializesFromJson() throws Exception { + assertEquals(observation, mapper.readValue(fixture("fixtures/Observation.json"), Observation.class)); + } + +} \ No newline at end of file diff --git a/backend/src/test/resources/fixtures/Observation.json b/backend/src/test/resources/fixtures/Observation.json new file mode 100644 index 0000000..0594395 --- /dev/null +++ b/backend/src/test/resources/fixtures/Observation.json @@ -0,0 +1,18 @@ +{ + "id":1, + "siteId":1, + "observed":"Just a test observation", + "type":"INITIAL", + "monitoring":1, + "control":2, + "conservatism":3, + "teamwork":4, + "knowledge":5, + "rawData":[{ + "type": "MONITORING", + "rating":5, + "strengths":"some", + "improvements":"another sum" + }], + "date":"2018-09-18T00:00:00.000Z" +} \ No newline at end of file