From c8b2da253a655345f7c4b4a7977c5b32916ea78c Mon Sep 17 00:00:00 2001 From: Michael Clancy Date: Tue, 19 Nov 2024 09:54:20 +0000 Subject: [PATCH] CDPS-1054: Updated resource and service tests for core person record. --- .../resource/CorePersonRecordV1Resource.kt | 4 +- .../CorePersonRecordV1ResourceIntTest.kt | 20 ++----- .../service/CorePersonRecordServiceTest.kt | 53 ++++++++++++++++++- 3 files changed, 58 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/personintegrationapi/corepersonrecord/resource/CorePersonRecordV1Resource.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/personintegrationapi/corepersonrecord/resource/CorePersonRecordV1Resource.kt index 8e16253..1c0c701 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/personintegrationapi/corepersonrecord/resource/CorePersonRecordV1Resource.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/personintegrationapi/corepersonrecord/resource/CorePersonRecordV1Resource.kt @@ -88,12 +88,14 @@ class CorePersonRecordV1Resource( fun patchByPrisonerNumber( @RequestParam(required = true) @Valid @ValidPrisonerNumber prisonerNumber: String, @RequestBody(required = true) @Valid corePersonRecordUpdateRequest: CorePersonRecordV1UpdateRequestDto, - ) { + ): ResponseEntity { corePersonRecordService.updateCorePersonRecordField( prisonerNumber, corePersonRecordUpdateRequest.fieldName, corePersonRecordUpdateRequest.fieldValue, ) + + return ResponseEntity.noContent().build() } @PutMapping( diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/personintegrationapi/corepersonrecord/resource/CorePersonRecordV1ResourceIntTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/personintegrationapi/corepersonrecord/resource/CorePersonRecordV1ResourceIntTest.kt index 0500604..ba27649 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/personintegrationapi/corepersonrecord/resource/CorePersonRecordV1ResourceIntTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/personintegrationapi/corepersonrecord/resource/CorePersonRecordV1ResourceIntTest.kt @@ -3,10 +3,8 @@ package uk.gov.justice.digital.hmpps.personintegrationapi.corepersonrecord.resou import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test -import org.springframework.http.HttpStatus import org.springframework.http.MediaType import uk.gov.justice.digital.hmpps.personintegrationapi.corepersonrecord.CorePersonRecordRoleConstants -import uk.gov.justice.digital.hmpps.personintegrationapi.corepersonrecord.enumeration.CorePersonRecordField import uk.gov.justice.digital.hmpps.personintegrationapi.integration.IntegrationTestBase import uk.gov.justice.digital.hmpps.personintegrationapi.integration.wiremock.PRISONER_NUMBER import uk.gov.justice.digital.hmpps.personintegrationapi.integration.wiremock.PRISONER_NUMBER_NOT_FOUND @@ -16,7 +14,7 @@ class CorePersonRecordV1ResourceIntTest : IntegrationTestBase() { @DisplayName("PATCH v1/core-person-record") @Nested - inner class PatchReligionByPrisonerNumberTest { + inner class PatchCorePersonRecordByPrisonerNumberTest { @Nested inner class Security { @@ -51,18 +49,7 @@ class CorePersonRecordV1ResourceIntTest : IntegrationTestBase() { .headers(setAuthorisation(roles = listOf(CorePersonRecordRoleConstants.CORE_PERSON_RECORD_WRITE_ROLE))) .bodyValue(VALID_PATCH_REQUEST_BODY) .exchange() - .expectStatus().isOk - .expectBody().json( - // language=json - """ - { - "status": "${HttpStatus.OK.reasonPhrase}", - "message": "${CorePersonRecordField.BIRTHPLACE.name} for prisoner $PRISONER_NUMBER successfully updated.", - "fieldName": "${CorePersonRecordField.BIRTHPLACE.name}", - "fieldValue": "London" - } - """.trimIndent(), - ) + .expectStatus().isNoContent } } @@ -71,7 +58,8 @@ class CorePersonRecordV1ResourceIntTest : IntegrationTestBase() { @Test fun `handles a 404 not found response from downstream api`() { - webTestClient.patch().uri("/v1/core-person-record?prisonerNumber=$PRISONER_NUMBER_NOT_FOUND") + webTestClient.patch() + .uri("/v1/core-person-record?prisonerNumber=$PRISONER_NUMBER_NOT_FOUND") .contentType(MediaType.APPLICATION_JSON) .headers(setAuthorisation(roles = listOf(CorePersonRecordRoleConstants.CORE_PERSON_RECORD_WRITE_ROLE))) .bodyValue(VALID_PATCH_REQUEST_BODY) diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/personintegrationapi/corepersonrecord/service/CorePersonRecordServiceTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/personintegrationapi/corepersonrecord/service/CorePersonRecordServiceTest.kt index 43713bb..8196ad3 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/personintegrationapi/corepersonrecord/service/CorePersonRecordServiceTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/personintegrationapi/corepersonrecord/service/CorePersonRecordServiceTest.kt @@ -1,10 +1,59 @@ package uk.gov.justice.digital.hmpps.personintegrationapi.corepersonrecord.service +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.api.extension.ExtendWith +import org.mockito.InjectMocks +import org.mockito.Mock +import org.mockito.junit.jupiter.MockitoExtension +import org.mockito.kotlin.reset +import org.mockito.kotlin.whenever +import org.springframework.http.ResponseEntity +import uk.gov.justice.digital.hmpps.personintegrationapi.common.client.PrisonApiClient +import uk.gov.justice.digital.hmpps.personintegrationapi.common.client.dto.UpdateBirthPlace +import uk.gov.justice.digital.hmpps.personintegrationapi.corepersonrecord.enumeration.CorePersonRecordField +import uk.gov.justice.digital.hmpps.personintegrationapi.corepersonrecord.exception.UnknownCorePersonFieldException +@ExtendWith(MockitoExtension::class) class CorePersonRecordServiceTest { - @Test - fun updateCorePersonRecordField() { + @Mock + lateinit var prisonApiClient: PrisonApiClient + + @InjectMocks + lateinit var underTest: CorePersonRecordService + + @AfterEach + fun afterEach() { + reset(prisonApiClient) + } + + @BeforeEach + fun beforeEach() { + whenever(prisonApiClient.updateBirthPlaceForWorkingName(PRISONER_NUMBER, TEST_BIRTHPLACE_BODY)).thenReturn(ResponseEntity.noContent().build()) + } + + @Nested + inner class UpdateCorePersonRecordField { + @Test + fun `can update the birthplace field`() { + underTest.updateCorePersonRecordField(PRISONER_NUMBER, CorePersonRecordField.BIRTHPLACE, TEST_BIRTHPLACE_VALUE) + } + + @Test + fun `throws an exception if the field type is not supported`() { + assertThrows { + underTest.updateCorePersonRecordField(PRISONER_NUMBER, CorePersonRecordField.COUNTRY_OF_BIRTH, "") + } + } + } + + private companion object { + const val PRISONER_NUMBER = "A1234AA" + const val TEST_BIRTHPLACE_VALUE = "London" + val TEST_BIRTHPLACE_BODY = UpdateBirthPlace("London") } }