From 319e59354ae0ea9fb56f5f394c035e9b6940eccb Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Fri, 3 May 2019 13:20:38 +0100 Subject: [PATCH] Added mechanism for retrieving in-progress session data. --- .../GroupSessionManager.kt | 6 ++++- .../controller/GroupSessionController.kt | 12 ++++++++- .../controller/GroupSessionControllerTest.kt | 25 +++++++++++++++++-- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/GroupSessionManager.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/GroupSessionManager.kt index cab71ce..b3bfdba 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/GroupSessionManager.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/GroupSessionManager.kt @@ -64,7 +64,7 @@ object GroupSessionManager { } fun dataComplete(): Boolean { - if(observations.isEmpty()){ + if (observations.isEmpty()) { logger.warn("Observations is currently empty") return false } @@ -80,4 +80,8 @@ object GroupSessionManager { fun updateObservationData(input: GroupObservation) { observations[input.person] = input } + + fun participantExistsInSession(name: String): Boolean = observations.containsKey(name) + + fun getObservationDataForParticipant(name: String): GroupObservation? = observations[name] } \ No newline at end of file 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 ffe7caf..d2348e0 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 @@ -120,13 +120,23 @@ class GroupSessionController { return committedObservation } + /** + * Get the current observation data for a user with [name] in the current session. + */ + @GetMapping("/participant/{name}") + fun getParticipantData(@PathVariable name: String): GroupObservation{ + if(GroupSessionManager.participantExistsInSession(name)) + return GroupSessionManager.getObservationDataForParticipant(name)!! + throw ResponseStatusException(HttpStatus.NOT_FOUND, "No participant with the name:'$name'") + } + /** * Submit an observation to be added to the session state and actual database */ @PostMapping("/submit") fun addGroupObservation(@Valid @RequestBody observationData: GroupObservation) { val titles = observationData.scenarios.map { it.title } - if(GroupSessionManager.scenarioTitles!!.size != titles.size || !GroupSessionManager.scenarioTitles!!.containsAll(titles)) { + if (GroupSessionManager.scenarioTitles!!.size != titles.size || !GroupSessionManager.scenarioTitles!!.containsAll(titles)) { logger.warn("Received scenario data but titles did not match\nInput:$titles\nRequired:${GroupSessionManager.scenarioTitles}") throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Submission data contains non-matching title(s)") } 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 9153ab9..98b1f42 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 @@ -12,7 +12,6 @@ import org.mockito.Mockito import org.mockito.Mockito.times import org.mockito.Mockito.verify import org.mockito.junit.MockitoJUnitRunner -import org.springframework.beans.factory.annotation.Autowired import org.springframework.messaging.simp.SimpMessagingTemplate import org.springframework.web.server.ResponseStatusException import uk.co.neviyn.observationdatabase.GroupObservation @@ -89,7 +88,7 @@ class GroupSessionControllerTest { } @Test - fun testCompleteSession(){ + fun testCompleteSession() { val site = Site(1, "Test site") val tutor = Tutor(1, "Mr X", site) Mockito.doReturn(Optional.of(site)).`when`(siteRepository).findById(1) @@ -150,4 +149,26 @@ class GroupSessionControllerTest { assertEquals("A Student", GroupSessionManager.observations.keys.first()) assertEquals("A Student", GroupSessionManager.observations.values.first().person) } + + @Test(expected = ResponseStatusException::class) + fun testGetParticipantData_NoParticipant() { + controller.getParticipantData("Someone") + } + + @Test(expected = ResponseStatusException::class) + fun testGetParticipantData_WrongName() { + val rc = RatingComponent(rating = 5) + val testData = GroupObservation("A Student", listOf(Scenario(0, "Sample title", rc, rc, rc, rc, rc, rc, rc, rc))) + GroupSessionManager.observations["A Student"] = testData + controller.getParticipantData("Another Student") + } + + @Test + fun testGetParticipantData() { + val rc = RatingComponent(rating = 5) + val testData = GroupObservation("A Student", listOf(Scenario(0, "Sample title", rc, rc, rc, rc, rc, rc, rc, rc))) + GroupSessionManager.observations["A Student"] = testData + val output = controller.getParticipantData("A Student") + assertEquals(testData, output) + } } \ No newline at end of file