|
| 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 | + }) |
0 commit comments