Skip to content

Commit 66388cb

Browse files
HIA-697 - Adding controller and initial test
1 parent 0fc61cb commit 66388cb

File tree

9 files changed

+140
-7
lines changed

9 files changed

+140
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1.person
2+
3+
import org.springframework.beans.factory.annotation.Autowired
4+
import org.springframework.web.bind.annotation.GetMapping
5+
import org.springframework.web.bind.annotation.PathVariable
6+
import org.springframework.web.bind.annotation.RequestMapping
7+
import org.springframework.web.bind.annotation.RestController
8+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.exception.EntityNotFoundException
9+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.decodeUrlCharacters
10+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PersonResponsibleOfficer
11+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
12+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetCommunityOffenderManagerForPersonService
13+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetPrisonOffenderManagerForPersonService
14+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService
15+
16+
@RestController
17+
@RequestMapping("/v1/persons")
18+
class PersonResponsibleOfficerController(
19+
@Autowired val auditService: AuditService,
20+
@Autowired val getPrisonOffenderManagerForPersonService: GetPrisonOffenderManagerForPersonService,
21+
@Autowired val getCommunityOffenderManagerForPersonService: GetCommunityOffenderManagerForPersonService,
22+
) {
23+
@GetMapping("{encodedHmppsId}/person-responsible-officer")
24+
fun getPersonResponsibleOfficer(
25+
@PathVariable encodedHmppsId: String,
26+
): Map<String, PersonResponsibleOfficer> {
27+
val hmppsId = encodedHmppsId.decodeUrlCharacters()
28+
val prisonOffenderManager = getPrisonOffenderManagerForPersonService.execute(hmppsId)
29+
val communityOffenderManager = getCommunityOffenderManagerForPersonService.execute(hmppsId)
30+
31+
if (prisonOffenderManager.hasError(UpstreamApiError.Type.ENTITY_NOT_FOUND)) {
32+
throw EntityNotFoundException("Could not find prison offender manager related to id: $hmppsId")
33+
}
34+
35+
if (communityOffenderManager.hasError(UpstreamApiError.Type.ENTITY_NOT_FOUND)) {
36+
throw EntityNotFoundException("Could not find community offender manager related to id: $hmppsId")
37+
}
38+
39+
val mergedData =
40+
PersonResponsibleOfficer(
41+
prisonOffenderManager = prisonOffenderManager.data,
42+
communityOffenderManager = communityOffenderManager.data,
43+
)
44+
45+
auditService.createEvent("GET_PERSON_RESPONSIBLE_OFFICER", mapOf("hmppsId" to hmppsId))
46+
return mapOf("data" to mergedData)
47+
}
48+
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class NDeliusGateway(
9292
}
9393
}
9494

95-
fun getCommunityOffenderManagerForPerson(id: String): Response<CommunityOffenderManager?> {
95+
fun getCommunityOffenderManagerForPerson(id: String): Response<CommunityOffenderManager> {
9696
val result =
9797
webClient.request<NDeliusSupervisions>(
9898
HttpMethod.GET,
@@ -102,12 +102,12 @@ class NDeliusGateway(
102102
)
103103
return when (result) {
104104
is WebClientWrapperResponse.Success -> {
105-
Response(data = result.data.communityManager?.toCommunityOffenderManager())
105+
Response(data = result.data.communityManager.toCommunityOffenderManager())
106106
}
107107

108108
is WebClientWrapperResponse.Error -> {
109109
Response(
110-
data = null,
110+
data = CommunityOffenderManager(),
111111
errors = result.errors,
112112
)
113113
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.ndelius
22

33
data class NDeliusSupervisions(
4-
val communityManager: NDeliusCommunityManager? = null,
4+
val communityManager: NDeliusCommunityManager,
55
val mappaDetail: NDeliusMappaDetail? = null,
66
val supervisions: List<NDeliusSupervision>,
77
)

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services
22

33
import org.springframework.beans.factory.annotation.Autowired
4+
import org.springframework.stereotype.Service
45
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NDeliusGateway
56
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.CommunityOffenderManager
67
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
78

9+
@Service
810
class GetCommunityOffenderManagerForPersonService(
911
@Autowired val getPersonService: GetPersonService,
1012
@Autowired val nDeliusGateway: NDeliusGateway,
1113
) {
12-
fun execute(hmppsId: String): Response<CommunityOffenderManager?> {
14+
fun execute(hmppsId: String): Response<CommunityOffenderManager> {
1315
val personResponse = getPersonService.execute(hmppsId = hmppsId)
1416

1517
val deliusCrn = personResponse.data?.identifiers?.deliusCrn
16-
var nDeliusMappaDetailResponse: Response<CommunityOffenderManager?> = Response(data = CommunityOffenderManager())
18+
var nDeliusMappaDetailResponse: Response<CommunityOffenderManager> = Response(data = CommunityOffenderManager())
1719

1820
if (deliusCrn != null) {
1921
nDeliusMappaDetailResponse = nDeliusGateway.getCommunityOffenderManagerForPerson(id = deliusCrn)
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,79 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1.person
22

3-
class PersonResponsibleOfficerControllerTest
3+
import io.kotest.core.spec.style.DescribeSpec
4+
import io.kotest.matchers.shouldBe
5+
import org.mockito.Mockito
6+
import org.mockito.kotlin.whenever
7+
import org.springframework.beans.factory.annotation.Autowired
8+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
9+
import org.springframework.boot.test.mock.mockito.MockBean
10+
import org.springframework.http.HttpStatus
11+
import org.springframework.test.context.ActiveProfiles
12+
import org.springframework.test.web.servlet.MockMvc
13+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.IntegrationAPIMockMvc
14+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.CommunityOffenderManager
15+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PersonResponsibleOfficerName
16+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Prison
17+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PrisonOffenderManager
18+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
19+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetCommunityOffenderManagerForPersonService
20+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetPrisonOffenderManagerForPersonService
21+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService
22+
import java.net.URLEncoder
23+
import java.nio.charset.StandardCharsets
24+
25+
@WebMvcTest(controllers = [PersonResponsibleOfficerController::class])
26+
@ActiveProfiles("test")
27+
internal class PersonResponsibleOfficerControllerTest(
28+
@Autowired var springMockMvc: MockMvc,
29+
@MockBean val auditService: AuditService,
30+
@MockBean val getCommunityOffenderManagerForPersonService: GetCommunityOffenderManagerForPersonService,
31+
@MockBean val getPrisonOffenderManagerForPersonService: GetPrisonOffenderManagerForPersonService,
32+
) : DescribeSpec() {
33+
init {
34+
val hmppsId = "9999/11111A"
35+
val encodedHmppsId = URLEncoder.encode(hmppsId, StandardCharsets.UTF_8)
36+
val path = "/v1/persons/$encodedHmppsId/person-responsible-officer"
37+
val mockMvc = IntegrationAPIMockMvc(springMockMvc)
38+
39+
describe("GET $path") {
40+
beforeTest {
41+
Mockito.reset(getPrisonOffenderManagerForPersonService)
42+
Mockito.reset(getCommunityOffenderManagerForPersonService)
43+
whenever(getPrisonOffenderManagerForPersonService.execute(hmppsId)).thenReturn(
44+
Response(
45+
PrisonOffenderManager(
46+
forename = "Paul",
47+
surname = "Reds",
48+
prison = Prison(code = "PrisonCode1"),
49+
),
50+
),
51+
)
52+
53+
whenever(getCommunityOffenderManagerForPersonService.execute(hmppsId)).thenReturn(
54+
Response(
55+
CommunityOffenderManager(
56+
name = PersonResponsibleOfficerName("Helen", surname = "Miller"),
57+
email = "helenemail@email.com",
58+
telephoneNumber = "0987654321",
59+
team =
60+
PersonResponsibleOfficerTeam(
61+
code = "PrisonCode2",
62+
description = "Description",
63+
email = "email_again@email.com",
64+
telephoneNumber = "01234567890",
65+
),
66+
),
67+
),
68+
)
69+
Mockito.reset(auditService)
70+
}
71+
72+
it("returns a 200 OK status code") {
73+
val result = mockMvc.performAuthorised(path)
74+
75+
result.response.status.shouldBe(HttpStatus.OK.value())
76+
}
77+
}
78+
}
79+
}

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/ndelius/GetMappaDetailForPersonTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class GetMappaDetailForPersonTest(
7979
deliusCrn,
8080
"""
8181
{
82+
"communityManager": {},
8283
"mappaDetail": {},
8384
"supervisions": []
8485
}

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/ndelius/GetOffencesForPersonTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class GetOffencesForPersonTest(
139139
deliusCrn,
140140
"""
141141
{
142+
"communityManager": {},
142143
"mappaDetail": null,
143144
"supervisions": [] }
144145
""",

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/ndelius/GetSentencesForPersonTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class GetSentencesForPersonTest(
104104
deliusCrn,
105105
"""
106106
{
107+
"communityManager": {},
107108
"mappaDetail": {},
108109
"supervisions": [] }
109110
""",

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

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class SupervisionsTest : DescribeSpec(
1717
it("maps one-to-one attributes to integration API attributes") {
1818
val supervisions =
1919
NDeliusSupervisions(
20+
communityManager = NDeliusCommunityManager(),
2021
mappaDetail = NDeliusMappaDetail(),
2122
supervisions =
2223
listOf(
@@ -83,6 +84,7 @@ class SupervisionsTest : DescribeSpec(
8384
it("does not local date parse additional offence date if no date is provided") {
8485
val supervisions =
8586
NDeliusSupervisions(
87+
communityManager = NDeliusCommunityManager(),
8688
mappaDetail = NDeliusMappaDetail(),
8789
supervisions =
8890
listOf(
@@ -122,6 +124,7 @@ class SupervisionsTest : DescribeSpec(
122124
it("does local date parse additional offence date if a date is provided") {
123125
val supervisions =
124126
NDeliusSupervisions(
127+
communityManager = NDeliusCommunityManager(),
125128
mappaDetail = NDeliusMappaDetail(),
126129
supervisions =
127130
listOf(
@@ -166,6 +169,7 @@ class SupervisionsTest : DescribeSpec(
166169
it("maps one-to-one attributes to integration API attributes") {
167170
val supervisions =
168171
NDeliusSupervisions(
172+
communityManager = NDeliusCommunityManager(),
169173
mappaDetail = NDeliusMappaDetail(),
170174
supervisions =
171175
listOf(

0 commit comments

Comments
 (0)