Skip to content
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

better handling of nulls and test #519

Merged
merged 2 commits into from
Nov 18, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.time.Instant
import java.time.LocalDate

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
@JsonDeserialize(using = InductionScheduleDeserializer::class)
data class InductionSchedule(
@Schema(
Expand Down Expand Up @@ -69,8 +69,8 @@ class InductionScheduleDeserializer : JsonDeserializer<InductionSchedule>() {
val scheduleCalculationRule = node["scheduleCalculationRule"]?.asText()
val systemUpdatedBy = node["updatedByDisplayName"]?.asText()
val systemUpdatedAt = node["updatedAt"]?.asText()?.let { Instant.parse(it) }
val inductionPerformedBy = node["inductionPerformedBy"]?.asText()
val inductionPerformedAt = node["inductionPerformedAt"]?.asText()?.let { LocalDate.parse(it) }
val inductionPerformedBy = node["inductionPerformedBy"]?.takeUnless { it.isNull }?.asText()
val inductionPerformedAt = node["inductionPerformedAt"]?.takeUnless { it.isNull }?.asText()?.let { LocalDate.parse(it) }

return InductionSchedule(
deadlineDate = deadlineDate,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1.person

import com.fasterxml.jackson.databind.ObjectMapper
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.InductionSchedule
import java.time.Instant
import java.time.LocalDate

class InductionScheduleTest {
private val objectMapper =
ObjectMapper().apply {
// Register the custom deserializer
registerModule(
com.fasterxml.jackson.module.kotlin.KotlinModule.Builder().build(),
)
}

@Test
fun `should deserialize JSON into InductionSchedule object`() {
// Given
val json = """
{
"prisonNumber": "A1234BC",
"deadlineDate": "2023-09-01",
"scheduleStatus": "SCHEDULED",
"scheduleCalculationRule": "NEW_PRISON_ADMISSION",
"updatedByDisplayName": "John Smith",
"updatedAt": "2023-06-19T09:39:44Z",
"inductionPerformedBy": "Fred Jones",
"inductionPerformedAt": "2023-06-30"
}
"""

// When
val result = objectMapper.readValue(json, InductionSchedule::class.java)

// Then
assertEquals("A1234BC", result.nomisNumber)
assertEquals(LocalDate.of(2023, 9, 1), result.deadlineDate)
assertEquals("SCHEDULED", result.status)
assertEquals("NEW_PRISON_ADMISSION", result.calculationRule)
assertEquals("John Smith", result.systemUpdatedBy)
assertEquals(Instant.parse("2023-06-19T09:39:44Z"), result.systemUpdatedAt)
assertEquals("Fred Jones", result.inductionPerformedBy)
assertEquals(LocalDate.of(2023, 6, 30), result.inductionPerformedAt)
}

@Test
fun `should handle missing fields in JSON`() {
// Given
val json = """
{
"prisonNumber": "A1234BC"
}
"""

// When
val result = objectMapper.readValue(json, InductionSchedule::class.java)

// Then
assertEquals("A1234BC", result.nomisNumber)
assertNull(result.deadlineDate)
assertNull(result.status)
assertNull(result.calculationRule)
assertNull(result.systemUpdatedBy)
assertNull(result.systemUpdatedAt)
assertNull(result.inductionPerformedBy)
assertNull(result.inductionPerformedAt)
}

@Test
fun `should handle null fields in JSON`() {
// Given
val json = """
{
"prisonNumber": "A1234BC",
"deadlineDate": "2023-09-01",
"scheduleStatus": "SCHEDULED",
"scheduleCalculationRule": "NEW_PRISON_ADMISSION",
"updatedByDisplayName": "John Smith",
"updatedAt": "2023-06-19T09:39:44Z",
"inductionPerformedBy": null,
"inductionPerformedAt": null
}
"""

// When
val result = objectMapper.readValue(json, InductionSchedule::class.java)

// Then
assertEquals("A1234BC", result.nomisNumber)
assertEquals(LocalDate.of(2023, 9, 1), result.deadlineDate)
assertEquals("SCHEDULED", result.status)
assertEquals("NEW_PRISON_ADMISSION", result.calculationRule)
assertEquals("John Smith", result.systemUpdatedBy)
assertEquals(Instant.parse("2023-06-19T09:39:44Z"), result.systemUpdatedAt)
assertNull(result.inductionPerformedAt)
assertNull(result.inductionPerformedBy)
}
}
Loading