|
| 1 | +package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1.person |
| 2 | + |
| 3 | +import io.kotest.assertions.json.shouldContainJsonKeyValue |
| 4 | +import io.kotest.core.spec.style.DescribeSpec |
| 5 | +import io.kotest.matchers.shouldBe |
| 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.helpers.IntegrationAPIMockMvc |
| 17 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.Licence |
| 18 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.models.hmpps.LicenceCondition |
| 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.GetLicenseConditionService |
| 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 = [LicenceConditionController::class]) |
| 28 | +@ActiveProfiles("test") |
| 29 | +class LicenceConditionControllerTests( |
| 30 | + @Autowired var springMockMvc: MockMvc, |
| 31 | + @MockBean val getLicenseConditionService: GetLicenseConditionService, |
| 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/licences/conditions" |
| 38 | + val mockMvc = IntegrationAPIMockMvc(springMockMvc) |
| 39 | + |
| 40 | + describe("GET $path") { |
| 41 | + beforeTest { |
| 42 | + Mockito.reset(getLicenseConditionService) |
| 43 | + Mockito.reset(auditService) |
| 44 | + whenever(getLicenseConditionService.execute(hmppsId)).thenReturn( |
| 45 | + Response( |
| 46 | + data = listOf( |
| 47 | + Licence( |
| 48 | + id = "MockId", |
| 49 | + conditions = listOf(LicenceCondition(condition = "MockCondition")), |
| 50 | + ), |
| 51 | + ), |
| 52 | + ), |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + it("throws exception when no person found") { |
| 57 | + whenever(getLicenseConditionService.execute(hmppsId = "notfound")).thenReturn( |
| 58 | + Response( |
| 59 | + data = emptyList(), |
| 60 | + errors = listOf( |
| 61 | + UpstreamApiError( |
| 62 | + type = UpstreamApiError.Type.ENTITY_NOT_FOUND, |
| 63 | + causedBy = UpstreamApi.CVL, |
| 64 | + ), |
| 65 | + ), |
| 66 | + ), |
| 67 | + ) |
| 68 | + val noFoundPath = "/v1/persons/notfound/licences/conditions" |
| 69 | + val result = mockMvc.performAuthorised(noFoundPath) |
| 70 | + result.response.status.shouldBe(HttpStatus.NOT_FOUND.value()) |
| 71 | + } |
| 72 | + |
| 73 | + it("logs audit for licence condition") { |
| 74 | + mockMvc.performAuthorised(path) |
| 75 | + |
| 76 | + verify(auditService, VerificationModeFactory.times(1)).createEvent("GET_PERSON_LICENCE_CONDITION", "Person licence condition details with hmpps id: $hmppsId has been retrieved") |
| 77 | + } |
| 78 | + |
| 79 | + it("returns paginated licence condition results") { |
| 80 | + whenever(getLicenseConditionService.execute(hmppsId)).thenReturn( |
| 81 | + Response( |
| 82 | + data = List(20) { |
| 83 | + Licence( |
| 84 | + id = "MockId", |
| 85 | + conditions = listOf(LicenceCondition(condition = "MockCondition")), |
| 86 | + ) |
| 87 | + }, |
| 88 | + ), |
| 89 | + ) |
| 90 | + val result = mockMvc.performAuthorised("$path?page=1&perPage=15") |
| 91 | + |
| 92 | + result.response.contentAsString.shouldContainJsonKeyValue("$.pagination.page", 1) |
| 93 | + result.response.contentAsString.shouldContainJsonKeyValue("$.pagination.totalPages", 2) |
| 94 | + } |
| 95 | + } |
| 96 | + }, |
| 97 | +) |
0 commit comments