Skip to content

Commit

Permalink
CDPS-1054: Updated resource and service tests for core person record.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtac50 committed Nov 19, 2024
1 parent 33d77c3 commit c8b2da2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ class CorePersonRecordV1Resource(
fun patchByPrisonerNumber(
@RequestParam(required = true) @Valid @ValidPrisonerNumber prisonerNumber: String,
@RequestBody(required = true) @Valid corePersonRecordUpdateRequest: CorePersonRecordV1UpdateRequestDto,
) {
): ResponseEntity<Void> {
corePersonRecordService.updateCorePersonRecordField(
prisonerNumber,
corePersonRecordUpdateRequest.fieldName,
corePersonRecordUpdateRequest.fieldValue,
)

return ResponseEntity.noContent().build()
}

@PutMapping(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,7 +14,7 @@ class CorePersonRecordV1ResourceIntTest : IntegrationTestBase() {

@DisplayName("PATCH v1/core-person-record")
@Nested
inner class PatchReligionByPrisonerNumberTest {
inner class PatchCorePersonRecordByPrisonerNumberTest {

@Nested
inner class Security {
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<UnknownCorePersonFieldException> {
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")
}
}

0 comments on commit c8b2da2

Please sign in to comment.