|
| 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 io.kotest.matchers.string.shouldContain |
| 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.beans.factory.annotation.Autowired |
| 11 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest |
| 12 | +import org.springframework.boot.test.mock.mockito.MockBean |
| 13 | +import org.springframework.http.HttpStatus |
| 14 | +import org.springframework.test.context.ActiveProfiles |
| 15 | +import org.springframework.test.web.servlet.MockMvc |
| 16 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.extensions.removeWhitespaceAndNewlines |
| 17 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.IntegrationAPIMockMvc |
| 18 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.MappaDetail |
| 19 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Response |
| 20 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApi |
| 21 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.UpstreamApiError |
| 22 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetMappaDetailForPersonService |
| 23 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService |
| 24 | +import java.net.URLEncoder |
| 25 | +import java.nio.charset.StandardCharsets |
| 26 | + |
| 27 | +@WebMvcTest(controllers = [MappaDetailController::class]) |
| 28 | +@ActiveProfiles("test") |
| 29 | +internal class MappaDetailControllerTest( |
| 30 | + @Autowired var springMockMvc: MockMvc, |
| 31 | + @MockBean val getMappaDetailForPersonService: GetMappaDetailForPersonService, |
| 32 | + @MockBean val auditService: AuditService, |
| 33 | +) : DescribeSpec( |
| 34 | + { |
| 35 | + val hmppsId = "9999/11111A" |
| 36 | + val encodedHmppsId = URLEncoder.encode(hmppsId, StandardCharsets.UTF_8) |
| 37 | + val path = "/v1/persons/$encodedHmppsId/risks/mappadetail" |
| 38 | + val mockMvc = IntegrationAPIMockMvc(springMockMvc) |
| 39 | + |
| 40 | + describe("GET $path") { |
| 41 | + beforeTest { |
| 42 | + Mockito.reset(getMappaDetailForPersonService) |
| 43 | + whenever(getMappaDetailForPersonService.execute(hmppsId)).thenReturn( |
| 44 | + Response( |
| 45 | + MappaDetail( |
| 46 | + level = 2, |
| 47 | + levelDescription = "A high level of risk", |
| 48 | + category = 3, |
| 49 | + categoryDescription = "Behaviour", |
| 50 | + startDate = "2024-03-08", |
| 51 | + reviewDate = "2024-10-08", |
| 52 | + notes = "Review in a week", |
| 53 | + ), |
| 54 | + ), |
| 55 | + ) |
| 56 | + |
| 57 | + Mockito.reset(auditService) |
| 58 | + } |
| 59 | + |
| 60 | + it("returns a 200 OK status code") { |
| 61 | + val result = mockMvc.performAuthorised(path) |
| 62 | + |
| 63 | + result.response.status.shouldBe(HttpStatus.OK.value()) |
| 64 | + } |
| 65 | + |
| 66 | + it("gets the mappa detail for a person with the matching ID") { |
| 67 | + mockMvc.performAuthorised(path) |
| 68 | + verify(getMappaDetailForPersonService, VerificationModeFactory.times(1)).execute(hmppsId) |
| 69 | + } |
| 70 | + |
| 71 | + it("logs audit") { |
| 72 | + mockMvc.performAuthorised(path) |
| 73 | + |
| 74 | + verify(auditService, VerificationModeFactory.times(1)).createEvent("GET_MAPPA_DETAIL", "Mappa detail for person with hmpps id: $hmppsId has been retrieved") |
| 75 | + } |
| 76 | + |
| 77 | + it("returns the risk categories for a person with the matching ID") { |
| 78 | + val result = mockMvc.performAuthorised(path) |
| 79 | + |
| 80 | + result.response.contentAsString.shouldContain( |
| 81 | + """ |
| 82 | + "data": { |
| 83 | + "level": 2, |
| 84 | + "levelDescription": "A high level of risk", |
| 85 | + "category": 3, |
| 86 | + "categoryDescription": "Behaviour", |
| 87 | + "startDate": "2024-03-08", |
| 88 | + "reviewDate": "2024-10-08", |
| 89 | + "notes": "Review in a week" |
| 90 | + } |
| 91 | + """.removeWhitespaceAndNewlines(), |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + it("returns a 404 NOT FOUND status code when person isn't found in the upstream API") { |
| 96 | + whenever(getMappaDetailForPersonService.execute(hmppsId)).thenReturn( |
| 97 | + Response( |
| 98 | + data = MappaDetail(), |
| 99 | + errors = listOf( |
| 100 | + UpstreamApiError( |
| 101 | + causedBy = UpstreamApi.PROBATION_OFFENDER_SEARCH, |
| 102 | + type = UpstreamApiError.Type.ENTITY_NOT_FOUND, |
| 103 | + ), |
| 104 | + ), |
| 105 | + ), |
| 106 | + ) |
| 107 | + |
| 108 | + val result = mockMvc.performAuthorised(path) |
| 109 | + |
| 110 | + result.response.status.shouldBe(HttpStatus.NOT_FOUND.value()) |
| 111 | + } |
| 112 | + } |
| 113 | + }, |
| 114 | +) |
0 commit comments