|
| 1 | +package uk.gov.justice.digital.hmpps.hmppsintegrationapi.services |
| 2 | + |
| 3 | +import io.kotest.core.spec.style.DescribeSpec |
| 4 | +import org.mockito.Mockito |
| 5 | +import org.mockito.internal.verification.VerificationModeFactory |
| 6 | +import org.mockito.kotlin.verify |
| 7 | +import org.mockito.kotlin.whenever |
| 8 | +import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer |
| 9 | +import org.springframework.boot.test.mock.mockito.MockBean |
| 10 | +import org.springframework.test.context.ContextConfiguration |
| 11 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.RiskManagementGateway |
| 12 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.assessRisksAndNeeds.CrnRiskManagementPlan |
| 13 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.assessRisksAndNeeds.CrnRiskManagementPlans |
| 14 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response |
| 15 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi |
| 16 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError |
| 17 | + |
| 18 | +@ContextConfiguration( |
| 19 | + initializers = [ConfigDataApplicationContextInitializer::class], |
| 20 | + classes = [GetRiskManagementPlansForCrnService::class], |
| 21 | +) |
| 22 | +class GetRiskManagementPlansForCrnServiceTest ( |
| 23 | + @MockBean val riskManagementGateway: RiskManagementGateway, |
| 24 | + private val serviceUnderTest: GetRiskManagementPlansForCrnService |
| 25 | +): DescribeSpec({ |
| 26 | + |
| 27 | + val crn = "D1974X" |
| 28 | + val badCrn = "Not a real CRN" |
| 29 | + |
| 30 | + beforeEach { |
| 31 | + val testPlan = CrnRiskManagementPlans( |
| 32 | + crn = crn, |
| 33 | + limitedAccessOffender = "true", |
| 34 | + riskManagementPlan = listOf( |
| 35 | + CrnRiskManagementPlan( |
| 36 | + assessmentId = "123450", |
| 37 | + dateCompleted = "2024-05-04T01 =04 =20", |
| 38 | + partcompStatus = "string", |
| 39 | + initiationDate = "2024-05-04T01 =04 =20", |
| 40 | + assessmentStatus = "string", |
| 41 | + assessmentType = "string", |
| 42 | + superStatus = "string", |
| 43 | + keyInformationCurrentSituation = "string", |
| 44 | + furtherConsiderationsCurrentSituation = "string", |
| 45 | + supervision = "string", |
| 46 | + monitoringAndControl = "string", |
| 47 | + interventionsAndTreatment = "string", |
| 48 | + victimSafetyPlanning = "string", |
| 49 | + contingencyPlans = "string", |
| 50 | + laterWIPAssessmentExists = "true", |
| 51 | + latestWIPDate = "2024-05-04T01 =04 =20", |
| 52 | + laterSignLockAssessmentExists = "true", |
| 53 | + latestSignLockDate = "2024-05-04T01 =04 =20", |
| 54 | + laterPartCompUnsignedAssessmentExists = "true", |
| 55 | + latestPartCompUnsignedDate = "2024-05-04T01 =04 =20", |
| 56 | + laterPartCompSignedAssessmentExists = "true", |
| 57 | + latestPartCompSignedDate = "2024-05-04T01 =04 =20", |
| 58 | + laterCompleteAssessmentExists = "true", |
| 59 | + latestCompleteDate = "2024-05-04T01 =04 =20" |
| 60 | + ), |
| 61 | + CrnRiskManagementPlan( |
| 62 | + assessmentId = "123450", |
| 63 | + dateCompleted = "2024-05-04T01 =04 =20", |
| 64 | + partcompStatus = "string", |
| 65 | + initiationDate = "2024-05-04T01 =04 =20", |
| 66 | + assessmentStatus = "string", |
| 67 | + assessmentType = "string", |
| 68 | + superStatus = "string", |
| 69 | + keyInformationCurrentSituation = "string", |
| 70 | + furtherConsiderationsCurrentSituation = "string", |
| 71 | + supervision = "string", |
| 72 | + monitoringAndControl = "string", |
| 73 | + interventionsAndTreatment = "string", |
| 74 | + victimSafetyPlanning = "string", |
| 75 | + contingencyPlans = "string", |
| 76 | + laterWIPAssessmentExists = "true", |
| 77 | + latestWIPDate = "2024-05-04T01 =04 =20", |
| 78 | + laterSignLockAssessmentExists = "true", |
| 79 | + latestSignLockDate = "2024-05-04T01 =04 =20", |
| 80 | + laterPartCompUnsignedAssessmentExists = "true", |
| 81 | + latestPartCompUnsignedDate = "2024-05-04T01 =04 =20", |
| 82 | + laterPartCompSignedAssessmentExists = "true", |
| 83 | + latestPartCompSignedDate = "2024-05-04T01 =04 =20", |
| 84 | + laterCompleteAssessmentExists = "true", |
| 85 | + latestCompleteDate = "2024-05-04T01 =04 =20" |
| 86 | + ) |
| 87 | + ) |
| 88 | + ) |
| 89 | + |
| 90 | + Mockito.reset(riskManagementGateway) |
| 91 | + |
| 92 | + whenever(riskManagementGateway.getRiskManagementPlansForCrn(crn)).thenReturn( |
| 93 | + Response(data = testPlan, errors = emptyList()), |
| 94 | + ) |
| 95 | + whenever(riskManagementGateway.getRiskManagementPlansForCrn(badCrn)).thenReturn( |
| 96 | + Response(data = null, errors = listOf( |
| 97 | + UpstreamApiError( |
| 98 | + causedBy = UpstreamApi.RISK_MANAGEMENT_PLAN, |
| 99 | + type = UpstreamApiError.Type.ENTITY_NOT_FOUND, |
| 100 | + description = "Lorem Ipsum dolor sit amet" |
| 101 | + ) |
| 102 | + )), |
| 103 | + ) |
| 104 | + } |
| 105 | + |
| 106 | + describe("Get risk management") { |
| 107 | + |
| 108 | + it ("returns plans with valid CRN") { |
| 109 | + val result = serviceUnderTest.execute(crn) |
| 110 | + verify(riskManagementGateway, VerificationModeFactory.times(1)).getRiskManagementPlansForCrn(crn) |
| 111 | + assert(result.data?.size == 2) |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + it ("Returns error without valid CRN") { |
| 116 | + val result = serviceUnderTest.execute(badCrn) |
| 117 | + verify(riskManagementGateway, VerificationModeFactory.times(1)).getRiskManagementPlansForCrn(badCrn) |
| 118 | + assert(result.data?.size == 0) |
| 119 | + assert(result.errors.isNotEmpty()) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | +}) |
| 124 | + |
0 commit comments