Skip to content

Commit 646bdfe

Browse files
Controller change
1 parent 436d3bc commit 646bdfe

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

src/main/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/controllers/v1/person/AlertsController.kt

+16
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,20 @@ class AlertsController(
3636
auditService.createEvent("GET_PERSON_ALERTS", mapOf("hmppsId" to hmppsId))
3737
return response.data.paginateWith(page, perPage)
3838
}
39+
40+
@GetMapping("{encodedHmppsId}/alerts/pnd")
41+
fun getPersonAlertsPND(
42+
@PathVariable encodedHmppsId: String,
43+
@RequestParam(required = false, defaultValue = "1", name = "page") page: Int,
44+
@RequestParam(required = false, defaultValue = "10", name = "perPage") perPage: Int,
45+
): PaginatedResponse<Alert> {
46+
val hmppsId = encodedHmppsId.decodeUrlCharacters()
47+
val response = getAlertsForPersonService.execute(hmppsId)
48+
49+
if (response.hasError(UpstreamApiError.Type.ENTITY_NOT_FOUND)) {
50+
throw EntityNotFoundException("Could not find person with id: $hmppsId")
51+
}
52+
auditService.createEvent("GET_PERSON_ALERTS_PND", mapOf("hmppsId" to hmppsId))
53+
return response.data.paginateWith(page, perPage)
54+
}
3955
}

src/main/resources/application-test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ authorisation:
4747
- "/v1/persons/.*/addresses"
4848
- "/v1/persons/.*/offences"
4949
- "/v1/persons/.*/alerts"
50+
- "/v1/persons/.*/alerts/pnd"
5051
- "/v1/persons/.*/sentences"
5152
- "/v1/persons/.*/sentences/latest-key-dates-and-adjustments"
5253
- "/v1/persons/.*/risks/scores"

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

+90
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ internal class AlertsControllerTest(
3939
val hmppsId = "9999/11111A"
4040
val encodedHmppsId = URLEncoder.encode(hmppsId, StandardCharsets.UTF_8)
4141
val path = "/v1/persons/$encodedHmppsId/alerts"
42+
val pndPath = "/v1/persons/$encodedHmppsId/alerts/pnd"
4243
val mockMvc = IntegrationAPIMockMvc(springMockMvc)
4344

4445
describe("GET $path") {
@@ -188,5 +189,94 @@ internal class AlertsControllerTest(
188189
)
189190
}
190191
}
192+
describe("GET $pndPath") {
193+
beforeTest {
194+
Mockito.reset(getAlertsForPersonService)
195+
Mockito.reset(auditService)
196+
whenever(getAlertsForPersonService.execute(hmppsId)).thenReturn(
197+
Response(
198+
data =
199+
listOf(
200+
Alert(
201+
offenderNo = "A1111BB",
202+
type = "B",
203+
typeDescription = "Security again",
204+
code = "BBB",
205+
codeDescription = "For Release",
206+
comment = "IS83",
207+
dateCreated = LocalDate.parse("2022-09-01"),
208+
dateExpired = LocalDate.parse("2023-09-01"),
209+
expired = false,
210+
active = false,
211+
),
212+
),
213+
),
214+
)
215+
}
216+
217+
it("returns a 200 OK status code for PND") {
218+
val result = mockMvc.performAuthorised(pndPath)
219+
result.response.status.shouldBe(HttpStatus.OK.value())
220+
}
221+
222+
it("returns paginated results for PND") {
223+
whenever(getAlertsForPersonService.execute(hmppsId)).thenReturn(
224+
Response(
225+
data =
226+
List(20) {
227+
Alert(
228+
offenderNo = "B0000ZZ",
229+
type = "Z",
230+
typeDescription = "Threat",
231+
code = "BBB",
232+
codeDescription = "Not For Release",
233+
comment = "IS91",
234+
dateCreated = LocalDate.parse("2022-09-01"),
235+
dateExpired = LocalDate.parse("2023-10-01"),
236+
expired = false,
237+
active = false,
238+
)
239+
},
240+
),
241+
)
242+
243+
val result = mockMvc.performAuthorised("$pndPath?page=1&perPage=10")
244+
245+
result.response.contentAsString.shouldContainJsonKeyValue("$.pagination.page", 1)
246+
result.response.contentAsString.shouldContainJsonKeyValue("$.pagination.totalPages", 2)
247+
}
248+
249+
it("returns an empty list embedded in a JSON object when no alerts are found for PND") {
250+
val hmppsIdForPersonWithNoAlerts = "1111/22334A"
251+
val encodedHmppsIdForPersonWithNoAlerts =
252+
URLEncoder.encode(hmppsIdForPersonWithNoAlerts, StandardCharsets.UTF_8)
253+
val alertPath = "/v1/persons/$encodedHmppsIdForPersonWithNoAlerts/alerts/pnd"
254+
255+
whenever(getAlertsForPersonService.execute(hmppsIdForPersonWithNoAlerts)).thenReturn(
256+
Response(
257+
data = emptyList(),
258+
),
259+
)
260+
261+
val result = mockMvc.performAuthorised(alertPath)
262+
263+
result.response.contentAsString.shouldContain("\"data\":[]".removeWhitespaceAndNewlines())
264+
}
265+
266+
it("logs audit for PND") {
267+
mockMvc.performAuthorised(pndPath)
268+
269+
verify(
270+
auditService,
271+
VerificationModeFactory.times(1),
272+
).createEvent("GET_PERSON_ALERTS_PND", mapOf("hmppsId" to hmppsId))
273+
}
274+
275+
it("gets the alerts for PND for a person with the matching ID") {
276+
mockMvc.performAuthorised(pndPath)
277+
278+
verify(getAlertsForPersonService, VerificationModeFactory.times(1)).execute(hmppsId)
279+
}
280+
}
191281
},
192282
)

0 commit comments

Comments
 (0)