From b72060cd2ad1f0ba442cebdffa16df60d8897679 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Mon, 4 Mar 2019 11:15:41 +0000 Subject: [PATCH] Added some tests for GroupSessionController --- .../controller/GroupSessionControllerTest.kt | 79 +++++++++++++++++++ .../ObservationsControllerTest.kt | 19 ++++- 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionControllerTest.kt rename backend/src/test/kotlin/uk/co/neviyn/observationdatabase/{ => controller}/ObservationsControllerTest.kt (89%) diff --git a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionControllerTest.kt b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionControllerTest.kt new file mode 100644 index 0000000..de3b725 --- /dev/null +++ b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionControllerTest.kt @@ -0,0 +1,79 @@ +package uk.co.neviyn.observationdatabase.controller + +import org.junit.After +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.InjectMocks +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.junit.MockitoJUnitRunner +import org.springframework.web.server.ResponseStatusException +import uk.co.neviyn.observationdatabase.GroupObservationInit +import uk.co.neviyn.observationdatabase.GroupSessionManager +import uk.co.neviyn.observationdatabase.ObservationRepository +import uk.co.neviyn.observationdatabase.PersonRepository +import uk.co.neviyn.observationdatabase.Site +import uk.co.neviyn.observationdatabase.SiteRepository +import uk.co.neviyn.observationdatabase.TrainingType +import uk.co.neviyn.observationdatabase.Tutor +import uk.co.neviyn.observationdatabase.TutorRepository +import java.util.* + +@RunWith(MockitoJUnitRunner::class) +class GroupSessionControllerTest{ + + @InjectMocks + lateinit var controller: GroupSessionController + + @Mock + lateinit var siteRepository: SiteRepository + @Mock + lateinit var tutorRepository: TutorRepository + /** + @Mock + lateinit var observationRepository: ObservationRepository + @Mock + lateinit var personRepository: PersonRepository + **/ + + @After + fun tearDown() { + GroupSessionManager.invalidate() + } + + @Test + fun testStartSession(){ + val site = Site(1, "Test site") + Mockito.doReturn(Optional.of(site)).`when`(siteRepository).findById(1) + Mockito.doReturn(listOf(Tutor(1, "Mr X", site))).`when`(tutorRepository).findAllById(listOf(1)) + controller.startGroupObservation(GroupObservationInit(1, TrainingType.INITIAL, listOf(1), listOf("Sample title"))) + assertTrue(GroupSessionManager.isValid()) + } + + @Test(expected = ResponseStatusException::class) + fun testStartSession_NoSite(){ + controller.startGroupObservation(GroupObservationInit(0, TrainingType.INITIAL, listOf(1), listOf("Sample title"))) + } + + @Test(expected = ResponseStatusException::class) + fun testStartSession_NoTutor(){ + val site = Site(1, "Test site") + Mockito.doReturn(Optional.of(site)).`when`(siteRepository).findById(1) + controller.startGroupObservation(GroupObservationInit(1, TrainingType.INITIAL, listOf(0), listOf("Sample title"))) + } + + @Test + fun testRecoverSession(){ + val site = Site(1, "Test site") + Mockito.doReturn(Optional.of(site)).`when`(siteRepository).findById(1) + Mockito.doReturn(listOf(Tutor(1, "Mr X", site))).`when`(tutorRepository).findAllById(listOf(1)) + controller.startGroupObservation(GroupObservationInit(1, TrainingType.INITIAL, listOf(1), listOf("Sample title"))) + assertNotNull(controller.reconnectToGroupObservation()) + } + + @Test(expected = ResponseStatusException::class) + fun testRecoverSession_NoActiveSession(){ + controller.reconnectToGroupObservation() + } +} \ No newline at end of file diff --git a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ObservationsControllerTest.kt b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt similarity index 89% rename from backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ObservationsControllerTest.kt rename to backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt index 503d816..47bb365 100644 --- a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/ObservationsControllerTest.kt +++ b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/ObservationsControllerTest.kt @@ -1,4 +1,4 @@ -package uk.co.neviyn.observationdatabase +package uk.co.neviyn.observationdatabase.controller import junit.framework.TestCase.* import org.joda.time.LocalDate @@ -8,7 +8,22 @@ import org.mockito.* import org.mockito.Mockito.* import org.mockito.junit.MockitoJUnitRunner import org.springframework.web.server.ResponseStatusException -import uk.co.neviyn.observationdatabase.controller.ObservationsController +import uk.co.neviyn.observationdatabase.NameValue +import uk.co.neviyn.observationdatabase.NewObservation +import uk.co.neviyn.observationdatabase.NewSite +import uk.co.neviyn.observationdatabase.NewTutor +import uk.co.neviyn.observationdatabase.Observation +import uk.co.neviyn.observationdatabase.ObservationRepository +import uk.co.neviyn.observationdatabase.ObservationsRequest +import uk.co.neviyn.observationdatabase.Person +import uk.co.neviyn.observationdatabase.PersonRepository +import uk.co.neviyn.observationdatabase.RatingComponent +import uk.co.neviyn.observationdatabase.Scenario +import uk.co.neviyn.observationdatabase.Site +import uk.co.neviyn.observationdatabase.SiteRepository +import uk.co.neviyn.observationdatabase.TrainingType +import uk.co.neviyn.observationdatabase.Tutor +import uk.co.neviyn.observationdatabase.TutorRepository import java.util.* @RunWith(MockitoJUnitRunner::class)