From 707b8a1b26bd1eb72b35c6e6549eb20a00b1ea68 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Mon, 4 Mar 2019 11:45:40 +0000 Subject: [PATCH] Added better error handling for spring environment issues --- .../controller/GroupSessionController.kt | 11 +++++++++-- .../controller/GroupSessionControllerTest.kt | 14 ++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionController.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionController.kt index 6ecdf09..34d696e 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionController.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionController.kt @@ -176,8 +176,15 @@ class GroupSessionController { retryCount++ Thread.sleep(1_000) // Sleep for 1 second } - if (ipv4 != null) + if (ipv4 != null && this::environment.isInitialized) return mapOf("ip" to ipv4, "port" to environment["local.server.port"]) - return mapOf("error" to "Could not determine IP Address") + else if(ipv4 == null) { + logger.error("IP Address could not be determined") + return mapOf("error" to "Could not determine IP Address") + } + else { + logger.error("Port could not be determined, environment not initialised") + return mapOf("error" to "Could not determine port") + } } } \ No newline at end of file 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 index de3b725..8d08af9 100644 --- a/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionControllerTest.kt +++ b/backend/src/test/kotlin/uk/co/neviyn/observationdatabase/controller/GroupSessionControllerTest.kt @@ -21,7 +21,7 @@ import uk.co.neviyn.observationdatabase.TutorRepository import java.util.* @RunWith(MockitoJUnitRunner::class) -class GroupSessionControllerTest{ +class GroupSessionControllerTest { @InjectMocks lateinit var controller: GroupSessionController @@ -30,12 +30,10 @@ class GroupSessionControllerTest{ lateinit var siteRepository: SiteRepository @Mock lateinit var tutorRepository: TutorRepository - /** @Mock lateinit var observationRepository: ObservationRepository @Mock lateinit var personRepository: PersonRepository - **/ @After fun tearDown() { @@ -43,7 +41,7 @@ class GroupSessionControllerTest{ } @Test - fun testStartSession(){ + 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)) @@ -52,19 +50,19 @@ class GroupSessionControllerTest{ } @Test(expected = ResponseStatusException::class) - fun testStartSession_NoSite(){ + fun testStartSession_NoSite() { controller.startGroupObservation(GroupObservationInit(0, TrainingType.INITIAL, listOf(1), listOf("Sample title"))) } @Test(expected = ResponseStatusException::class) - fun testStartSession_NoTutor(){ + 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(){ + 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)) @@ -73,7 +71,7 @@ class GroupSessionControllerTest{ } @Test(expected = ResponseStatusException::class) - fun testRecoverSession_NoActiveSession(){ + fun testRecoverSession_NoActiveSession() { controller.reconnectToGroupObservation() } } \ No newline at end of file