Skip to content

Commit 41786e1

Browse files
committed
Revised prison filter logic and rectified tests
1 parent 056b349 commit 41786e1

File tree

8 files changed

+20
-76
lines changed

8 files changed

+20
-76
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/PrisonerOffenderSearchGateway.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class PrisonerOffenderSearchGateway(
6060
}
6161
}
6262

63-
fun getPrisonerByCriteria(
63+
fun getPrisonerDetails(
6464
firstName: String?,
6565
lastName: String?,
6666
dateOfBirth: String?,

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

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ open class Person(
2727
@Schema(description = "HMPPS identifier", example = "2008/0545166T")
2828
val hmppsId: String? = null,
2929
val contactDetails: ContactDetailsWithEmailAndPhone? = null,
30-
val prisonId: String? = null,
3130
val currentRestriction: Boolean? = null,
3231
val restrictionMessage: String? = null,
3332
val currentExclusion: Boolean? = null,

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

+2-20
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class GetPrisonersService(
2222
): Response<List<Person>> {
2323
val prisonIds = filters?.prisons
2424
val responseFromPrisonerOffenderSearch =
25-
if (prisonIds != null && prisonIds.isEmpty()) {
25+
if (prisonIds == null) {
2626
// Hit global-search endpoint
2727
prisonerOffenderSearchGateway.getPersons(
2828
firstName,
@@ -32,13 +32,9 @@ class GetPrisonersService(
3232
)
3333
} else {
3434
// Hit prisoner-details endpoint
35-
prisonerOffenderSearchGateway.getPrisonerByCriteria(firstName, lastName, dateOfBirth, searchWithinAliases, prisonIds)
35+
prisonerOffenderSearchGateway.getPrisonerDetails(firstName, lastName, dateOfBirth, searchWithinAliases, prisonIds)
3636
}
3737

38-
if (responseFromPrisonerOffenderSearch == null) {
39-
return Response(emptyList(), listOf(UpstreamApiError(UpstreamApi.PRISONER_OFFENDER_SEARCH, UpstreamApiError.Type.ENTITY_NOT_FOUND, "Not found")))
40-
}
41-
4238
if (responseFromPrisonerOffenderSearch.errors.isNotEmpty()) {
4339
return Response(emptyList(), responseFromPrisonerOffenderSearch.errors)
4440
}
@@ -47,20 +43,6 @@ class GetPrisonersService(
4743
return Response(emptyList(), listOf(UpstreamApiError(UpstreamApi.PRISONER_OFFENDER_SEARCH, UpstreamApiError.Type.ENTITY_NOT_FOUND, "Not found")))
4844
}
4945

50-
if (!prisonIds.isNullOrEmpty()) {
51-
// Check each prisoner has a valid prisonId that matches the consumers filter config, if prison filter exists against the consumer
52-
responseFromPrisonerOffenderSearch.data.forEach { prisoner ->
53-
if (
54-
!filters.matchesPrison(prisoner.prisonId)
55-
) {
56-
return Response(
57-
data = emptyList(),
58-
errors = listOf(UpstreamApiError(UpstreamApi.PRISONER_OFFENDER_SEARCH, UpstreamApiError.Type.ENTITY_NOT_FOUND, "Not found")),
59-
)
60-
}
61-
}
62-
}
63-
6446
return Response(data = responseFromPrisonerOffenderSearch.data)
6547
}
6648
}

src/main/resources/application-test.yml

-6
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,12 @@ authorisation:
8787
- "/v1/prison/prisoners/[^/]*$"
8888
- "/v1/prison/prisoners"
8989
- "/v1/prison/.*/prisoners/.*/balances$"
90-
filters:
91-
prisons:
9290
config-test:
9391
include:
9492
- "/v1/config/authorisation"
9593
no-include:
9694
empty-include:
9795
include:
98-
populated-prison-filters:
99-
filters:
100-
prisons:
101-
- "NOMATCH_PRISON"
10296
limited-prisons:
10397
include:
10498
- "/v1/prison/.*/prisoners/.*/balances$"

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ class BalancesControllerTest(
5353
it("gets the balances for a person with the matching ID") {
5454
mockMvc.performAuthorised(basePath)
5555

56-
verify(getBalancesForPersonService, VerificationModeFactory.times(1)).execute(prisonId, hmppsId, ConsumerFilters(emptyList()))
56+
verify(getBalancesForPersonService, VerificationModeFactory.times(1)).execute(prisonId, hmppsId, null)
5757
}
5858

5959
it("returns the correct balances data") {
60-
whenever(getBalancesForPersonService.execute(prisonId, hmppsId, ConsumerFilters(emptyList()))).thenReturn(
60+
whenever(getBalancesForPersonService.execute(prisonId, hmppsId, null)).thenReturn(
6161
Response(
6262
data = balance,
6363
),
@@ -91,7 +91,7 @@ class BalancesControllerTest(
9191
}
9292

9393
it("returns a 404 NOT FOUND status code when person isn't found in probation offender search") {
94-
whenever(getBalancesForPersonService.execute(prisonId, hmppsId, ConsumerFilters(emptyList()))).thenReturn(
94+
whenever(getBalancesForPersonService.execute(prisonId, hmppsId, null)).thenReturn(
9595
Response(
9696
data = null,
9797
errors =
@@ -110,7 +110,7 @@ class BalancesControllerTest(
110110
}
111111

112112
it("returns a 404 NOT FOUND status code when person isn't found in Nomis") {
113-
whenever(getBalancesForPersonService.execute(prisonId, hmppsId, ConsumerFilters(emptyList()))).thenReturn(
113+
whenever(getBalancesForPersonService.execute(prisonId, hmppsId, null)).thenReturn(
114114
Response(
115115
data = null,
116116
errors =
@@ -129,7 +129,7 @@ class BalancesControllerTest(
129129
}
130130

131131
it("returns a 400 BAD REQUEST status code when account isn't found in the upstream API") {
132-
whenever(getBalancesForPersonService.execute(prisonId, hmppsId, ConsumerFilters(emptyList()))).thenReturn(
132+
whenever(getBalancesForPersonService.execute(prisonId, hmppsId, null)).thenReturn(
133133
Response(
134134
data = null,
135135
errors =
@@ -148,7 +148,7 @@ class BalancesControllerTest(
148148
}
149149

150150
it("returns a 500 INTERNAL SERVER ERROR status code when balance isn't found in the upstream API") {
151-
whenever(getBalancesForPersonService.execute(prisonId, hmppsId, ConsumerFilters(emptyList()))).thenThrow(
151+
whenever(getBalancesForPersonService.execute(prisonId, hmppsId, null)).thenThrow(
152152
IllegalStateException("Error occurred while trying to get accounts for person with id: $hmppsId"),
153153
)
154154

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

-2
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ internal class PersonControllerTest(
334334
"pncId":null,
335335
"hmppsId":null,
336336
"contactDetails":null,
337-
"prisonId": null,
338337
"currentRestriction": null,
339338
"restrictionMessage": null,
340339
"currentExclusion": null,
@@ -358,7 +357,6 @@ internal class PersonControllerTest(
358357
"pncId":null,
359358
"hmppsId":null,
360359
"contactDetails":null,
361-
"prisonId": null,
362360
"currentRestriction": null,
363361
"restrictionMessage": null,
364362
"currentExclusion": null,

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/prison/PrisonControllerTest.kt

+4-33
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal class PrisonControllerTest(
4545
val firstName = "Barry"
4646
val lastName = "Allen"
4747
val dateOfBirth = "2023-03-01"
48-
val emptyConsumerFilter = ConsumerFilters(emptyList())
48+
val emptyConsumerFilter = null
4949

5050
describe("GET $basePath") {
5151
}
@@ -80,7 +80,6 @@ internal class PrisonControllerTest(
8080
exclusionMessage = "An exclusion is present",
8181
currentRestriction = true,
8282
restrictionMessage = "A restriction is present",
83-
prisonId = "VALID_PRISON",
8483
),
8584
),
8685
)
@@ -106,7 +105,6 @@ internal class PrisonControllerTest(
106105
"pncId": "PNC123456",
107106
"hmppsId": null,
108107
"contactDetails": null,
109-
"prisonId": "VALID_PRISON",
110108
"currentRestriction": true,
111109
"restrictionMessage": "A restriction is present",
112110
"currentExclusion": true,
@@ -211,9 +209,9 @@ internal class PrisonControllerTest(
211209
result.response.status.shouldBe(500)
212210
}
213211

214-
it("returns 404 when prisoner query gets no result") {
212+
it("returns 500 when prisoner query gets no result") {
215213

216-
whenever(getPrisonersService.execute("Barry", "Allen", "2023-03-01", false, emptyConsumerFilter)).thenReturn(
214+
whenever(getPrisonersService.execute("Barry", "Allen", "2023-03-01", false, ConsumerFilters(emptyList()))).thenReturn(
217215
Response(
218216
data = emptyList(),
219217
errors =
@@ -228,7 +226,7 @@ internal class PrisonControllerTest(
228226
)
229227

230228
val result = mockMvc.performAuthorised("$basePath/prisoners?first_name=$firstName&last_name=$lastName&date_of_birth=$dateOfBirth")
231-
result.response.status.shouldBe(404)
229+
result.response.status.shouldBe(500)
232230
}
233231

234232
it("returns a 200 OK status code") {
@@ -255,32 +253,5 @@ internal class PrisonControllerTest(
255253

256254
result.response.status.shouldBe(HttpStatus.OK.value())
257255
}
258-
259-
it("returns a 403 when the consumer filters prisonIds does not match the prisonId") {
260-
whenever(getPrisonersService.execute(firstName, lastName, dateOfBirth, false, ConsumerFilters(prisons = listOf("NOMATCH_PRISON")))).thenReturn(
261-
Response(
262-
data =
263-
listOf(
264-
Person(
265-
firstName = "Barry",
266-
lastName = "Allen",
267-
middleName = "Jonas",
268-
dateOfBirth = LocalDate.parse("2023-03-01"),
269-
prisonId = "INVALID_PRISON",
270-
),
271-
Person(
272-
firstName = "Barry",
273-
lastName = "Allen",
274-
middleName = "Rock",
275-
dateOfBirth = LocalDate.parse("2022-07-22"),
276-
prisonId = "INVALID_PRISON",
277-
),
278-
),
279-
),
280-
)
281-
val result = mockMvc.performAuthorisedWithCN("$basePath/prisoners?first_name=$firstName&last_name=$lastName&date_of_birth=$dateOfBirth&search_within_aliases=false", "populated-prison-filters")
282-
283-
result.response.status.shouldBe(HttpStatus.FORBIDDEN.value())
284-
}
285256
},
286257
)

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal class GetPrisonersServiceTest(
4646
),
4747
)
4848

49-
val result = getPrisonersService.execute("Qui-gon", "Jin", "1966-10-25", false, ConsumerFilters(emptyList()))
49+
val result = getPrisonersService.execute("Qui-gon", "Jin", "1966-10-25", false, null)
5050
result?.errors.shouldBe(
5151
listOf(
5252
UpstreamApiError(UpstreamApi.PRISONER_OFFENDER_SEARCH, UpstreamApiError.Type.ENTITY_NOT_FOUND, "MockError"),
@@ -62,7 +62,7 @@ internal class GetPrisonersServiceTest(
6262
),
6363
)
6464

65-
val result = getPrisonersService.execute("Qui-gon", "Jin", "1966-10-25", false, ConsumerFilters(emptyList()))
65+
val result = getPrisonersService.execute("Qui-gon", "Jin", "1966-10-25", false, null)
6666
result?.data?.shouldBe(
6767
people,
6868
)
@@ -80,18 +80,18 @@ internal class GetPrisonersServiceTest(
8080
),
8181
)
8282

83-
val result = getPrisonersService.execute("Qui-gon", "Jin", "1966-10-25", false, ConsumerFilters(prisons = listOf("")))
83+
val result = getPrisonersService.execute("Qui-gon", "Jin", "1966-10-25", false, null)
8484
result?.errors.shouldBe(
8585
listOf(
86-
UpstreamApiError(UpstreamApi.PRISONER_OFFENDER_SEARCH, UpstreamApiError.Type.ENTITY_NOT_FOUND, "Not found"),
86+
UpstreamApiError(UpstreamApi.PRISONER_OFFENDER_SEARCH, UpstreamApiError.Type.ENTITY_NOT_FOUND, "MockError"),
8787
),
8888
)
8989
}
9090

9191
it("returns an error when the person queried for is not in a prisonId matching their config") {
9292
val prisonIds = ConsumerFilters(prisons = listOf("FAKE_PRISON"))
9393
whenever(request.getAttribute("filters")).thenReturn(prisonIds)
94-
whenever(prisonerOffenderSearchGateway.getPrisonerByCriteria("Qui-gon", "Jin", "1966-10-25", false, prisonIds.prisons)).thenReturn(
94+
whenever(prisonerOffenderSearchGateway.getPrisonerDetails("Qui-gon", "Jin", "1966-10-25", false, prisonIds.prisons)).thenReturn(
9595
Response(
9696
errors =
9797
listOf(
@@ -110,10 +110,10 @@ internal class GetPrisonersServiceTest(
110110
}
111111

112112
it("returns the persons data when the person queried for is in a prisonId matching their config") {
113-
val people = listOf(Person(firstName = "Qui-gon", lastName = "Jin", prisonId = "VALID_PRISON"), Person(firstName = "John", lastName = "Jin", prisonId = "VALID_PRISON"))
113+
val people = listOf(Person(firstName = "Qui-gon", lastName = "Jin"), Person(firstName = "John", lastName = "Jin"))
114114
val prisonIds = ConsumerFilters(prisons = listOf("VALID_PRISON"))
115115
whenever(request.getAttribute("filters")).thenReturn(prisonIds)
116-
whenever(prisonerOffenderSearchGateway.getPrisonerByCriteria("Qui-gon", "Jin", "1966-10-25", false, prisonIds.prisons)).thenReturn(
116+
whenever(prisonerOffenderSearchGateway.getPrisonerDetails("Qui-gon", "Jin", "1966-10-25", false, prisonIds.prisons)).thenReturn(
117117
Response(
118118
data = people,
119119
),

0 commit comments

Comments
 (0)