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 a8e4afd..45d782b 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 @@ -22,6 +22,12 @@ import java.util.Set; @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") public class Observation extends BaseEntity implements Serializable { + @NonNull + @JsonProperty + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + @JsonProperty @ManyToOne @JoinColumn(name="site_id", nullable=false) diff --git a/backend/src/main/java/uk/co/neviyn/Observations/core/Site.java b/backend/src/main/java/uk/co/neviyn/Observations/core/Site.java index 5879a05..9930839 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/core/Site.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/core/Site.java @@ -1,25 +1,27 @@ package uk.co.neviyn.Observations.core; -import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.ObjectIdGenerators; import lombok.*; -import javax.persistence.Entity; -import javax.persistence.OneToMany; -import javax.persistence.Table; +import javax.persistence.*; import java.io.Serializable; import java.util.Set; -@EqualsAndHashCode(callSuper = true) +@EqualsAndHashCode @ToString(callSuper = true) @Entity @Table(name = "SITE") @Data @NoArgsConstructor -@RequiredArgsConstructor -@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") -public class Site extends BaseEntity implements Serializable { +@Builder +@AllArgsConstructor +public class Site implements Serializable { + + @NonNull + @JsonProperty + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; @NonNull @JsonProperty @@ -33,8 +35,4 @@ public class Site extends BaseEntity implements Serializable { @OneToMany(mappedBy = "site") private Set observations; - public Site(long id, String name) { - super(id); - this.name = name; - } } diff --git a/backend/src/main/java/uk/co/neviyn/Observations/core/Tutor.java b/backend/src/main/java/uk/co/neviyn/Observations/core/Tutor.java index e5d3aab..e29d018 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/core/Tutor.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/core/Tutor.java @@ -1,24 +1,27 @@ package uk.co.neviyn.Observations.core; -import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.ObjectIdGenerators; import lombok.*; import javax.persistence.*; import java.io.Serializable; import java.util.Set; -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) +@EqualsAndHashCode +@ToString @Entity @Table(name = "TUTOR") @Data @NoArgsConstructor @AllArgsConstructor @Builder -@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") -public class Tutor extends BaseEntity implements Serializable { +public class Tutor implements Serializable { + + @NonNull + @JsonProperty + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; @NonNull @JsonProperty diff --git a/backend/src/main/java/uk/co/neviyn/Observations/resources/SiteResource.java b/backend/src/main/java/uk/co/neviyn/Observations/resources/SiteResource.java index 5495fa2..526c1a0 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/resources/SiteResource.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/resources/SiteResource.java @@ -25,7 +25,7 @@ public class SiteResource { @POST @UnitOfWork public Site add(@Auth User user, String name) { - return dao.persist(new Site(name)); + return dao.persist(Site.builder().name(name).build()); } @Path("/all") 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 index f16f873..1773762 100644 --- a/backend/src/test/java/uk/co/neviyn/Observations/core/ObservationTest.java +++ b/backend/src/test/java/uk/co/neviyn/Observations/core/ObservationTest.java @@ -9,15 +9,16 @@ import org.junit.Test; import java.util.Collections; import static io.dropwizard.testing.FixtureHelpers.fixture; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; @Slf4j public class ObservationTest { private static final ObjectMapper mapper = Jackson.newObjectMapper(); - private final Observation observation = new Observation(null, null, "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") - ); + private final Observation observation = Observation.builder().observed("Just a test observation").type(TrainingType.INITIAL) + .monitoring(1).control(2).conservatism(3).teamwork(4).knowledge(5) + .observations(Collections.singletonList(new ObservationEntry("MONITORING", 5, "some", "another sum"))) + .date(DateTime.parse("2018-09-18T00:00:00.000Z")).build(); @Test public void serializesToJson() throws Exception { diff --git a/backend/src/test/java/uk/co/neviyn/Observations/dao/ObservationDaoTest.java b/backend/src/test/java/uk/co/neviyn/Observations/dao/ObservationDaoTest.java index d3ee4c1..13f3e6f 100644 --- a/backend/src/test/java/uk/co/neviyn/Observations/dao/ObservationDaoTest.java +++ b/backend/src/test/java/uk/co/neviyn/Observations/dao/ObservationDaoTest.java @@ -1,14 +1,14 @@ package uk.co.neviyn.Observations.dao; -import io.dropwizard.testing.junit.DAOTestRule; import org.apache.commons.lang3.SerializationUtils; import org.joda.time.DateTime; -import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import uk.co.neviyn.Observations.api.AverageStats; -import uk.co.neviyn.Observations.core.*; +import uk.co.neviyn.Observations.core.Observation; +import uk.co.neviyn.Observations.core.ObservationEntry; +import uk.co.neviyn.Observations.core.Site; +import uk.co.neviyn.Observations.core.TrainingType; import java.util.Collections; import java.util.List; @@ -20,11 +20,12 @@ public class ObservationDaoTest extends DaoTestBase { private ObservationDao dao; private SiteDao siteDao; - private final Site site = new Site("Test site"); + private final Site site = Site.builder().name("Test site").build(); - private final Observation observation = new Observation(site, null, "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") - ); + private final Observation observation = Observation.builder().observed("Just a test observation").type(TrainingType.INITIAL) + .site(site).monitoring(1).control(2).conservatism(3).teamwork(4).knowledge(5) + .observations(Collections.singletonList(new ObservationEntry("MONITORING", 5, "some", "another sum"))) + .date(DateTime.parse("2018-09-18T00:00:00.000Z")).build(); @Before public void setUp() { @@ -97,7 +98,7 @@ public class ObservationDaoTest extends DaoTestBase { Observation hiddenEntity = SerializationUtils.clone(observation); - Site hiddenSite = new Site("Area 51"); + Site hiddenSite = Site.builder().name("Area 51").build(); hiddenEntity.setSite(hiddenSite); testRule.inTransaction(() -> { dao.persist(observation); diff --git a/backend/src/test/java/uk/co/neviyn/Observations/dao/SiteDaoTest.java b/backend/src/test/java/uk/co/neviyn/Observations/dao/SiteDaoTest.java index edfc777..007f367 100644 --- a/backend/src/test/java/uk/co/neviyn/Observations/dao/SiteDaoTest.java +++ b/backend/src/test/java/uk/co/neviyn/Observations/dao/SiteDaoTest.java @@ -1,10 +1,11 @@ package uk.co.neviyn.Observations.dao; -import java.util.List; import org.junit.Before; import org.junit.Test; import uk.co.neviyn.Observations.core.Site; +import java.util.List; + import static org.junit.Assert.*; public class SiteDaoTest extends DaoTestBase{ @@ -19,9 +20,9 @@ public class SiteDaoTest extends DaoTestBase{ @Test public void listAll() { testRule.inTransaction(() -> { - dao.persist(new Site("Site A")); - dao.persist(new Site("Site B")); - dao.persist(new Site("Site C")); + dao.persist(Site.builder().name("Site A").build()); + dao.persist(Site.builder().name("Site B").build()); + dao.persist(Site.builder().name("Site C").build()); }); List result = dao.listAll(); assertNotNull(result); @@ -37,7 +38,7 @@ public class SiteDaoTest extends DaoTestBase{ @Test public void persistAndGet() { - Site insert = testRule.inTransaction(() -> dao.persist(new Site("Test Site"))); + Site insert = testRule.inTransaction(() -> dao.persist(Site.builder().name("Test Site").build())); assertNotNull(insert); assertTrue(insert.getId() > 0); assertNotNull(dao.get(insert.getId())); diff --git a/backend/src/test/java/uk/co/neviyn/Observations/dao/TutorDaoTest.java b/backend/src/test/java/uk/co/neviyn/Observations/dao/TutorDaoTest.java index 461eca6..d17be1a 100644 --- a/backend/src/test/java/uk/co/neviyn/Observations/dao/TutorDaoTest.java +++ b/backend/src/test/java/uk/co/neviyn/Observations/dao/TutorDaoTest.java @@ -1,19 +1,19 @@ package uk.co.neviyn.Observations.dao; -import java.util.List; import org.junit.Before; import org.junit.Test; -import uk.co.neviyn.Observations.core.Observation; import uk.co.neviyn.Observations.core.Site; import uk.co.neviyn.Observations.core.Tutor; +import java.util.List; + import static org.junit.Assert.*; public class TutorDaoTest extends DaoTestBase{ private TutorDao dao; private SiteDao siteDao; - private final Site site = new Site("Test site"); + private final Site site = Site.builder().name("Test site").build(); @Before public void setUp() { @@ -25,9 +25,9 @@ public class TutorDaoTest extends DaoTestBase{ @Test public void listAll() { testRule.inTransaction(() -> { - dao.persist(new Tutor("Mr Test", site, null)); - dao.persist(new Tutor("Mr Test2", site, null)); - dao.persist(new Tutor("Mr Test3", site, null)); + dao.persist(Tutor.builder().name("Mr Test").site(site).build()); + dao.persist(Tutor.builder().name("Mr Test2").site(site).build()); + dao.persist(Tutor.builder().name("Mr Test3").site(site).build()); }); List result = dao.listAll(); assertNotNull(result); @@ -43,7 +43,7 @@ public class TutorDaoTest extends DaoTestBase{ @Test public void persistAndGet() { - Tutor insert = testRule.inTransaction(() -> dao.persist(new Tutor("Mr Test", site, null))); + Tutor insert = testRule.inTransaction(() -> dao.persist(Tutor.builder().name("Mr Test").site(site).build())); assertNotNull(insert); assertTrue(insert.getId() > 0); assertNotNull(dao.get(insert.getId()));