Skip to content

Commit

Permalink
PI-2586 WIP pre-sentence event creation
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-bcl committed Nov 21, 2024
1 parent aff355b commit f5debcf
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,38 @@ class DataLoader(
entityManager.run {
persist(BusinessInteractionGenerator.INSERT_PERSON)
persist(BusinessInteractionGenerator.INSERT_ADDRESS)
persist(BusinessInteractionGenerator.INSERT_EVENT)
persist(BusinessInteractionGenerator.INSERT_COURT_APPEARANCE)
persist(DatasetGenerator.GENDER)
persist(DatasetGenerator.OM_ALLOCATION_REASON)
persist(DatasetGenerator.ADDRESS_STATUS)
persist(DatasetGenerator.ADDRESS_TYPE)
persist(DatasetGenerator.ORDER_ALLOCATION_REASON)
persist(DatasetGenerator.COURT_APPEARANCE_TYPE)
persist(DatasetGenerator.REMAND_STATUS)
persist(DatasetGenerator.PLEA)
persist(DatasetGenerator.COURT_APPEARANCE_OUTCOME)
persist(ReferenceDataGenerator.GENDER_MALE)
persist(ReferenceDataGenerator.GENDER_FEMALE)
persist(ReferenceDataGenerator.INITIAL_ALLOCATION)
persist(ReferenceDataGenerator.MAIN_ADDRESS_STATUS)
persist(ReferenceDataGenerator.AWAITING_ASSESSMENT)
persist(ReferenceDataGenerator.ORDER_MANAGER_INITIAL_ALLOCATION)
persist(ReferenceDataGenerator.GUILTY_PLEA)
persist(ReferenceDataGenerator.TRIAL_ADJOURNMENT_APPEARANCE_TYPE)
persist(ReferenceDataGenerator.REMANDED_IN_CUSTODY_OUTCOME)
persist(ReferenceDataGenerator.REMANDED_IN_CUSTODY_STATUS)
persist(TransferReasonGenerator.CASE_ORDER)
persist(ProviderGenerator.DEFAULT)
persist(TeamGenerator.ALLOCATED)
persist(TeamGenerator.UNALLOCATED)
persist(StaffGenerator.UNALLOCATED)
persist(StaffGenerator.ALLOCATED)
persist(CourtGenerator.UNKNOWN_COURT_N07_PROVIDER)
persist(OffenceGenerator.DEFAULT)
entityManager.merge(DetailedOffenceGenerator.DEFAULT)
entityManager.merge(OffenceGenerator.DEFAULT)
entityManager.merge(ContactTypeGenerator.EAPP)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@ object BusinessInteractionGenerator {
BusinessInteractionCode.INSERT_ADDRESS.code,
ZonedDateTime.now().minusMonths(6)
)
val INSERT_EVENT = BusinessInteraction(
IdGenerator.getAndIncrement(),
BusinessInteractionCode.INSERT_EVENT.code,
ZonedDateTime.now().minusMonths(6)
)
val INSERT_COURT_APPEARANCE = BusinessInteraction(
IdGenerator.getAndIncrement(),
BusinessInteractionCode.INSERT_COURT_APPEARANCE.code,
ZonedDateTime.now().minusMonths(6)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object MainOffenceGenerator {
}

object DetailedOffenceGenerator {
val DEFAULT = generate(code = "00100", description = "Murder", homeOfficeCode = "123")
val DEFAULT = generate(code = "AA00000", description = "Murder", homeOfficeCode = "00100")
fun generate(
id: Long = IdGenerator.getAndIncrement(),
code: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ object CourtGenerator {
object ContactTypeGenerator {
val EAPP = generate(
code = ContactTypeCode.COURT_APPEARANCE.code,
description = "EAPP"
description = "Court Appearance"
)
fun generate(code: String, id: Long = IdGenerator.getAndIncrement(), description: String) =
ContactType(id = id, code = code, description = description)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import uk.gov.justice.digital.hmpps.audit.entity.AuditedInteraction
import uk.gov.justice.digital.hmpps.audit.service.AuditedInteractionService
import uk.gov.justice.digital.hmpps.data.generator.MessageGenerator
import uk.gov.justice.digital.hmpps.integrations.delius.audit.BusinessInteractionCode
import uk.gov.justice.digital.hmpps.integrations.delius.entity.Person
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.entity.*
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.EventService
import uk.gov.justice.digital.hmpps.service.PersonService
import uk.gov.justice.digital.hmpps.telemetry.TelemetryMessagingExtensions.notificationReceived
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
Expand Down Expand Up @@ -60,12 +59,33 @@ internal class IntegrationTest {
@SpyBean
lateinit var personRepository: PersonRepository

@SpyBean
lateinit var personManagerRepository: PersonManagerRepository

@SpyBean
lateinit var addressRepository: PersonAddressRepository

@SpyBean
lateinit var eventRepository: EventRepository

@SpyBean
lateinit var courtAppearanceRepository: CourtAppearanceRepository

@SpyBean
lateinit var contactRepository: ContactRepository

@SpyBean
lateinit var mainOffenceRepository: MainOffenceRepository

@SpyBean
lateinit var orderManagerRepository: OrderManagerRepository

@SpyBean
lateinit var personService: PersonService

@SpyBean
lateinit var eventService: EventService

@BeforeEach
fun setup() {
doReturn("A111111").whenever(personService).generateCrn()
Expand Down Expand Up @@ -333,9 +353,58 @@ internal class IntegrationTest {

private fun thenNoRecordsAreInserted() {
verify(personService, never()).insertAddress(any())
verify(eventService, never()).insertEvent(any(), any(), any(), any(), any())
verify(eventService, never()).insertCourtAppearance(any(), any(), any(), any())
verify(addressRepository, never()).save(any())
verify(personRepository, never()).save(any())
verify(auditedInteractionService, Mockito.never())
.createAuditedInteraction(any(), any(), eq(AuditedInteraction.Outcome.SUCCESS), any(), anyOrNull())
}

@Test
fun `A hearing message with a remanded offence is received and an event is inserted`() {
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(eventService).insertEvent(any(), any(), any(), any(), any())
verify(eventService, never()).insertCourtAppearance(any(), any(), any(), any())

verify(eventRepository).save(check<Event> {
assertThat(it.person.forename, Matchers.equalTo("Example First Name"))
assertThat(it.person.surname, Matchers.equalTo("Example Last Name"))
assertThat(it.referralDate, Matchers.equalTo(LocalDate.of(2024, 1, 1)))
assertTrue(it.active)
assertThat(it.number, Matchers.equalTo("1"))
})

verify(auditedInteractionService).createAuditedInteraction(
eq(BusinessInteractionCode.INSERT_EVENT),
any(),
eq(AuditedInteraction.Outcome.SUCCESS),
any(),
anyOrNull()
)
}

@AfterEach
fun cleanup() {
courtAppearanceRepository.deleteAll()
mainOffenceRepository.deleteAll()
orderManagerRepository.deleteAll()
eventRepository.deleteAll()
addressRepository.deleteAll()
contactRepository.deleteAll()
personManagerRepository.deleteAll()
personRepository.deleteAll()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import uk.gov.justice.digital.hmpps.audit.InteractionCode
enum class BusinessInteractionCode(override val code: String) : InteractionCode {
INSERT_PERSON("OIBI025"),
INSERT_ADDRESS("OIBI029"),
INSERT_EVENT("CWBI006")
INSERT_EVENT("CWBI006"),
INSERT_COURT_APPEARANCE("CWBI007")
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package uk.gov.justice.digital.hmpps.integrations.delius.entity

import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import org.springframework.data.annotation.CreatedBy
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedBy
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.time.LocalDate
import java.time.ZonedDateTime

@Entity
@Immutable
@Table(name = "court_appearance")
@SequenceGenerator(name = "court_appearance_id_seq", sequenceName = "court_appearance_id_seq", allocationSize = 1)
class CourtAppearance(

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "court_appearance_id_seq")
@Column(name = "court_appearance_id")
val id: Long? = null,

Expand Down Expand Up @@ -95,4 +96,13 @@ class CourtAppearance(
val person: Person,
)

interface CourtAppearanceRepository : JpaRepository<CourtAppearance, Long>
interface CourtAppearanceRepository : JpaRepository<CourtAppearance, Long> {
@Query(
"""
SELECT c FROM CourtAppearance c
WHERE c.crownCourtCalendarNumber = :crownCourtCalendarNumber
ORDER BY c.appearanceDate DESC
"""
)
fun findLatestByCaseUrn(crownCourtCalendarNumber: String): CourtAppearance?
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ class Event(
)

@Entity
@SequenceGenerator(name = "order_manager_id_seq", sequenceName = "order_manager_id_seq", allocationSize = 1)
@Table(name = "order_manager")
class OrderManager(
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "order_manager_id_seq")
@Column(name = "order_manager_id")
val id: Long? = null,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class ReferenceData(
AWAITING_ASSESSMENT("A16"),
REMANDED_IN_CUSTODY_STATUS("Y"),
REMANDED_IN_CUSTODY_OUTCOME("112"),
GUILTY("G")
GUILTY("G"),
NOT_GUILTY("N"),
NOT_KNOWN_PLEA("Q")
}
}

Expand Down
Loading

0 comments on commit f5debcf

Please sign in to comment.