Skip to content

Commit

Permalink
Move out incentive level additional information from polymorphic doma…
Browse files Browse the repository at this point in the history
…in event field
  • Loading branch information
ushkarev committed Apr 4, 2023
1 parent 0f65743 commit 23814db
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
)
Expand All @@ -77,3 +77,5 @@ class IncentiveLevelAuditedService(
)
}
}

data class IncentiveLevelRef(val incentiveLevel: String)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ data class AdditionalInformation(
val bookingId: Long? = null,
val alertsAdded: List<String>? = null,
val alertsRemoved: List<String>? = null,
val incentiveLevel: String? = null,
val prisonId: String? = null,
)

data class HMPPSDomainEvent<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,15 +78,19 @@ class IncentiveLevelResourceTestBase : SqsIntegrationTestBase() {
assertThat(queueSize).isEqualTo(0)
}

protected fun assertDomainEventSent(eventType: String): HMPPSDomainEvent<Map<String, *>?> {
protected val anyDomainEvent = object : TypeReference<HMPPSDomainEvent<Any?>>() {}
protected val incentiveLevelDomainEvent = object : TypeReference<HMPPSDomainEvent<IncentiveLevelRef>>() {}
protected val prisonIncentiveLevelDomainEvent = object : TypeReference<HMPPSDomainEvent<PrisonIncentiveLevelRef>>() {}

protected fun <T> assertDomainEventSent(eventType: String, typeReference: TypeReference<HMPPSDomainEvent<T>>): HMPPSDomainEvent<T> {
val sqsClient = incentivesQueue.sqsClient
val queueSize = sqsClient.getApproxQueueSize(testDomainEventQueueUrl!!)
assertThat(queueSize).isEqualTo(1)

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<HMPPSDomainEvent<Map<String, *>?>>() {})
val domainEvent = objectMapper.readValue(message, typeReference)
assertThat(domainEvent.eventType).isEqualTo(eventType)

return domainEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 23814db

Please sign in to comment.