Removed BaseEntity. More use of builders.

This commit is contained in:
neviyn 2018-09-24 09:55:59 +01:00
parent 3dc6e6e10b
commit cc4a728ffc
8 changed files with 55 additions and 45 deletions

View File

@ -22,6 +22,12 @@ import java.util.Set;
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Observation extends BaseEntity implements Serializable { public class Observation extends BaseEntity implements Serializable {
@NonNull
@JsonProperty
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@JsonProperty @JsonProperty
@ManyToOne @ManyToOne
@JoinColumn(name="site_id", nullable=false) @JoinColumn(name="site_id", nullable=false)

View File

@ -1,25 +1,27 @@
package uk.co.neviyn.Observations.core; package uk.co.neviyn.Observations.core;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.*; import lombok.*;
import javax.persistence.Entity; import javax.persistence.*;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.io.Serializable; import java.io.Serializable;
import java.util.Set; import java.util.Set;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode
@ToString(callSuper = true) @ToString(callSuper = true)
@Entity @Entity
@Table(name = "SITE") @Table(name = "SITE")
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@RequiredArgsConstructor @Builder
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") @AllArgsConstructor
public class Site extends BaseEntity implements Serializable { public class Site implements Serializable {
@NonNull
@JsonProperty
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NonNull @NonNull
@JsonProperty @JsonProperty
@ -33,8 +35,4 @@ public class Site extends BaseEntity implements Serializable {
@OneToMany(mappedBy = "site") @OneToMany(mappedBy = "site")
private Set<Observation> observations; private Set<Observation> observations;
public Site(long id, String name) {
super(id);
this.name = name;
}
} }

View File

@ -1,24 +1,27 @@
package uk.co.neviyn.Observations.core; package uk.co.neviyn.Observations.core;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.*; import lombok.*;
import javax.persistence.*; import javax.persistence.*;
import java.io.Serializable; import java.io.Serializable;
import java.util.Set; import java.util.Set;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode
@ToString(callSuper = true) @ToString
@Entity @Entity
@Table(name = "TUTOR") @Table(name = "TUTOR")
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") public class Tutor implements Serializable {
public class Tutor extends BaseEntity implements Serializable {
@NonNull
@JsonProperty
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@NonNull @NonNull
@JsonProperty @JsonProperty

View File

@ -25,7 +25,7 @@ public class SiteResource {
@POST @POST
@UnitOfWork @UnitOfWork
public Site add(@Auth User user, String name) { public Site add(@Auth User user, String name) {
return dao.persist(new Site(name)); return dao.persist(Site.builder().name(name).build());
} }
@Path("/all") @Path("/all")

View File

@ -9,15 +9,16 @@ import org.junit.Test;
import java.util.Collections; import java.util.Collections;
import static io.dropwizard.testing.FixtureHelpers.fixture; import static io.dropwizard.testing.FixtureHelpers.fixture;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
@Slf4j @Slf4j
public class ObservationTest { public class ObservationTest {
private static final ObjectMapper mapper = Jackson.newObjectMapper(); private static final ObjectMapper mapper = Jackson.newObjectMapper();
private final Observation observation = new Observation(null, null, "Just a test observation", TrainingType.INITIAL, 1, 2, 3, private final Observation observation = Observation.builder().observed("Just a test observation").type(TrainingType.INITIAL)
4, 5, Collections.singletonList(new ObservationEntry("MONITORING", 5, "some", "another sum")), DateTime.parse("2018-09-18T00:00:00.000Z") .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 @Test
public void serializesToJson() throws Exception { public void serializesToJson() throws Exception {

View File

@ -1,14 +1,14 @@
package uk.co.neviyn.Observations.dao; package uk.co.neviyn.Observations.dao;
import io.dropwizard.testing.junit.DAOTestRule;
import org.apache.commons.lang3.SerializationUtils; import org.apache.commons.lang3.SerializationUtils;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import uk.co.neviyn.Observations.api.AverageStats; 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.Collections;
import java.util.List; import java.util.List;
@ -20,11 +20,12 @@ public class ObservationDaoTest extends DaoTestBase {
private ObservationDao dao; private ObservationDao dao;
private SiteDao siteDao; 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, private final Observation observation = Observation.builder().observed("Just a test observation").type(TrainingType.INITIAL)
4, 5, Collections.singletonList(new ObservationEntry("MONITORING", 5, "some", "another sum")), DateTime.parse("2018-09-18T00:00:00.000Z") .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 @Before
public void setUp() { public void setUp() {
@ -97,7 +98,7 @@ public class ObservationDaoTest extends DaoTestBase {
Observation hiddenEntity = SerializationUtils.clone(observation); Observation hiddenEntity = SerializationUtils.clone(observation);
Site hiddenSite = new Site("Area 51"); Site hiddenSite = Site.builder().name("Area 51").build();
hiddenEntity.setSite(hiddenSite); hiddenEntity.setSite(hiddenSite);
testRule.inTransaction(() -> { testRule.inTransaction(() -> {
dao.persist(observation); dao.persist(observation);

View File

@ -1,10 +1,11 @@
package uk.co.neviyn.Observations.dao; package uk.co.neviyn.Observations.dao;
import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import uk.co.neviyn.Observations.core.Site; import uk.co.neviyn.Observations.core.Site;
import java.util.List;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class SiteDaoTest extends DaoTestBase{ public class SiteDaoTest extends DaoTestBase{
@ -19,9 +20,9 @@ public class SiteDaoTest extends DaoTestBase{
@Test @Test
public void listAll() { public void listAll() {
testRule.inTransaction(() -> { testRule.inTransaction(() -> {
dao.persist(new Site("Site A")); dao.persist(Site.builder().name("Site A").build());
dao.persist(new Site("Site B")); dao.persist(Site.builder().name("Site B").build());
dao.persist(new Site("Site C")); dao.persist(Site.builder().name("Site C").build());
}); });
List<Site> result = dao.listAll(); List<Site> result = dao.listAll();
assertNotNull(result); assertNotNull(result);
@ -37,7 +38,7 @@ public class SiteDaoTest extends DaoTestBase{
@Test @Test
public void persistAndGet() { 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); assertNotNull(insert);
assertTrue(insert.getId() > 0); assertTrue(insert.getId() > 0);
assertNotNull(dao.get(insert.getId())); assertNotNull(dao.get(insert.getId()));

View File

@ -1,19 +1,19 @@
package uk.co.neviyn.Observations.dao; package uk.co.neviyn.Observations.dao;
import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import uk.co.neviyn.Observations.core.Observation;
import uk.co.neviyn.Observations.core.Site; import uk.co.neviyn.Observations.core.Site;
import uk.co.neviyn.Observations.core.Tutor; import uk.co.neviyn.Observations.core.Tutor;
import java.util.List;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class TutorDaoTest extends DaoTestBase{ public class TutorDaoTest extends DaoTestBase{
private TutorDao dao; private TutorDao dao;
private SiteDao siteDao; private SiteDao siteDao;
private final Site site = new Site("Test site"); private final Site site = Site.builder().name("Test site").build();
@Before @Before
public void setUp() { public void setUp() {
@ -25,9 +25,9 @@ public class TutorDaoTest extends DaoTestBase{
@Test @Test
public void listAll() { public void listAll() {
testRule.inTransaction(() -> { testRule.inTransaction(() -> {
dao.persist(new Tutor("Mr Test", site, null)); dao.persist(Tutor.builder().name("Mr Test").site(site).build());
dao.persist(new Tutor("Mr Test2", site, null)); dao.persist(Tutor.builder().name("Mr Test2").site(site).build());
dao.persist(new Tutor("Mr Test3", site, null)); dao.persist(Tutor.builder().name("Mr Test3").site(site).build());
}); });
List<Tutor> result = dao.listAll(); List<Tutor> result = dao.listAll();
assertNotNull(result); assertNotNull(result);
@ -43,7 +43,7 @@ public class TutorDaoTest extends DaoTestBase{
@Test @Test
public void persistAndGet() { 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); assertNotNull(insert);
assertTrue(insert.getId() > 0); assertTrue(insert.getId() > 0);
assertNotNull(dao.get(insert.getId())); assertNotNull(dao.get(insert.getId()));