Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Added notifier to publish sns messages (probation-case.engagement.created) when insert person is performed
  • Loading branch information
joseph-bcl committed Oct 21, 2024
1 parent f378f0d commit b838293
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 7 deletions.
2 changes: 2 additions & 0 deletions projects/common-platform-and-delius/deploy/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -66,7 +72,7 @@ internal class IntegrationTest {

@BeforeEach
fun setup() {
doReturn("A000001").whenever(personService).generateCrn()
doReturn("A111111").whenever(personService).generateCrn()
}

@Test
Expand Down Expand Up @@ -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<Person> {
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)
}
}
Original file line number Diff line number Diff line change
@@ -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")
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ 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

@Service
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)
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Notification<*>>())
notifier.caseCreated(PersonGenerator.DEFAULT)
verify(topicPublisher, times(1)).publish(any<Notification<*>>())
verifyNoMoreInteractions(topicPublisher)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -44,6 +45,9 @@ class PersonServiceTest {
@Mock
private lateinit var referenceDataRepository: ReferenceDataRepository

@Mock
lateinit var notifier: Notifier

@Mock
private lateinit var jdbcTemplate: JdbcTemplate

Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit b838293

Please sign in to comment.