|
| 1 | +package uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.nomis |
| 2 | + |
| 3 | +import io.kotest.core.spec.style.DescribeSpec |
| 4 | +import io.kotest.matchers.collections.shouldBeEmpty |
| 5 | +import io.kotest.matchers.collections.shouldHaveSize |
| 6 | +import io.kotest.matchers.shouldBe |
| 7 | +import org.mockito.Mockito |
| 8 | +import org.mockito.internal.verification.VerificationModeFactory |
| 9 | +import org.mockito.kotlin.verify |
| 10 | +import org.mockito.kotlin.whenever |
| 11 | +import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer |
| 12 | +import org.springframework.http.HttpStatus |
| 13 | +import org.springframework.test.context.ActiveProfiles |
| 14 | +import org.springframework.test.context.ContextConfiguration |
| 15 | +import org.springframework.test.context.bean.override.mockito.MockitoBean |
| 16 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.removeWhitespaceAndNewlines |
| 17 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.HmppsAuthGateway |
| 18 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NomisGateway |
| 19 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.HmppsAuthMockServer |
| 20 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.NomisApiMockServer |
| 21 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi |
| 22 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError |
| 23 | + |
| 24 | +@ActiveProfiles("test") |
| 25 | +@ContextConfiguration( |
| 26 | + initializers = [ConfigDataApplicationContextInitializer::class], |
| 27 | + classes = [NomisGateway::class], |
| 28 | +) |
| 29 | +class GetTransactionsForPersonTest( |
| 30 | + @MockitoBean val hmppsAuthGateway: HmppsAuthGateway, |
| 31 | + val nomisGateway: NomisGateway, |
| 32 | +) : DescribeSpec( |
| 33 | + { |
| 34 | + val nomisApiMockServer = NomisApiMockServer() |
| 35 | + val nomisNumber = "AA1234Z" |
| 36 | + val prisonId = "XYZ" |
| 37 | + val accountCode = "spends" |
| 38 | + val fromDate = "2019-04-01" |
| 39 | + val toDate = "2019-04-05" |
| 40 | + val transactionsPath = "/api/v1/prison/$prisonId/offenders/$nomisNumber/accounts/$accountCode/transactions?from_date=$fromDate&to_date=$toDate" |
| 41 | + |
| 42 | + beforeEach { |
| 43 | + nomisApiMockServer.start() |
| 44 | + nomisApiMockServer.stubNomisApiResponse( |
| 45 | + transactionsPath, |
| 46 | + """ |
| 47 | + { |
| 48 | + "transactions": [ |
| 49 | + { |
| 50 | + "id": "204564839-3", |
| 51 | + "type": { |
| 52 | + "code": "spends", |
| 53 | + "desc": "Spends account code" |
| 54 | + }, |
| 55 | + "description": "Transfer In Regular from caseload PVR", |
| 56 | + "amount": 12345, |
| 57 | + "date": "2016-10-21" |
| 58 | + } |
| 59 | + ] |
| 60 | + } |
| 61 | + |
| 62 | + """.removeWhitespaceAndNewlines(), |
| 63 | + ) |
| 64 | + |
| 65 | + Mockito.reset(hmppsAuthGateway) |
| 66 | + whenever(hmppsAuthGateway.getClientToken("NOMIS")).thenReturn(HmppsAuthMockServer.TOKEN) |
| 67 | + } |
| 68 | + |
| 69 | + afterTest { |
| 70 | + nomisApiMockServer.stop() |
| 71 | + } |
| 72 | + |
| 73 | + it("authenticates using HMPPS Auth with credentials") { |
| 74 | + nomisGateway.getTransactionsForPerson( |
| 75 | + prisonId, |
| 76 | + nomisNumber, |
| 77 | + accountCode, |
| 78 | + fromDate, |
| 79 | + toDate, |
| 80 | + ) |
| 81 | + |
| 82 | + verify(hmppsAuthGateway, VerificationModeFactory.times(1)).getClientToken("NOMIS") |
| 83 | + } |
| 84 | + |
| 85 | + it("returns transactions for the matching person ID") { |
| 86 | + val response = |
| 87 | + nomisGateway.getTransactionsForPerson( |
| 88 | + prisonId, |
| 89 | + nomisNumber, |
| 90 | + accountCode, |
| 91 | + fromDate, |
| 92 | + toDate, |
| 93 | + ) |
| 94 | + |
| 95 | + response.errors.shouldBeEmpty() |
| 96 | + response.data |
| 97 | + ?.transactions |
| 98 | + ?.get(0) |
| 99 | + ?.id |
| 100 | + .shouldBe("204564839-3") |
| 101 | + response.data |
| 102 | + ?.transactions |
| 103 | + ?.get(0) |
| 104 | + ?.type |
| 105 | + ?.code |
| 106 | + .shouldBe("spends") |
| 107 | + response.data |
| 108 | + ?.transactions |
| 109 | + ?.get(0) |
| 110 | + ?.type |
| 111 | + ?.desc |
| 112 | + .shouldBe("Spends account code") |
| 113 | + response.data |
| 114 | + ?.transactions |
| 115 | + ?.get(0) |
| 116 | + ?.description |
| 117 | + .shouldBe("Transfer In Regular from caseload PVR") |
| 118 | + response.data |
| 119 | + ?.transactions |
| 120 | + ?.get(0) |
| 121 | + ?.amount |
| 122 | + .shouldBe(12345) |
| 123 | + response.data |
| 124 | + ?.transactions |
| 125 | + ?.get(0) |
| 126 | + ?.date |
| 127 | + .shouldBe("2016-10-21") |
| 128 | + } |
| 129 | + |
| 130 | + it("returns an error when 404 Not Found is returned because no person is found") { |
| 131 | + nomisApiMockServer.stubNomisApiResponse(transactionsPath, "", HttpStatus.NOT_FOUND) |
| 132 | + |
| 133 | + val response = nomisGateway.getAccountsForPerson(prisonId, nomisNumber) |
| 134 | + |
| 135 | + response.errors.shouldHaveSize(1) |
| 136 | + response.errors |
| 137 | + .first() |
| 138 | + .causedBy |
| 139 | + .shouldBe(UpstreamApi.NOMIS) |
| 140 | + response.errors |
| 141 | + .first() |
| 142 | + .type |
| 143 | + .shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) |
| 144 | + } |
| 145 | + }, |
| 146 | + ) |
0 commit comments