diff --git a/backend/src/main/java/uk/co/neviyn/Observations/api/NewObservation.java b/backend/src/main/java/uk/co/neviyn/Observations/api/NewObservation.java index 5842339..1a9dc3a 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/api/NewObservation.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/api/NewObservation.java @@ -1,13 +1,14 @@ package uk.co.neviyn.Observations.api; import com.fasterxml.jackson.annotation.JsonProperty; -import java.util.List; - import lombok.*; import uk.co.neviyn.Observations.core.ObservationEntry; +import java.util.List; + @Data @NoArgsConstructor +@Builder @AllArgsConstructor(access = AccessLevel.PROTECTED) public class NewObservation { diff --git a/backend/src/main/java/uk/co/neviyn/Observations/api/SelectOption.java b/backend/src/main/java/uk/co/neviyn/Observations/api/SelectOption.java index 6bd68e2..252d05d 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/api/SelectOption.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/api/SelectOption.java @@ -1,10 +1,14 @@ 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 SelectOption { @NonNull @JsonProperty diff --git a/backend/src/main/java/uk/co/neviyn/Observations/core/BaseEntity.java b/backend/src/main/java/uk/co/neviyn/Observations/core/BaseEntity.java deleted file mode 100644 index cdf840b..0000000 --- a/backend/src/main/java/uk/co/neviyn/Observations/core/BaseEntity.java +++ /dev/null @@ -1,25 +0,0 @@ -package uk.co.neviyn.Observations.core; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; -import lombok.NonNull; - -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.MappedSuperclass; -import java.io.Serializable; - -@Data -@MappedSuperclass -@NoArgsConstructor -@AllArgsConstructor -class BaseEntity implements Serializable { - @NonNull - @JsonProperty - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private long id; -} 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 45d782b..d96716b 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 @@ -11,7 +11,6 @@ import java.io.Serializable; import java.util.List; import java.util.Set; -@EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) @Entity @Table(name = "OBSERVATION") @@ -20,7 +19,7 @@ import java.util.Set; @AllArgsConstructor @Builder @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") -public class Observation extends BaseEntity implements Serializable { +public class Observation implements Serializable { @NonNull @JsonProperty 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 9930839..60fd954 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 @@ -8,7 +8,7 @@ import java.io.Serializable; import java.util.Set; @EqualsAndHashCode -@ToString(callSuper = true) +@ToString @Entity @Table(name = "SITE") @Data 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 e29d018..423f1ef 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 @@ -7,7 +7,6 @@ import javax.persistence.*; import java.io.Serializable; import java.util.Set; -@EqualsAndHashCode @ToString @Entity @Table(name = "TUTOR") diff --git a/backend/src/main/java/uk/co/neviyn/Observations/resources/TutorResource.java b/backend/src/main/java/uk/co/neviyn/Observations/resources/TutorResource.java index 05bdb16..93f0cd8 100644 --- a/backend/src/main/java/uk/co/neviyn/Observations/resources/TutorResource.java +++ b/backend/src/main/java/uk/co/neviyn/Observations/resources/TutorResource.java @@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import io.dropwizard.auth.Auth; import io.dropwizard.hibernate.UnitOfWork; import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; +import lombok.NonNull; import lombok.RequiredArgsConstructor; import uk.co.neviyn.Observations.core.Site; import uk.co.neviyn.Observations.core.Tutor; @@ -43,10 +45,12 @@ public class TutorResource { } @AllArgsConstructor - private class NewTutor { + @NoArgsConstructor + static class NewTutor { @JsonProperty - final long siteId; + long siteId; + @NonNull @JsonProperty - final String name; + String name; } } diff --git a/backend/src/test/java/uk/co/neviyn/Observations/resources/ObservationResourceTest.java b/backend/src/test/java/uk/co/neviyn/Observations/resources/ObservationResourceTest.java new file mode 100644 index 0000000..341c2b6 --- /dev/null +++ b/backend/src/test/java/uk/co/neviyn/Observations/resources/ObservationResourceTest.java @@ -0,0 +1,68 @@ +package uk.co.neviyn.Observations.resources; + +import io.dropwizard.testing.junit.ResourceTestRule; +import org.junit.After; +import org.junit.ClassRule; +import org.junit.Test; +import uk.co.neviyn.Observations.api.NewObservation; +import uk.co.neviyn.Observations.core.Observation; +import uk.co.neviyn.Observations.core.Site; +import uk.co.neviyn.Observations.core.Tutor; +import uk.co.neviyn.Observations.dao.ObservationDao; +import uk.co.neviyn.Observations.dao.SiteDao; +import uk.co.neviyn.Observations.dao.TutorDao; + +import javax.ws.rs.client.Entity; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.mockito.Mockito.*; + +public class ObservationResourceTest { + + private static final ObservationDao dao = mock(ObservationDao.class); + private static final SiteDao siteDao = mock(SiteDao.class); + private static final TutorDao tutorDao = mock(TutorDao.class); + + @ClassRule + public static final ResourceTestRule resources = ResourceTestRule.builder() + .addResource(new ObservationResource(dao, tutorDao, siteDao)) + .build(); + + @After + public void tearDown() { + reset(dao); + reset(siteDao); + reset(tutorDao); + } + + @Test + public void add() { + final Site testSite = Site.builder().id(1).name("Test Site").build(); + final List tutors = Arrays.asList(Tutor.builder().id(1).name("Mr A").build(), Tutor.builder().id(2).name("Mr B").build()); + when(siteDao.get(1)).thenReturn(testSite); + when(tutorDao.get(1)).thenReturn(tutors.get(0)); + when(tutorDao.get(2)).thenReturn(tutors.get(1)); + resources.target("/observation").request().post(Entity.json(NewObservation.builder().type("INITIAL") + .conservatism(1).control(2).knowledge(3).monitoring(4).teamwork(5).siteId(1).tutorIds(Arrays.asList(1L, 2L)) + .observed("").rawData(new ArrayList<>()).build())); + verify(dao, times(1)).persist(any(Observation.class)); + } + + @Test + public void averageObservationScores() { + } + + @Test + public void averageStatsChartJs() { + } + + @Test + public void averageObservationScoresForSite() { + } + + @Test + public void averageObservationScoresForSiteChartJs() { + } +} \ No newline at end of file diff --git a/backend/src/test/java/uk/co/neviyn/Observations/resources/SiteResourceTest.java b/backend/src/test/java/uk/co/neviyn/Observations/resources/SiteResourceTest.java new file mode 100644 index 0000000..3477708 --- /dev/null +++ b/backend/src/test/java/uk/co/neviyn/Observations/resources/SiteResourceTest.java @@ -0,0 +1,74 @@ +package uk.co.neviyn.Observations.resources; + +import io.dropwizard.auth.AuthDynamicFeature; +import io.dropwizard.auth.AuthValueFactoryProvider; +import io.dropwizard.auth.basic.BasicCredentialAuthFilter; +import io.dropwizard.testing.junit.ResourceTestRule; +import org.junit.After; +import org.junit.ClassRule; +import org.junit.Test; +import uk.co.neviyn.Observations.api.SelectOption; +import uk.co.neviyn.Observations.auth.SimpleAuthenticator; +import uk.co.neviyn.Observations.core.Site; +import uk.co.neviyn.Observations.core.Tutor; +import uk.co.neviyn.Observations.core.User; +import uk.co.neviyn.Observations.dao.SiteDao; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.GenericType; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.*; + +public class SiteResourceTest { + + private static final SiteDao dao = mock(SiteDao.class); + private final String httpAuth = "Basic dGVzdDpURVNUUFc="; + + @ClassRule + public static final ResourceTestRule resources = ResourceTestRule.builder() + .addResource(new AuthDynamicFeature( + new BasicCredentialAuthFilter.Builder() + .setAuthenticator(new SimpleAuthenticator("TESTPW")) + .setRealm("SECURITY") + .buildAuthFilter() + )) + .addProvider(new AuthValueFactoryProvider.Binder<>(User.class)) + .addResource(new SiteResource(dao)) + .build(); + + @After + public void tearDown() { + reset(dao); + } + + @Test + public void add() { + resources.target("/site").request().header("Authorization", httpAuth).post(Entity.json("New Site")); + verify(dao, times(1)).persist(Site.builder().name("New Site").build()); + } + + @Test + public void allSites() { + when(dao.listAll()).thenReturn(Arrays.asList(Site.builder().id(1).name("Site 1").build(), Site.builder().id(2).name("Site 2").build())); + List> sites = resources.target("/site/all").request().get(new GenericType>>() { + }); + assertNotNull(sites); + assertEquals(2, sites.size()); + } + + @Test + public void getSiteTutors() { + Set tutors = new HashSet<>(Arrays.asList(Tutor.builder().id(1).name("Test 1").build(), Tutor.builder().id(2).name("Test 2").build())); + when(dao.get(1)).thenReturn(Site.builder().id(1).name("Site").tutors(tutors).build()); + Set result = resources.target("/site/1/tutors").request().get(new GenericType>() { + }); + assertNotNull(result); + assertEquals(tutors, result); + } +} \ No newline at end of file diff --git a/backend/src/test/java/uk/co/neviyn/Observations/resources/TutorResourceTest.java b/backend/src/test/java/uk/co/neviyn/Observations/resources/TutorResourceTest.java new file mode 100644 index 0000000..a8ee81c --- /dev/null +++ b/backend/src/test/java/uk/co/neviyn/Observations/resources/TutorResourceTest.java @@ -0,0 +1,67 @@ +package uk.co.neviyn.Observations.resources; + +import io.dropwizard.auth.AuthDynamicFeature; +import io.dropwizard.auth.AuthValueFactoryProvider; +import io.dropwizard.auth.basic.BasicCredentialAuthFilter; +import io.dropwizard.testing.junit.ResourceTestRule; +import org.junit.After; +import org.junit.ClassRule; +import org.junit.Test; +import uk.co.neviyn.Observations.auth.SimpleAuthenticator; +import uk.co.neviyn.Observations.core.Site; +import uk.co.neviyn.Observations.core.Tutor; +import uk.co.neviyn.Observations.core.User; +import uk.co.neviyn.Observations.dao.SiteDao; +import uk.co.neviyn.Observations.dao.TutorDao; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.GenericType; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.*; + +public class TutorResourceTest { + + private static final TutorDao dao = mock(TutorDao.class); + private static final SiteDao siteDao = mock(SiteDao.class); + private final String httpAuth = "Basic dGVzdDpURVNUUFc="; + + @ClassRule + public static final ResourceTestRule resources = ResourceTestRule.builder() + .addResource(new AuthDynamicFeature( + new BasicCredentialAuthFilter.Builder() + .setAuthenticator(new SimpleAuthenticator("TESTPW")) + .setRealm("SECURITY") + .buildAuthFilter() + )) + .addProvider(new AuthValueFactoryProvider.Binder<>(User.class)) + .addResource(new TutorResource(dao, siteDao)) + .build(); + + @After + public void tearDown() { + reset(dao); + reset(siteDao); + } + + @Test + public void add() { + final Site testSite = Site.builder().id(1).name("Test Site").build(); + when(siteDao.get(1)).thenReturn(testSite); + resources.target("/tutor").request().header("Authorization", httpAuth).post(Entity.json(new TutorResource.NewTutor(1, "Mr X"))); + verify(dao, times(1)).persist(Tutor.builder().name("Mr X").site(testSite).build()); + } + + @Test + public void allTutors() { + List tutors = Arrays.asList(Tutor.builder().id(1).name("Mr A").build(), Tutor.builder().id(2).name("Mr B").build()); + when(dao.listAll()).thenReturn(tutors); + List result = resources.target("/tutor/all").request().get(new GenericType>() { + }); + for (Tutor t : result) { + assertTrue(tutors.contains(t)); + } + } +} \ No newline at end of file