Skip to content

Commit

Permalink
PI-2258 prison custody status to delius transaction not rolling back (#…
Browse files Browse the repository at this point in the history
…3890)

* PI-2258 replicate transaction rollback error

Signed-off-by: Amardeep Chimber <[email protected]>

* PI-2258 move transaction annotation to handler and update tests

Signed-off-by: Amardeep Chimber <[email protected]>

* PI-2258 undo change to dev queue timeout value

Signed-off-by: Amardeep Chimber <[email protected]>

* PI-2258 move transaction rollback test in a separate file

Signed-off-by: Amardeep Chimber <[email protected]>

* Formatting changes

* Empty-Commit

* PI-2258 refactor test attribute names

Signed-off-by: Amardeep Chimber <[email protected]>

---------

Signed-off-by: Amardeep Chimber <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
achimber-moj and github-actions[bot] authored Jun 11, 2024
1 parent 0323be3 commit 4d7d0b2
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import uk.gov.justice.digital.hmpps.integrations.delius.licencecondition.entity.
import uk.gov.justice.digital.hmpps.integrations.delius.licencecondition.entity.LicenceConditionRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.manager.prison.entity.PrisonManager
import uk.gov.justice.digital.hmpps.integrations.delius.person.manager.prison.entity.PrisonManagerRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.manager.probation.entity.PersonManagerRepository
import uk.gov.justice.digital.hmpps.integrations.delius.probationarea.institution.entity.InstitutionRepository
import uk.gov.justice.digital.hmpps.integrations.delius.recall.entity.RecallReasonRepository
Expand All @@ -34,6 +36,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.staff.entity.StaffReposi
import uk.gov.justice.digital.hmpps.integrations.delius.team.entity.TeamRepository
import uk.gov.justice.digital.hmpps.user.AuditUser
import uk.gov.justice.digital.hmpps.user.AuditUserRepository
import java.time.ZoneId
import java.time.ZonedDateTime

@Component
Expand All @@ -60,7 +63,8 @@ class DataLoader(
private val teamRepository: TeamRepository,
private val probationAreaRepository: ProbationAreaRepository,
private val licenceConditionRepository: LicenceConditionRepository,
private val licenceConditionCategoryRepository: LicenceConditionCategoryRepository
private val licenceConditionCategoryRepository: LicenceConditionCategoryRepository,
private val prisonManagerRepository: PrisonManagerRepository,
) : ApplicationListener<ApplicationReadyEvent> {

@PostConstruct
Expand Down Expand Up @@ -102,7 +106,8 @@ class DataLoader(
ReferenceDataGenerator.PERSON_MANAGER_ALLOCATION_REASON.set,
ReferenceDataGenerator.PRISON_MANAGER_ALLOCATION_REASON.set,
ReferenceDataSetGenerator.ACCEPTED_DECISION,
ReferenceDataSetGenerator.LICENCE_AREA_TRANSFER_REJECTION_REASON
ReferenceDataSetGenerator.LICENCE_AREA_TRANSFER_REJECTION_REASON,
ReferenceDataSetGenerator.AUTO_TRANSFER_REASON
)
)
referenceDataRepository.saveAll(
Expand All @@ -115,15 +120,53 @@ class DataLoader(
ReferenceDataGenerator.PRISON_MANAGER_ALLOCATION_REASON,
ReferenceDataGenerator.LICENCE_CONDITION_TERMINATION_REASON,
ReferenceDataGenerator.LC_REJECTED_DECISION,
ReferenceDataGenerator.LC_REJECTED_REASON
ReferenceDataGenerator.LC_REJECTED_REASON,
ReferenceDataGenerator.AUTO_TRANSFER
)
)
recallReasonRepository.saveAll(ReferenceDataGenerator.RECALL_REASON.values)
contactTypeRepository.saveAll(ReferenceDataGenerator.CONTACT_TYPE.values)
institutionRepository.saveAll(listOf(InstitutionGenerator.DEFAULT, InstitutionGenerator.MOVED_TO))
probationAreaRepository.save(InstitutionGenerator.DEFAULT.probationArea!!)
institutionRepository.saveAll(
listOf(
InstitutionGenerator.DEFAULT,
InstitutionGenerator.MOVED_TO,
InstitutionGenerator.MOVED_TO_WITH_POM
)
)
probationAreaRepository.saveAll(
listOf(
InstitutionGenerator.DEFAULT.probationArea!!,
InstitutionGenerator.MOVED_TO_WITH_POM.probationArea!!
)
)
val team = teamRepository.save(TeamGenerator.allStaff(InstitutionGenerator.DEFAULT.probationArea!!))
val teamBir =
teamRepository.save(TeamGenerator.allStaff(InstitutionGenerator.MOVED_TO_WITH_POM.probationArea!!))
val prisonManager = PrisonManager(
IdGenerator.getAndIncrement(),
0,
PersonGenerator.MATCHABLE_WITH_POM.id,
ZonedDateTime.of(2023, 1, 1, 1, 0, 0, 0, ZoneId.systemDefault()),
ReferenceDataGenerator.AUTO_TRANSFER,
StaffGenerator.UNALLOCATED,
teamBir,
InstitutionGenerator.MOVED_TO_WITH_POM.probationArea!!,
false
)
val prisonManager1 = PrisonManager(
IdGenerator.getAndIncrement(),
0,
PersonGenerator.MATCHABLE_WITH_POM.id,
ZonedDateTime.of(2023, 2, 1, 1, 0, 0, 0, ZoneId.systemDefault()),
ReferenceDataGenerator.AUTO_TRANSFER,
StaffGenerator.UNALLOCATED,
teamBir,
InstitutionGenerator.MOVED_TO_WITH_POM.probationArea!!,
false
)
prisonManagerRepository.saveAll(listOf(prisonManager, prisonManager1))
staffRepository.save(StaffGenerator.unallocated(team))
staffRepository.save(StaffGenerator.unallocated(teamBir))
institutionRepository.saveAll(InstitutionGenerator.STANDARD_INSTITUTIONS.values)
probationAreaRepository.saveAll(InstitutionGenerator.STANDARD_INSTITUTIONS.values.mapNotNull { it.probationArea })
val teams = teamRepository.saveAll(
Expand Down Expand Up @@ -163,6 +206,8 @@ class DataLoader(
private fun createMatchablePerson() {
createPerson(PersonGenerator.MATCHABLE)
createEvent(EventGenerator.custodialEvent(PersonGenerator.MATCHABLE, InstitutionGenerator.DEFAULT))
createPerson(PersonGenerator.MATCHABLE_WITH_POM)
createEvent(EventGenerator.custodialEvent(PersonGenerator.MATCHABLE_WITH_POM, InstitutionGenerator.DEFAULT))
}

private fun createNewCustodyPerson() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ object BookingGenerator {
"INT",
prisonId = InstitutionGenerator.MOVED_TO.nomisCdeCode!!
)
val MATCHED_WITH_POM = generateReceive(
PersonGenerator.MATCHABLE_WITH_POM.nomsNumber,
"INT",
prisonId = InstitutionGenerator.MOVED_TO_WITH_POM.nomisCdeCode!!
)
val NEW_CUSTODY = generateReceive(PersonGenerator.NEW_CUSTODY.nomsNumber, "N")
val RECALLED = generateReceive(PersonGenerator.RECALLED.nomsNumber, "24")
val HOSPITAL_RELEASE = generateRelease(PersonGenerator.HOSPITAL_RELEASED.nomsNumber, "HO")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object InstitutionGenerator {
val DEFAULT = generate("WSIHMP", "WSI")
val STANDARD_INSTITUTIONS = InstitutionCode.entries.associateWith { generate(it.code, null) }
val MOVED_TO = generate("SWIHMP", "SWI")
val MOVED_TO_WITH_POM = generate("BIRHMP", "BIR")

fun generate(code: String, prisonId: String?): Institution {
val institution = Institution(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object NotificationGenerator {
val PRISONER_RECEIVED = ResourceLoader.notification<HmppsDomainEvent>("prisoner-received")
val PRISONER_DIED = ResourceLoader.notification<HmppsDomainEvent>("prisoner-died")
val PRISONER_MATCHED = ResourceLoader.notification<HmppsDomainEvent>("prisoner-matched")
val PRISONER_MATCHED_WITH_POM = ResourceLoader.notification<HmppsDomainEvent>("prisoner-matched-with-pom")
val PRISONER_NEW_CUSTODY = ResourceLoader.notification<HmppsDomainEvent>("prisoner-received-new-custody")
val PRISONER_RECALLED = ResourceLoader.notification<HmppsDomainEvent>("prisoner-received-recalled")
val PRISONER_HOSPITAL_RELEASED =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object PersonGenerator {
val RECALLABLE = generate(NotificationGenerator.PRISONER_RECEIVED.nomsId())
val DIED = generate(NotificationGenerator.PRISONER_DIED.nomsId())
val MATCHABLE = generate(NotificationGenerator.PRISONER_MATCHED.nomsId())
val MATCHABLE_WITH_POM = generate(NotificationGenerator.PRISONER_MATCHED_WITH_POM.nomsId())
val NEW_CUSTODY = generate(NotificationGenerator.PRISONER_NEW_CUSTODY.nomsId())
val RECALLED = generate(NotificationGenerator.PRISONER_RECALLED.nomsId())
val HOSPITAL_RELEASED = generate(NotificationGenerator.PRISONER_HOSPITAL_RELEASED.nomsId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ object ProbationAreaGenerator {

fun generate(
code: String,
description: String = "description for $code"
) = ProbationArea(IdGenerator.getAndIncrement(), code, description)
description: String = "description for $code",

) = ProbationArea(IdGenerator.getAndIncrement(), code, description)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.data.generator.ReferenceDataSetGenerator.ACCEPTED_DECISION
import uk.gov.justice.digital.hmpps.data.generator.ReferenceDataSetGenerator.AUTO_TRANSFER_REASON
import uk.gov.justice.digital.hmpps.data.generator.ReferenceDataSetGenerator.LICENCE_AREA_TRANSFER_REJECTION_REASON
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactType
import uk.gov.justice.digital.hmpps.integrations.delius.recall.entity.RecallReason
Expand Down Expand Up @@ -42,6 +43,8 @@ object ReferenceDataGenerator {
val LC_REJECTED_DECISION = generate("R", ACCEPTED_DECISION)
val LC_REJECTED_REASON = generate("TWR", LICENCE_AREA_TRANSFER_REJECTION_REASON)

val AUTO_TRANSFER = generate("AUT", AUTO_TRANSFER_REASON)

fun generate(
code: String,
dataset: ReferenceDataSet,
Expand All @@ -57,6 +60,7 @@ object ReferenceDataSetGenerator {
val TRANSFER_STATUS = generate("TRANSFER STATUS")
val ACCEPTED_DECISION = generate("ACCEPTED DECISION")
val LICENCE_AREA_TRANSFER_REJECTION_REASON = generate("LICENCE AREA TRANSFER REJECTION REASON")
val AUTO_TRANSFER_REASON = generate("Automatic Transfer")

fun generate(name: String, id: Long = IdGenerator.getAndIncrement()) = ReferenceDataSet(id, name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"Type": "Notification",
"MessageId": "b11f8b6c-a7bc-4a3e-af9c-3e4870924eaa",
"TopicArn": "",
"Message": "{\"eventType\":\"probation-case.prison-identifier.added\",\"version\":1,\"occurredAt\":\"2023-07-31T09:26:39+01:00\",\"publishedAt\":\"2023-07-31T09:26:39.362Z+01:00\",\"description\":\"A prisoner identifier has been added\",\"personReference\":{\"identifiers\":[{\"type\":\"NOMS\",\"value\":\"B5295DZ\"}]}}",
"Timestamp": "2023-07-31T09:26:39.362Z",
"SignatureVersion": "1",
"Signature": "",
"SigningCertURL": "",
"UnsubscribeURL": "",
"MessageAttributes": {
"eventType": {
"Type": "String",
"Value": "probation-case.prison-identifier.added"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package uk.gov.justice.digital.hmpps

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.springframework.test.context.TestPropertySource
import uk.gov.justice.digital.hmpps.data.generator.BookingGenerator
import uk.gov.justice.digital.hmpps.data.generator.InstitutionGenerator
import uk.gov.justice.digital.hmpps.data.generator.NotificationGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator

@TestPropertySource(properties = ["logging.level.org.springframework.transaction=DEBUG", "logging.level.org.hibernate.engine.transaction.internal.TransactionImpl=DEBUG"])
class PcstdIntegrationTransactionRollbackTest : PcstdIntegrationTestBase() {
private val releaseOnLicence = "Released on Licence"

@Test
fun `when a prisoner is matched with more than one pom`() {

val notification = NotificationGenerator.PRISONER_MATCHED_WITH_POM
val person = PersonGenerator.MATCHABLE_WITH_POM
withBooking(
BookingGenerator.MATCHED_WITH_POM,
BookingGenerator.MATCHED_WITH_POM.lastMovement(notification.message.occurredAt)
)
val before = getCustody(person.nomsNumber)
assertThat(before.institution?.code, equalTo(InstitutionGenerator.DEFAULT.code))

channelManager.getChannel(queueName).publishAndWait(notification)

val custody = getCustody(person.nomsNumber)
assertTrue(custody.isInCustody())
assertThat(custody.institution?.code, equalTo(InstitutionGenerator.DEFAULT.code))

verifyCustodyHistory(
custody
)
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package uk.gov.justice.digital.hmpps.messaging

import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import uk.gov.justice.digital.hmpps.exception.IgnorableMessageException
import uk.gov.justice.digital.hmpps.integrations.delius.event.EventService

@Service
class ActionProcessor(actionsList: List<PrisonerMovementAction>, private val eventService: EventService) {
private val actions = actionsList.associateBy { it.name }

@Transactional(noRollbackFor = [IgnorableMessageException::class])
fun processActions(prisonerMovement: PrisonerMovement, actionNames: List<String>): List<ActionResult> =
try {
eventService.getActiveCustodialEvents(prisonerMovement.nomsId)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.justice.digital.hmpps.messaging

import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import uk.gov.justice.digital.hmpps.converter.NotificationConverter
import uk.gov.justice.digital.hmpps.datetime.EuropeLondon
import uk.gov.justice.digital.hmpps.exception.IgnorableMessageException
Expand All @@ -16,6 +17,7 @@ import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
import java.time.ZonedDateTime

@Component
@Transactional(noRollbackFor = [IgnorableMessageException::class])
class Handler(
configContainer: PrisonerMovementConfigs,
private val featureFlags: FeatureFlags,
Expand Down

0 comments on commit 4d7d0b2

Please sign in to comment.