Some routes now return actual HTTP error codes

This commit is contained in:
neviyn 2019-03-04 10:59:32 +00:00
parent 6f4da05ddc
commit cb6c15146e
3 changed files with 27 additions and 17 deletions

View File

@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.env.Environment import org.springframework.core.env.Environment
import org.springframework.core.env.get import org.springframework.core.env.get
import org.springframework.http.HttpStatus
import org.springframework.messaging.simp.SimpMessagingTemplate import org.springframework.messaging.simp.SimpMessagingTemplate
import org.springframework.web.bind.annotation.CrossOrigin import org.springframework.web.bind.annotation.CrossOrigin
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
@ -14,6 +15,7 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ResponseStatusException
import uk.co.neviyn.observationdatabase.GroupObservation import uk.co.neviyn.observationdatabase.GroupObservation
import uk.co.neviyn.observationdatabase.GroupObservationInit import uk.co.neviyn.observationdatabase.GroupObservationInit
import uk.co.neviyn.observationdatabase.GroupSessionManager import uk.co.neviyn.observationdatabase.GroupSessionManager
@ -56,11 +58,11 @@ class GroupSessionController {
val tutors = tutorRepository.findAllById(initData.tutors).toSet() val tutors = tutorRepository.findAllById(initData.tutors).toSet()
if (!site.isPresent) { if (!site.isPresent) {
logger.info("Attempted to add Observation without a site.") logger.info("Attempted to add Observation without a site.")
return mapOf("error" to "Site required") throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Site required")
} }
if (tutors.isEmpty() || tutors.size != initData.tutors.size) { if (tutors.isEmpty() || tutors.size != initData.tutors.size) {
logger.info("Attempted to add Observation without a tutor") logger.info("Attempted to add Observation without a tutor")
return mapOf("error" to "Tutor(s) required") throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Tutor required")
} }
val sessionId = GroupSessionManager.startNewSession(site.get(), tutors, initData.type, initData.scenarioTitles) val sessionId = GroupSessionManager.startNewSession(site.get(), tutors, initData.type, initData.scenarioTitles)
return getConnectionDetails().plus("id" to sessionId.toString()) return getConnectionDetails().plus("id" to sessionId.toString())
@ -76,7 +78,7 @@ class GroupSessionController {
return getConnectionDetails().plus(mapOf("id" to GroupSessionManager.sessionId.toString(), "scenarios" to GroupSessionManager.asScenarioView())) return getConnectionDetails().plus(mapOf("id" to GroupSessionManager.sessionId.toString(), "scenarios" to GroupSessionManager.asScenarioView()))
} }
logger.info("Tried to recover a session but no session is active") logger.info("Tried to recover a session but no session is active")
return mapOf("error" to "No session currently active") throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "No group session is currently running")
} }
/** /**
@ -87,8 +89,12 @@ class GroupSessionController {
if (GroupSessionManager.isValid(id)) { if (GroupSessionManager.isValid(id)) {
return mapOf("titles" to GroupSessionManager.scenarioTitles!!) return mapOf("titles" to GroupSessionManager.scenarioTitles!!)
} }
if (GroupSessionManager.isValid()) {
logger.warn("Group observation requested with id $id but id is currently ${GroupSessionManager.sessionId})")
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Session ID incorrect")
}
logger.warn("Group observation requested with id $id but there is no valid session") logger.warn("Group observation requested with id $id but there is no valid session")
return mapOf("error" to "no valid session") throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "No group session is currently running")
} }
/** /**
@ -151,9 +157,9 @@ class GroupSessionController {
websocketMessenger.convertAndSend("/ws/status", mapOf("status" to "complete")) websocketMessenger.convertAndSend("/ws/status", mapOf("status" to "complete"))
return mapOf("success" to "The submission was successfully completed.") return mapOf("success" to "The submission was successfully completed.")
} else if (!GroupSessionManager.dataComplete()) { } else if (!GroupSessionManager.dataComplete()) {
return mapOf("error" to "Data is incomplete") throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Data is incomplete")
} }
return mapOf("error" to "Session was not valid") throw ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "No valid session")
} }
/** /**

View File

@ -5,6 +5,7 @@ import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cache.annotation.Cacheable import org.springframework.cache.annotation.Cacheable
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.CrossOrigin import org.springframework.web.bind.annotation.CrossOrigin
import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PathVariable
@ -12,6 +13,7 @@ import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ResponseStatusException
import uk.co.neviyn.observationdatabase.AfiPieChart import uk.co.neviyn.observationdatabase.AfiPieChart
import uk.co.neviyn.observationdatabase.AfiPieChartDataset import uk.co.neviyn.observationdatabase.AfiPieChartDataset
import uk.co.neviyn.observationdatabase.AverageData import uk.co.neviyn.observationdatabase.AverageData
@ -73,7 +75,7 @@ class ObservationsController {
val site = siteRepository.findById(id) val site = siteRepository.findById(id)
if (site.isPresent) if (site.isPresent)
return site.map { site1 -> site1.tutors.map { NameValue(it.name, it.id) } }.get() return site.map { site1 -> site1.tutors.map { NameValue(it.name, it.id) } }.get()
return null throw ResponseStatusException(HttpStatus.BAD_REQUEST, "No Site with given ID")
} }
/** /**
@ -89,14 +91,15 @@ class ObservationsController {
@PostMapping("/tutor") @PostMapping("/tutor")
fun addTutor(@Valid @RequestBody newTutor: NewTutor): NameValue? { fun addTutor(@Valid @RequestBody newTutor: NewTutor): NameValue? {
logger.debug("Attempting to add tutor\n$newTutor") logger.debug("Attempting to add tutor\n$newTutor")
var nameValue: NameValue? = null val site = siteRepository.findById(newTutor.siteId)
siteRepository.findById(newTutor.siteId).ifPresent { if (site.isPresent) {
val tutor = tutorRepository.save(Tutor(name = newTutor.name, site = it)) val data = site.get()
it.tutors.add(tutor) val tutor = tutorRepository.save(Tutor(name = newTutor.name, site = data))
siteRepository.save(it) data.tutors.add(tutor)
nameValue = NameValue(tutor.name, tutor.id) siteRepository.save(data)
return NameValue(tutor.name, tutor.id)
} }
return nameValue throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Site invalid")
} }
/** /**
@ -123,11 +126,11 @@ class ObservationsController {
val tutors = tutorRepository.findAllById(newObservation.tutors).toSet() val tutors = tutorRepository.findAllById(newObservation.tutors).toSet()
if (!site.isPresent) { if (!site.isPresent) {
logger.info("Attempted to add Observation without a site.") logger.info("Attempted to add Observation without a site.")
return null throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Observation requires a Site")
} }
if (tutors.isEmpty() || tutors.size != newObservation.tutors.size) { if (tutors.isEmpty() || tutors.size != newObservation.tutors.size) {
logger.info("Attempted to add Observation without a tutor") logger.info("Attempted to add Observation without a tutor")
return null throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Observation requires at least one Tutor")
} }
var observation = Observation( var observation = Observation(
site = site.get(), site = site.get(),

View File

@ -7,6 +7,7 @@ import org.junit.runner.RunWith
import org.mockito.* import org.mockito.*
import org.mockito.Mockito.* import org.mockito.Mockito.*
import org.mockito.junit.MockitoJUnitRunner import org.mockito.junit.MockitoJUnitRunner
import org.springframework.web.server.ResponseStatusException
import uk.co.neviyn.observationdatabase.controller.ObservationsController import uk.co.neviyn.observationdatabase.controller.ObservationsController
import java.util.* import java.util.*
@ -58,7 +59,7 @@ class ObservationsControllerTest {
assertTrue(result.contains(NameValue("Bar", 2))) assertTrue(result.contains(NameValue("Bar", 2)))
} }
@Test @Test(expected = ResponseStatusException::class)
fun testGetTutorsForNullSite() { fun testGetTutorsForNullSite() {
doReturn(Optional.ofNullable(null)).`when`(siteRepository).findById(1) doReturn(Optional.ofNullable(null)).`when`(siteRepository).findById(1)
val result: List<NameValue>? = controller.getTutorsForSite(1) val result: List<NameValue>? = controller.getTutorsForSite(1)