|
| 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.internal.verification.VerificationModeFactory |
| 7 | +import org.mockito.kotlin.verify |
| 8 | +import org.mockito.kotlin.whenever |
| 9 | +import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer |
| 10 | +import org.springframework.boot.test.mock.mockito.MockBean |
| 11 | +import org.springframework.test.context.ContextConfiguration |
| 12 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.gateways.NDeliusGateway |
| 13 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.CommunityOffenderManager |
| 14 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Identifiers |
| 15 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person |
| 16 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PersonResponsibleOfficerName |
| 17 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PersonResponsibleOfficerTeam |
| 18 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response |
| 19 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi |
| 20 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError |
| 21 | + |
| 22 | +@ContextConfiguration( |
| 23 | + initializers = [ConfigDataApplicationContextInitializer::class], |
| 24 | + classes = [GetCommunityOffenderManagerForPersonService::class], |
| 25 | +) |
| 26 | +class GetCommunityOffenderManagerForPersonServiceTest( |
| 27 | + @MockBean val nDeliusGateway: NDeliusGateway, |
| 28 | + @MockBean val getPersonService: GetPersonService, |
| 29 | + private val getCommunityOffenderManagerForPersonService: GetCommunityOffenderManagerForPersonService, |
| 30 | +) : DescribeSpec( |
| 31 | + { |
| 32 | + val hmppsId = "1234/56789B" |
| 33 | + val deliusCrn = "X224466" |
| 34 | + |
| 35 | + val person = Person(firstName = "Sam", lastName = "Smith", identifiers = Identifiers(deliusCrn = deliusCrn)) |
| 36 | + |
| 37 | + val communityOffenderManager = CommunityOffenderManager(name = PersonResponsibleOfficerName(forename = "Michael", surname = "Green"), email = "email@email.com", telephoneNumber = "07471234567", team = PersonResponsibleOfficerTeam(code = "Code2", description = "Service description", email = "email2@email2.com", telephoneNumber = "07170987654")) |
| 38 | + |
| 39 | + beforeEach { |
| 40 | + Mockito.reset(getPersonService) |
| 41 | + Mockito.reset(nDeliusGateway) |
| 42 | + |
| 43 | + whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn(Response(person)) |
| 44 | + whenever(nDeliusGateway.getCommunityOffenderManagerForPerson(id = deliusCrn)).thenReturn(Response(communityOffenderManager)) |
| 45 | + } |
| 46 | + |
| 47 | + it("performs a search according to hmpps Id") { |
| 48 | + getCommunityOffenderManagerForPersonService.execute(hmppsId) |
| 49 | + verify(getPersonService, VerificationModeFactory.times(1)).execute(hmppsId = hmppsId) |
| 50 | + } |
| 51 | + |
| 52 | + it("Returns a community offender manager for person given a hmppsId") { |
| 53 | + whenever(getPersonService.execute(hmppsId = hmppsId)).thenReturn( |
| 54 | + Response( |
| 55 | + data = person, |
| 56 | + ), |
| 57 | + ) |
| 58 | + val result = getCommunityOffenderManagerForPersonService.execute(hmppsId) |
| 59 | + result.shouldBe(Response(data = communityOffenderManager)) |
| 60 | + } |
| 61 | + |
| 62 | + it("should return a list of errors if person not found") { |
| 63 | + whenever(getPersonService.execute(hmppsId = "NOT_FOUND")).thenReturn( |
| 64 | + Response( |
| 65 | + data = null, |
| 66 | + errors = |
| 67 | + listOf( |
| 68 | + UpstreamApiError( |
| 69 | + causedBy = UpstreamApi.NDELIUS, |
| 70 | + type = UpstreamApiError.Type.ENTITY_NOT_FOUND, |
| 71 | + ), |
| 72 | + ), |
| 73 | + ), |
| 74 | + ) |
| 75 | + val result = getCommunityOffenderManagerForPersonService.execute("NOT_FOUND") |
| 76 | + result.data.shouldBe(CommunityOffenderManager()) |
| 77 | + result.errors.first().type.shouldBe(UpstreamApiError.Type.ENTITY_NOT_FOUND) |
| 78 | + } |
| 79 | + }, |
| 80 | + ) |
0 commit comments