Added JSON test for Observation.

This commit is contained in:
neviyn 2018-09-18 13:18:24 +01:00
parent 411f6ca38c
commit ff1622c830
6 changed files with 111 additions and 10 deletions

View File

@ -46,6 +46,17 @@
<version>${dropwizard.version}</version> <version>${dropwizard.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.22.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>
<dependency> <dependency>
<groupId>com.h2database</groupId> <groupId>com.h2database</groupId>
<artifactId>h2</artifactId> <artifactId>h2</artifactId>

View File

@ -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;
}

View File

@ -1,17 +1,22 @@
package uk.co.neviyn.Observations.core; package uk.co.neviyn.Observations.core;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import lombok.Data; import java.util.List;
import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.*;
import org.apache.commons.lang3.SerializationUtils;
import org.jdbi.v3.core.mapper.RowMapper; import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext; import org.jdbi.v3.core.statement.StatementContext;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import uk.co.neviyn.Observations.api.ObservationEntry;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor
public class Observation { public class Observation {
@NonNull @NonNull
@ -36,14 +41,14 @@ public class Observation {
@NonNull @NonNull
@JsonProperty @JsonProperty
private String rawData; private List<ObservationEntry> rawData;
@NonNull @NonNull
@JsonProperty @JsonProperty
private DateTime date; private DateTime date;
public Observation(int siteId, String observed, int monitoring, int control, int conservatism, int teamwork, public Observation(int siteId, String observed, int monitoring, int control, int conservatism, int teamwork,
int knowledge, String rawData) { int knowledge, List<ObservationEntry> rawData) {
this.siteId = siteId; this.siteId = siteId;
this.observed = observed; this.observed = observed;
this.monitoring = monitoring; 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, 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.id = id;
this.siteId = siteId; this.siteId = siteId;
this.observed = observed; this.observed = observed;
@ -65,16 +70,20 @@ public class Observation {
this.conservatism = conservatism; this.conservatism = conservatism;
this.teamwork = teamwork; this.teamwork = teamwork;
this.knowledge = knowledge; this.knowledge = knowledge;
this.rawData = rawData; this.rawData = SerializationUtils.deserialize(rawData);
this.date = date; this.date = date;
} }
public byte[] serializeRawData(){
return SerializationUtils.serialize((Serializable) rawData);
}
public static class Mapper implements RowMapper<Observation> { public static class Mapper implements RowMapper<Observation> {
public Observation map(ResultSet rs, StatementContext ctx) throws SQLException { public Observation map(ResultSet rs, StatementContext ctx) throws SQLException {
return new Observation(rs.getInt("id"), rs.getInt("siteId"), rs.getString("observed"), rs.getString("type"), 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("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")));
} }
} }
} }

View File

@ -17,7 +17,7 @@ public interface ObservationDao {
@SqlUpdate("CREATE TABLE IF NOT EXISTS observations (id INTEGER PRIMARY KEY, siteId INTEGER, " + @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, " + "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(); void createObservationTable();
@SqlUpdate("CREATE TABLE IF NOT EXISTS observation_tutor (tutorId INT NOT NULL, observationId INT NOT NULL, " + @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, " + @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) VALUES (:siteId, :observed, :type, :monitoring, :control, :conservatism, :teamwork, :knowledge, " +
":rawData, :date)") ":serializeRawData, :date)")
@GetGeneratedKeys @GetGeneratedKeys
int addObservation(@BindBean NewObservation observation, @Bind("date") DateTime date); int addObservation(@BindBean NewObservation observation, @Bind("date") DateTime date);

View File

@ -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));
}
}

View File

@ -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"
}