Skip to content

Commit b8f4702

Browse files
authored
Hia 765 cell location not found if person has no probation record (#531)
* HIA-765 - update logic to check if nomis number is provided * HIA-765 - update test * HIA-765 - update test * HIA-765 - minor refactor * HIA-765 - fix lint errors
1 parent 05d75b5 commit b8f4702

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetCellLocationForPersonService.kt

+17-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,23 @@ class GetCellLocationForPersonService(
1212
@Autowired val prisonerOffenderSearchGateway: PrisonerOffenderSearchGateway,
1313
) {
1414
fun execute(hmppsId: String): Response<CellLocation?> {
15-
val personResponse = getPersonService.execute(hmppsId = hmppsId)
16-
1715
val prisonResponse =
18-
personResponse.data?.identifiers?.nomisNumber?.let {
19-
prisonerOffenderSearchGateway.getPrisonOffender(nomsNumber = it)
16+
when (isNomsNumber(hmppsId)) {
17+
true -> prisonerOffenderSearchGateway.getPrisonOffender(nomsNumber = hmppsId)
18+
else -> {
19+
val personResponse = getPersonService.execute(hmppsId = hmppsId)
20+
21+
if (personResponse.data == null) {
22+
return Response(
23+
data = CellLocation(),
24+
errors = personResponse.errors,
25+
)
26+
}
27+
28+
personResponse.data.identifiers.nomisNumber?.let {
29+
prisonerOffenderSearchGateway.getPrisonOffender(nomsNumber = it)
30+
}
31+
}
2032
}
2133

2234
val cellLocation =
@@ -28,7 +40,7 @@ class GetCellLocationForPersonService(
2840

2941
return Response(
3042
data = cellLocation,
31-
errors = personResponse.errors + (prisonResponse?.errors ?: emptyList()),
43+
errors = prisonResponse?.errors ?: emptyList(),
3244
)
3345
}
3446
}

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetPersonService.kt

+4
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,7 @@ class GetPersonService(
106106

107107
fun getPersonFromNomis(nomisNumber: String) = prisonerOffenderSearchGateway.getPrisonOffender(nomisNumber)
108108
}
109+
110+
fun isNomsNumber(id: String?): Boolean {
111+
return id?.matches(Regex("^[A-Z]\\d{4}[A-Z]{2}+$")) == true
112+
}

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/integration/person/PersonIntegrationTest.kt

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.integration.person
22

33
import org.junit.jupiter.api.Test
4+
import org.junit.jupiter.params.ParameterizedTest
5+
import org.junit.jupiter.params.provider.ValueSource
46
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
57
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
68
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.integration.IntegrationTestBase
@@ -44,9 +46,10 @@ class PersonIntegrationTest : IntegrationTestBase() {
4446
)
4547
}
4648

47-
@Test
48-
fun `returns person cell location if in prison`() {
49-
callApi("$basePath/$pnc/cell-location")
49+
@ParameterizedTest
50+
@ValueSource(strings = ["2004%2F13116M", "G2996UX"])
51+
fun `returns person cell location if in prison`(hmppsId: String) {
52+
callApi("$basePath/$hmppsId/cell-location")
5053
.andExpect(status().isOk)
5154
.andExpect(
5255
content().json(

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/services/GetCellLocationForPersonServiceTest.kt

+24-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import org.mockito.internal.verification.VerificationModeFactory
88
import org.mockito.kotlin.verify
99
import org.mockito.kotlin.whenever
1010
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer
11-
import org.springframework.boot.test.mock.mockito.MockBean
1211
import org.springframework.test.context.ContextConfiguration
12+
import org.springframework.test.context.bean.override.mockito.MockitoBean
1313
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.PrisonerOffenderSearchGateway
1414
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.CellLocation
1515
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Identifiers
@@ -24,8 +24,8 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.prisoneroffenders
2424
classes = [GetCellLocationForPersonService::class],
2525
)
2626
internal class GetCellLocationForPersonServiceTest(
27-
@MockBean val getPersonService: GetPersonService,
28-
@MockBean val prisonerOffenderSearchGateway: PrisonerOffenderSearchGateway,
27+
@MockitoBean val getPersonService: GetPersonService,
28+
@MockitoBean val prisonerOffenderSearchGateway: PrisonerOffenderSearchGateway,
2929
private val getCellLocationForPersonService: GetCellLocationForPersonService,
3030
) : DescribeSpec(
3131
{
@@ -76,5 +76,26 @@ internal class GetCellLocationForPersonServiceTest(
7676
response.errors.first().causedBy.shouldBe(UpstreamApi.PROBATION_OFFENDER_SEARCH)
7777
response.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND)
7878
}
79+
80+
it("returns the upstream error when nomis id is not found") {
81+
whenever(prisonerOffenderSearchGateway.getPrisonOffender(nomisNumber)).thenReturn(
82+
Response(
83+
data = null,
84+
errors =
85+
listOf(
86+
UpstreamApiError(
87+
causedBy = UpstreamApi.PRISONER_OFFENDER_SEARCH,
88+
type = UpstreamApiError.Type.ENTITY_NOT_FOUND,
89+
),
90+
),
91+
),
92+
)
93+
94+
val response = getCellLocationForPersonService.execute(nomisNumber)
95+
96+
response.errors.shouldHaveSize(1)
97+
response.errors.first().causedBy.shouldBe(UpstreamApi.PRISONER_OFFENDER_SEARCH)
98+
response.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND)
99+
}
79100
},
80101
)

0 commit comments

Comments
 (0)