Resource tests!

This commit is contained in:
neviyn 2018-09-24 13:24:48 +01:00
parent cc4a728ffc
commit 9de6ce48cc
10 changed files with 225 additions and 34 deletions

View File

@ -1,13 +1,14 @@
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 java.util.List;
import lombok.*; import lombok.*;
import uk.co.neviyn.Observations.core.ObservationEntry; import uk.co.neviyn.Observations.core.ObservationEntry;
import java.util.List;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor(access = AccessLevel.PROTECTED)
public class NewObservation { public class NewObservation {

View File

@ -1,10 +1,14 @@
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.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull; import lombok.NonNull;
@Data @Data
@AllArgsConstructor
@NoArgsConstructor
public class SelectOption<T> { public class SelectOption<T> {
@NonNull @NonNull
@JsonProperty @JsonProperty

View File

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

View File

@ -11,7 +11,6 @@ import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true) @ToString(callSuper = true)
@Entity @Entity
@Table(name = "OBSERVATION") @Table(name = "OBSERVATION")
@ -20,7 +19,7 @@ import java.util.Set;
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Observation extends BaseEntity implements Serializable { public class Observation implements Serializable {
@NonNull @NonNull
@JsonProperty @JsonProperty

View File

@ -8,7 +8,7 @@ import java.io.Serializable;
import java.util.Set; import java.util.Set;
@EqualsAndHashCode @EqualsAndHashCode
@ToString(callSuper = true) @ToString
@Entity @Entity
@Table(name = "SITE") @Table(name = "SITE")
@Data @Data

View File

@ -7,7 +7,6 @@ import javax.persistence.*;
import java.io.Serializable; import java.io.Serializable;
import java.util.Set; import java.util.Set;
@EqualsAndHashCode
@ToString @ToString
@Entity @Entity
@Table(name = "TUTOR") @Table(name = "TUTOR")

View File

@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import io.dropwizard.auth.Auth; import io.dropwizard.auth.Auth;
import io.dropwizard.hibernate.UnitOfWork; import io.dropwizard.hibernate.UnitOfWork;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
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;
@ -43,10 +45,12 @@ public class TutorResource {
} }
@AllArgsConstructor @AllArgsConstructor
private class NewTutor { @NoArgsConstructor
static class NewTutor {
@JsonProperty @JsonProperty
final long siteId; long siteId;
@NonNull
@JsonProperty @JsonProperty
final String name; String name;
} }
} }

View File

@ -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<Tutor> 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() {
}
}

View File

@ -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<User>()
.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<SelectOption<Long>> sites = resources.target("/site/all").request().get(new GenericType<List<SelectOption<Long>>>() {
});
assertNotNull(sites);
assertEquals(2, sites.size());
}
@Test
public void getSiteTutors() {
Set<Tutor> 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<Tutor> result = resources.target("/site/1/tutors").request().get(new GenericType<Set<Tutor>>() {
});
assertNotNull(result);
assertEquals(tutors, result);
}
}

View File

@ -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<User>()
.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<Tutor> 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<Tutor> result = resources.target("/tutor/all").request().get(new GenericType<List<Tutor>>() {
});
for (Tutor t : result) {
assertTrue(tutors.contains(t));
}
}
}