-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API for fetching /combine target vehicle schedule frame
Similar to the /to-replace API (from which this is largely copypasted) that already existed for /replace.
- Loading branch information
Showing
5 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/test/kotlin/fi/hsl/jore4/timetables/api/TimetablesToCombineApiTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package fi.hsl.jore4.timetables.api | ||
|
||
import com.ninjasquad.springmockk.MockkBean | ||
import fi.hsl.jore.jore4.jooq.vehicle_schedule.tables.pojos.VehicleScheduleFrame | ||
import fi.hsl.jore4.timetables.enumerated.TimetablesPriority | ||
import fi.hsl.jore4.timetables.service.CombineTimetablesService | ||
import io.mockk.every | ||
import io.mockk.junit5.MockKExtension | ||
import io.mockk.verify | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.http.MediaType | ||
import org.springframework.test.context.ActiveProfiles | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.ResultActions | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
import java.time.LocalDate | ||
import java.util.UUID | ||
|
||
@ExtendWith(MockKExtension::class) | ||
@AutoConfigureMockMvc | ||
@SpringBootTest | ||
@ActiveProfiles("test") | ||
class TimetablesToCombineApiTest(@Autowired val mockMvc: MockMvc) { | ||
@MockkBean | ||
private lateinit var combineTimetablesService: CombineTimetablesService | ||
|
||
private val defaultTargetFrame = VehicleScheduleFrame( | ||
vehicleScheduleFrameId = UUID.fromString("379076ee-d595-47e3-8050-2610d594b57c"), | ||
validityStart = LocalDate.now(), | ||
validityEnd = LocalDate.now(), | ||
priority = 20, | ||
label = "label" | ||
) | ||
private val defaultToCombineTargetId = defaultTargetFrame.vehicleScheduleFrameId | ||
private fun executeToCombineTimetablesRequest( | ||
stagingFrameId: UUID, | ||
targetPriority: Int | ||
): ResultActions { | ||
return mockMvc.perform( | ||
MockMvcRequestBuilders.get("/timetables/to-combine") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.param("stagingVehicleScheduleFrameId", stagingFrameId.toString()) | ||
.param("targetPriority", targetPriority.toString()) | ||
) | ||
} | ||
|
||
@Test | ||
fun `returns 200 and correct response when called successfully`() { | ||
val stagingVehicleScheduleFrameId = UUID.fromString("81f109d1-dbe2-412a-996e-aa510416b2e4") | ||
val targetPriority = 10 | ||
|
||
every { | ||
combineTimetablesService.fetchTargetVehicleScheduleFrame( | ||
stagingVehicleScheduleFrameId, | ||
TimetablesPriority.fromInt(targetPriority) | ||
) | ||
} answers { defaultTargetFrame } | ||
|
||
executeToCombineTimetablesRequest(stagingVehicleScheduleFrameId, targetPriority) | ||
.andExpect(status().isOk) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect( | ||
content().json( | ||
""" | ||
{ | ||
"toCombineTargetVehicleScheduleFrameId": $defaultToCombineTargetId | ||
} | ||
""".trimIndent(), | ||
true | ||
) | ||
) | ||
|
||
verify(exactly = 1) { | ||
combineTimetablesService.fetchTargetVehicleScheduleFrame( | ||
stagingVehicleScheduleFrameId, | ||
TimetablesPriority.fromInt(targetPriority) | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun `throws a 400 error when parsing target priority fails`() { | ||
val errorMessage = "Failed to parse target priority" | ||
val stagingVehicleScheduleFrameId = UUID.fromString("023281cd-51e9-4544-a2af-7b7e268e3a3a") | ||
val invalidTargetPriorityInput = 9999 | ||
|
||
executeToCombineTimetablesRequest(stagingVehicleScheduleFrameId, invalidTargetPriorityInput) | ||
.andExpect(status().isBadRequest) | ||
.andExpect(content().contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect( | ||
content().json( | ||
""" | ||
{ | ||
"message": "$errorMessage", | ||
"extensions": { | ||
"code": 400, | ||
"type": "TargetPriorityParsingError", | ||
"targetPriority": $invalidTargetPriorityInput | ||
} | ||
} | ||
""".trimIndent(), | ||
true | ||
) | ||
) | ||
verify(exactly = 0) { | ||
combineTimetablesService.fetchTargetVehicleScheduleFrame( | ||
stagingVehicleScheduleFrameId, | ||
any() | ||
) | ||
} | ||
} | ||
} |