diff --git a/projects/common-platform-and-delius/deploy/values.yaml b/projects/common-platform-and-delius/deploy/values.yaml index 456c964b0f..74b48d27df 100644 --- a/projects/common-platform-and-delius/deploy/values.yaml +++ b/projects/common-platform-and-delius/deploy/values.yaml @@ -23,6 +23,8 @@ generic-service: SENTRY_DSN: SENTRY_DSN common-platform-and-delius-queue: MESSAGING_CONSUMER_QUEUE: QUEUE_NAME + hmpps-domain-events-topic: + MESSAGING_PRODUCER_TOPIC: topic_arn generic-prometheus-alerts: targetApplication: common-platform-and-delius diff --git a/projects/common-platform-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt b/projects/common-platform-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt index 109f7eadfc..8ddd10bc57 100644 --- a/projects/common-platform-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt +++ b/projects/common-platform-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt @@ -4,8 +4,7 @@ import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock.* import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers -import org.junit.jupiter.api.Assertions.assertNotNull -import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.mockito.Mockito @@ -26,6 +25,7 @@ import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonRepository import uk.gov.justice.digital.hmpps.integrations.delius.entity.ReferenceData import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonAddress import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonAddressRepository +import uk.gov.justice.digital.hmpps.message.HmppsDomainEvent import uk.gov.justice.digital.hmpps.message.Notification import uk.gov.justice.digital.hmpps.messaging.HmppsChannelManager import uk.gov.justice.digital.hmpps.service.AddressService @@ -40,9 +40,15 @@ internal class IntegrationTest { @Value("\${messaging.consumer.queue}") lateinit var queueName: String + @Value("\${messaging.producer.topic}") + lateinit var topicName: String + @Autowired lateinit var channelManager: HmppsChannelManager + @Autowired + lateinit var hmppsChannelManager: HmppsChannelManager + @Autowired lateinit var wireMockServer: WireMockServer @@ -66,7 +72,7 @@ internal class IntegrationTest { @BeforeEach fun setup() { - doReturn("A000001").whenever(personService).generateCrn() + doReturn("A111111").whenever(personService).generateCrn() } @Test @@ -248,4 +254,40 @@ internal class IntegrationTest { anyOrNull() ) } + + @Test + fun `engagement created message is published on insert person`() { + wireMockServer.stubFor( + post(urlPathEqualTo("/probation-search/match")) + .willReturn( + aResponse() + .withStatus(200) + .withHeader("Content-Type", "application/json") + .withBodyFile("probation-search-no-results.json") + ) + ) + + val notification = Notification(message = MessageGenerator.COMMON_PLATFORM_EVENT) + channelManager.getChannel(queueName).publishAndWait(notification) + + verify(personService).insertPerson(any(), any()) + + verify(personRepository).save(check { + assertThat(it.forename, Matchers.equalTo("Example First Name")) + assertThat(it.surname, Matchers.equalTo("Example Last Name")) + assertThat(it.mobileNumber, Matchers.equalTo("07000000000")) + assertThat(it.telephoneNumber, Matchers.equalTo("01234567890")) + }) + + val topic = hmppsChannelManager.getChannel(topicName) + val messages = topic.pollFor(1) + + val engagementCreated = messages.first { it.eventType == "probation-case.engagement.created" }.message as HmppsDomainEvent + + assertEquals("probation-case.engagement.created", engagementCreated.eventType) + assertEquals(1, engagementCreated.version) + assertEquals("A probation case record for a person has been created in Delius", engagementCreated.description) + assertEquals("A111111", engagementCreated.personReference.findCrn()!!) + assertNull(engagementCreated.detailUrl) + } } \ No newline at end of file diff --git a/projects/common-platform-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Notifier.kt b/projects/common-platform-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Notifier.kt new file mode 100644 index 0000000000..18aaed2e56 --- /dev/null +++ b/projects/common-platform-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/messaging/Notifier.kt @@ -0,0 +1,40 @@ +package uk.gov.justice.digital.hmpps.messaging + +import org.openfolder.kotlinasyncapi.annotation.Schema +import org.openfolder.kotlinasyncapi.annotation.channel.Channel +import org.openfolder.kotlinasyncapi.annotation.channel.Message +import org.openfolder.kotlinasyncapi.annotation.channel.Subscribe +import org.springframework.beans.factory.annotation.Qualifier +import org.springframework.stereotype.Service +import uk.gov.justice.digital.hmpps.integrations.delius.entity.Person +import uk.gov.justice.digital.hmpps.message.* +import uk.gov.justice.digital.hmpps.publisher.NotificationPublisher + +@Service +@Channel("hmpps-domain-events-topic") +class Notifier( + @Qualifier("topicPublisher") private val topicPublisher: NotificationPublisher, +) { + @Subscribe( + messages = [ + Message(title = "probation-case.engagement.created", payload = Schema(HmppsDomainEvent::class)) + ] + ) + fun caseCreated(person: Person) { + topicPublisher.publish( + Notification( + message = HmppsDomainEvent( + version = 1, + eventType = "probation-case.engagement.created", + description = "A probation case record for a person has been created in Delius", + personReference = PersonReference( + identifiers = listOf( + PersonIdentifier("CRN", person.crn), + ), + ), + ), + attributes = MessageAttributes("probation-case.engagement.created") + ) + ) + } +} \ No newline at end of file diff --git a/projects/common-platform-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/PersonService.kt b/projects/common-platform-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/PersonService.kt index fefb382ead..7455ef7e18 100644 --- a/projects/common-platform-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/PersonService.kt +++ b/projects/common-platform-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/PersonService.kt @@ -9,6 +9,7 @@ import uk.gov.justice.digital.hmpps.audit.service.AuditableService import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService import uk.gov.justice.digital.hmpps.integrations.delius.audit.BusinessInteractionCode import uk.gov.justice.digital.hmpps.integrations.delius.entity.* +import uk.gov.justice.digital.hmpps.messaging.Notifier import java.sql.Types import java.time.LocalDateTime @@ -16,13 +17,14 @@ import java.time.LocalDateTime class PersonService( jdbcTemplate: JdbcTemplate, auditedInteractionService: AuditedInteractionService, + private val notifier: Notifier, private val personRepository: PersonRepository, private val courtRepository: CourtRepository, private val equalityRepository: EqualityRepository, private val personManagerRepository: PersonManagerRepository, private val teamRepository: TeamRepository, private val staffRepository: StaffRepository, - private val referenceDataRepository: ReferenceDataRepository, + private val referenceDataRepository: ReferenceDataRepository ) : AuditableService(auditedInteractionService) { private val generateCrn = SimpleJdbcCall(jdbcTemplate) @@ -44,6 +46,8 @@ class PersonService( val unallocatedTeam = teamRepository.findByCode(courtLinkedProvider.code + "UAT") val unallocatedStaff = staffRepository.findByCode(unallocatedTeam.code + "U") + notifier.caseCreated(savedPerson) + // Person manager record val manager = PersonManager( person = savedPerson, diff --git a/projects/common-platform-and-delius/src/main/resources/application.yml b/projects/common-platform-and-delius/src/main/resources/application.yml index 9d57424855..52522fea63 100644 --- a/projects/common-platform-and-delius/src/main/resources/application.yml +++ b/projects/common-platform-and-delius/src/main/resources/application.yml @@ -51,6 +51,8 @@ spring: jpa: hibernate.ddl-auto: create-drop +messaging.producer.topic: domain-events + seed.database: true wiremock.enabled: true context.initializer.classes: uk.gov.justice.digital.hmpps.wiremock.WireMockInitialiser diff --git a/projects/common-platform-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/messaging/HandlerTest.kt b/projects/common-platform-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/messaging/HandlerTest.kt index 0aa8fb0d94..be7b3df781 100644 --- a/projects/common-platform-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/messaging/HandlerTest.kt +++ b/projects/common-platform-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/messaging/HandlerTest.kt @@ -1,6 +1,5 @@ package uk.gov.justice.digital.hmpps.messaging -import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows import org.junit.jupiter.api.extension.ExtendWith @@ -56,7 +55,7 @@ internal class HandlerTest { matchedBy = "NONE" ) ) - whenever(personService.generateCrn()).thenReturn("A000001") + whenever(personService.generateCrn()).thenReturn("A111111") whenever(personService.insertPerson(any(), any())).thenReturn(PersonGenerator.DEFAULT) whenever( referenceDataRepository.findByCodeAndDatasetCode( diff --git a/projects/common-platform-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/messaging/NotifierTest.kt b/projects/common-platform-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/messaging/NotifierTest.kt new file mode 100644 index 0000000000..07efc335b0 --- /dev/null +++ b/projects/common-platform-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/messaging/NotifierTest.kt @@ -0,0 +1,35 @@ +package uk.gov.justice.digital.hmpps.messaging + +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.mockito.Mock +import org.mockito.junit.jupiter.MockitoExtension +import org.mockito.kotlin.* +import org.springframework.boot.test.system.CapturedOutput +import org.springframework.boot.test.system.OutputCaptureExtension +import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator +import uk.gov.justice.digital.hmpps.message.Notification +import uk.gov.justice.digital.hmpps.publisher.NotificationPublisher + +@ExtendWith(MockitoExtension::class) +@ExtendWith(OutputCaptureExtension::class) +class NotifierTest { + @Mock + lateinit var topicPublisher: NotificationPublisher + + lateinit var notifier: Notifier + + @BeforeEach + fun setUp() { + notifier = Notifier(topicPublisher) + } + + @Test + fun `test notification`(output: CapturedOutput) { + doNothing().whenever(topicPublisher).publish(any>()) + notifier.caseCreated(PersonGenerator.DEFAULT) + verify(topicPublisher, times(1)).publish(any>()) + verifyNoMoreInteractions(topicPublisher) + } +} \ No newline at end of file diff --git a/projects/common-platform-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/PersonServiceTest.kt b/projects/common-platform-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/PersonServiceTest.kt index c4163ab49b..4bb05c1c2a 100644 --- a/projects/common-platform-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/PersonServiceTest.kt +++ b/projects/common-platform-and-delius/src/test/kotlin/uk/gov/justice/digital/hmpps/service/PersonServiceTest.kt @@ -16,6 +16,7 @@ import org.springframework.jdbc.core.JdbcTemplate import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService import uk.gov.justice.digital.hmpps.data.generator.* import uk.gov.justice.digital.hmpps.integrations.delius.entity.* +import uk.gov.justice.digital.hmpps.messaging.Notifier import java.time.LocalDateTime @ExtendWith(MockitoExtension::class) @@ -44,6 +45,9 @@ class PersonServiceTest { @Mock private lateinit var referenceDataRepository: ReferenceDataRepository + @Mock + lateinit var notifier: Notifier + @Mock private lateinit var jdbcTemplate: JdbcTemplate @@ -71,7 +75,7 @@ class PersonServiceTest { whenever(staffRepository.findByCode(anyString())).thenReturn(unallocatedStaff) personService.insertPerson(person, court.code) - + verify(notifier).caseCreated(any()) verify(personRepository).save(person) verify(personManagerRepository).save(any()) verify(equalityRepository).save(any())