|
| 1 | +package uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.createAndVaryLicence |
| 2 | + |
| 3 | +import io.kotest.core.spec.style.DescribeSpec |
| 4 | +import io.kotest.matchers.booleans.shouldBeTrue |
| 5 | +import io.kotest.matchers.shouldBe |
| 6 | +import org.mockito.Mockito |
| 7 | +import org.mockito.internal.verification.VerificationModeFactory |
| 8 | +import org.mockito.kotlin.verify |
| 9 | +import org.mockito.kotlin.whenever |
| 10 | +import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer |
| 11 | +import org.springframework.boot.test.mock.mockito.MockBean |
| 12 | +import org.springframework.http.HttpStatus |
| 13 | +import org.springframework.test.context.ActiveProfiles |
| 14 | +import org.springframework.test.context.ContextConfiguration |
| 15 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.CreateAndVaryLicenceGateway |
| 16 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.HmppsAuthGateway |
| 17 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.CreateAndVaryLicenceApiMockServer |
| 18 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.HmppsAuthMockServer |
| 19 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence |
| 20 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError |
| 21 | + |
| 22 | +@ActiveProfiles("test") |
| 23 | +@ContextConfiguration( |
| 24 | + initializers = [ConfigDataApplicationContextInitializer::class], |
| 25 | + classes = [CreateAndVaryLicenceGateway::class], |
| 26 | +) |
| 27 | +class GetLicenceSummariesTest( |
| 28 | + @MockBean val hmppsAuthGateway: HmppsAuthGateway, |
| 29 | + private val createAndVaryLicenceGateway: CreateAndVaryLicenceGateway, |
| 30 | +) : DescribeSpec( |
| 31 | + { |
| 32 | + val createAndVaryLicenceApiMockServer = CreateAndVaryLicenceApiMockServer() |
| 33 | + val deliusCrn = "X777776" |
| 34 | + |
| 35 | + beforeEach { |
| 36 | + createAndVaryLicenceApiMockServer.start() |
| 37 | + createAndVaryLicenceApiMockServer.stubGetLicenceSummaries( |
| 38 | + deliusCrn, |
| 39 | + """ |
| 40 | + { |
| 41 | + "prisonNumber": "1140484" |
| 42 | + } |
| 43 | + """, |
| 44 | + ) |
| 45 | + |
| 46 | + Mockito.reset(hmppsAuthGateway) |
| 47 | + whenever(hmppsAuthGateway.getClientToken("CVL")).thenReturn(HmppsAuthMockServer.TOKEN) |
| 48 | + } |
| 49 | + |
| 50 | + afterTest { |
| 51 | + createAndVaryLicenceApiMockServer.stop() |
| 52 | + } |
| 53 | + |
| 54 | + it("authenticates using HMPPS Auth with credentials") { |
| 55 | + createAndVaryLicenceGateway.getLicenceSummaries(deliusCrn) |
| 56 | + |
| 57 | + verify(hmppsAuthGateway, VerificationModeFactory.times(1)).getClientToken("CVL") |
| 58 | + } |
| 59 | + |
| 60 | + it("returns licences for a person with the matching ID") { |
| 61 | + val response = createAndVaryLicenceGateway.getLicenceSummaries(deliusCrn) |
| 62 | + |
| 63 | + response.data.shouldBe( |
| 64 | + Licence( |
| 65 | + offenderNumber = "1140484", |
| 66 | + ), |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + it("returns an error when 404 NOT FOUND is returned") { |
| 71 | + createAndVaryLicenceApiMockServer.stubGetLicenceSummaries( |
| 72 | + deliusCrn, |
| 73 | + """ |
| 74 | + { |
| 75 | + "developerMessage": "cannot find person" |
| 76 | + } |
| 77 | + """, |
| 78 | + HttpStatus.NOT_FOUND, |
| 79 | + ) |
| 80 | + |
| 81 | + val response = createAndVaryLicenceGateway.getLicenceSummaries(deliusCrn) |
| 82 | + |
| 83 | + response.hasError(UpstreamApiError.Type.ENTITY_NOT_FOUND).shouldBeTrue() |
| 84 | + } |
| 85 | + }, |
| 86 | +) |
0 commit comments