Skip to content

Commit

Permalink
PI-1599 addeded .updated meessage processing
Browse files Browse the repository at this point in the history
  • Loading branch information
stevomcallister committed Nov 1, 2023
1 parent 1393dc8 commit 3dd9e81
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package uk.gov.justice.digital.hmpps

import com.fasterxml.jackson.databind.ObjectMapper
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock.aResponse
import com.github.tomakehurst.wiremock.client.WireMock.get
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
import org.hamcrest.MatcherAssert
import org.hamcrest.Matchers
import org.junit.jupiter.api.MethodOrderer
Expand Down Expand Up @@ -31,6 +34,7 @@ import uk.gov.justice.digital.hmpps.messaging.crn
import uk.gov.justice.digital.hmpps.resourceloader.ResourceLoader
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
import uk.gov.justice.digital.hmpps.telemetry.notificationReceived
import java.time.ZonedDateTime

@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = RANDOM_PORT)
Expand Down Expand Up @@ -85,14 +89,23 @@ internal class CASIntegrationTest {
fun `booking cancelled message is processed correctly`() {
val eventName = "booking-cancelled"
val event = prepEvent(eventName, wireMockServer.port())
val eventDetails = ResourceLoader.file<EventDetails<BookingCancelled>>("cas3-$eventName")
val eventDetailsCopy = eventDetails.copy(timestamp = ZonedDateTime.now().minusSeconds(3))
wireMockServer.stubFor(
get(urlEqualTo("/cas3-api/events/booking-cancelled/1234"))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(objectMapper.writeValueAsString(eventDetailsCopy))
)
)

// When it is received
channelManager.getChannel(queueName).publishAndWait(event)

// Then it is logged to telemetry
Mockito.verify(telemetryService).notificationReceived(event)

val eventDetails = ResourceLoader.file<EventDetails<BookingCancelled>>("cas3-$eventName")
val contact = contactRepository.getByExternalReference(eventDetails.eventDetails.urn)

MatcherAssert.assertThat(contact!!.type.code, Matchers.equalTo("EACA"))
Expand Down Expand Up @@ -139,14 +152,23 @@ internal class CASIntegrationTest {
fun `person arrived message is processed correctly`() {
val eventName = "person-arrived"
val event = prepEvent(eventName, wireMockServer.port())
val eventDetails = ResourceLoader.file<EventDetails<PersonArrived>>("cas3-$eventName")
val eventDetailsCopy = eventDetails.copy(timestamp = ZonedDateTime.now().minusSeconds(3))
wireMockServer.stubFor(
get(urlEqualTo("/cas3-api/events/person-arrived/1234"))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(objectMapper.writeValueAsString(eventDetailsCopy))
)
)

// When it is received
channelManager.getChannel(queueName).publishAndWait(event)

// Then it is logged to telemetry
Mockito.verify(telemetryService).notificationReceived(event)

val eventDetails = ResourceLoader.file<EventDetails<PersonArrived>>("cas3-$eventName")
val contact = contactRepository.getByExternalReference(eventDetails.eventDetails.urn)

MatcherAssert.assertThat(contact!!.type.code, Matchers.equalTo("EAAR"))
Expand All @@ -166,14 +188,23 @@ internal class CASIntegrationTest {
fun `person departed message is processed correctly`() {
val eventName = "person-departed"
val event = prepEvent(eventName, wireMockServer.port())
val eventDetails = ResourceLoader.file<EventDetails<PersonDeparted>>("cas3-$eventName")
val eventDetailsCopy = eventDetails.copy(timestamp = ZonedDateTime.now().minusSeconds(3))
wireMockServer.stubFor(
get(urlEqualTo("/cas3-api/events/person-departed/1234"))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(objectMapper.writeValueAsString(eventDetailsCopy))
)
)

// When it is received
channelManager.getChannel(queueName).publishAndWait(event)

// Then it is logged to telemetry
Mockito.verify(telemetryService).notificationReceived(event)

val eventDetails = ResourceLoader.file<EventDetails<PersonDeparted>>("cas3-$eventName")
val contact = contactRepository.getByExternalReference(eventDetails.eventDetails.urn)

MatcherAssert.assertThat(contact!!.type.code, Matchers.equalTo("EADP"))
Expand All @@ -190,12 +221,22 @@ internal class CASIntegrationTest {
val existingEventDetails = ResourceLoader.file<EventDetails<PersonDeparted>>("cas3-person-departed")
val existingContact = contactRepository.getByExternalReference(existingEventDetails.eventDetails.urn)
val existingNotes = existingContact!!.notes
val eventDetails = ResourceLoader.file<EventDetails<PersonDeparted>>("cas3-$eventName")
val eventDetailsCopy = eventDetails.copy(timestamp = ZonedDateTime.now())
wireMockServer.stubFor(
get(urlEqualTo("/cas3-api/events/person-departed/12345"))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(objectMapper.writeValueAsString(eventDetailsCopy))
)
)

// When it is received
channelManager.getChannel(queueName).publishAndWait(event)

// Then it is logged to telemetry
Mockito.verify(telemetryService).notificationReceived(event)
val eventDetails = ResourceLoader.file<EventDetails<PersonDeparted>>("cas3-$eventName")
val contact = contactRepository.getByExternalReference(eventDetails.eventDetails.urn)

MatcherAssert.assertThat(
Expand All @@ -212,14 +253,23 @@ internal class CASIntegrationTest {
val existingEventDetails = ResourceLoader.file<EventDetails<PersonArrived>>("cas3-person-arrived-update")
val existingContact = contactRepository.getByExternalReference(existingEventDetails.eventDetails.urn)
val existingNotes = existingContact!!.notes
val eventDetails = ResourceLoader.file<EventDetails<PersonArrived>>("cas3-$eventName")
val eventDetailsCopy = eventDetails.copy(timestamp = ZonedDateTime.now())
wireMockServer.stubFor(
get(urlEqualTo("/cas3-api/events/person-arrived/12345"))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(objectMapper.writeValueAsString(eventDetailsCopy))
)
)

// When it is received
channelManager.getChannel(queueName).publishAndWait(event)

// Then it is logged to telemetry
Mockito.verify(telemetryService).notificationReceived(event)

val eventDetails = ResourceLoader.file<EventDetails<PersonArrived>>("cas3-$eventName")
val contact = contactRepository.getByExternalReference(eventDetails.eventDetails.urn)

MatcherAssert.assertThat(
Expand All @@ -236,14 +286,23 @@ internal class CASIntegrationTest {
val existingEventDetails = ResourceLoader.file<EventDetails<BookingCancelled>>("cas3-booking-cancelled-update")
val existingContact = contactRepository.getByExternalReference(existingEventDetails.eventDetails.urn)
val existingNotes = existingContact!!.notes
val eventDetails = ResourceLoader.file<EventDetails<BookingCancelled>>("cas3-$eventName")
val eventDetailsCopy = eventDetails.copy(timestamp = ZonedDateTime.now())
wireMockServer.stubFor(
get(urlEqualTo("/cas3-api/events/booking-cancelled/12345"))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json")
.withBody(objectMapper.writeValueAsString(eventDetailsCopy))
)
)

// When it is received
channelManager.getChannel(queueName).publishAndWait(event)

// Then it is logged to telemetry
Mockito.verify(telemetryService).notificationReceived(event)

val eventDetails = ResourceLoader.file<EventDetails<BookingCancelled>>("cas3-$eventName")
val contact = contactRepository.getByExternalReference(eventDetails.eventDetails.urn)

MatcherAssert.assertThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ContactService(
val personId = person?.id ?: personRepository.getByCrn(crn).id
val existing = contactRepository.getByExternalReference(event.eventDetails.urn)
if (existing != null) {
if (existing.startTime < event.timestamp) {
if (existing.lastModifiedDateTime < event.timestamp) {
if (replaceNotes) {
existing.notes = event.eventDetails.noteText
} else {
Expand Down

0 comments on commit 3dd9e81

Please sign in to comment.