Skip to content

Commit 698bab9

Browse files
lint
1 parent 6234dee commit 698bab9

File tree

3 files changed

+105
-7
lines changed

3 files changed

+105
-7
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/hmpps/InductionSchedule.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import java.time.Instant
1212
import java.time.LocalDate
1313

1414
@JsonIgnoreProperties(ignoreUnknown = true)
15-
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
15+
@JsonInclude(JsonInclude.Include.USE_DEFAULTS)
1616
@JsonDeserialize(using = InductionScheduleDeserializer::class)
1717
data class InductionSchedule(
1818
@Schema(
@@ -69,8 +69,8 @@ class InductionScheduleDeserializer : JsonDeserializer<InductionSchedule>() {
6969
val scheduleCalculationRule = node["scheduleCalculationRule"]?.asText()
7070
val systemUpdatedBy = node["updatedByDisplayName"]?.asText()
7171
val systemUpdatedAt = node["updatedAt"]?.asText()?.let { Instant.parse(it) }
72-
val inductionPerformedBy = node["inductionPerformedBy"]?.asText()
73-
val inductionPerformedAt = node["inductionPerformedAt"]?.asText()?.let { LocalDate.parse(it) }
72+
val inductionPerformedBy = node["inductionPerformedBy"]?.takeUnless { it.isNull }?.asText()
73+
val inductionPerformedAt = node["inductionPerformedAt"]?.takeUnless { it.isNull }?.asText()?.let { LocalDate.parse(it) }
7474

7575
return InductionSchedule(
7676
deadlineDate = deadlineDate,

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/person/InductionSchedule.kt

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1.person
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import org.junit.jupiter.api.Assertions.assertEquals
5+
import org.junit.jupiter.api.Assertions.assertNull
6+
import org.junit.jupiter.api.Test
7+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.InductionSchedule
8+
import java.time.Instant
9+
import java.time.LocalDate
10+
11+
class InductionScheduleTest {
12+
private val objectMapper =
13+
ObjectMapper().apply {
14+
// Register the custom deserializer
15+
registerModule(
16+
com.fasterxml.jackson.module.kotlin.KotlinModule.Builder().build(),
17+
)
18+
}
19+
20+
@Test
21+
fun `should deserialize JSON into InductionSchedule object`() {
22+
// Given
23+
val json = """
24+
{
25+
"prisonNumber": "A1234BC",
26+
"deadlineDate": "2023-09-01",
27+
"scheduleStatus": "SCHEDULED",
28+
"scheduleCalculationRule": "NEW_PRISON_ADMISSION",
29+
"updatedByDisplayName": "John Smith",
30+
"updatedAt": "2023-06-19T09:39:44Z",
31+
"inductionPerformedBy": "Fred Jones",
32+
"inductionPerformedAt": "2023-06-30"
33+
}
34+
"""
35+
36+
// When
37+
val result = objectMapper.readValue(json, InductionSchedule::class.java)
38+
39+
// Then
40+
assertEquals("A1234BC", result.nomisNumber)
41+
assertEquals(LocalDate.of(2023, 9, 1), result.deadlineDate)
42+
assertEquals("SCHEDULED", result.status)
43+
assertEquals("NEW_PRISON_ADMISSION", result.calculationRule)
44+
assertEquals("John Smith", result.systemUpdatedBy)
45+
assertEquals(Instant.parse("2023-06-19T09:39:44Z"), result.systemUpdatedAt)
46+
assertEquals("Fred Jones", result.inductionPerformedBy)
47+
assertEquals(LocalDate.of(2023, 6, 30), result.inductionPerformedAt)
48+
}
49+
50+
@Test
51+
fun `should handle missing fields in JSON`() {
52+
// Given
53+
val json = """
54+
{
55+
"prisonNumber": "A1234BC"
56+
}
57+
"""
58+
59+
// When
60+
val result = objectMapper.readValue(json, InductionSchedule::class.java)
61+
62+
// Then
63+
assertEquals("A1234BC", result.nomisNumber)
64+
assertNull(result.deadlineDate)
65+
assertNull(result.status)
66+
assertNull(result.calculationRule)
67+
assertNull(result.systemUpdatedBy)
68+
assertNull(result.systemUpdatedAt)
69+
assertNull(result.inductionPerformedBy)
70+
assertNull(result.inductionPerformedAt)
71+
}
72+
73+
@Test
74+
fun `should handle null fields in JSON`() {
75+
// Given
76+
val json = """
77+
{
78+
"prisonNumber": "A1234BC",
79+
"deadlineDate": "2023-09-01",
80+
"scheduleStatus": "SCHEDULED",
81+
"scheduleCalculationRule": "NEW_PRISON_ADMISSION",
82+
"updatedByDisplayName": "John Smith",
83+
"updatedAt": "2023-06-19T09:39:44Z",
84+
"inductionPerformedBy": null,
85+
"inductionPerformedAt": null
86+
}
87+
"""
88+
89+
// When
90+
val result = objectMapper.readValue(json, InductionSchedule::class.java)
91+
92+
// Then
93+
assertEquals("A1234BC", result.nomisNumber)
94+
assertEquals(LocalDate.of(2023, 9, 1), result.deadlineDate)
95+
assertEquals("SCHEDULED", result.status)
96+
assertEquals("NEW_PRISON_ADMISSION", result.calculationRule)
97+
assertEquals("John Smith", result.systemUpdatedBy)
98+
assertEquals(Instant.parse("2023-06-19T09:39:44Z"), result.systemUpdatedAt)
99+
assertNull(result.inductionPerformedAt)
100+
assertNull(result.inductionPerformedBy)
101+
}
102+
}

0 commit comments

Comments
 (0)