|
| 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.test.context.ContextConfiguration |
| 9 | +import org.springframework.test.context.bean.override.mockito.MockitoBean |
| 10 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NomisGateway |
| 11 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.NomisNumber |
| 12 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response |
| 13 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi |
| 14 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError |
| 15 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.nomis.visits.VisitBalances |
| 16 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.roleconfig.ConsumerFilters |
| 17 | + |
| 18 | +@ContextConfiguration( |
| 19 | + initializers = [ConfigDataApplicationContextInitializer::class], |
| 20 | + classes = [GetVisitBalancesForPersonService::class], |
| 21 | +) |
| 22 | +class GetVisitBalancesForPersonServiceTest( |
| 23 | + @MockitoBean val nomisGateway: NomisGateway, |
| 24 | + @MockitoBean val getPersonService: GetPersonService, |
| 25 | + private val getVisitBalancesForPersonService: GetVisitBalancesForPersonService, |
| 26 | +) : DescribeSpec({ |
| 27 | + val hmppsId = "1234/56789B" |
| 28 | + val nomisNumber = "Z99999ZZ" |
| 29 | + val filters = ConsumerFilters(null) |
| 30 | + val exampleVisitBalances = VisitBalances(remainingPvo = 1073741824, remainingVo = 1073741824) |
| 31 | + |
| 32 | + beforeEach { |
| 33 | + Mockito.reset(nomisGateway) |
| 34 | + Mockito.reset(getPersonService) |
| 35 | + |
| 36 | + require(hmppsId.matches(Regex("^[0-9]+/[0-9A-Za-z]+$"))) { |
| 37 | + "Invalid Hmpps Id format: $hmppsId" |
| 38 | + } |
| 39 | + |
| 40 | + whenever(getPersonService.getNomisNumberWithPrisonFilter(hmppsId = hmppsId, filters)).thenReturn( |
| 41 | + Response(data = NomisNumber(nomisNumber = nomisNumber)), |
| 42 | + ) |
| 43 | + |
| 44 | + whenever(nomisGateway.getVisitBalances(nomisNumber)).thenReturn(Response(data = exampleVisitBalances)) |
| 45 | + } |
| 46 | + |
| 47 | + it("returns visitor balances for a valid HMPPS ID") { |
| 48 | + val response = getVisitBalancesForPersonService.execute(hmppsId, filters) |
| 49 | + response.data shouldBe (exampleVisitBalances) |
| 50 | + } |
| 51 | + |
| 52 | + it("returns an error when getNomisNumberWithPrisonFilter returns an error") { |
| 53 | + whenever(getPersonService.getNomisNumberWithPrisonFilter(hmppsId = hmppsId, filters)).thenReturn( |
| 54 | + Response( |
| 55 | + data = null, |
| 56 | + errors = |
| 57 | + listOf( |
| 58 | + UpstreamApiError( |
| 59 | + type = UpstreamApiError.Type.BAD_REQUEST, |
| 60 | + causedBy = UpstreamApi.NOMIS, |
| 61 | + ), |
| 62 | + ), |
| 63 | + ), |
| 64 | + ) |
| 65 | + |
| 66 | + val response = getVisitBalancesForPersonService.execute(hmppsId, filters) |
| 67 | + response |
| 68 | + .hasErrorCausedBy( |
| 69 | + causedBy = UpstreamApi.NOMIS, |
| 70 | + type = UpstreamApiError.Type.BAD_REQUEST, |
| 71 | + ).shouldBe(true) |
| 72 | + } |
| 73 | + |
| 74 | + it("records 404 when Nomis number is null") { |
| 75 | + whenever(getPersonService.getNomisNumberWithPrisonFilter(hmppsId = hmppsId, filters)).thenReturn( |
| 76 | + Response( |
| 77 | + data = NomisNumber(nomisNumber = null), |
| 78 | + ), |
| 79 | + ) |
| 80 | + |
| 81 | + val response = getVisitBalancesForPersonService.execute(hmppsId, filters) |
| 82 | + response |
| 83 | + .hasErrorCausedBy( |
| 84 | + causedBy = UpstreamApi.NOMIS, |
| 85 | + type = UpstreamApiError.Type.ENTITY_NOT_FOUND, |
| 86 | + ).shouldBe(true) |
| 87 | + } |
| 88 | + |
| 89 | + it("return an error when nomisGateway.getVisitBalances returns an error") { |
| 90 | + whenever(nomisGateway.getVisitBalances(nomisNumber)).thenReturn( |
| 91 | + Response( |
| 92 | + data = null, |
| 93 | + errors = |
| 94 | + listOf( |
| 95 | + UpstreamApiError( |
| 96 | + type = UpstreamApiError.Type.BAD_REQUEST, |
| 97 | + causedBy = UpstreamApi.NOMIS, |
| 98 | + ), |
| 99 | + ), |
| 100 | + ), |
| 101 | + ) |
| 102 | + |
| 103 | + val response = getVisitBalancesForPersonService.execute(hmppsId, filters) |
| 104 | + response |
| 105 | + .hasErrorCausedBy( |
| 106 | + causedBy = UpstreamApi.NOMIS, |
| 107 | + type = UpstreamApiError.Type.BAD_REQUEST, |
| 108 | + ).shouldBe(true) |
| 109 | + } |
| 110 | + }) |
0 commit comments