|
| 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.ints.shouldBeGreaterThan |
| 7 | +import io.kotest.matchers.nulls.shouldNotBeNull |
| 8 | +import io.kotest.matchers.shouldBe |
| 9 | +import org.mockito.Mockito |
| 10 | +import org.mockito.internal.verification.VerificationModeFactory |
| 11 | +import org.mockito.kotlin.verify |
| 12 | +import org.mockito.kotlin.whenever |
| 13 | +import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer |
| 14 | +import org.springframework.http.HttpStatus |
| 15 | +import org.springframework.test.context.ActiveProfiles |
| 16 | +import org.springframework.test.context.ContextConfiguration |
| 17 | +import org.springframework.test.context.bean.override.mockito.MockitoBean |
| 18 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.removeWhitespaceAndNewlines |
| 19 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.HmppsAuthGateway |
| 20 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NomisGateway |
| 21 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.HmppsAuthMockServer |
| 22 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.NomisApiMockServer |
| 23 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi |
| 24 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError |
| 25 | + |
| 26 | +@ActiveProfiles("test") |
| 27 | +@ContextConfiguration( |
| 28 | + initializers = [ConfigDataApplicationContextInitializer::class], |
| 29 | + classes = [NomisGateway::class], |
| 30 | +) |
| 31 | +class GetOffenderVisitRestrictionsTest( |
| 32 | + @MockitoBean val hmppsAuthGateway: HmppsAuthGateway, |
| 33 | + val nomisGateway: NomisGateway, |
| 34 | +) : DescribeSpec( |
| 35 | + { |
| 36 | + val nomisApiMockServer = NomisApiMockServer() |
| 37 | + val offenderNo = "zyx987" |
| 38 | + val offenderRestrictionsPath = "/api/offenders/$offenderNo/offender-restrictions" |
| 39 | + |
| 40 | + beforeEach { |
| 41 | + nomisApiMockServer.start() |
| 42 | + nomisApiMockServer.stubNomisApiResponse( |
| 43 | + offenderRestrictionsPath, |
| 44 | + """ |
| 45 | + { |
| 46 | + "bookingId": 9007199254740991, |
| 47 | + "offenderRestrictions": [ |
| 48 | + { |
| 49 | + "restrictionId": 9007199254740991, |
| 50 | + "comment": "string", |
| 51 | + "restrictionType": "string", |
| 52 | + "restrictionTypeDescription": "string", |
| 53 | + "startDate": "1980-01-01", |
| 54 | + "expiryDate": "1980-01-01", |
| 55 | + "active": true |
| 56 | + } |
| 57 | + ] |
| 58 | + } |
| 59 | + """.removeWhitespaceAndNewlines(), |
| 60 | + ) |
| 61 | + |
| 62 | + Mockito.reset(hmppsAuthGateway) |
| 63 | + whenever(hmppsAuthGateway.getClientToken("NOMIS")).thenReturn(HmppsAuthMockServer.TOKEN) |
| 64 | + } |
| 65 | + |
| 66 | + afterTest { |
| 67 | + nomisApiMockServer.stop() |
| 68 | + } |
| 69 | + |
| 70 | + it("authenticates using HMPPS Auth with credentials") { |
| 71 | + nomisGateway.getOffenderVisitRestrictions(offenderNo) |
| 72 | + |
| 73 | + verify(hmppsAuthGateway, VerificationModeFactory.times(1)).getClientToken("NOMIS") |
| 74 | + } |
| 75 | + |
| 76 | + it("returns offender visit restrictions for the matching person ID") { |
| 77 | + val response = nomisGateway.getOffenderVisitRestrictions(offenderNo) |
| 78 | + |
| 79 | + response.data.shouldNotBeNull() |
| 80 | + response.data!!.count().shouldBeGreaterThan(0) |
| 81 | + } |
| 82 | + |
| 83 | + it("returns a person with an empty list of restrictions when no restrictions are found") { |
| 84 | + nomisApiMockServer.stubNomisApiResponse( |
| 85 | + offenderRestrictionsPath, |
| 86 | + """ |
| 87 | + { |
| 88 | + "bookingId": 9007199254740991, |
| 89 | + "offenderRestrictions": [] |
| 90 | + } |
| 91 | + """.removeWhitespaceAndNewlines(), |
| 92 | + ) |
| 93 | + |
| 94 | + val response = nomisGateway.getOffenderVisitRestrictions(offenderNo) |
| 95 | + |
| 96 | + response.data.shouldBeEmpty() |
| 97 | + } |
| 98 | + |
| 99 | + it("returns an error when 404 Not Found is returned because no person is found") { |
| 100 | + nomisApiMockServer.stubNomisApiResponse(offenderRestrictionsPath, "", HttpStatus.NOT_FOUND) |
| 101 | + |
| 102 | + val response = nomisGateway.getOffenderVisitRestrictions(offenderNo) |
| 103 | + |
| 104 | + response.errors.shouldHaveSize(1) |
| 105 | + response.errors |
| 106 | + .first() |
| 107 | + .causedBy |
| 108 | + .shouldBe(UpstreamApi.NOMIS) |
| 109 | + response.errors |
| 110 | + .first() |
| 111 | + .type |
| 112 | + .shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) |
| 113 | + } |
| 114 | + |
| 115 | + it("returns an error when 400 Bad Request is returned because of an invalid request") { |
| 116 | + nomisApiMockServer.stubNomisApiResponse(offenderRestrictionsPath, "", HttpStatus.BAD_REQUEST) |
| 117 | + |
| 118 | + val response = nomisGateway.getOffenderVisitRestrictions(offenderNo) |
| 119 | + |
| 120 | + response.errors.shouldHaveSize(1) |
| 121 | + response.errors |
| 122 | + .first() |
| 123 | + .causedBy |
| 124 | + .shouldBe(UpstreamApi.NOMIS) |
| 125 | + response.errors |
| 126 | + .first() |
| 127 | + .type |
| 128 | + .shouldBe(UpstreamApiError.Type.BAD_REQUEST) |
| 129 | + } |
| 130 | + }, |
| 131 | + ) |
0 commit comments