Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* PI-2110

* PI-2110 updated tests

* PI-2110 added main address details

* Formatting changes

* Updated entity graph

* PI-2110

* PI-2110 updated tests

* PI-2110 added main address details

* Formatting changes

* Updated entity graph

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
anthony-britton-moj and github-actions[bot] authored Apr 18, 2024
1 parent 645fc25 commit aeb48b2
Show file tree
Hide file tree
Showing 12 changed files with 486 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uk.gov.justice.digital.hmpps.epf.entity
import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import org.hibernate.annotations.SQLRestriction
import org.springframework.data.jpa.repository.EntityGraph
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import uk.gov.justice.digital.hmpps.exception.NotFoundException
Expand Down Expand Up @@ -124,6 +125,7 @@ fun EventRepository.getEvent(crn: String, number: String) =
findEventByCrnAndEventNumber(crn, number) ?: throw NotFoundException("Event", "crn", crn)

interface KeyDateRepository : JpaRepository<KeyDate, Long> {
@EntityGraph(attributePaths = ["type"])
fun findByCustodyIdAndTypeCode(custodyId: Long, typeCode: String): KeyDate?
}

Expand Down
5 changes: 5 additions & 0 deletions projects/oasys-and-delius/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ dependencies {
configure<ClassPathExtension> {
jacocoExclusions = listOf(
"**/config/**",
"**/model/**",
"**/entity/**",
"**/AppKt.class"
)
sonarExclusions = listOf(
"**/entity/**",
"**/model/**"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ 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.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.*
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.CUSTODY_PERSON
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.DETAILED_PERSON
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.REGISTERED_PERSON
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.RELEASED_PERSON
import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator.DEFAULT_PROVIDER
import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator.DEFAULT_TEAM
import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator.JOHN_SMITH
import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator.UNALLOCATED_STAFF
import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator
import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator.ANOTHER_TYPE
import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator.CATEGORY
import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator.DEFAULT_TYPE
import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator.FLAG
import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator.LEVEL
import uk.gov.justice.digital.hmpps.data.generator.SentenceGenerator
import uk.gov.justice.digital.hmpps.data.generator.UserGenerator
import uk.gov.justice.digital.hmpps.integration.delius.sentence.entity.Custody
import uk.gov.justice.digital.hmpps.user.AuditUserRepository
import java.time.ZonedDateTime
Expand All @@ -41,6 +41,7 @@ class DataLoader(
override fun onApplicationEvent(are: ApplicationReadyEvent) {
referenceData()
providerData()
personData()
registrationData()
custodialData()
}
Expand All @@ -51,7 +52,12 @@ class DataLoader(
LEVEL,
CATEGORY,
DEFAULT_TYPE,
ANOTHER_TYPE
ANOTHER_TYPE,
PersonGenerator.GENDER,
PersonGenerator.ETHNICITY,
PersonGenerator.LANGUAGE,
PersonGenerator.RELIGION,
PersonGenerator.MAIN_ADDRESS_STATUS
)
}

Expand All @@ -64,8 +70,14 @@ class DataLoader(
)
}

fun personData() {
entityManager.saveAll(REGISTERED_PERSON, RELEASED_PERSON, CUSTODY_PERSON, DETAILED_PERSON)
entityManager.flush()
entityManager.persist(PersonGenerator.DETAIL_ADDRESS)
}

fun registrationData() {
val person = REGISTERED_PERSON
val person = REGISTERED_PERSON.asPerson()
val registration1 = RegistrationGenerator.generate(person)
val review1 = RegistrationGenerator.generateReview(registration1)
val registration2 = RegistrationGenerator.generate(
Expand All @@ -77,7 +89,7 @@ class DataLoader(
createdDateTime = ZonedDateTime.now().minusHours(1)
)
val review2 = RegistrationGenerator.generateReview(registration2)
entityManager.saveAll(person, registration1, review1, registration2, review2)
entityManager.saveAll(registration1, review1, registration2, review2)
}

fun custodialData() {
Expand All @@ -86,9 +98,7 @@ class DataLoader(
SentenceGenerator.DEFAULT_INSTITUTION,
SentenceGenerator.CUSTODY_STATUS,
SentenceGenerator.RELEASE_TYPE,
SentenceGenerator.RECALL_REASON,
PersonGenerator.CUSTODY_PERSON,
PersonGenerator.RELEASED_PERSON
SentenceGenerator.RECALL_REASON
)
persistCustody(SentenceGenerator.CUSTODIAL_SENTENCE)
persistCustody(SentenceGenerator.RELEASED_SENTENCE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,130 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.integration.delius.person.entity.Person
import uk.gov.justice.digital.hmpps.integration.delius.person.entity.PersonAddress
import uk.gov.justice.digital.hmpps.integration.delius.person.entity.PersonDetail
import uk.gov.justice.digital.hmpps.integration.delius.reference.entity.ReferenceData
import java.time.LocalDate

object PersonGenerator {
val REGISTERED_PERSON = generate("R123456")
val RELEASED_PERSON = generate("B123456")
val CUSTODY_PERSON = generate("C123456")
val REGISTERED_PERSON =
generate("R123456", firstName = "Reginald", surname = "Regency", dob = LocalDate.now().minusYears(27))
val RELEASED_PERSON =
generate("B123456", firstName = "Barry", surname = "Brown", dob = LocalDate.now().minusYears(39))
val CUSTODY_PERSON =
generate("C123456", firstName = "Charles", surname = "Chaplin", dob = LocalDate.now().minusYears(42))

val GENDER = ReferenceDataGenerator.generate("GEN", "Gender")
val ETHNICITY = ReferenceDataGenerator.generate("ETH", "Ethnicity")
val LANGUAGE = ReferenceDataGenerator.generate("LAN", "Language")
val RELIGION = ReferenceDataGenerator.generate("REL", "Religion")

val MAIN_ADDRESS_STATUS = ReferenceDataGenerator.generate(PersonAddress.MAIN_STATUS_CODE, "Main Address")

val DETAILED_PERSON =
generate(
crn = "D123456",
firstName = "Daniel",
secondName = "David",
surname = "Danube",
dob = LocalDate.now().minusYears(36),
noms = "D1234YZ",
pnc = "2011/0593710D",
cro = "89861/11W",
gender = GENDER,
ethnicity = ETHNICITY,
language = LANGUAGE,
religion = RELIGION,
emailAddress = "[email protected]",
telephoneNumber = "0191 256 7234",
mobileNumber = "07345617263"
)

val DETAIL_ADDRESS = generateAddress(
DETAILED_PERSON.id,
MAIN_ADDRESS_STATUS,
addressNumber = "23",
streetName = "Mantle Place",
postcode = "H34 7TH"
)

fun generate(
crn: String,
noms: String? = null,
pnc: String? = null,
cro: String? = null,
firstName: String,
secondName: String? = null,
thirdName: String? = null,
surname: String,
dob: LocalDate,
telephoneNumber: String? = null,
mobileNumber: String? = null,
emailAddress: String? = null,
gender: ReferenceData? = null,
ethnicity: ReferenceData? = null,
language: ReferenceData? = null,
religion: ReferenceData? = null,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = PersonDetail(
crn,
noms,
pnc,
cro,
firstName,
secondName,
thirdName,
surname,
dob,
telephoneNumber,
mobileNumber,
emailAddress,
gender,
ethnicity,
language,
religion,
softDeleted,
id
)

fun generatePerson(
crn: String,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = Person(crn, softDeleted, id)
}

fun generateAddress(
personId: Long,
status: ReferenceData,
noFixedAbode: Boolean = false,
buildingName: String? = null,
addressNumber: String? = null,
streetName: String? = null,
district: String? = null,
town: String? = null,
county: String? = null,
postcode: String? = null,
startDate: LocalDate = LocalDate.now().minusDays(1),
endDate: LocalDate? = null,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = PersonAddress(
personId,
status,
buildingName,
addressNumber,
streetName,
district,
town,
county,
postcode,
noFixedAbode,
startDate,
endDate,
softDeleted,
id
)
}

fun PersonDetail.asPerson() = Person(crn, softDeleted, id)
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ object SentenceGenerator {
val CUSTODY_STATUS = ReferenceDataGenerator.generate("C")
val RELEASE_TYPE = ReferenceDataGenerator.generate("REL")
val RECALL_REASON = generateRecallReason("REC")
val CUSTODIAL_SENTENCE = generateCustodialSentence(PersonGenerator.CUSTODY_PERSON)
val RELEASED_SENTENCE = generateCustodialSentence(PersonGenerator.RELEASED_PERSON)
val CUSTODIAL_SENTENCE = generateCustodialSentence(PersonGenerator.CUSTODY_PERSON.asPerson())
val RELEASED_SENTENCE = generateCustodialSentence(PersonGenerator.RELEASED_PERSON.asPerson())
val RELEASE =
generateRelease(RELEASED_SENTENCE, date = ZonedDateTime.now().minusDays(1), institution = DEFAULT_INSTITUTION)
val RECALL = generateRecall(RELEASE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import uk.gov.justice.digital.hmpps.api.model.*
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.SentenceGenerator
import uk.gov.justice.digital.hmpps.integration.delius.person.entity.Person
import uk.gov.justice.digital.hmpps.integration.delius.person.entity.PersonDetail
import uk.gov.justice.digital.hmpps.service.codeDescription
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken
Expand Down Expand Up @@ -74,7 +75,7 @@ internal class IntegrationTest {

@ParameterizedTest
@MethodSource("custodialSentences")
fun `releases are correctly returned`(person: Person, releaseRecall: ReleaseRecall) {
fun `releases are correctly returned`(person: PersonDetail, releaseRecall: ReleaseRecall) {
val res = mockMvc
.perform(get("/probation-cases/${person.crn}/release").withToken())
.andExpect(status().is2xxSuccessful)
Expand All @@ -83,6 +84,77 @@ internal class IntegrationTest {
assertThat(res, equalTo(releaseRecall))
}

@Test
fun `should retrieve case details`() {
val person = PersonGenerator.DETAILED_PERSON
val address = PersonGenerator.DETAIL_ADDRESS
val res = mockMvc
.perform(get("/probation-cases/${person.crn}").withToken())
.andExpect(status().is2xxSuccessful)
.andReturn().response.contentAsJson<CaseDetails>()

assertThat(
res, equalTo(
CaseDetails(
Identifiers(person.crn, person.noms, person.pnc, person.cro),
Person(
Name(
person.surname,
person.firstName,
listOf(person.secondName!!)
),
person.dob,
PersonGenerator.GENDER.codeDescription()
),
Profile(
PersonGenerator.LANGUAGE.codeDescription(),
PersonGenerator.ETHNICITY.codeDescription(),
PersonGenerator.RELIGION.codeDescription()
),
ContactDetails(
Address.from(
noFixedAbode = address.noFixedAbode,
buildingNumber = address.addressNumber,
streetName = address.streetName,
postcode = address.postcode
),
person.emailAddress,
person.telephoneNumber,
person.mobileNumber
)
)
)
)
}

@Test
fun `should retrieve minimal case details`() {
val person = PersonGenerator.REGISTERED_PERSON
val res = mockMvc
.perform(get("/probation-cases/${person.crn}").withToken())
.andExpect(status().is2xxSuccessful)
.andReturn().response.contentAsJson<CaseDetails>()

assertThat(
res, equalTo(
CaseDetails(
Identifiers(person.crn, person.noms, person.pnc, person.cro),
Person(
Name(
person.surname,
person.firstName,
listOf()
),
person.dob,
null
),
Profile.from(null, null, null),
ContactDetails.from(null, null, null)
)
)
)
}

companion object {
private val institution = SentenceGenerator.DEFAULT_INSTITUTION

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.justice.digital.hmpps.api

import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.*
import uk.gov.justice.digital.hmpps.service.ProbationCaseService

@RestController
@RequestMapping("probation-cases/{crn}")
class ProbationCaseResource(private val probationCaseService: ProbationCaseService) {
@PreAuthorize("hasRole('PROBATION_API__OASYS__CASE_DETAIL')")
@GetMapping
fun getCaseDetails(@PathVariable("crn") crn: String) =
probationCaseService.findCase(crn)
}
Loading

0 comments on commit aeb48b2

Please sign in to comment.