Skip to content

Commit a323228

Browse files
[HIA-721]
WIP Fixed gateway properly and all the tests pass
1 parent 4142a42 commit a323228

File tree

8 files changed

+87
-89
lines changed

8 files changed

+87
-89
lines changed

gradle.properties

100755100644
File mode changed.

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.RiskManagem
1111
@RestController
1212
@EnableConfigurationProperties(AuthorisationConfig::class)
1313
class RiskManagementController {
14-
1514
@RequestMapping("/v1/persons/{encodedHmppsId}/risk-management-plan")
1615
fun getRiskManagementPlans(
1716
@PathVariable encodedHmppsId: String,
18-
) : Response<List<RiskManagementPlan>> {
19-
return Response(emptyList(), emptyList() )
17+
): Response<List<RiskManagementPlan>> {
18+
return Response(emptyList(), emptyList())
2019
}
2120
}

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response
1010
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi
1111

1212
@Component
13-
class RiskManagementGateway (
13+
class RiskManagementGateway(
1414
@Value("\${services.risk-management-plan-search.base-url}") baseUrl: String,
15-
){
16-
15+
) {
1716
private val webClient = WebClientWrapper(baseUrl)
1817

1918
@Autowired
2019
lateinit var hmppsAuthGateway: HmppsAuthGateway
2120

22-
fun getRiskManagementPlansForCrn(crn: String): Response<CrnRiskManagementPlans> {
23-
21+
fun getRiskManagementPlansForCrn(crn: String): Response<CrnRiskManagementPlans?> {
2422
val requestBody =
2523
mapOf("crn" to crn)
2624

@@ -39,7 +37,7 @@ class RiskManagementGateway (
3937

4038
is WebClientWrapper.WebClientWrapperResponse.Error -> {
4139
Response(
42-
data = CrnRiskManagementPlans("", "", emptyList()),
40+
data = null,
4341
errors = result.errors,
4442
)
4543
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import java.util.stream.Collectors
66
data class CrnRiskManagementPlans(
77
val crn: String,
88
val limitedAccessOffender: String,
9-
val riskManagementPlan: List<CrnRiskManagementPlan>
9+
val riskManagementPlan: List<CrnRiskManagementPlan>,
1010
) {
1111
fun toRiskManagementPlans(): List<RiskManagementPlan> {
1212
return this.riskManagementPlan.stream()
@@ -57,4 +57,3 @@ data class CrnRiskManagementPlan(
5757
val laterCompleteAssessmentExists: String,
5858
val latestCompleteDate: String,
5959
)
60-

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

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

3-
data class RiskManagementPlan (
4-
3+
data class RiskManagementPlan(
54
val assessmentId: String,
65
val dateCompleted: String,
76
val initiationDate: String,
@@ -15,5 +14,4 @@ data class RiskManagementPlan (
1514
val victimSafetyPlanning: String,
1615
val latestSignLockDate: String,
1716
val latestCompleteDate: String,
18-
1917
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.riskManagement
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.verify
8+
import org.mockito.kotlin.whenever
9+
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer
10+
import org.springframework.boot.test.mock.mockito.MockBean
11+
import org.springframework.test.context.ActiveProfiles
12+
import org.springframework.test.context.ContextConfiguration
13+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.HmppsAuthGateway
14+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.RiskManagementGateway
15+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.HmppsAuthMockServer
16+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.RiskManagementApiMockServer
17+
import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError
18+
import java.io.File
19+
20+
@ActiveProfiles("test")
21+
@ContextConfiguration(
22+
initializers = [ConfigDataApplicationContextInitializer::class],
23+
classes = [RiskManagementGateway::class],
24+
)
25+
class RiskManagementGatewayTest(
26+
@MockBean val hmppsAuthGateway: HmppsAuthGateway,
27+
private val riskManagementGateway: RiskManagementGateway,
28+
) : DescribeSpec({
29+
30+
val riskManagementMockServer = RiskManagementApiMockServer()
31+
32+
beforeEach {
33+
riskManagementMockServer.start()
34+
Mockito.reset(hmppsAuthGateway)
35+
36+
whenever(hmppsAuthGateway.getClientToken("Risk Management Plan Search")).thenReturn(HmppsAuthMockServer.TOKEN)
37+
}
38+
39+
afterTest {
40+
riskManagementMockServer.stop()
41+
}
42+
43+
describe("Get risks for given CRN") {
44+
val crn = "D1974X"
45+
46+
beforeEach {
47+
riskManagementMockServer.stubGetRiskManagementPlan(
48+
crn,
49+
File(
50+
"src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/riskManagement/fixtures/GetRiskManagementPlanResponse.json",
51+
).readText(),
52+
)
53+
}
54+
55+
it("authenticates using HMPPS Auth with credentials") {
56+
riskManagementGateway.getRiskManagementPlansForCrn(crn)
57+
verify(hmppsAuthGateway, VerificationModeFactory.times(1)).getClientToken("Risk Management Plan Search")
58+
}
59+
60+
it("returns a risk management plan when searching with a valid CRN") {
61+
val response = riskManagementGateway.getRiskManagementPlansForCrn(crn)
62+
val riskPlan = response.data
63+
riskPlan?.crn.shouldBe(crn)
64+
riskPlan?.riskManagementPlan?.size.shouldBe(1)
65+
response.errors.size.shouldBe(0)
66+
}
67+
68+
it("returns an error response when searching with an invalid CRN") {
69+
val response = riskManagementGateway.getRiskManagementPlansForCrn("Not a valid CRN")
70+
val riskPlan = response.data
71+
riskPlan.shouldBe(null)
72+
response.errors.size.shouldBe(1)
73+
response.errors[0].type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND)
74+
}
75+
}
76+
})

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/riskManagement/riskManagementGatewayTest.kt

-71
This file was deleted.

src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/mockservers/RiskManagementApiMockServer.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers
22

33
import com.github.tomakehurst.wiremock.WireMockServer
4-
import com.github.tomakehurst.wiremock.client.WireMock.matching
5-
import com.github.tomakehurst.wiremock.client.WireMock.get
64
import com.github.tomakehurst.wiremock.client.WireMock.aResponse
5+
import com.github.tomakehurst.wiremock.client.WireMock.get
6+
import com.github.tomakehurst.wiremock.client.WireMock.matching
77
import org.springframework.http.HttpStatus
88

99
class RiskManagementApiMockServer : WireMockServer(WIREMOCK_PORT) {
@@ -17,7 +17,7 @@ class RiskManagementApiMockServer : WireMockServer(WIREMOCK_PORT) {
1717
status: HttpStatus = HttpStatus.OK,
1818
) {
1919
stubFor(
20-
get("risks/crn/$crn/risk-management-plan")
20+
get("/risks/crn/$crn/risk-management-plan")
2121
.withHeader("Authorization", matching("Bearer ${HmppsAuthMockServer.TOKEN}"))
2222
.willReturn(
2323
aResponse()
@@ -27,5 +27,4 @@ class RiskManagementApiMockServer : WireMockServer(WIREMOCK_PORT) {
2727
),
2828
)
2929
}
30-
3130
}

0 commit comments

Comments
 (0)