|
| 1 | +package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1.prison |
| 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.times |
| 7 | +import org.mockito.kotlin.verify |
| 8 | +import org.mockito.kotlin.whenever |
| 9 | +import org.springframework.beans.factory.annotation.Autowired |
| 10 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest |
| 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.extensions.removeWhitespaceAndNewlines |
| 15 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.IntegrationAPIMockMvc |
| 16 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Person |
| 17 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response |
| 18 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi |
| 19 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError |
| 20 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError.Type.ENTITY_NOT_FOUND |
| 21 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetPersonService |
| 22 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService |
| 23 | +import java.time.LocalDate |
| 24 | + |
| 25 | +@WebMvcTest(controllers = [PrisonController::class]) |
| 26 | +@ActiveProfiles("test") |
| 27 | +internal class PrisonControllerTest( |
| 28 | + @Autowired var springMockMvc: MockMvc, |
| 29 | + @MockitoBean val getPersonService: GetPersonService, |
| 30 | + @MockitoBean val auditService: AuditService, |
| 31 | +) : DescribeSpec({ |
| 32 | + val hmppsId = "200313116M" |
| 33 | + val basePath = "/v1/prison" |
| 34 | + val mockMvc = IntegrationAPIMockMvc(springMockMvc) |
| 35 | + |
| 36 | + describe("GET $basePath") { |
| 37 | + } |
| 38 | + |
| 39 | + afterTest { |
| 40 | + Mockito.reset(getPersonService) |
| 41 | + Mockito.reset(auditService) |
| 42 | + } |
| 43 | + |
| 44 | + it("returns 500 when service throws an exception") { |
| 45 | + whenever(getPersonService.getPrisoner(hmppsId)).thenThrow(RuntimeException("Service error")) |
| 46 | + |
| 47 | + val result = mockMvc.performAuthorised("$basePath/prisoners/$hmppsId") |
| 48 | + |
| 49 | + result.response.status.shouldBe(500) |
| 50 | + } |
| 51 | + |
| 52 | + it("returns a person with all fields populated") { |
| 53 | + whenever(getPersonService.getPrisoner(hmppsId)).thenReturn( |
| 54 | + Response( |
| 55 | + data = |
| 56 | + Person( |
| 57 | + firstName = "Barry", |
| 58 | + lastName = "Allen", |
| 59 | + middleName = "Jonas", |
| 60 | + dateOfBirth = LocalDate.parse("2023-03-01"), |
| 61 | + gender = "Male", |
| 62 | + ethnicity = "Caucasian", |
| 63 | + pncId = "PNC123456", |
| 64 | + ), |
| 65 | + ), |
| 66 | + ) |
| 67 | + |
| 68 | + val result = mockMvc.performAuthorised("$basePath/prisoners/$hmppsId") |
| 69 | + |
| 70 | + result.response.contentAsString.shouldBe( |
| 71 | + """ |
| 72 | + { |
| 73 | + "data":{ |
| 74 | + "firstName":"Barry", |
| 75 | + "lastName":"Allen", |
| 76 | + "middleName":"Jonas", |
| 77 | + "dateOfBirth":"2023-03-01", |
| 78 | + "gender":"Male", |
| 79 | + "ethnicity":"Caucasian", |
| 80 | + "aliases":[], |
| 81 | + "identifiers":{ |
| 82 | + "nomisNumber":null, |
| 83 | + "croNumber":null, |
| 84 | + "deliusCrn":null |
| 85 | + }, |
| 86 | + "pncId": "PNC123456", |
| 87 | + "hmppsId": null, |
| 88 | + "contactDetails": null |
| 89 | + } |
| 90 | + } |
| 91 | + """.removeWhitespaceAndNewlines(), |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + it("logs audit event") { |
| 96 | + whenever(getPersonService.getPrisoner(hmppsId)).thenReturn( |
| 97 | + Response( |
| 98 | + data = |
| 99 | + Person( |
| 100 | + firstName = "Barry", |
| 101 | + lastName = "Allen", |
| 102 | + middleName = "Jonas", |
| 103 | + dateOfBirth = LocalDate.parse("2023-03-01"), |
| 104 | + gender = "Male", |
| 105 | + ethnicity = "Caucasian", |
| 106 | + pncId = "PNC123456", |
| 107 | + ), |
| 108 | + ), |
| 109 | + ) |
| 110 | + |
| 111 | + mockMvc.performAuthorised("$basePath/prisoners/$hmppsId") |
| 112 | + verify( |
| 113 | + auditService, |
| 114 | + times(1), |
| 115 | + ).createEvent( |
| 116 | + "GET_PERSON_DETAILS", |
| 117 | + mapOf("hmppsId" to hmppsId), |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + it("returns 404 when prisoner is not found") { |
| 122 | + whenever(getPersonService.getPrisoner(hmppsId)).thenReturn( |
| 123 | + Response( |
| 124 | + data = null, |
| 125 | + errors = |
| 126 | + listOf( |
| 127 | + UpstreamApiError( |
| 128 | + type = ENTITY_NOT_FOUND, |
| 129 | + causedBy = UpstreamApi.PROBATION_OFFENDER_SEARCH, |
| 130 | + description = "Prisoner not found", |
| 131 | + ), |
| 132 | + ), |
| 133 | + ), |
| 134 | + ) |
| 135 | + |
| 136 | + val result = mockMvc.performAuthorised("$basePath/prisoners/$hmppsId") |
| 137 | + |
| 138 | + result.response.status.shouldBe(404) |
| 139 | + } |
| 140 | + }) |
0 commit comments