|
| 1 | +package uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.ndelius |
| 2 | + |
| 3 | +import io.kotest.core.spec.style.DescribeSpec |
| 4 | +import io.kotest.matchers.collections.shouldHaveSize |
| 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.HmppsAuthGateway |
| 16 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NDeliusGateway |
| 17 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.HmppsAuthMockServer |
| 18 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.mockservers.NDeliusApiMockServer |
| 19 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.CommunityOffenderManager |
| 20 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PersonResponsibleOfficerName |
| 21 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PersonResponsibleOfficerTeam |
| 22 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi |
| 23 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError |
| 24 | +import java.io.File |
| 25 | + |
| 26 | +@ActiveProfiles("test") |
| 27 | +@ContextConfiguration( |
| 28 | + initializers = [ConfigDataApplicationContextInitializer::class], |
| 29 | + classes = [NDeliusGateway::class], |
| 30 | +) |
| 31 | +class GetCommunityOffenderManagerForPersonTest( |
| 32 | + @MockBean |
| 33 | + val hmppsAuthGateway: HmppsAuthGateway, |
| 34 | + val nDeliusGateway: NDeliusGateway, |
| 35 | +) : DescribeSpec( |
| 36 | + { |
| 37 | + val nDeliusApiMockServer = NDeliusApiMockServer() |
| 38 | + val deliusCrn = "X777776" |
| 39 | + |
| 40 | + beforeEach { |
| 41 | + nDeliusApiMockServer.start() |
| 42 | + nDeliusApiMockServer.stubGetSupervisionsForPerson( |
| 43 | + deliusCrn, |
| 44 | + File( |
| 45 | + "src/test/kotlin/uk/gov/justice/digital/hmpps/hmppsintegrationapi/gateways/ndelius/fixtures/GetSupervisionsResponse.json", |
| 46 | + ).readText(), |
| 47 | + ) |
| 48 | + |
| 49 | + Mockito.reset(hmppsAuthGateway) |
| 50 | + whenever(hmppsAuthGateway.getClientToken("nDelius")).thenReturn(HmppsAuthMockServer.TOKEN) |
| 51 | + } |
| 52 | + |
| 53 | + afterTest { |
| 54 | + nDeliusApiMockServer.stop() |
| 55 | + } |
| 56 | + |
| 57 | + it("authenticates using HMPPS Auth with credentials") { |
| 58 | + nDeliusGateway.getCommunityOffenderManagerForPerson(deliusCrn) |
| 59 | + |
| 60 | + verify(hmppsAuthGateway, VerificationModeFactory.times(1)).getClientToken("nDelius") |
| 61 | + } |
| 62 | + |
| 63 | + it("returns community offender manager related to the matching CRN") { |
| 64 | + val response = nDeliusGateway.getCommunityOffenderManagerForPerson(deliusCrn) |
| 65 | + |
| 66 | + response.data.shouldBe( |
| 67 | + CommunityOffenderManager( |
| 68 | + name = PersonResponsibleOfficerName(forename = "John", surname = "Smith"), |
| 69 | + email = "johnsmith@email.com", |
| 70 | + telephoneNumber = "077777777", |
| 71 | + team = |
| 72 | + PersonResponsibleOfficerTeam( |
| 73 | + code = "Code1", |
| 74 | + description = "Description", |
| 75 | + email = "team@email.com", |
| 76 | + telephoneNumber = "07888888", |
| 77 | + ), |
| 78 | + ), |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + it("returns an empty object if no community offender manager is found") { |
| 83 | + nDeliusApiMockServer.stubGetSupervisionsForPerson( |
| 84 | + deliusCrn, |
| 85 | + """ |
| 86 | + { |
| 87 | + "communityManager": {}, |
| 88 | + "mappaDetail": {}, |
| 89 | + "supervisions": [] |
| 90 | + } |
| 91 | + """, |
| 92 | + ) |
| 93 | + |
| 94 | + val response = nDeliusGateway.getCommunityOffenderManagerForPerson(deliusCrn) |
| 95 | + |
| 96 | + response.data.shouldBe(CommunityOffenderManager()) |
| 97 | + } |
| 98 | + |
| 99 | + it("returns an error when 404 Not Found is returned because no person is found") { |
| 100 | + nDeliusApiMockServer.stubGetSupervisionsForPerson(deliusCrn, "", HttpStatus.NOT_FOUND) |
| 101 | + |
| 102 | + val response = nDeliusGateway.getCommunityOffenderManagerForPerson(deliusCrn) |
| 103 | + |
| 104 | + response.errors.shouldHaveSize(1) |
| 105 | + response.errors.first().causedBy.shouldBe(UpstreamApi.NDELIUS) |
| 106 | + response.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) |
| 107 | + } |
| 108 | + }, |
| 109 | + ) |
0 commit comments