Implemented testing on ObservationDao. Added Builder for Observation.

This commit is contained in:
neviyn 2018-09-19 10:13:14 +01:00
parent b4ca1a8fc7
commit 9a6a27240a
10 changed files with 161 additions and 32 deletions

View File

@ -1,15 +1,12 @@
package uk.co.neviyn.Observations.api; package uk.co.neviyn.Observations.api;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data; import lombok.*;
import lombok.NoArgsConstructor; import org.joda.time.DateTime;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.joda.time.LocalDate;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@RequiredArgsConstructor @AllArgsConstructor
public class AverageStats { public class AverageStats {
@NonNull @NonNull
@ -18,5 +15,5 @@ public class AverageStats {
@NonNull @NonNull
@JsonProperty @JsonProperty
private LocalDate date; private DateTime date;
} }

View File

@ -9,12 +9,13 @@ import lombok.NonNull;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.MappedSuperclass; import javax.persistence.MappedSuperclass;
import java.io.Serializable;
@Data @Data
@MappedSuperclass @MappedSuperclass
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
class BaseEntity { class BaseEntity implements Serializable {
@NonNull @NonNull
@JsonProperty @JsonProperty
@Id @Id

View File

@ -2,6 +2,7 @@ package uk.co.neviyn.Observations.core;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -11,12 +12,14 @@ import org.joda.time.DateTime;
import javax.persistence.*; import javax.persistence.*;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Entity @Entity
@Table(name = "OBSERVATION") @Table(name = "OBSERVATION")
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor
public class Observation extends BaseEntity{ @Builder
public class Observation extends BaseEntity implements Serializable {
@JsonProperty @JsonProperty
@ManyToOne @ManyToOne
@ -41,6 +44,7 @@ public class Observation extends BaseEntity{
@NonNull @NonNull
@JsonProperty @JsonProperty
@ElementCollection
private List<ObservationEntry> observations; private List<ObservationEntry> observations;
@NonNull @NonNull

View File

@ -3,11 +3,14 @@ package uk.co.neviyn.Observations.core;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*; import lombok.*;
@EqualsAndHashCode(callSuper = true) import javax.persistence.Embeddable;
import java.io.Serializable;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class ObservationEntry extends BaseEntity{ @Embeddable
public class ObservationEntry implements Serializable {
@NonNull @NonNull
@JsonProperty @JsonProperty

View File

@ -1,6 +1,8 @@
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.util.Set; import java.util.Set;
import lombok.*; import lombok.*;
@ -8,11 +10,13 @@ import lombok.*;
import javax.persistence.*; import javax.persistence.*;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Entity @Entity
@Table(name = "SITE") @Table(name = "SITE")
@Data @Data
@NoArgsConstructor @NoArgsConstructor
public class Site extends BaseEntity { @RequiredArgsConstructor
public class Site extends BaseEntity implements Serializable {
@NonNull @NonNull
@JsonProperty @JsonProperty

View File

@ -4,14 +4,16 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*; import lombok.*;
import javax.persistence.*; import javax.persistence.*;
import java.io.Serializable;
import java.util.Set; import java.util.Set;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Entity @Entity
@Table(name = "TUTOR") @Table(name = "TUTOR")
@Data @Data
@NoArgsConstructor @NoArgsConstructor
public class Tutor extends BaseEntity { public class Tutor extends BaseEntity implements Serializable {
@NonNull @NonNull
@JsonProperty @JsonProperty

View File

@ -22,19 +22,19 @@ public class ObservationDao extends AbstractDAO<Observation> {
} }
public List<Observation> listAll(){ public List<Observation> listAll(){
return list(criteriaQuery()); return currentSession().createQuery("from Observation", Observation.class).list();
} }
public List<AverageStats> averageStatsForAll(){ public List<AverageStats> averageStatsForAll(){
final String hql = "select avg(observation.monitoring), avg(observation.control), avg(observation.conservatism), " + final String hql = "select new uk.co.neviyn.Observations.api.AverageStats(avg(obs.monitoring), avg(obs.control), avg(obs.conservatism), " +
"avg(observation.teamwork), avg(observation.knowledge) from observation where group by observation.date"; "avg(obs.teamwork), avg(obs.knowledge), obs.date) from Observation obs group by obs.date order by obs.date";
return currentSession().createQuery(hql, AverageStats.class).list(); return currentSession().createQuery(hql, AverageStats.class).list();
} }
public List<AverageStats> averageStatsForSite(Site site){ public List<AverageStats> averageStatsForSite(Site site){
final String hql = "select avg(observation.monitoring), avg(observation.control), avg(observation.conservatism), " + final String hql = "select new uk.co.neviyn.Observations.api.AverageStats(avg(obs.monitoring), avg(obs.control), avg(obs.conservatism)," +
"avg(observation.teamwork), avg(observation.knowledge) from observation where observation.site = :site " + "avg(obs.teamwork), avg(obs.knowledge), obs.date) from Observation obs where obs.site = :site " +
"group by observation.date"; "group by obs.date order by obs.date";
return currentSession().createQuery(hql, AverageStats.class).setParameter("site", site).list(); return currentSession().createQuery(hql, AverageStats.class).setParameter("site", site).list();
} }
} }

View File

@ -18,4 +18,8 @@ public class SiteDao extends AbstractDAO<Site> {
public Site get(long id){ public Site get(long id){
return super.get(id); return super.get(id);
} }
public Site persist(Site site){
return super.persist(site);
}
} }

View File

@ -50,18 +50,19 @@ public class ObservationResource {
tutors.add(tutorDao.get(l)); tutors.add(tutorDao.get(l));
} }
final Site site = siteDao.get(newObservation.getSiteId()); final Site site = siteDao.get(newObservation.getSiteId());
Observation observation = new Observation(); Observation observation = Observation.builder()
observation.setSite(site); .site(site)
observation.setTutors(tutors); .tutors(tutors)
observation.setObserved(newObservation.getObserved()); .observed(newObservation.getObserved())
observation.setType(TrainingType.valueOf(newObservation.getType())); .type(TrainingType.valueOf(newObservation.getType()))
observation.setMonitoring(newObservation.getMonitoring()); .monitoring(newObservation.getMonitoring())
observation.setControl(newObservation.getControl()); .control(newObservation.getControl())
observation.setConservatism(newObservation.getConservatism()); .conservatism(newObservation.getConservatism())
observation.setTeamwork(newObservation.getTeamwork()); .teamwork(newObservation.getTeamwork())
observation.setKnowledge(newObservation.getKnowledge()); .knowledge(newObservation.getKnowledge())
observation.setObservations(newObservation.getRawData()); .observations(newObservation.getRawData())
observation.setDate(submissionDate); .date(submissionDate)
.build();
observation = dao.persist(observation); observation = dao.persist(observation);
log.info("Created observation with ID " + observation.getId() + " at " + DateTime.now().toString()); log.info("Created observation with ID " + observation.getId() + " at " + DateTime.now().toString());
return observation.getId(); return observation.getId();

View File

@ -0,0 +1,113 @@
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 java.util.Collections;
import java.util.List;
import static org.junit.Assert.*;
public class ObservationDaoTest {
@Rule
public DAOTestRule testRule = DAOTestRule.newBuilder().addEntityClass(Observation.class).addEntityClass(Site.class).addEntityClass(Tutor.class).build();
private ObservationDao dao;
private SiteDao siteDao;
private final Site site = new Site("Test site");
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")
);
@Before
public void setUp() throws Exception {
dao = new ObservationDao(testRule.getSessionFactory());
siteDao = new SiteDao(testRule.getSessionFactory());
testRule.inTransaction(() -> siteDao.persist(site));
}
@Test
public void persistAndGet(){
Observation insert = testRule.inTransaction(() -> dao.persist(observation));
assertNotNull(insert);
assertTrue(insert.getId() > 0);
}
@Test
public void listAll() {
int numberOfObservations = 5;
testRule.inTransaction(() -> {
for(int i = 0; i < numberOfObservations; i++){
// Make actual copies, otherwise they will merely overwrite.
dao.persist(SerializationUtils.clone(observation));
}
});
List<Observation> observations = dao.listAll();
assertNotNull(observations);
assertEquals(numberOfObservations, observations.size());
}
@Test
public void averageStatsForAll() {
Observation otherEntry = SerializationUtils.clone(observation);
otherEntry.setMonitoring(5);
otherEntry.setControl(5);
otherEntry.setConservatism(5);
otherEntry.setTeamwork(5);
Observation differentDay = SerializationUtils.clone(observation);
differentDay.setDate(DateTime.now());
testRule.inTransaction(() -> {
dao.persist(observation);
dao.persist(otherEntry);
dao.persist(differentDay);
});
List<AverageStats> stats = dao.averageStatsForAll();
assertNotNull(stats);
assertEquals(2, stats.size());
final AverageStats statObject = stats.get(0);
assertEquals(3, statObject.getMonitoring(), 0);
assertEquals(3.5, statObject.getControl(), 0);
assertEquals(4, statObject.getConservatism(), 0);
assertEquals(4.5, statObject.getTeamwork(), 0);
assertEquals(5, statObject.getKnowledge(), 0);
}
@Test
public void averageStatsForSite() {
Observation otherEntry = SerializationUtils.clone(observation);
otherEntry.setMonitoring(5);
otherEntry.setControl(5);
otherEntry.setConservatism(5);
otherEntry.setTeamwork(5);
Observation hiddenEntity = SerializationUtils.clone(observation);
Site hiddenSite = new Site("Area 51");
hiddenEntity.setSite(hiddenSite);
testRule.inTransaction(() -> {
dao.persist(observation);
dao.persist(otherEntry);
siteDao.persist(hiddenSite);
dao.persist(hiddenEntity);
});
List<AverageStats> stats = dao.averageStatsForSite(site);
assertNotNull(stats);
assertEquals(1, stats.size());
final AverageStats statObject = stats.get(0);
assertEquals(3, statObject.getMonitoring(), 0);
assertEquals(3.5, statObject.getControl(), 0);
assertEquals(4, statObject.getConservatism(), 0);
assertEquals(4.5, statObject.getTeamwork(), 0);
assertEquals(5, statObject.getKnowledge(), 0);
}
}