|
| 1 | +package uk.gov.justice.digital.hmpps.hmppsintegrationapi.controllers.v1 |
| 2 | + |
| 3 | +import io.kotest.core.spec.style.DescribeSpec |
| 4 | +import io.kotest.matchers.shouldBe |
| 5 | +import org.mockito.Mockito |
| 6 | +import org.mockito.internal.verification.VerificationModeFactory |
| 7 | +import org.mockito.kotlin.doThrow |
| 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 org.springframework.web.reactive.function.client.WebClientResponseException |
| 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.prisonIntegrationpes.PESPrisonerDetails |
| 20 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.GetPESPrisonerDetailsService |
| 21 | +import uk.gov.justice.digital.hmpps.hmppsintegrationapi.services.internal.AuditService |
| 22 | + |
| 23 | +@WebMvcTest(controllers = [PESPrisonerDetailController::class]) |
| 24 | +@ActiveProfiles("test") |
| 25 | +internal class PESPrisonerDetailControllerTest( |
| 26 | + @Autowired var springMockMvc: MockMvc, |
| 27 | + @MockBean val getPESPrisonerDetailsService: GetPESPrisonerDetailsService, |
| 28 | + @MockBean val auditService: AuditService, |
| 29 | +) : DescribeSpec({ |
| 30 | + val hmppsId = "X12345" |
| 31 | + val path = "/v1/pes/prisoner-details/$hmppsId" |
| 32 | + val mockMvc = IntegrationAPIMockMvc(springMockMvc) |
| 33 | + |
| 34 | + describe("GET $path") { |
| 35 | + beforeTest { |
| 36 | + Mockito.reset(getPESPrisonerDetailsService) |
| 37 | + whenever(getPESPrisonerDetailsService.execute(hmppsId)).thenReturn( |
| 38 | + Response( |
| 39 | + data = PESPrisonerDetails(prisonerNumber = "ABC123", "Molly", lastName = "Mob", prisonId = "LEI", prisonName = "HMP Leeds", cellLocation = "6-2-006"), |
| 40 | + ), |
| 41 | + ) |
| 42 | + Mockito.reset(auditService) |
| 43 | + } |
| 44 | + |
| 45 | + it("returns a 200 OK status code") { |
| 46 | + val result = mockMvc.performAuthorised(path) |
| 47 | + |
| 48 | + result.response.status.shouldBe(HttpStatus.OK.value()) |
| 49 | + } |
| 50 | + |
| 51 | + it("gets the prisoner detail for a prisoner with the matching ID") { |
| 52 | + mockMvc.performAuthorised(path) |
| 53 | + |
| 54 | + verify(getPESPrisonerDetailsService, VerificationModeFactory.times(1)).execute(hmppsId) |
| 55 | + } |
| 56 | + |
| 57 | + it("logs audit") { |
| 58 | + mockMvc.performAuthorised(path) |
| 59 | + verify( |
| 60 | + auditService, |
| 61 | + VerificationModeFactory.times(1), |
| 62 | + ).createEvent( |
| 63 | + "GET_PES_PRISONER_INFORMATION", |
| 64 | + mapOf("hmppsId" to hmppsId), |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + it("returns a 500 INTERNAL SERVER ERROR status code when upstream api return expected error") { |
| 69 | + |
| 70 | + whenever(getPESPrisonerDetailsService.execute(hmppsId)).doThrow( |
| 71 | + WebClientResponseException(500, "MockError", null, null, null, null), |
| 72 | + ) |
| 73 | + |
| 74 | + val result = mockMvc.performAuthorised(path) |
| 75 | + assert(result.response.status == 500) |
| 76 | + assert( |
| 77 | + result.response.contentAsString.equals( |
| 78 | + "{\"status\":500,\"errorCode\":null,\"userMessage\":\"500 MockError\",\"developerMessage\":\"Unable to complete request as an upstream service is not responding\",\"moreInfo\":null}", |
| 79 | + ), |
| 80 | + ) |
| 81 | + } |
| 82 | + } |
| 83 | + }) |
0 commit comments