Skip to content

Commit 8ea34d8

Browse files
committed
Add Prisoner Education endpoint tests
1 parent 1a75af0 commit 8ea34d8

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1
2+
3+
import io.kotest.core.spec.style.DescribeSpec
4+
import io.kotest.matchers.shouldBe
5+
import org.mockito.Mockito
6+
import org.mockito.internal.verification.VerificationModeFactory
7+
import org.mockito.kotlin.doThrow
8+
import org.mockito.kotlin.verify
9+
import org.mockito.kotlin.whenever
10+
import org.springframework.beans.factory.annotation.Autowired
11+
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
12+
import org.springframework.boot.test.mock.mockito.MockBean
13+
import org.springframework.http.HttpStatus
14+
import org.springframework.test.context.ActiveProfiles
15+
import org.springframework.test.web.servlet.MockMvc
16+
import org.springframework.web.reactive.function.client.WebClientResponseException
17+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.IntegrationAPIMockMvc
18+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
19+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.prisonIntegrationpes.PESPrisonerDetails
20+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetPESPrisonerDetailsService
21+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService
22+
23+
@WebMvcTest(controllers = [PESPrisonerDetailController::class])
24+
@ActiveProfiles("test")
25+
internal class PESPrisonerDetailControllerTest(
26+
@Autowired var springMockMvc: MockMvc,
27+
@MockBean val getPESPrisonerDetailsService: GetPESPrisonerDetailsService,
28+
@MockBean val auditService: AuditService,
29+
) : DescribeSpec({
30+
val hmppsId = "X12345"
31+
val path = "/v1/pes/prisoner-details/$hmppsId"
32+
val mockMvc = IntegrationAPIMockMvc(springMockMvc)
33+
34+
describe("GET $path") {
35+
beforeTest {
36+
Mockito.reset(getPESPrisonerDetailsService)
37+
whenever(getPESPrisonerDetailsService.execute(hmppsId)).thenReturn(
38+
Response(
39+
data = PESPrisonerDetails(prisonerNumber = "ABC123", "Molly", lastName = "Mob", prisonId = "LEI", prisonName = "HMP Leeds", cellLocation = "6-2-006"),
40+
),
41+
)
42+
Mockito.reset(auditService)
43+
}
44+
45+
it("returns a 200 OK status code") {
46+
val result = mockMvc.performAuthorised(path)
47+
48+
result.response.status.shouldBe(HttpStatus.OK.value())
49+
}
50+
51+
it("gets the prisoner detail for a prisoner with the matching ID") {
52+
mockMvc.performAuthorised(path)
53+
54+
verify(getPESPrisonerDetailsService, VerificationModeFactory.times(1)).execute(hmppsId)
55+
}
56+
57+
it("logs audit") {
58+
mockMvc.performAuthorised(path)
59+
verify(
60+
auditService,
61+
VerificationModeFactory.times(1),
62+
).createEvent(
63+
"GET_PES_PRISONER_INFORMATION",
64+
mapOf("hmppsId" to hmppsId),
65+
)
66+
}
67+
68+
it("returns a 500 INTERNAL SERVER ERROR status code when upstream api return expected error") {
69+
70+
whenever(getPESPrisonerDetailsService.execute(hmppsId)).doThrow(
71+
WebClientResponseException(500, "MockError", null, null, null, null),
72+
)
73+
74+
val result = mockMvc.performAuthorised(path)
75+
assert(result.response.status == 500)
76+
assert(
77+
result.response.contentAsString.equals(
78+
"{\"status\":500,\"errorCode\":null,\"userMessage\":\"500 MockError\",\"developerMessage\":\"Unable to complete request as an upstream service is not responding\",\"moreInfo\":null}",
79+
),
80+
)
81+
}
82+
}
83+
})

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/models/prisoneroffendersearch/PrisonerTest.kt

+33
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class PrisonerTest : DescribeSpec(
1111
{
1212
describe("#toPerson") {
1313
it("maps one-to-one attributes to person attributes") {
14+
1415
val prisoner =
1516
POSPrisoner(
1617
firstName = "First Name",
@@ -43,5 +44,37 @@ class PrisonerTest : DescribeSpec(
4344
person.pncId.shouldBe(prisoner.pncNumber)
4445
}
4546
}
47+
describe("#toPESPrisonerDetails") {
48+
it("maps one-to-one attributes to PESPrisonerDetails attributes") {
49+
50+
val prisoner =
51+
POSPrisoner(
52+
firstName = "First Name",
53+
lastName = "Last Name",
54+
middleNames = "Middle Name",
55+
dateOfBirth = LocalDate.parse("2023-03-01"),
56+
gender = "Gender",
57+
ethnicity = "Ethnicity",
58+
prisonerNumber = "prisonerNumber",
59+
pncNumber = "pncNumber",
60+
croNumber = "croNumber",
61+
aliases =
62+
listOf(
63+
POSPrisonerAlias(firstName = "Alias First Name", lastName = "Alias Last Name"),
64+
),
65+
prisonId = "LEI",
66+
prisonName = "HMP Leeds",
67+
cellLocation = "6-2-006",
68+
)
69+
70+
val pesPrisoner = prisoner.toPESPrisonerDetails()
71+
pesPrisoner.prisonerNumber.shouldBe(prisoner.prisonerNumber)
72+
pesPrisoner.firstName.shouldBe(prisoner.firstName)
73+
pesPrisoner.lastName.shouldBe(prisoner.lastName)
74+
pesPrisoner.prisonId.shouldBe(prisoner.prisonId)
75+
pesPrisoner.prisonName.shouldBe(prisoner.prisonName)
76+
pesPrisoner.cellLocation.shouldBe(prisoner.cellLocation)
77+
}
78+
}
4679
},
4780
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services
2+
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.boot.test.context.ConfigDataApplicationContextInitializer
8+
import org.springframework.boot.test.mock.mockito.MockBean
9+
import org.springframework.test.context.ContextConfiguration
10+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.PrisonerOffenderSearchGateway
11+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
12+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.prisonIntegrationpes.PESPrisonerDetails
13+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.prisoneroffendersearch.POSPrisoner
14+
15+
@ContextConfiguration(
16+
initializers = [ConfigDataApplicationContextInitializer::class],
17+
classes = [GetPESPrisonerDetailsService::class],
18+
)
19+
internal class GetPESPrisonerDetailsServiceTest(
20+
@MockBean val prisonerSearchGateway: PrisonerOffenderSearchGateway,
21+
private val getPESPrisonerDetailsService: GetPESPrisonerDetailsService,
22+
) : DescribeSpec(
23+
{
24+
val hmppsId = "X123456"
25+
val expectedResult = PESPrisonerDetails(prisonerNumber = "X123456", "Molly", lastName = "Mob", prisonId = "LEI", prisonName = "HMP Leeds", cellLocation = "6-2-006")
26+
27+
beforeEach {
28+
Mockito.reset(prisonerSearchGateway)
29+
30+
whenever(prisonerSearchGateway.getPrisonOffender(hmppsId)).thenReturn(
31+
Response(
32+
data = POSPrisoner(prisonerNumber = hmppsId, firstName = "Molly", lastName = "Mob", prisonId = "LEI", prisonName = "HMP Leeds", cellLocation = "6-2-006"),
33+
),
34+
)
35+
}
36+
37+
it("Returns a prisoner name record according to the provided HMPPS ID") {
38+
val result = getPESPrisonerDetailsService.execute(hmppsId)
39+
40+
result.shouldBe(Response(data = expectedResult))
41+
}
42+
},
43+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.smoke
2+
3+
import io.kotest.assertions.json.shouldEqualJson
4+
import io.kotest.core.spec.style.DescribeSpec
5+
import io.kotest.matchers.shouldBe
6+
import org.springframework.http.HttpStatus
7+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.removeWhitespaceAndNewlines
8+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.IntegrationAPIHttpClient
9+
10+
class PESPrisonerDetailSmokeTest : DescribeSpec(
11+
{
12+
val hmppsId = "X123456"
13+
val basePath = "v1/pes/prisoner-details/$hmppsId"
14+
15+
val httpClient = IntegrationAPIHttpClient()
16+
17+
it("returns a prisoner detail for a HmppsID") {
18+
val response = httpClient.performAuthorised(basePath)
19+
20+
response.statusCode().shouldBe(HttpStatus.OK.value())
21+
response.body().shouldEqualJson(
22+
"""
23+
{
24+
"data": {
25+
"prisonerNumber": "A1234AA",
26+
"firstName": "Robert",
27+
"lastName": "Larsen",
28+
"prisonId": "MDI",
29+
"prisonName": "HMP Leeds",
30+
"cellLocation": "A-1-002"
31+
},
32+
"errors": []
33+
}
34+
""".removeWhitespaceAndNewlines(),
35+
)
36+
}
37+
},
38+
)

0 commit comments

Comments
 (0)