Skip to content

Commit

Permalink
PI-1939 - add call to get offender name to add to response object
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Mar 21, 2024
1 parent 4525097 commit fb293d8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event
interface EventSentenceRepository : JpaRepository<Event, Long> {
@Query(
"SELECT e FROM Event e " +
"JOIN Person p ON p.id = e.personId " +
"LEFT JOIN FETCH e.court c " +
"LEFT JOIN FETCH e.mainOffence m " +
"LEFT JOIN FETCH e.additionalOffences ao " +
"LEFT JOIN FETCH m.offence mo " +
"LEFT JOIN FETCH ao.offence aoo " +
"WHERE p.crn = :crn " +
"WHERE e.personId = :id " +
"AND e.active = true "
)
fun findActiveSentencesByCrn(crn: String): List<Event>
fun findActiveSentencesByPersonId(id: Long): List<Event>
}

interface CourtAppearanceRepository : JpaRepository<CourtAppearance, Long> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package uk.gov.justice.digital.hmpps.service
import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.api.model.sentence.*
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Event
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.getSummary
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.AdditionalSentenceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearance
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearanceRepository
Expand All @@ -13,10 +15,12 @@ import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.Addition
class SentenceService(
private val eventRepository: EventSentenceRepository,
private val courtApperanceRepository: CourtAppearanceRepository,
private val additionalSentenceRepository: AdditionalSentenceRepository
private val additionalSentenceRepository: AdditionalSentenceRepository,
private val personRepository: PersonRepository
) {
fun getMostRecentActiveEvent(crn: String): SentenceOverview {
val events = eventRepository.findActiveSentencesByCrn(crn)
val person = personRepository.getSummary(crn)
val events = eventRepository.findActiveSentencesByPersonId(person.id)
return SentenceOverview(events.map {
val courtAppearance = courtApperanceRepository.getFirstCourtAppearanceByEventIdOrderByDate(it.id)
val additionalSentences = additionalSentenceRepository.getAllByEventId(it.id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package uk.gov.justice.digital.hmpps.service

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
Expand All @@ -12,6 +13,9 @@ import uk.gov.justice.digital.hmpps.data.generator.AdditionalSentenceGenerator
import uk.gov.justice.digital.hmpps.data.generator.CourtAppearanceGenerator
import uk.gov.justice.digital.hmpps.data.generator.CourtGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.PersonRepository
import uk.gov.justice.digital.hmpps.integrations.delius.overview.entity.PersonSummaryEntity
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.AdditionalSentenceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.CourtAppearanceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.sentence.entity.EventSentenceRepository
Expand All @@ -29,24 +33,43 @@ class SentenceServiceTest {
@Mock
lateinit var additionalSentenceRepository: AdditionalSentenceRepository

@Mock
lateinit var personRepository: PersonRepository

@InjectMocks
lateinit var service: SentenceService

private lateinit var personSummary: PersonalDetailsServiceTest.Summary

@BeforeEach
fun setup() {
val crn = "X000005"
personSummary = PersonalDetailsServiceTest.Summary(
id = 1,
forename = "TestName",
surname = "TestSurname", crn = "CRN", pnc = "PNC", dateOfBirth = LocalDate.now().minusYears(50)
)
}

@Test
fun `no active sentences`() {

whenever(eventRepository.findActiveSentencesByCrn(PersonGenerator.OVERVIEW.crn)).thenReturn(
whenever(personRepository.findSummary(PersonGenerator.OVERVIEW.crn)).thenReturn(personSummary)
whenever(eventRepository.findActiveSentencesByPersonId(personSummary.id)).thenReturn(
listOf()
)

val response = service.getMostRecentActiveEvent(PersonGenerator.OVERVIEW.crn)

assertEquals(SentenceOverview(listOf()), response)
verify(eventRepository, times(1)).findActiveSentencesByCrn(PersonGenerator.OVERVIEW.crn)
verify(personRepository, times(1)).findSummary(PersonGenerator.OVERVIEW.crn)
verify(eventRepository, times(1)).findActiveSentencesByPersonId(personSummary.id)

verifyNoMoreInteractions(eventRepository)
verifyNoMoreInteractions(personRepository)
verifyNoInteractions(courtAppearanceRepository)
verifyNoInteractions(additionalSentenceRepository)

}

@Test
Expand All @@ -63,7 +86,8 @@ class SentenceServiceTest {
additionalOffences = listOf(PersonGenerator.ADDITIONAL_OFFENCE_1)
)

whenever(eventRepository.findActiveSentencesByCrn(PersonGenerator.OVERVIEW.crn)).thenReturn(
whenever(personRepository.findSummary(PersonGenerator.OVERVIEW.crn)).thenReturn(personSummary)
whenever(eventRepository.findActiveSentencesByPersonId(personSummary.id)).thenReturn(
listOf(
event
)
Expand Down Expand Up @@ -105,10 +129,24 @@ class SentenceServiceTest {
)

assertEquals(expected, response)
verify(eventRepository, times(1)).findActiveSentencesByCrn(PersonGenerator.OVERVIEW.crn)
verify(eventRepository, times(1)).findActiveSentencesByPersonId(personSummary.id)
verify(additionalSentenceRepository, times(1)).getAllByEventId(event.id)
verify(courtAppearanceRepository, times(1)).getFirstCourtAppearanceByEventIdOrderByDate(event.id)

verifyNoMoreInteractions(eventRepository)
verifyNoMoreInteractions(additionalSentenceRepository)
verifyNoMoreInteractions(courtAppearanceRepository)

}

data class Summary(
override val id: Long,
override val forename: String,
override val secondName: String? = null,
override val thirdName: String? = null,
override val surname: String,
override val crn: String,
override val pnc: String?,
override val dateOfBirth: LocalDate
) : PersonSummaryEntity
}

0 comments on commit fb293d8

Please sign in to comment.