From 23814db4bdd7351083c748fc3e6edbabc468e11b Mon Sep 17 00:00:00 2001 From: Igor Date: Tue, 4 Apr 2023 12:38:52 +0100 Subject: [PATCH] Move out incentive level additional information from polymorphic domain event field --- .../service/IncentiveLevelAuditedService.kt | 4 ++- .../PrisonIncentiveLevelAuditedService.kt | 4 ++- .../hmpps/incentivesapi/service/SnsService.kt | 2 -- .../IncentiveLevelResourceTestBase.kt | 10 +++++-- .../resource/IncentiveLevelResourceTest.kt | 26 ++++++++-------- .../PrisonIncentiveLevelResourceTest.kt | 30 +++++++++---------- 6 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/service/IncentiveLevelAuditedService.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/service/IncentiveLevelAuditedService.kt index 0f23cd4bb..bd366145e 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/service/IncentiveLevelAuditedService.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/service/IncentiveLevelAuditedService.kt @@ -62,7 +62,7 @@ class IncentiveLevelAuditedService( IncentivesDomainEventType.INCENTIVE_LEVEL_CHANGED, "An incentive level has been changed: $levelCode", occurredAt = LocalDateTime.now(clock), - AdditionalInformation( + IncentiveLevelRef( incentiveLevel = levelCode, ), ) @@ -77,3 +77,5 @@ class IncentiveLevelAuditedService( ) } } + +data class IncentiveLevelRef(val incentiveLevel: String) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/service/PrisonIncentiveLevelAuditedService.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/service/PrisonIncentiveLevelAuditedService.kt index b402e968e..70b5d7679 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/service/PrisonIncentiveLevelAuditedService.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/service/PrisonIncentiveLevelAuditedService.kt @@ -47,10 +47,12 @@ class PrisonIncentiveLevelAuditedService( IncentivesDomainEventType.INCENTIVE_PRISON_LEVEL_CHANGED, "Incentive level (${prisonIncentiveLevel.levelCode}) in prison ${prisonIncentiveLevel.prisonId} has been updated", occurredAt = LocalDateTime.now(clock), - AdditionalInformation( + PrisonIncentiveLevelRef( incentiveLevel = prisonIncentiveLevel.levelCode, prisonId = prisonIncentiveLevel.prisonId, ), ) } } + +data class PrisonIncentiveLevelRef(val incentiveLevel: String, val prisonId: String) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/service/SnsService.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/service/SnsService.kt index 13340edef..0a16b3447 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/service/SnsService.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/service/SnsService.kt @@ -64,8 +64,6 @@ data class AdditionalInformation( val bookingId: Long? = null, val alertsAdded: List? = null, val alertsRemoved: List? = null, - val incentiveLevel: String? = null, - val prisonId: String? = null, ) data class HMPPSDomainEvent( diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/integration/IncentiveLevelResourceTestBase.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/integration/IncentiveLevelResourceTestBase.kt index 8fdc366fb..642eaf1d2 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/integration/IncentiveLevelResourceTestBase.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/integration/IncentiveLevelResourceTestBase.kt @@ -17,6 +17,8 @@ import uk.gov.justice.digital.hmpps.incentivesapi.jpa.repository.PrisonIncentive import uk.gov.justice.digital.hmpps.incentivesapi.service.AuditEvent import uk.gov.justice.digital.hmpps.incentivesapi.service.HMPPSDomainEvent import uk.gov.justice.digital.hmpps.incentivesapi.service.HMPPSMessage +import uk.gov.justice.digital.hmpps.incentivesapi.service.IncentiveLevelRef +import uk.gov.justice.digital.hmpps.incentivesapi.service.PrisonIncentiveLevelRef import java.time.Clock import java.time.Instant import java.time.LocalDateTime @@ -76,7 +78,11 @@ class IncentiveLevelResourceTestBase : SqsIntegrationTestBase() { assertThat(queueSize).isEqualTo(0) } - protected fun assertDomainEventSent(eventType: String): HMPPSDomainEvent?> { + protected val anyDomainEvent = object : TypeReference>() {} + protected val incentiveLevelDomainEvent = object : TypeReference>() {} + protected val prisonIncentiveLevelDomainEvent = object : TypeReference>() {} + + protected fun assertDomainEventSent(eventType: String, typeReference: TypeReference>): HMPPSDomainEvent { val sqsClient = incentivesQueue.sqsClient val queueSize = sqsClient.getApproxQueueSize(testDomainEventQueueUrl!!) assertThat(queueSize).isEqualTo(1) @@ -84,7 +90,7 @@ class IncentiveLevelResourceTestBase : SqsIntegrationTestBase() { val body = sqsClient.receiveMessage(testDomainEventQueueUrl).messages[0].body val (message, attributes) = objectMapper.readValue(body, HMPPSMessage::class.java) assertThat(attributes.eventType.Value).isEqualTo(eventType) - val domainEvent = objectMapper.readValue(message, object : TypeReference?>>() {}) + val domainEvent = objectMapper.readValue(message, typeReference) assertThat(domainEvent.eventType).isEqualTo(eventType) return domainEvent diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/resource/IncentiveLevelResourceTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/resource/IncentiveLevelResourceTest.kt index e56844240..d95bfb11c 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/resource/IncentiveLevelResourceTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/resource/IncentiveLevelResourceTest.kt @@ -186,9 +186,9 @@ class IncentiveLevelResourceTest : IncentiveLevelResourceTestBase() { assertThat(prisonIncentiveLevelRepository.count()).isEqualTo(0) } - assertDomainEventSent("incentives.level.changed").let { + assertDomainEventSent("incentives.level.changed", incentiveLevelDomainEvent).let { assertThat(it.description).isEqualTo("An incentive level has been changed: EN4") - assertThat(it.additionalInformation?.get("incentiveLevel")).isEqualTo("EN4") + assertThat(it.additionalInformation.incentiveLevel).isEqualTo("EN4") } assertAuditMessageSentWithMap("INCENTIVE_LEVEL_ADDED").let { assertThat(it["code"]).isEqualTo("EN4") @@ -442,7 +442,7 @@ class IncentiveLevelResourceTest : IncentiveLevelResourceTestBase() { } } - assertDomainEventSent("incentives.levels.reordered").let { + assertDomainEventSent("incentives.levels.reordered", anyDomainEvent).let { assertThat(it.description).isEqualTo("Incentive levels have been re-ordered") assertThat(it.additionalInformation).isNull() } @@ -581,9 +581,9 @@ class IncentiveLevelResourceTest : IncentiveLevelResourceTestBase() { assertThat(incentiveLevel?.whenUpdated).isEqualTo(now) } - assertDomainEventSent("incentives.level.changed").let { + assertDomainEventSent("incentives.level.changed", incentiveLevelDomainEvent).let { assertThat(it.description).isEqualTo("An incentive level has been changed: STD") - assertThat(it.additionalInformation?.get("incentiveLevel")).isEqualTo("STD") + assertThat(it.additionalInformation.incentiveLevel).isEqualTo("STD") } assertAuditMessageSentWithMap("INCENTIVE_LEVEL_UPDATED").let { assertThat(it["description"]).isEqualTo("Silver") @@ -823,9 +823,9 @@ class IncentiveLevelResourceTest : IncentiveLevelResourceTestBase() { assertThat(incentiveLevel?.whenUpdated).isEqualTo(now) } - assertDomainEventSent("incentives.level.changed").let { + assertDomainEventSent("incentives.level.changed", incentiveLevelDomainEvent).let { assertThat(it.description).isEqualTo("An incentive level has been changed: STD") - assertThat(it.additionalInformation?.get("incentiveLevel")).isEqualTo("STD") + assertThat(it.additionalInformation.incentiveLevel).isEqualTo("STD") } assertAuditMessageSentWithMap("INCENTIVE_LEVEL_UPDATED").let { assertThat(it["description"]).isEqualTo("Silver") @@ -974,9 +974,9 @@ class IncentiveLevelResourceTest : IncentiveLevelResourceTestBase() { assertThat(incentiveLevel?.whenUpdated).isNotEqualTo(now) } - assertDomainEventSent("incentives.level.changed").let { + assertDomainEventSent("incentives.level.changed", incentiveLevelDomainEvent).let { assertThat(it.description).isEqualTo("An incentive level has been changed: ENH") - assertThat(it.additionalInformation?.get("incentiveLevel")).isEqualTo("ENH") + assertThat(it.additionalInformation.incentiveLevel).isEqualTo("ENH") } assertAuditMessageSentWithMap("INCENTIVE_LEVEL_UPDATED").let { assertThat(it["code"]).isEqualTo("ENH") @@ -1063,9 +1063,9 @@ class IncentiveLevelResourceTest : IncentiveLevelResourceTestBase() { assertThat(incentiveLevel?.whenUpdated).isEqualTo(now) } - assertDomainEventSent("incentives.level.changed").let { + assertDomainEventSent("incentives.level.changed", incentiveLevelDomainEvent).let { assertThat(it.description).isEqualTo("An incentive level has been changed: EN2") - assertThat(it.additionalInformation?.get("incentiveLevel")).isEqualTo("EN2") + assertThat(it.additionalInformation.incentiveLevel).isEqualTo("EN2") } assertAuditMessageSentWithMap("INCENTIVE_LEVEL_UPDATED").let { assertThat(it["code"]).isEqualTo("EN2") @@ -1095,9 +1095,9 @@ class IncentiveLevelResourceTest : IncentiveLevelResourceTestBase() { assertThat(incentiveLevel?.whenUpdated).isEqualTo(now) } - assertDomainEventSent("incentives.level.changed").let { + assertDomainEventSent("incentives.level.changed", incentiveLevelDomainEvent).let { assertThat(it.description).isEqualTo("An incentive level has been changed: ENT") - assertThat(it.additionalInformation?.get("incentiveLevel")).isEqualTo("ENT") + assertThat(it.additionalInformation.incentiveLevel).isEqualTo("ENT") } assertAuditMessageSentWithMap("INCENTIVE_LEVEL_UPDATED").let { assertThat(it["code"]).isEqualTo("ENT") diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/resource/PrisonIncentiveLevelResourceTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/resource/PrisonIncentiveLevelResourceTest.kt index 2e4b727d3..6e06a24b9 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/resource/PrisonIncentiveLevelResourceTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/incentivesapi/resource/PrisonIncentiveLevelResourceTest.kt @@ -210,10 +210,10 @@ class PrisonIncentiveLevelResourceTest : IncentiveLevelResourceTestBase() { assertThat(prisonIncentiveLevel?.whenUpdated).isEqualTo(now) } - assertDomainEventSent("incentives.prison-level.changed").let { + assertDomainEventSent("incentives.prison-level.changed", prisonIncentiveLevelDomainEvent).let { assertThat(it.description).isEqualTo("Incentive level (STD) in prison MDI has been updated") - assertThat(it.additionalInformation?.get("incentiveLevel")).isEqualTo("STD") - assertThat(it.additionalInformation?.get("prisonId")).isEqualTo("MDI") + assertThat(it.additionalInformation.incentiveLevel).isEqualTo("STD") + assertThat(it.additionalInformation.prisonId).isEqualTo("MDI") } assertAuditMessageSentWithMap("PRISON_INCENTIVE_LEVEL_UPDATED").let { assertThat(it).containsAllEntriesOf( @@ -278,10 +278,10 @@ class PrisonIncentiveLevelResourceTest : IncentiveLevelResourceTestBase() { assertThat(defaultLevelCodes).isEqualTo(listOf("WRI" to "ENH")) } - assertDomainEventSent("incentives.prison-level.changed").let { + assertDomainEventSent("incentives.prison-level.changed", prisonIncentiveLevelDomainEvent).let { assertThat(it.description).isEqualTo("Incentive level (ENH) in prison WRI has been updated") - assertThat(it.additionalInformation?.get("incentiveLevel")).isEqualTo("ENH") - assertThat(it.additionalInformation?.get("prisonId")).isEqualTo("WRI") + assertThat(it.additionalInformation.incentiveLevel).isEqualTo("ENH") + assertThat(it.additionalInformation.prisonId).isEqualTo("WRI") } assertAuditMessageSentWithMap("PRISON_INCENTIVE_LEVEL_UPDATED").let { assertThat(it).containsAllEntriesOf( @@ -632,10 +632,10 @@ class PrisonIncentiveLevelResourceTest : IncentiveLevelResourceTestBase() { assertThat(prisonIncentiveLevel?.whenUpdated).isEqualTo(now) } - assertDomainEventSent("incentives.prison-level.changed").let { + assertDomainEventSent("incentives.prison-level.changed", prisonIncentiveLevelDomainEvent).let { assertThat(it.description).isEqualTo("Incentive level (BAS) in prison BAI has been updated") - assertThat(it.additionalInformation?.get("incentiveLevel")).isEqualTo("BAS") - assertThat(it.additionalInformation?.get("prisonId")).isEqualTo("BAI") + assertThat(it.additionalInformation.incentiveLevel).isEqualTo("BAS") + assertThat(it.additionalInformation.prisonId).isEqualTo("BAI") } assertAuditMessageSentWithMap("PRISON_INCENTIVE_LEVEL_UPDATED").let { assertThat(it).containsAllEntriesOf( @@ -699,10 +699,10 @@ class PrisonIncentiveLevelResourceTest : IncentiveLevelResourceTestBase() { assertThat(defaultLevelCodes).isEqualTo(listOf("WRI" to "ENH")) } - assertDomainEventSent("incentives.prison-level.changed").let { + assertDomainEventSent("incentives.prison-level.changed", prisonIncentiveLevelDomainEvent).let { assertThat(it.description).isEqualTo("Incentive level (ENH) in prison WRI has been updated") - assertThat(it.additionalInformation?.get("incentiveLevel")).isEqualTo("ENH") - assertThat(it.additionalInformation?.get("prisonId")).isEqualTo("WRI") + assertThat(it.additionalInformation.incentiveLevel).isEqualTo("ENH") + assertThat(it.additionalInformation.prisonId).isEqualTo("WRI") } assertAuditMessageSentWithMap("PRISON_INCENTIVE_LEVEL_UPDATED").let { assertThat(it).containsAllEntriesOf( @@ -1000,10 +1000,10 @@ class PrisonIncentiveLevelResourceTest : IncentiveLevelResourceTestBase() { assertThat(prisonIncentiveLevel?.whenUpdated).isEqualTo(now) } - assertDomainEventSent("incentives.prison-level.changed").let { + assertDomainEventSent("incentives.prison-level.changed", prisonIncentiveLevelDomainEvent).let { assertThat(it.description).isEqualTo("Incentive level (EN2) in prison WRI has been updated") - assertThat(it.additionalInformation?.get("incentiveLevel")).isEqualTo("EN2") - assertThat(it.additionalInformation?.get("prisonId")).isEqualTo("WRI") + assertThat(it.additionalInformation.incentiveLevel).isEqualTo("EN2") + assertThat(it.additionalInformation.prisonId).isEqualTo("WRI") } assertAuditMessageSentWithMap("PRISON_INCENTIVE_LEVEL_UPDATED").let { assertThat(it).containsAllEntriesOf(