Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj authored Jan 30, 2024
1 parent 8da2fdf commit af11f44
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.person.manager.prison.entity.PrisonManager
import uk.gov.justice.digital.hmpps.integrations.delius.probationarea.entity.ProbationArea
import java.time.ZonedDateTime

object PrisonManagerGenerator {
fun generate(
person: Person,
startDate: ZonedDateTime = ZonedDateTime.now().minusDays(1),
endDate: ZonedDateTime? = null
endDate: ZonedDateTime? = null,
probationArea: ProbationArea = ProbationAreaGenerator.DEFAULT
) = PrisonManager(
id = IdGenerator.getAndIncrement(),
personId = person.id,
Expand All @@ -17,6 +19,6 @@ object PrisonManagerGenerator {
allocationReason = ReferenceDataGenerator.PERSON_MANAGER_ALLOCATION_REASON,
staff = StaffGenerator.UNALLOCATED,
team = TeamGenerator.DEFAULT,
probationArea = ProbationAreaGenerator.DEFAULT
probationArea = probationArea
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ package uk.gov.justice.digital.hmpps

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import org.mockito.kotlin.whenever
import uk.gov.justice.digital.hmpps.data.generator.BookingGenerator
Expand All @@ -29,7 +26,7 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
@Test
fun `release a prisoner`() {
val notification = NotificationGenerator.PRISONER_RELEASED
withBooking(BookingGenerator.RELEASED)
withBooking(BookingGenerator.RELEASED, BookingGenerator.RELEASED.lastMovement(notification.message.occurredAt))
val nomsNumber = notification.nomsId()
assertTrue(getCustody(nomsNumber).isInCustody())

Expand All @@ -55,6 +52,8 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
mapOf(
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to "A0001AA",
"previousInstitution" to "WSI",
"institution" to "OUT",
"reason" to "RELEASED",
"movementReason" to "NCS",
"movementType" to "Released"
Expand All @@ -65,7 +64,7 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
@Test
fun `recall a prisoner`() {
val notification = NotificationGenerator.PRISONER_RECEIVED
withBooking(BookingGenerator.RECEIVED)
withBooking(BookingGenerator.RECEIVED, BookingGenerator.RECEIVED.lastMovement(notification.message.occurredAt))
val nomsNumber = notification.nomsId()
assertFalse(getCustody(nomsNumber).isInCustody())

Expand Down Expand Up @@ -109,6 +108,7 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
mapOf(
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to nomsNumber,
"previousInstitution" to "WSI",
"institution" to "WSI",
"reason" to "ADMISSION",
"movementReason" to "R1",
Expand All @@ -119,12 +119,12 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {

@Test
fun `when a prisoner is matched`() {
val notification = NotificationGenerator.PRISONER_MATCHED
val person = PersonGenerator.MATCHABLE
withBooking(BookingGenerator.MATCHED)
withBooking(BookingGenerator.MATCHED, BookingGenerator.MATCHED.lastMovement(notification.message.occurredAt))
val before = getCustody(person.nomsNumber)
assertThat(before.institution?.code, equalTo(InstitutionGenerator.DEFAULT.code))

val notification = NotificationGenerator.PRISONER_MATCHED
channelManager.getChannel(queueName).publishAndWait(notification)

val custody = getCustody(person.nomsNumber)
Expand All @@ -140,8 +140,9 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {

verifyTelemetry("RecallNotRequired", "PrisonerStatusCorrect", "LocationUpdated") {
mapOf(
"occurredAt" to ZonedDateTime.parse("2023-07-31T09:26:39+01:00[Europe/London]").toString(),
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to PersonGenerator.MATCHABLE.nomsNumber,
"previousInstitution" to InstitutionGenerator.DEFAULT.nomisCdeCode!!,
"institution" to InstitutionGenerator.MOVED_TO.nomisCdeCode!!,
"reason" to "TRANSFERRED",
"movementReason" to "INT",
Expand All @@ -153,9 +154,9 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
@Test
fun `a person died in custody alerts manager`() {
val person = PersonGenerator.DIED
withBooking(BookingGenerator.DIED)

val notification = NotificationGenerator.PRISONER_DIED
withBooking(BookingGenerator.DIED, BookingGenerator.DIED.lastMovement(notification.message.occurredAt))

channelManager.getChannel(queueName).publishAndWait(notification)

val dus = contactRepository.findAll().firstOrNull { it.person.id == person.id }
Expand All @@ -172,6 +173,8 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
mapOf(
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to person.nomsNumber,
"previousInstitution" to "WSI",
"institution" to "OUT",
"reason" to "RELEASED",
"movementReason" to "DEC",
"movementType" to "Released"
Expand All @@ -182,7 +185,10 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
@Test
fun `receieve a new custodial sentence`() {
val notification = NotificationGenerator.PRISONER_NEW_CUSTODY
withBooking(BookingGenerator.NEW_CUSTODY)
withBooking(
BookingGenerator.NEW_CUSTODY,
BookingGenerator.NEW_CUSTODY.lastMovement(notification.message.occurredAt)
)
val nomsNumber = notification.nomsId()
val before = getCustody(nomsNumber)
assertThat(before.status.code, equalTo(CustodialStatusCode.SENTENCED_IN_CUSTODY.code))
Expand Down Expand Up @@ -212,6 +218,7 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
mapOf(
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to "A0004AA",
"previousInstitution" to "WSI",
"institution" to "WSI",
"reason" to "ADMISSION",
"movementReason" to "N",
Expand All @@ -223,7 +230,7 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
@Test
fun `receieve a prisoner already recalled in delius`() {
val notification = NotificationGenerator.PRISONER_RECALLED
withBooking(BookingGenerator.RECALLED)
withBooking(BookingGenerator.RECALLED, BookingGenerator.RECALLED.lastMovement(notification.message.occurredAt))
val nomsNumber = notification.nomsId()

channelManager.getChannel(queueName).publishAndWait(notification)
Expand All @@ -250,6 +257,7 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
mapOf(
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to "A0006AA",
"previousInstitution" to "WSI",
"institution" to "WSI",
"reason" to "ADMISSION",
"movementReason" to "24",
Expand All @@ -262,7 +270,10 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
fun `hospital release when released on licence in delius`() {
whenever(featureFlags.enabled("messages_released_hospital")).thenReturn(true)
val notification = NotificationGenerator.PRISONER_HOSPITAL_RELEASED
withBooking(BookingGenerator.HOSPITAL_RELEASE)
withBooking(
BookingGenerator.HOSPITAL_RELEASE,
BookingGenerator.HOSPITAL_RELEASE.lastMovement(notification.message.occurredAt)
)
val nomsNumber = notification.nomsId()
assertFalse(getCustody(nomsNumber).isInCustody())

Expand Down Expand Up @@ -292,6 +303,8 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
mapOf(
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to "A0005AA",
"previousInstitution" to "WSI",
"institution" to "OUT",
"reason" to "RELEASED_TO_HOSPITAL",
"movementReason" to "HO",
"movementType" to "Released"
Expand All @@ -303,7 +316,10 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
fun `hospital release when in custody in delius`() {
whenever(featureFlags.enabled("messages_released_hospital")).thenReturn(true)
val notification = NotificationGenerator.PRISONER_HOSPITAL_IN_CUSTODY
withBooking(BookingGenerator.HOSPITAL_CUSTODY)
withBooking(
BookingGenerator.HOSPITAL_CUSTODY,
BookingGenerator.HOSPITAL_CUSTODY.lastMovement(notification.message.occurredAt)
)
val nomsNumber = notification.nomsId()
assertTrue(getCustody(nomsNumber).isInCustody())

Expand Down Expand Up @@ -332,6 +348,8 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
mapOf(
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to "A0007AA",
"previousInstitution" to "WSI",
"institution" to "OUT",
"reason" to "RELEASED_TO_HOSPITAL",
"movementReason" to "HQ",
"movementType" to "Released"
Expand All @@ -342,7 +360,10 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
@Test
fun `received into prison when on rotl`() {
val notification = NotificationGenerator.PRISONER_ROTL_RETURN
withBooking(BookingGenerator.ROTL_RETURN)
withBooking(
BookingGenerator.ROTL_RETURN,
BookingGenerator.ROTL_RETURN.lastMovement(notification.message.occurredAt)
)
val nomsNumber = notification.nomsId()
assertFalse(getCustody(nomsNumber).isInCustody())

Expand All @@ -367,6 +388,7 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
mapOf(
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to "A0008AA",
"previousInstitution" to "WSI",
"institution" to "WSI",
"reason" to "TEMPORARY_ABSENCE_RETURN",
"movementReason" to "24",
Expand All @@ -378,7 +400,10 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
@Test
fun `IRC release when released on licence in delius`() {
val notification = NotificationGenerator.PRISONER_IRC_RELEASED
withBooking(BookingGenerator.IRC_RELEASED)
withBooking(
BookingGenerator.IRC_RELEASED,
BookingGenerator.IRC_RELEASED.lastMovement(notification.message.occurredAt)
)
val nomsNumber = notification.nomsId()
assertFalse(getCustody(nomsNumber).isInCustody())

Expand Down Expand Up @@ -406,6 +431,8 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
mapOf(
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to "A0009AA",
"previousInstitution" to "WSI",
"institution" to "OUT",
"reason" to "RELEASED",
"movementReason" to "DE",
"movementType" to "Released"
Expand All @@ -416,7 +443,10 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
@Test
fun `irc release when in custody in delius`() {
val notification = NotificationGenerator.PRISONER_IRC_IN_CUSTODY
withBooking(BookingGenerator.IRC_CUSTODY)
withBooking(
BookingGenerator.IRC_CUSTODY,
BookingGenerator.IRC_CUSTODY.lastMovement(notification.message.occurredAt)
)
val nomsNumber = notification.nomsId()
assertTrue(getCustody(nomsNumber).isInCustody())

Expand All @@ -441,6 +471,8 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
mapOf(
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to "A0011AA",
"previousInstitution" to "WSI",
"institution" to "OUT",
"reason" to "RELEASED",
"movementReason" to "DD",
"movementType" to "Released"
Expand All @@ -451,7 +483,10 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
@Test
fun `release a ecsl prisoner with feature active`() {
val notification = NotificationGenerator.PRISONER_RELEASED_ECSL_ACTIVE
withBooking(BookingGenerator.ECSL_ACTIVE)
withBooking(
BookingGenerator.ECSL_ACTIVE,
BookingGenerator.ECSL_ACTIVE.lastMovement(notification.message.occurredAt)
)
val nomsNumber = notification.nomsId()
assertTrue(getCustody(nomsNumber).isInCustody())

Expand All @@ -477,6 +512,8 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
mapOf(
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to "A0013AA",
"previousInstitution" to "WSI",
"institution" to "OUT",
"reason" to "RELEASED",
"movementReason" to "ECSL",
"movementType" to "Released"
Expand All @@ -488,7 +525,10 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
fun `prisoner absconded - unlawfully at large`() {
whenever(featureFlags.enabled("messages_released_absconded")).thenReturn(true)
val notification = NotificationGenerator.PRISONER_ABSCONDED
withBooking(BookingGenerator.ABSCONDED)
withBooking(
BookingGenerator.ABSCONDED,
BookingGenerator.ABSCONDED.lastMovement(notification.message.occurredAt)
)
val nomsNumber = notification.nomsId()
assertFalse(getCustody(nomsNumber).isInCustody())

Expand Down Expand Up @@ -528,6 +568,8 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to nomsNumber,
"reason" to "RELEASED",
"previousInstitution" to "WSI",
"institution" to "OUT",
"movementReason" to "UAL",
"movementType" to "Released"
)
Expand All @@ -537,7 +579,10 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
@Test
fun `etr release when in custody in delius`() {
val notification = NotificationGenerator.PRISONER_ETR_IN_CUSTODY
withBooking(BookingGenerator.ETR_CUSTODY)
withBooking(
BookingGenerator.ETR_CUSTODY,
BookingGenerator.ETR_CUSTODY.lastMovement(notification.message.occurredAt)
)
val nomsNumber = notification.nomsId()

channelManager.getChannel(queueName).publishAndWait(notification)
Expand All @@ -560,6 +605,8 @@ class PcstdIntegrationTest : PcstdIntegrationTestBase() {
mapOf(
"occurredAt" to notification.message.occurredAt.toString(),
"nomsNumber" to "A0019AA",
"previousInstitution" to "WSI",
"institution" to "OUT",
"reason" to "RELEASED",
"movementReason" to "ETR",
"movementType" to "Released"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock
import com.github.tomakehurst.wiremock.client.WireMock.aResponse
import com.github.tomakehurst.wiremock.client.WireMock.get
import com.github.tomakehurst.wiremock.client.WireMock.equalToJson
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.*
import org.mockito.kotlin.verify
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.mock.mockito.MockBean
import uk.gov.justice.digital.hmpps.data.generator.InstitutionGenerator
import uk.gov.justice.digital.hmpps.datetime.EuropeLondon
import uk.gov.justice.digital.hmpps.flags.FeatureFlags
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactAlertRepository
Expand All @@ -31,8 +32,11 @@ import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.wellknown.
import uk.gov.justice.digital.hmpps.integrations.delius.referencedata.wellknown.ReleaseTypeCode
import uk.gov.justice.digital.hmpps.integrations.delius.release.entity.ReleaseRepository
import uk.gov.justice.digital.hmpps.integrations.prison.Booking
import uk.gov.justice.digital.hmpps.integrations.prison.Movement
import uk.gov.justice.digital.hmpps.messaging.HmppsChannelManager
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZonedDateTime
import java.time.temporal.ChronoUnit

Expand Down Expand Up @@ -87,17 +91,35 @@ open class PcstdIntegrationTestBase {
@MockBean
internal lateinit var featureFlags: FeatureFlags

internal fun withBooking(booking: Booking) {
internal fun withBooking(booking: Booking, lastMovement: Movement = booking.lastMovement()) {
wireMockServer.stubFor(
get(WireMock.urlPathEqualTo("/api/bookings/offenderNo/${booking.personReference}"))
WireMock.get(WireMock.urlPathEqualTo("/api/bookings/offenderNo/${booking.personReference}"))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(objectMapper.writeValueAsString(booking))
)
)
wireMockServer.stubFor(
WireMock.post(WireMock.urlPathEqualTo("/api/movements/offenders"))
.withRequestBody(equalToJson(objectMapper.writeValueAsString(listOf(booking.personReference))))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(objectMapper.writeValueAsString(listOf(lastMovement)))
)
)
}

internal fun Booking.lastMovement(dateTime: ZonedDateTime = ZonedDateTime.now()) = Movement(
InstitutionGenerator.DEFAULT.nomisCdeCode!!,
agencyId,
movementType,
movementReason,
dateTime.toLocalDate(),
dateTime.toLocalTime()
)

internal fun getPersonId(nomsNumber: String) =
personRepository.findByNomsNumberAndSoftDeletedIsFalse(nomsNumber).single().id

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class RestClientConfig(private val oauth2Client: RestClient) {
fun prisonApiClient(@Value("\${integrations.prison-api.url}") prisonApiBaseUrl: String) =
createClient<PrisonApiClient>(
oauth2Client.mutate()
.baseUrl("$prisonApiBaseUrl/api/bookings")
.baseUrl("$prisonApiBaseUrl/api")
.build()
)
}
Loading

0 comments on commit af11f44

Please sign in to comment.