Skip to content

Commit

Permalink
PI-2248: Ignore 404s from sentence changed events (#3914)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcphee77 authored Jun 17, 2024
1 parent 717f249 commit a0848f8
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ class DataLoader(
PersonGenerator.HANDOVER,
PersonGenerator.CREATE_HANDOVER_AND_START,
PersonGenerator.UPDATE_HANDOVER_AND_START,
PersonGenerator.CREATE_SENTENCE_CHANGED
PersonGenerator.CREATE_SENTENCE_CHANGED,
PersonGenerator.PERSON_NOT_FOUND
)
)
personManagerRepository.saveAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import uk.gov.justice.digital.hmpps.resourceloader.ResourceLoader

object MessageGenerator {
val SENTENCE_CHANGED = ResourceLoader.message<ProbationOffenderEvent>("sentence-changed")
val SENTENCE_CHANGED_NOT_FOUND = ResourceLoader.message<ProbationOffenderEvent>("sentence-changed-not-found")
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ object PersonGenerator {
val UPDATE_HANDOVER_AND_START = generate("H123457", "A2048BY")
val CREATE_HANDOVER_AND_START = generate("H123458", "A4096BY")
val CREATE_SENTENCE_CHANGED = generate("H123459", "A4096CY")
val PERSON_NOT_FOUND = generate("H123410", "A4096DY")

fun generate(
crn: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"Type": "Notification",
"MessageId": "20e13002-d1be-56e7-be8c-66cdd7e23341",
"TopicArn": "arn:aws:sns:eu-west-2:754256621582:cloud-platform-Digital-Prison-Services-f221e27fcfcf78f6ab4f4c3cc165eee7",
"Message": "{\"eventType\":\"SENTENCE_CHANGED\",\"eventDatetime\":\"2020-02-25T11:24:32.935401\",\"offenderId\":1,\"crn\":\"H123410\",\"sourceId\":\"2500974056\"}",
"Timestamp": "2020-02-25T11:25:16.169Z",
"SignatureVersion": "1",
"Signature": "h5p3FnnbsSHxj53RFePh8HR40cbVvgEZa6XUVTlYs/yuqfDsi17MPA+bX4ijKmmTT2l6xG2xYhcmRAbJWQ4wrwncTBm2azgiwSO5keRNWYVdiC0rI484KLZboP1SDsE+Y7hOU/R0dz49q7+0yd+QIocPteKB/8xG7/6kjGStAZKf3cEdlxOwLhN+7RU1Yk2ENuwAJjVRtvlAa76yKB3xvL2hId7P7ZLmHGlzZDNZNYxbg9C8HGxteOzZ9ZeeQsWDf9jmZ+5+7dKXQoW9LeqwHxEAq2vuwSZ8uwM5JljXbtS5w1P0psXPYNoin2gU1F5MDK8RPzjUtIvjINx08rmEOA==",
"SigningCertURL": "https://sns.eu-west-2.amazonaws.com/SimpleNotificationService-a86cb10b4e1f29c941702d737128f7b6.pem",
"UnsubscribeURL": "https://sns.eu-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-2:754256621582:cloud-platform-Digital-Prison-Services-f221e27fcfcf78f6ab4f4c3cc165eee7:92545cfe-de5d-43e1-8339-c366bf0172aa",
"MessageAttributes": {
"eventType": {
"Type": "String",
"Value": "SENTENCE_CHANGED"
},
"id": {
"Type": "String",
"Value": "cb4645f2-d0c1-4677-806a-8036ed54bf69"
},
"contentType": {
"Type": "String",
"Value": "text/plain;charset=UTF-8"
},
"timestamp": {
"Type": "Number.java.lang.Long",
"Value": "1582629916147"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@
"bodyFileName": "sentence-changed-dates.json"
}
},
{
"request": {
"method": "GET",
"url": "/api/handovers/A4096DY"
},
"response": {
"headers": {
"Content-Type": "application/json"
},
"status": 404,
"bodyFileName": "sentence-changed-dates.json"
}
},
{
"request": {
"method": "GET",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ internal class HandoverMessagingIntegrationTest {
@Value("\${messaging.consumer.queue}")
lateinit var queueName: String

@Value("\${mpc.handover.url}")
lateinit var handoverUrl: String

@Autowired
lateinit var channelManager: HmppsChannelManager

Expand Down Expand Up @@ -120,4 +123,21 @@ internal class HandoverMessagingIntegrationTest {
assertThat(handoverDates[KeyDate.TypeCode.HANDOVER_DATE.value]?.date, equalTo(LocalDate.of(2023, 6, 11)))
assertThat(handoverDates[KeyDate.TypeCode.HANDOVER_START_DATE.value]?.date, equalTo(LocalDate.of(2023, 6, 7)))
}

@Test
fun `ignores a sentence changed event causing 404`() {
val notification = Notification(
message = MessageGenerator.SENTENCE_CHANGED_NOT_FOUND,
attributes = MessageAttributes(eventType = "SENTENCE_CHANGED")
)

channelManager.getChannel(queueName).publishAndWait(notification)

verify(telemetryService).trackEvent(
"Handovers api returned not found for sentence changed event",
mapOf(
"detailUrl" to "$handoverUrl/api/handovers/A4096DY"
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import org.openfolder.kotlinasyncapi.annotation.channel.Message
import org.openfolder.kotlinasyncapi.annotation.channel.Publish
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Primary
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Component
import org.springframework.web.client.HttpClientErrorException
import uk.gov.justice.digital.hmpps.converter.NotificationConverter
import uk.gov.justice.digital.hmpps.exception.IgnorableMessageException
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonRepository
import uk.gov.justice.digital.hmpps.message.HmppsDomainEvent
import uk.gov.justice.digital.hmpps.message.Notification
Expand Down Expand Up @@ -39,32 +42,51 @@ class PomCaseMessageHandler(
)
override fun handle(notification: Notification<Any>) {
telemetryService.notificationReceived(notification)
when (val message = notification.message) {
is HmppsDomainEvent -> when (notification.eventType) {
"offender-management.handover.changed" -> handoverDatesChanged.process(
HandoverMessage(
message.personReference,
message.detailUrl
)
)

"offender-management.allocation.changed" -> pomAllocated.process(message)
else -> throw NotImplementedError("Unhandled message type received: ${notification.eventType}")
}

is ProbationOffenderEvent -> when (notification.eventType) {
"SENTENCE_CHANGED",
-> personRepository.findNomsIdByCrn(message.crn)?.let {
handoverDatesChanged.process(
try {
when (val message = notification.message) {
is HmppsDomainEvent -> when (notification.eventType) {
"offender-management.handover.changed" -> handoverDatesChanged.process(
HandoverMessage(
PersonReference(listOf(PersonIdentifier("NOMS", it))),
"$mpcHandoverUrl/api/handovers/$it"
message.personReference,
message.detailUrl
)
)

"offender-management.allocation.changed" -> pomAllocated.process(message)
else -> throw NotImplementedError("Unhandled message type received: ${notification.eventType}")
}

else -> throw NotImplementedError("Unexpected offender event type: ${notification.eventType}")
is ProbationOffenderEvent -> when (notification.eventType) {
"SENTENCE_CHANGED",
-> personRepository.findNomsIdByCrn(message.crn)?.let {
try {
handoverDatesChanged.process(
HandoverMessage(
PersonReference(listOf(PersonIdentifier("NOMS", it))),
"$mpcHandoverUrl/api/handovers/$it"
)
)
} catch (e: HttpClientErrorException) {
if (e.statusCode == HttpStatus.NOT_FOUND) {
throw IgnorableMessageException(
"Handovers api returned not found for sentence changed event",
mapOf("detailUrl" to "$mpcHandoverUrl/api/handovers/$it")
)
} else {
throw e
}
}
}

else -> throw NotImplementedError("Unexpected offender event type: ${notification.eventType}")
}
}
} catch (ime: IgnorableMessageException) {
telemetryService.trackEvent(
ime.message,
ime.additionalProperties
)
}
}
}
Expand Down

0 comments on commit a0848f8

Please sign in to comment.