|
| 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.Response |
| 19 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.RiskAssessment |
| 20 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.RiskCategory |
| 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.services.GetRiskCategoriesForPersonService |
| 24 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService |
| 25 | +import java.net.URLEncoder |
| 26 | +import java.nio.charset.StandardCharsets |
| 27 | + |
| 28 | +@WebMvcTest(controllers = [RiskCategoriesController::class]) |
| 29 | +@ActiveProfiles("test") |
| 30 | +internal class RiskCategoriesControllerTest( |
| 31 | + @Autowired var springMockMvc: MockMvc, |
| 32 | + @MockBean val getRiskCategoriesForPersonService: GetRiskCategoriesForPersonService, |
| 33 | + @MockBean val auditService: AuditService, |
| 34 | +) : DescribeSpec( |
| 35 | + { |
| 36 | + val hmppsId = "9999/11111A" |
| 37 | + val encodedHmppsId = URLEncoder.encode(hmppsId, StandardCharsets.UTF_8) |
| 38 | + val path = "/v1/persons/$encodedHmppsId/risks/categories" |
| 39 | + val mockMvc = IntegrationAPIMockMvc(springMockMvc) |
| 40 | + |
| 41 | + describe("GET $path") { |
| 42 | + beforeTest { |
| 43 | + Mockito.reset(getRiskCategoriesForPersonService) |
| 44 | + whenever(getRiskCategoriesForPersonService.execute(hmppsId)).thenReturn( |
| 45 | + Response( |
| 46 | + data = RiskCategory( |
| 47 | + offenderNo = "A1234AA", |
| 48 | + assessments = listOf(RiskAssessment(classificationCode = "C")), |
| 49 | + ), |
| 50 | + ), |
| 51 | + ) |
| 52 | + |
| 53 | + Mockito.reset(auditService) |
| 54 | + } |
| 55 | + |
| 56 | + it("returns a 200 OK status code") { |
| 57 | + val result = mockMvc.performAuthorised(path) |
| 58 | + |
| 59 | + result.response.status.shouldBe(HttpStatus.OK.value()) |
| 60 | + } |
| 61 | + |
| 62 | + it("gets the risk categories for a person with the matching ID") { |
| 63 | + mockMvc.performAuthorised(path) |
| 64 | + verify(getRiskCategoriesForPersonService, VerificationModeFactory.times(1)).execute(hmppsId) |
| 65 | + } |
| 66 | + |
| 67 | + it("logs audit") { |
| 68 | + mockMvc.performAuthorised(path) |
| 69 | + |
| 70 | + verify(auditService, VerificationModeFactory.times(1)).createEvent("GET_PERSON_RISK_CATEGORIES", "Person risk categories with hmpps id: $hmppsId has been retrieved") |
| 71 | + } |
| 72 | + |
| 73 | + it("returns the risk categories for a person with the matching ID") { |
| 74 | + val result = mockMvc.performAuthorised(path) |
| 75 | + |
| 76 | + result.response.contentAsString.shouldContain( |
| 77 | + """ |
| 78 | + "data": { |
| 79 | + "offenderNo": "A1234AA", |
| 80 | + "assessments": [ |
| 81 | + { |
| 82 | + "classificationCode": "C" |
| 83 | + } |
| 84 | + ] |
| 85 | + } |
| 86 | + """.removeWhitespaceAndNewlines(), |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + it("returns a 404 NOT FOUND status code when person isn't found in the upstream API") { |
| 91 | + whenever(getRiskCategoriesForPersonService.execute(hmppsId)).thenReturn( |
| 92 | + Response( |
| 93 | + data = RiskCategory(), |
| 94 | + errors = listOf( |
| 95 | + UpstreamApiError( |
| 96 | + causedBy = UpstreamApi.NOMIS, |
| 97 | + type = UpstreamApiError.Type.ENTITY_NOT_FOUND, |
| 98 | + ), |
| 99 | + ), |
| 100 | + ), |
| 101 | + ) |
| 102 | + |
| 103 | + val result = mockMvc.performAuthorised(path) |
| 104 | + |
| 105 | + result.response.status.shouldBe(HttpStatus.NOT_FOUND.value()) |
| 106 | + } |
| 107 | + } |
| 108 | + }, |
| 109 | +) |
0 commit comments