Skip to content

Get rid of chunked import responses #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/main/kotlin/fi/hsl/jore4/hastus/api/ImportController.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package fi.hsl.jore4.hastus.api

import com.fasterxml.jackson.databind.ObjectMapper
import fi.hsl.jore4.hastus.Constants.MIME_TYPE_CSV
import fi.hsl.jore4.hastus.service.importing.ImportService
import mu.KotlinLogging
import org.springframework.http.HttpStatus
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.PostMapping
Expand All @@ -12,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestHeader
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.server.ResponseStatusException
import java.io.Serializable
import java.util.UUID
import kotlin.time.ExperimentalTime
import kotlin.time.measureTimedValue
Expand All @@ -22,22 +25,23 @@ private val LOGGER = KotlinLogging.logger {}
@RestController
@RequestMapping("import")
class ImportController(
private val importService: ImportService
private val importService: ImportService,
private val objectMapper: ObjectMapper
) {

data class ImportTimetablesSuccessResult(
val vehicleScheduleFrameId: UUID?
)
) : Serializable

data class ImportTimetablesFailureResult(
val reason: String?
)
) : Serializable

@PostMapping("", consumes = [MIME_TYPE_CSV])
@PostMapping("", consumes = [MIME_TYPE_CSV], produces = [APPLICATION_JSON_VALUE])
fun importCsvFile(
@RequestBody csv: String,
@RequestHeader headers: Map<String, String>
): ImportTimetablesSuccessResult {
): ResponseEntity<String> {
val (nullableVehicleScheduleFrameId, elapsed) = measureTimedValue {
LOGGER.info("Starting to import timetables from CSV file...")

Expand All @@ -55,26 +59,44 @@ class ImportController(

LOGGER.info { "CSV import processing completed in $elapsed" }

return ImportTimetablesSuccessResult(nullableVehicleScheduleFrameId)
return ResponseEntity
.ok()
.body(
serialise(
ImportTimetablesSuccessResult(nullableVehicleScheduleFrameId)
)
)
}

@ExceptionHandler
fun handleExportException(ex: Exception): ResponseEntity<ImportTimetablesFailureResult> {
fun handleExportException(ex: Exception): ResponseEntity<String> {
return when (ex) {
is ResponseStatusException -> {
ResponseEntity
.status(ex.statusCode)
.body(ImportTimetablesFailureResult(ex.reason))
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_VALUE)
.body(
serialise(
ImportTimetablesFailureResult(ex.reason)
)
)
}

else -> {
LOGGER.error { "Exception during import request:$ex" }
LOGGER.error(ex.stackTraceToString())

ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ImportTimetablesFailureResult(ex.message))
.internalServerError()
.header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_VALUE)
.body(
serialise(
ImportTimetablesFailureResult(ex.message)
)
)
}
}
}

private fun serialise(obj: Serializable): String = objectMapper.writeValueAsString(obj)
}
32 changes: 20 additions & 12 deletions src/test/kotlin/fi/hsl/jore4/hastus/api/ImportControllerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.ResultActions
import org.springframework.test.web.servlet.ResultMatcher
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.header
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import java.util.UUID

@ExtendWith(MockKExtension::class)
Expand All @@ -48,12 +50,12 @@ class ImportControllerTest @Autowired constructor(

return mockMvc
.perform(
MockMvcRequestBuilders.post("/import")
post("/import")
.headers(hasuraHeaders)
.contentType(MIME_TYPE_CSV)
.content(csv)
)
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
}

@Test
Expand All @@ -65,9 +67,10 @@ class ImportControllerTest @Autowired constructor(
} answers { resultVehicleScheduleFrameId }

executeImportTimetablesRequest("<some_csv_content>")
.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(status().isOk)
.andExpect(header().exists(HttpHeaders.CONTENT_LENGTH))
.andExpect(
MockMvcResultMatchers.content().json(
content().json(
"""
{
"vehicleScheduleFrameId": $resultVehicleScheduleFrameId
Expand All @@ -91,7 +94,8 @@ class ImportControllerTest @Autowired constructor(
} throws InvalidHastusDataException(resultErrorMessage)

executeImportTimetablesRequest("<invalid_csv_content>")
.andExpect(MockMvcResultMatchers.status().isBadRequest)
.andExpect(status().isBadRequest)
.andExpect(header().exists(HttpHeaders.CONTENT_LENGTH))
.andExpect(
constructExpectedErrorBody(resultErrorMessage)
)
Expand All @@ -113,7 +117,8 @@ class ImportControllerTest @Autowired constructor(
)

executeImportTimetablesRequest("<csv_content>")
.andExpect(MockMvcResultMatchers.status().isBadRequest)
.andExpect(status().isBadRequest)
.andExpect(header().exists(HttpHeaders.CONTENT_LENGTH))
.andExpect(
constructExpectedErrorBody(
"Could not find journey pattern reference for Hastus trips with the following route " +
Expand All @@ -135,7 +140,8 @@ class ImportControllerTest @Autowired constructor(
)

executeImportTimetablesRequest("<csv_content>")
.andExpect(MockMvcResultMatchers.status().isBadRequest)
.andExpect(status().isBadRequest)
.andExpect(header().exists(HttpHeaders.CONTENT_LENGTH))
.andExpect(
constructExpectedErrorBody(
"No journey pattern reference was found whose stop points correspond to the Hastus trip: " +
Expand All @@ -157,7 +163,8 @@ class ImportControllerTest @Autowired constructor(
} throws GraphQLAuthenticationFailedException(resultErrorMessage)

executeImportTimetablesRequest("<csv_content>")
.andExpect(MockMvcResultMatchers.status().isForbidden)
.andExpect(status().isForbidden)
.andExpect(header().exists(HttpHeaders.CONTENT_LENGTH))
.andExpect(
constructExpectedErrorBody(resultErrorMessage)
)
Expand All @@ -176,7 +183,8 @@ class ImportControllerTest @Autowired constructor(
} throws Exception(resultErrorMessage)

executeImportTimetablesRequest("<csv_content>")
.andExpect(MockMvcResultMatchers.status().isInternalServerError)
.andExpect(status().isInternalServerError)
.andExpect(header().exists(HttpHeaders.CONTENT_LENGTH))
.andExpect(
constructExpectedErrorBody(resultErrorMessage)
)
Expand All @@ -188,7 +196,7 @@ class ImportControllerTest @Autowired constructor(

companion object {
private fun constructExpectedErrorBody(errorMessage: String): ResultMatcher {
return MockMvcResultMatchers.content().json(
return content().json(
"""
{
"reason": "$errorMessage"
Expand Down