|
| 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 jakarta.validation.ValidationException |
| 6 | +import org.mockito.kotlin.any |
| 7 | +import org.mockito.kotlin.doNothing |
| 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.http.HttpStatus |
| 12 | +import org.springframework.test.context.ActiveProfiles |
| 13 | +import org.springframework.test.context.bean.override.mockito.MockitoBean |
| 14 | +import org.springframework.test.web.servlet.MockMvc |
| 15 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.exception.EntityNotFoundException |
| 16 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.helpers.IntegrationAPIMockMvc |
| 17 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.PutExpressionInterestService |
| 18 | + |
| 19 | +@WebMvcTest(controllers = [ExpressionInterestController::class]) |
| 20 | +@ActiveProfiles("test") |
| 21 | +class ExpressionInterestControllerTest( |
| 22 | + @Autowired var springMockMvc: MockMvc, |
| 23 | + @MockitoBean val expressionOfInterestService: PutExpressionInterestService, |
| 24 | +) : DescribeSpec({ |
| 25 | + val mockMvc = IntegrationAPIMockMvc(springMockMvc) |
| 26 | + val basePath = "/v1/persons" |
| 27 | + val validHmppsId = "AABCD1ABC" |
| 28 | + val invalidHmppsId = "INVALID_ID" |
| 29 | + val jobId = "5678" |
| 30 | + |
| 31 | + describe("PUT $basePath/{hmppsId}/expression-of-interest/jobs/{jobId}") { |
| 32 | + it("should return 404 Not Found if ENTITY_NOT_FOUND error occurs") { |
| 33 | + validHmppsId.let { id -> |
| 34 | + whenever(expressionOfInterestService.sendExpressionOfInterest(id, jobId)).thenThrow(EntityNotFoundException("Could not find person with id: $id")) |
| 35 | + } |
| 36 | + |
| 37 | + val result = mockMvc.performAuthorisedPut("$basePath/$validHmppsId/expression-of-interest/jobs/$jobId") |
| 38 | + result.response.status.shouldBe(HttpStatus.NOT_FOUND.value()) |
| 39 | + } |
| 40 | + |
| 41 | + it("should throw ValidationException if an invalid hmppsId is provided") { |
| 42 | + invalidHmppsId.let { id -> |
| 43 | + whenever(expressionOfInterestService.sendExpressionOfInterest(id, jobId)).thenThrow(ValidationException("Invalid HMPPS ID: $id")) |
| 44 | + } |
| 45 | + |
| 46 | + val result = mockMvc.performAuthorisedPut("$basePath/$invalidHmppsId/expression-of-interest/jobs/$jobId") |
| 47 | + result.response.status.shouldBe(HttpStatus.BAD_REQUEST.value()) |
| 48 | + } |
| 49 | + |
| 50 | + it("should return 200 OK on successful expression of interest submission") { |
| 51 | + validHmppsId.let { id -> |
| 52 | + doNothing().whenever(expressionOfInterestService).sendExpressionOfInterest(id, jobId) |
| 53 | + } |
| 54 | + |
| 55 | + val result = mockMvc.performAuthorisedPut("$basePath/$validHmppsId/expression-of-interest/jobs/$jobId") |
| 56 | + result.response.status.shouldBe(HttpStatus.OK.value()) |
| 57 | + } |
| 58 | + |
| 59 | + it("should return 500 Server Error if an exception occurs") { |
| 60 | + whenever(expressionOfInterestService.sendExpressionOfInterest(any(), any())).thenThrow(RuntimeException("Unexpected error")) |
| 61 | + |
| 62 | + val result = mockMvc.performAuthorisedPut("$basePath/$validHmppsId/expression-of-interest/jobs/$jobId") |
| 63 | + result.response.status.shouldBe(HttpStatus.INTERNAL_SERVER_ERROR.value()) |
| 64 | + } |
| 65 | + } |
| 66 | + }) |
0 commit comments