From 72e8b095c8b74ce87c84b5cabe34380685632e42 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Mon, 8 Oct 2018 10:05:17 +0100 Subject: [PATCH] More tests. --- .../neviyn/observationdatabase/Controller.kt | 10 ++- .../observationdatabase/ControllerTest.kt | 85 ++++++++++++++++--- 2 files changed, 81 insertions(+), 14 deletions(-) diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt index dd46e3f..8433a90 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt @@ -53,19 +53,21 @@ class Controller { fun getObservationsForTutor(@PathVariable(value = "id") id: Long): List = tutorRepository.findById(id).map { tutor -> tutor.observations.map { SimpleObservation(it) } - }.get() + }.orElse(listOf()) @PostMapping("/observation") - fun addObservation(@Valid @RequestBody newObservation: NewObservation): Long { + fun addObservation(@Valid @RequestBody newObservation: NewObservation): Long? { println(newObservation) - val site = siteRepository.findById(newObservation.site).get() + val site = siteRepository.findById(newObservation.site) val tutors = tutorRepository.findAllById(newObservation.tutors).toSet() + if(!site.isPresent) return null + if(tutors.isEmpty() || tutors.size != newObservation.tutors.size) return null val overallScores = newObservation.entries.asSequence().groupBy { it.type } .map { entry -> entry.key to entry.value.asSequence().mapNotNull { it.rating }.average() } .map { it.first to if (it.second > 0) it.second else null }.toList() .toMap() var observation = Observation( - site = site, + site = site.get(), date = LocalDate.now(), type = newObservation.type, observed = newObservation.observed, diff --git a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ControllerTest.kt b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ControllerTest.kt index bb52f01..175956a 100644 --- a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ControllerTest.kt +++ b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ControllerTest.kt @@ -1,17 +1,16 @@ package uk.co.neviyn.observationdatabase import junit.framework.TestCase.* +import org.joda.time.LocalDate import org.junit.Test import org.junit.runner.RunWith -import org.mockito.ArgumentMatchers -import org.mockito.InjectMocks -import org.mockito.Mock +import org.mockito.* import org.mockito.Mockito.* import org.mockito.junit.MockitoJUnitRunner import java.util.* @RunWith(MockitoJUnitRunner::class) -class ControllerTest{ +class ControllerTest { @InjectMocks lateinit var controller: Controller @@ -24,7 +23,7 @@ class ControllerTest{ lateinit var observationRepository: ObservationRepository @Test - fun testGetSites(){ + fun testGetSites() { doReturn(listOf(Site(1, "Site A"), Site(2, "Site B"))).`when`(siteRepository).findAll() val result = controller.getAllSites() assertNotNull(result) @@ -34,7 +33,7 @@ class ControllerTest{ } @Test - fun testAddSite(){ + fun testAddSite() { val newSite = NewSite("Test") doReturn(Site(1, "Test")).`when`(siteRepository).save(ArgumentMatchers.any()) val result = controller.addSite(newSite) @@ -44,7 +43,7 @@ class ControllerTest{ } @Test - fun testGetTutorsForSite(){ + fun testGetTutorsForSite() { val site = Site(1, "Site A") val tutors = listOf(Tutor(1, "Foo", site), Tutor(2, "Bar", site)) site.tutors.addAll(tutors) @@ -57,14 +56,14 @@ class ControllerTest{ } @Test - fun testGetTutorsForNullSite(){ + fun testGetTutorsForNullSite() { doReturn(Optional.ofNullable(null)).`when`(siteRepository).findById(1) val result: List? = controller.getTutorsForSite(1) assertNull(result) } @Test - fun testGetAllTutors(){ + fun testGetAllTutors() { doReturn(listOf(Tutor(1, "Foo", Site(1, "X")), Tutor(2, "Bar", Site(2, "A")))).`when`(tutorRepository).findAll() val result = controller.getAllTutors() assertNotNull(result) @@ -73,7 +72,7 @@ class ControllerTest{ } @Test - fun testAddTutor(){ + fun testAddTutor() { val site = Site(1, "X") doReturn(Optional.of(site)).`when`(siteRepository).findById(1) doReturn(Tutor(1, "Foo", site)).`when`(tutorRepository).save(ArgumentMatchers.any()) @@ -82,4 +81,70 @@ class ControllerTest{ assertEquals(NameValue("Foo", 1), result) } + @Test + fun testGetObservationsForTutor() { + val site = Site(1, "X") + val tutor = Tutor(1, "Foo Bar", site) + val observations = listOf(Observation( + site = site, + date = LocalDate.now(), + type = TrainingType.INITIAL, + observed = "1", + whom = "G1", + monitoring = 5.0, + control = 4.0, + conservatism = 3.0, + teamwork = 2.0, + knowledge = 1.0, + entries = listOf(), + tutors = setOf(tutor) + ), Observation( + site = site, + date = LocalDate.now(), + type = TrainingType.INITIAL, + observed = "2", + whom = "G2", + monitoring = 5.0, + control = 4.0, + conservatism = 3.0, + teamwork = 2.0, + knowledge = 1.0, + entries = listOf(), + tutors = setOf(tutor) + )) + tutor.observations.addAll(observations) + doReturn(Optional.of(tutor)).`when`(tutorRepository).findById(1) + val result = controller.getObservationsForTutor(1) + for (i in 0..1) { + val expected = observations[i] + val actual = result[i] + assertEquals(expected.date, actual.date) + assertEquals(expected.type, actual.type) + assertEquals(expected.observed, actual.observed) + assertEquals(expected.whom, actual.whom) + assertEquals(expected.entries, actual.entries) + } + } + + @Test + fun testGetObservationsForTutor_NoTutor() + { + doReturn(Optional.ofNullable(null)).`when`(tutorRepository).findById(1) + val result = controller.getObservationsForTutor(1) + assertTrue(result.isEmpty()) + } + + @Test + fun testAddObservation(){ + val site = Site(1, "X") + val tutor = Tutor(1, "Foo Bar", site) + doReturn(Optional.of(site)).`when`(siteRepository).findById(1) + doReturn(listOf(tutor)).`when`(tutorRepository).findAllById(listOf(1)) + val newData = NewObservation(1, TrainingType.INITIAL, "An Observation", "Group A", listOf(Entry(RatingCategory.MONITORING, 5, "", "")), listOf(1)) + val observation = Observation(1, site, LocalDate.now(), TrainingType.INITIAL, "An Observation", "Group A", 5.0, 5.0, 5.0, .05, 5.0, newData.entries, setOf(tutor)) + doReturn(observation).`when`(observationRepository).save(ArgumentMatchers.any()) + val result = controller.addObservation(newData) + assertEquals(1, result) + } + } \ No newline at end of file