Skip to content

Commit fa2e0d7

Browse files
HIA-697 - NDelius retrieve community-manager related data
1 parent f3b007f commit fa2e0d7

File tree

10 files changed

+200
-3
lines changed

10 files changed

+200
-3
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.springframework.http.HttpMethod
66
import org.springframework.stereotype.Component
77
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.WebClientWrapper
88
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.WebClientWrapper.WebClientWrapperResponse
9+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.CommunityOffenderManager
910
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.MappaDetail
1011
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Offence
1112
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
@@ -91,6 +92,28 @@ class NDeliusGateway(
9192
}
9293
}
9394

95+
fun getCommunityOffenderManagerForPerson(id: String): Response<CommunityOffenderManager?> {
96+
val result =
97+
webClient.request<NDeliusSupervisions>(
98+
HttpMethod.GET,
99+
"/case/$id/supervisions",
100+
authenticationHeader(),
101+
UpstreamApi.NDELIUS,
102+
)
103+
return when (result) {
104+
is WebClientWrapperResponse.Success -> {
105+
Response(data = result.data.communityManager?.toCommunityOffenderManager())
106+
}
107+
108+
is WebClientWrapperResponse.Error -> {
109+
Response(
110+
data = null,
111+
errors = result.errors,
112+
)
113+
}
114+
}
115+
}
116+
94117
private fun authenticationHeader(): Map<String, String> {
95118
val token = hmppsAuthGateway.getClientToken("nDelius")
96119

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

3-
class PersonResponsibleOfficerTeam(
3+
data class PersonResponsibleOfficerTeam(
44
val code: String? = null,
55
val description: String? = null,
6-
val teamEmail: String? = null,
7-
val teamTelephoneNumber: String? = null,
6+
val email: String? = null,
7+
val telephoneNumber: String? = null,
88
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.ndelius
2+
3+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.CommunityOffenderManager
4+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PersonResponsibleOfficerName
5+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PersonResponsibleOfficerTeam
6+
7+
data class NDeliusCommunityManager(
8+
val code: String? = null,
9+
val name: NDeliusName = NDeliusName(),
10+
val username: String? = null,
11+
val email: String? = null,
12+
val telephoneNumber: String? = null,
13+
val team: NDeliusTeam = NDeliusTeam(),
14+
val allocated: Boolean? = null,
15+
) {
16+
fun toCommunityOffenderManager(): CommunityOffenderManager =
17+
(
18+
CommunityOffenderManager(
19+
name = PersonResponsibleOfficerName(forename = this.name.forename, surname = this.name.surname),
20+
email = this.email,
21+
telephoneNumber = this.telephoneNumber,
22+
team = PersonResponsibleOfficerTeam(code = this.team.code, description = this.team.description, email = this.team.email, telephoneNumber = this.team.telephoneNumber),
23+
)
24+
)
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.ndelius
2+
3+
data class NDeliusName(
4+
val forename: String? = null,
5+
val surname: String? = null,
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.ndelius
2+
3+
data class NDeliusProvider(
4+
val code: String? = null,
5+
val description: String? = null,
6+
)
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.ndelius
22

33
data class NDeliusSupervisions(
4+
val communityManager: NDeliusCommunityManager? = null,
45
val mappaDetail: NDeliusMappaDetail? = null,
56
val supervisions: List<NDeliusSupervision>,
67
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.ndelius
2+
3+
data class NDeliusTeam(
4+
val code: String? = null,
5+
val description: String? = null,
6+
val email: String? = null,
7+
val telephoneNumber: String? = null,
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.ndelius
2+
3+
import io.kotest.core.spec.style.DescribeSpec
4+
import io.kotest.matchers.collections.shouldHaveSize
5+
import io.kotest.matchers.shouldBe
6+
import org.mockito.Mockito
7+
import org.mockito.internal.verification.VerificationModeFactory
8+
import org.mockito.kotlin.verify
9+
import org.mockito.kotlin.whenever
10+
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer
11+
import org.springframework.boot.test.mock.mockito.MockBean
12+
import org.springframework.http.HttpStatus
13+
import org.springframework.test.context.ActiveProfiles
14+
import org.springframework.test.context.ContextConfiguration
15+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.HmppsAuthGateway
16+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NDeliusGateway
17+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.HmppsAuthMockServer
18+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.NDeliusApiMockServer
19+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.CommunityOffenderManager
20+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PersonResponsibleOfficerName
21+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PersonResponsibleOfficerTeam
22+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
23+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
24+
import java.io.File
25+
26+
@ActiveProfiles("test")
27+
@ContextConfiguration(
28+
initializers = [ConfigDataApplicationContextInitializer::class],
29+
classes = [NDeliusGateway::class],
30+
)
31+
class GetCommunityOffenderManagerForPersonTest(
32+
@MockBean
33+
val hmppsAuthGateway: HmppsAuthGateway,
34+
val nDeliusGateway: NDeliusGateway,
35+
) : DescribeSpec(
36+
{
37+
val nDeliusApiMockServer = NDeliusApiMockServer()
38+
val deliusCrn = "X777776"
39+
40+
beforeEach {
41+
nDeliusApiMockServer.start()
42+
nDeliusApiMockServer.stubGetSupervisionsForPerson(
43+
deliusCrn,
44+
File(
45+
"src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/ndelius/fixtures/GetSupervisionsResponse.json",
46+
).readText(),
47+
)
48+
49+
Mockito.reset(hmppsAuthGateway)
50+
whenever(hmppsAuthGateway.getClientToken("nDelius")).thenReturn(HmppsAuthMockServer.TOKEN)
51+
}
52+
53+
afterTest {
54+
nDeliusApiMockServer.stop()
55+
}
56+
57+
it("authenticates using HMPPS Auth with credentials") {
58+
nDeliusGateway.getCommunityOffenderManagerForPerson(deliusCrn)
59+
60+
verify(hmppsAuthGateway, VerificationModeFactory.times(1)).getClientToken("nDelius")
61+
}
62+
63+
it("returns community offender manager related to the matching CRN") {
64+
val response = nDeliusGateway.getCommunityOffenderManagerForPerson(deliusCrn)
65+
66+
response.data.shouldBe(
67+
CommunityOffenderManager(
68+
name = PersonResponsibleOfficerName(forename = "John", surname = "Smith"),
69+
email = "johnsmith@email.com",
70+
telephoneNumber = "077777777",
71+
team =
72+
PersonResponsibleOfficerTeam(
73+
code = "Code1",
74+
description = "Description",
75+
email = "team@email.com",
76+
telephoneNumber = "07888888",
77+
),
78+
),
79+
)
80+
}
81+
82+
it("returns an empty object if no community offender manager is found") {
83+
nDeliusApiMockServer.stubGetSupervisionsForPerson(
84+
deliusCrn,
85+
"""
86+
{
87+
"communityManager": {},
88+
"mappaDetail": {},
89+
"supervisions": []
90+
}
91+
""",
92+
)
93+
94+
val response = nDeliusGateway.getCommunityOffenderManagerForPerson(deliusCrn)
95+
96+
response.data.shouldBe(CommunityOffenderManager())
97+
}
98+
99+
it("returns an error when 404 Not Found is returned because no person is found") {
100+
nDeliusApiMockServer.stubGetSupervisionsForPerson(deliusCrn, "", HttpStatus.NOT_FOUND)
101+
102+
val response = nDeliusGateway.getCommunityOffenderManagerForPerson(deliusCrn)
103+
104+
response.errors.shouldHaveSize(1)
105+
response.errors.first().causedBy.shouldBe(UpstreamApi.NDELIUS)
106+
response.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND)
107+
}
108+
},
109+
)

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/ndelius/fixtures/GetSupervisionsResponse.json

+17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
11
{
2+
"communityManager": {
3+
"code": "Code1",
4+
"name": {
5+
"forename": "John",
6+
"surname": "Smith"
7+
},
8+
"username": "JohnSmithUsername",
9+
"email": "johnsmith@email.com",
10+
"telephoneNumber": "077777777",
11+
"team": {
12+
"code": "Code1",
13+
"description": "Description",
14+
"email": "team@email.com",
15+
"telephoneNumber": "07888888"
16+
},
17+
"allocated": true
18+
},
219
"mappaDetail": {
320
"level": 1,
421
"levelDescription": "string",

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/ndelius/SupervisionsTest.kt

+2
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ class SupervisionsTest : DescribeSpec(
213213
it("maps one-to-one attributes to integration API sentence attributes") {
214214
val supervisions =
215215
NDeliusSupervisions(
216+
communityManager = NDeliusCommunityManager(),
216217
mappaDetail = NDeliusMappaDetail(),
217218
listOf(
218219
NDeliusSupervision(
@@ -276,6 +277,7 @@ class SupervisionsTest : DescribeSpec(
276277
it("can be constructed with NULL values") {
277278
val supervisions =
278279
NDeliusSupervisions(
280+
communityManager = NDeliusCommunityManager(),
279281
mappaDetail = NDeliusMappaDetail(),
280282
listOf(
281283
NDeliusSupervision(custodial = true),

0 commit comments

Comments
 (0)