Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Test Fixes + changed court code lookup to use nationalCourtCode db column on COURT table
  • Loading branch information
joseph-bcl committed Oct 16, 2024
1 parent 8cffa22 commit 3ff62c6
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import org.springframework.boot.context.event.ApplicationReadyEvent
import org.springframework.context.ApplicationListener
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import uk.gov.justice.digital.hmpps.data.generator.CourtGenerator
import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator
import uk.gov.justice.digital.hmpps.data.generator.ReferenceDataGenerator
import uk.gov.justice.digital.hmpps.data.generator.UserGenerator
import uk.gov.justice.digital.hmpps.data.generator.*
import uk.gov.justice.digital.hmpps.user.AuditUserRepository

@Component
Expand All @@ -28,9 +25,17 @@ class DataLoader(
@Transactional
override fun onApplicationEvent(are: ApplicationReadyEvent) {
entityManager.run {
persist(ProviderGenerator.DEFAULT)
persist(BusinessInteractionGenerator.INSERT_PERSON)
persist(DatasetGenerator.GENDER)
persist(DatasetGenerator.OM_ALLOCATION_REASON)
persist(ReferenceDataGenerator.GENDER_MALE)
persist(ReferenceDataGenerator.GENDER_FEMALE)
persist(ReferenceDataGenerator.INITIAL_ALLOCATION)
persist(ProviderGenerator.DEFAULT)
persist(TeamGenerator.ALLOCATED)
persist(TeamGenerator.UNALLOCATED)
persist(StaffGenerator.UNALLOCATED)
persist(StaffGenerator.ALLOCATED)
persist(CourtGenerator.UNKNOWN_COURT_N07_PROVIDER)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.audit.BusinessInteraction
import uk.gov.justice.digital.hmpps.integrations.delius.audit.BusinessInteractionCode
import java.time.ZonedDateTime

object BusinessInteractionGenerator {
val INSERT_PERSON = BusinessInteraction(
IdGenerator.getAndIncrement(),
BusinessInteractionCode.INSERT_PERSON.code,
ZonedDateTime.now().minusMonths(6)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@ object DatasetGenerator {
val ALL_DATASETS = DatasetCode.entries.map { Dataset(IdGenerator.getAndIncrement(), it) }.associateBy { it.code }
val GENDER = ALL_DATASETS[DatasetCode.GENDER]!!
val OM_ALLOCATION_REASON = ALL_DATASETS[DatasetCode.OM_ALLOCATION_REASON]!!
fun all() = ALL_DATASETS.values
}

object CourtGenerator {
val UNKNOWN_COURT_N07_PROVIDER = generate(code = "UNKNCT")
val UNKNOWN_COURT_N07_PROVIDER = generate(code = "UNKNCT", nationalCourtCode = "A00AA00")

fun generate(
id: Long = IdGenerator.getAndIncrement(),
code: String,
selectable: Boolean = true,
courtName: String = "Court not known",
provider: Provider = ProviderGenerator.DEFAULT,
) = Court(id = id, code = code, selectable = selectable, courtName = courtName, probationArea = provider)
nationalCourtCode: String? = null,
) = Court(
id = id,
code = code,
selectable = selectable,
courtName = courtName,
probationArea = provider,
nationalCourtCode = nationalCourtCode
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ 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.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.atLeastOnce
import org.mockito.kotlin.*
Expand All @@ -16,13 +15,12 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.boot.test.mock.mockito.SpyBean
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.repository.CourtRepository
import uk.gov.justice.digital.hmpps.integrations.delius.entity.repository.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.entity.repository.ReferenceDataRepository
import uk.gov.justice.digital.hmpps.message.Notification
import uk.gov.justice.digital.hmpps.messaging.HmppsChannelManager
import uk.gov.justice.digital.hmpps.service.PersonService
Expand All @@ -41,24 +39,23 @@ internal class IntegrationTest {
@Autowired
lateinit var wireMockServer: WireMockServer

@Mock
lateinit var auditedInteractionService: AuditedInteractionService

@MockBean
lateinit var telemetryService: TelemetryService

@MockBean
lateinit var referenceDataRepository: ReferenceDataRepository

@MockBean
lateinit var courtRepository: CourtRepository
@SpyBean
lateinit var auditedInteractionService: AuditedInteractionService

@SpyBean
lateinit var personRepository: PersonRepository

@SpyBean
lateinit var personService: PersonService

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

@Test
fun `Message is logged to telemetry`() {
val notification = Notification(message = MessageGenerator.COMMON_PLATFORM_EVENT)
Expand Down Expand Up @@ -92,7 +89,7 @@ internal class IntegrationTest {
}

@Test
fun `When a person is 10 years old or under a person is not inserted`() {
fun `When a message with no prosecution cases is found no insert is performed`() {
wireMockServer.stubFor(
post(urlPathEqualTo("/probation-search/match"))
.willReturn(
Expand All @@ -103,12 +100,9 @@ internal class IntegrationTest {
)
)

val notification = Notification(message = MessageGenerator.COMMON_PLATFORM_EVENT_VALIDATION_ERROR)
val exception = assertThrows<IllegalArgumentException> {
channelManager.getChannel(queueName).publishAndWait(notification)
}
val notification = Notification(message = MessageGenerator.COMMON_PLATFORM_EVENT_NO_CASES)

assert(exception.message!!.contains("Date of birth would indicate person is under ten years old"))
channelManager.getChannel(queueName).publishAndWait(notification)

verify(personService, never()).insertPerson(any(), any())
verify(personRepository, never()).save(any())
Expand Down Expand Up @@ -137,10 +131,11 @@ internal class IntegrationTest {
assertThat(it.forename, Matchers.equalTo("Example First Name"))
assertThat(it.surname, Matchers.equalTo("Example Last Name"))
})

verify(auditedInteractionService).createAuditedInteraction(
eq(BusinessInteractionCode.INSERT_PERSON),
any(),
any(),
eq(AuditedInteraction.Outcome.SUCCESS),
any(),
anyOrNull()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ class Court(
@ManyToOne
@JoinColumn(name = "probation_area_id")
val probationArea: Provider,
) {
enum class CourtCode(val commonPlatformValue: String, val deliusValue: String) {
TEST("A00AA00", "UNKNCT")
}
}

@Column(name = "national_court_code")
val nationalCourtCode: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ fun ReferenceDataRepository.initialAllocationReason() =
?: throw NotFoundException("Allocation Reason", "code", ReferenceData.AllocationCode.INITIAL_ALLOCATION.code)

interface CourtRepository : JpaRepository<Court, Long> {
fun findByCode(code: String): Court
fun findByNationalCourtCode(nationalCourtCode: String): Court
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import org.springframework.stereotype.Component
import uk.gov.justice.digital.hmpps.converter.NotificationConverter
import uk.gov.justice.digital.hmpps.integrations.client.ProbationMatchRequest
import uk.gov.justice.digital.hmpps.integrations.client.ProbationSearchClient
import uk.gov.justice.digital.hmpps.integrations.delius.entity.Court
import uk.gov.justice.digital.hmpps.integrations.delius.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.entity.ReferenceData
import uk.gov.justice.digital.hmpps.integrations.delius.entity.repository.ReferenceDataRepository
Expand All @@ -37,7 +36,8 @@ class Handler(

val defendant = notification.message.hearing.prosecutionCases.firstOrNull()?.defendants?.firstOrNull()
?: throw IllegalArgumentException("No prosecution cases found")
val courtCode = notification.message.hearing.courtCentre.code.toDeliusCourtCode()

val courtCode = notification.message.hearing.courtCentre.code

val dateOfBirth = notification.message.hearing.prosecutionCases
.firstOrNull()?.defendants?.firstOrNull()
Expand Down Expand Up @@ -99,9 +99,6 @@ class Handler(

fun String.toDeliusGender() = ReferenceData.GenderCode.entries.find { it.commonPlatformValue == this }?.deliusValue
?: throw IllegalStateException("Gender not found: $this")

fun String.toDeliusCourtCode() = Court.CourtCode.entries.find { it.commonPlatformValue == this }?.deliusValue
?: throw IllegalStateException("Court not found: $this")
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class PersonService(
// Person record
val savedPerson = personRepository.save(person)

val courtLinkedProvider = courtRepository.findByCode(courtCode).probationArea
val courtLinkedProvider = courtRepository.findByNationalCourtCode(courtCode).probationArea
val initialAllocation = referenceDataRepository.initialAllocationReason()
val unallocatedTeam = teamRepository.findByCode(courtLinkedProvider.code + "UAT")
val unallocatedStaff = staffRepository.findByCode(unallocatedTeam.code + "U")
Expand Down Expand Up @@ -72,7 +72,6 @@ class PersonService(
equalityRepository.save(equality)

audit["offenderId"] = savedPerson.id
true
}

fun generateCrn(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class PersonServiceTest {
val initialAllocation = ReferenceDataGenerator.INITIAL_ALLOCATION

whenever(personRepository.save(person)).thenReturn(savedPerson)
whenever(courtRepository.findByCode(anyString())).thenReturn(court)
whenever(courtRepository.findByNationalCourtCode(anyString())).thenReturn(court)
whenever(referenceDataRepository.findByCodeAndDatasetCode(anyString(), any())).thenReturn(initialAllocation)
whenever(teamRepository.findByCode(anyString())).thenReturn(unallocatedTeam)
whenever(staffRepository.findByCode(anyString())).thenReturn(unallocatedStaff)
Expand Down

0 comments on commit 3ff62c6

Please sign in to comment.