|
| 1 | +package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1.person |
| 2 | + |
| 3 | +import io.kotest.core.spec.style.DescribeSpec |
| 4 | +import io.kotest.matchers.shouldBe |
| 5 | +import org.mockito.Mockito |
| 6 | +import org.mockito.kotlin.verify |
| 7 | +import org.mockito.kotlin.whenever |
| 8 | +import org.springframework.beans.factory.annotation.Autowired |
| 9 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest |
| 10 | +import org.springframework.http.HttpStatus |
| 11 | +import org.springframework.test.context.ActiveProfiles |
| 12 | +import org.springframework.test.context.bean.override.mockito.MockitoBean |
| 13 | +import org.springframework.test.web.servlet.MockMvc |
| 14 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.IntegrationAPIMockMvc |
| 15 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.expressionOfInterest.ExpressionInterest |
| 16 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Identifiers |
| 17 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.OffenderSearchResponse |
| 18 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person |
| 19 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.PersonOnProbation |
| 20 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response |
| 21 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi |
| 22 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError |
| 23 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.prisoneroffendersearch.POSPrisoner |
| 24 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.ExpressionInterestService |
| 25 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetPersonService |
| 26 | + |
| 27 | +@WebMvcTest(controllers = [ExpressionInterestController::class]) |
| 28 | +@ActiveProfiles("test") |
| 29 | +class ExpressionInterestControllerTest( |
| 30 | + @Autowired private val springMockMvc: MockMvc, |
| 31 | + @MockitoBean private val getPersonService: GetPersonService, |
| 32 | + @MockitoBean private val expressionOfInterestService: ExpressionInterestService, |
| 33 | +) : DescribeSpec({ |
| 34 | + |
| 35 | + val mockMvc = IntegrationAPIMockMvc(springMockMvc) |
| 36 | + val basePath = "/v1/persons" |
| 37 | + val validHmppsId = "1234ABC" |
| 38 | + val invalidHmppsId = "INVALID_ID" |
| 39 | + val jobId = "5678" |
| 40 | + |
| 41 | + describe("PUT $basePath/{hmppsId}/expression-of-interest/jobs/{jobId}") { |
| 42 | + |
| 43 | + beforeTest { |
| 44 | + Mockito.reset(getPersonService, expressionOfInterestService) |
| 45 | + } |
| 46 | + |
| 47 | + it("should return 201 Created when the expression of interest is successfully submitted") { |
| 48 | + val personOnProbation = |
| 49 | + PersonOnProbation( |
| 50 | + person = |
| 51 | + Person( |
| 52 | + "Sam", |
| 53 | + "Smith", |
| 54 | + identifiers = Identifiers(nomisNumber = validHmppsId), |
| 55 | + hmppsId = validHmppsId, |
| 56 | + currentExclusion = true, |
| 57 | + exclusionMessage = "An exclusion exists", |
| 58 | + currentRestriction = false, |
| 59 | + ), |
| 60 | + underActiveSupervision = true, |
| 61 | + ) |
| 62 | + |
| 63 | + val probationResponse = Response(data = personOnProbation, errors = emptyList()) |
| 64 | + |
| 65 | + val prisonOffenderSearch = POSPrisoner("Kim", "Kardashian") |
| 66 | + val prisonResponse = Response(data = prisonOffenderSearch, errors = emptyList()) |
| 67 | + |
| 68 | + val offenderMap = |
| 69 | + OffenderSearchResponse( |
| 70 | + probationOffenderSearch = probationResponse.data, |
| 71 | + prisonerOffenderSearch = prisonResponse.data.toPerson(), |
| 72 | + ) |
| 73 | + |
| 74 | + whenever(getPersonService.getCombinedDataForPerson(validHmppsId)).thenReturn( |
| 75 | + Response(data = offenderMap, errors = emptyList()), |
| 76 | + ) |
| 77 | + |
| 78 | + val result = mockMvc.performAuthorised("$basePath/$validHmppsId/expression-of-interest/jobs/$jobId") |
| 79 | + |
| 80 | + result.response.status.shouldBe(HttpStatus.CREATED.value()) |
| 81 | + verify(expressionOfInterestService).sendExpressionOfInterest(ExpressionInterest(jobId, validHmppsId)) |
| 82 | + } |
| 83 | + |
| 84 | + it("should return 400 Bad Request when the HMPPS ID is not found") { |
| 85 | + whenever(getPersonService.getCombinedDataForPerson(invalidHmppsId)).thenReturn( |
| 86 | + Response( |
| 87 | + data = |
| 88 | + OffenderSearchResponse( |
| 89 | + probationOffenderSearch = null, |
| 90 | + prisonerOffenderSearch = null, |
| 91 | + ), |
| 92 | + errors = |
| 93 | + listOf( |
| 94 | + UpstreamApiError( |
| 95 | + type = UpstreamApiError.Type.ENTITY_NOT_FOUND, |
| 96 | + causedBy = UpstreamApi.PROBATION_OFFENDER_SEARCH, |
| 97 | + ), |
| 98 | + ), |
| 99 | + ), |
| 100 | + ) |
| 101 | + |
| 102 | + val result = mockMvc.performAuthorised("$basePath/$invalidHmppsId/expression-of-interest/jobs/$jobId") |
| 103 | + |
| 104 | + result.response.status.shouldBe(HttpStatus.BAD_REQUEST.value()) |
| 105 | + } |
| 106 | + |
| 107 | + it("should return 500 Internal Server Error when an unexpected error occurs") { |
| 108 | + whenever(getPersonService.getCombinedDataForPerson(validHmppsId)).thenThrow(RuntimeException("Unexpected error")) |
| 109 | + |
| 110 | + val result = mockMvc.performAuthorised("$basePath/$validHmppsId/expression-of-interest/jobs/$jobId") |
| 111 | + |
| 112 | + result.response.status.shouldBe(HttpStatus.INTERNAL_SERVER_ERROR.value()) |
| 113 | + } |
| 114 | + } |
| 115 | + }) |
0 commit comments