Skip to content

Commit

Permalink
Refactor TimetablesPriority.fromInt usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Leitsi committed Dec 14, 2023
1 parent 83ffb31 commit a454026
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
25 changes: 10 additions & 15 deletions src/main/kotlin/fi/hsl/jore4/timetables/api/TimetablesController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TimetablesController(

) {
@AssertTrue(message = "false")
fun isTargetPriorityValid(): Boolean = runCatching { TimetablesPriority.fromInt(targetPriority) }.isSuccess
fun isTargetPriorityValid(): Boolean = TimetablesPriority.fromInt(targetPriority) != null
}

data class CombineTimetablesResponseBody(
Expand All @@ -60,7 +60,7 @@ class TimetablesController(
LOGGER.debug { "Combine api, request: $requestBody" }
val combineResult = combineTimetablesService.combineTimetables(
requestBody.stagingVehicleScheduleFrameIds,
TimetablesPriority.fromInt(requestBody.targetPriority)
parseTargetPriority(requestBody.targetPriority)
)

return ResponseEntity
Expand All @@ -80,7 +80,7 @@ class TimetablesController(
LOGGER.debug { "Replace api, request: $requestBody" }
val replaceResult = replaceTimetablesService.replaceTimetables(
requestBody.stagingVehicleScheduleFrameIds,
TimetablesPriority.fromInt(requestBody.targetPriority)
parseTargetPriority(requestBody.targetPriority)
)

return ResponseEntity
Expand All @@ -107,14 +107,9 @@ class TimetablesController(
): ResponseEntity<ToReplaceTimetablesResponseBody> {
LOGGER.info { "ToReplace api, stagingVehicleScheduleFrameId: $stagingVehicleScheduleFrameId, targetPriority: $targetPriority" }

val targetPriorityEnumResult = runCatching { TimetablesPriority.fromInt(targetPriority) }
if (targetPriorityEnumResult.isFailure) {
throw TargetPriorityParsingException("Failed to parse target priority", targetPriority)
}

val vehicleScheduleFrameIds = replaceTimetablesService.fetchVehicleScheduleFramesToReplace(
stagingVehicleScheduleFrameId,
targetPriorityEnumResult.getOrThrow()
parseTargetPriority(targetPriority)
).mapNotNull { it.vehicleScheduleFrameId }

return ResponseEntity.status(HttpStatus.OK)
Expand All @@ -130,14 +125,9 @@ class TimetablesController(
): ResponseEntity<ToCombineTimetablesResponseBody> {
LOGGER.info { "ToCombine api, stagingVehicleScheduleFrameId: $stagingVehicleScheduleFrameId, targetPriority: $targetPriority" }

val targetPriorityEnumResult = runCatching { TimetablesPriority.fromInt(targetPriority) }
if (targetPriorityEnumResult.isFailure) {
throw TargetPriorityParsingException("Failed to parse target priority", targetPriority)
}

val targetVehicleScheduleFrame = combineTimetablesService.fetchTargetVehicleScheduleFrame(
stagingVehicleScheduleFrameId,
targetPriorityEnumResult.getOrThrow()
parseTargetPriority(targetPriority)
)

return ResponseEntity.status(HttpStatus.OK)
Expand Down Expand Up @@ -187,4 +177,9 @@ class TimetablesController(

return ResponseEntity(JoreErrorResponse(ex.message, errorExtensions), httpStatus)
}

companion object {
private fun parseTargetPriority(targetPriority: Int) = TimetablesPriority.fromInt(targetPriority)
?: throw TargetPriorityParsingException("Failed to parse target priority", targetPriority)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ enum class TimetablesPriority(val value: Int) {
STAGING(40);

companion object {
fun fromInt(value: Int): TimetablesPriority {
return values().first { it.value == value }
fun fromInt(value: Int): TimetablesPriority? {
return values().firstOrNull { it.value == value }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ class TimetablesToReplaceApiTest(@Autowired val mockMvc: MockMvc) {
@Test
fun `returns 200 and correct response when called successfully`() {
val stagingVehicleScheduleFrameId = UUID.fromString("9e758776-2af1-49c7-8bd0-c0805b833b20")
val targetPriority = 10
val targetPriority = TimetablesPriority.STANDARD

every {
replaceTimetablesService.fetchVehicleScheduleFramesToReplace(
stagingVehicleScheduleFrameId,
TimetablesPriority.fromInt(targetPriority)
targetPriority
)
} answers { defaultVehicleScheduleFrames }

executeToReplaceTimetablesRequest(stagingVehicleScheduleFrameId, targetPriority)
executeToReplaceTimetablesRequest(stagingVehicleScheduleFrameId, targetPriority.value)
.andExpect(status().isOk)
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(
Expand All @@ -88,7 +88,7 @@ class TimetablesToReplaceApiTest(@Autowired val mockMvc: MockMvc) {
verify(exactly = 1) {
replaceTimetablesService.fetchVehicleScheduleFramesToReplace(
stagingVehicleScheduleFrameId,
TimetablesPriority.fromInt(targetPriority)
targetPriority
)
}
}
Expand Down

0 comments on commit a454026

Please sign in to comment.