Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* PI-2005 initial person detail

* PI-2005 add aliases and addresses
  • Loading branch information
anthony-britton-moj authored Mar 21, 2024
1 parent 4d876af commit 13e1f4b
Show file tree
Hide file tree
Showing 14 changed files with 545 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
package uk.gov.justice.digital.hmpps.data

import jakarta.annotation.PostConstruct
import jakarta.persistence.EntityManager
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
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.UserGenerator
import uk.gov.justice.digital.hmpps.user.AuditUserRepository

@Component
@ConditionalOnProperty("seed.database")
class DataLoader(
private val auditUserRepository: AuditUserRepository
private val auditUserRepository: AuditUserRepository,
private val entityManager: EntityManager
) : ApplicationListener<ApplicationReadyEvent> {

@PostConstruct
fun saveAuditUser() {
auditUserRepository.save(UserGenerator.AUDIT_USER)
}

@Transactional
override fun onApplicationEvent(are: ApplicationReadyEvent) {
// Perform dev/test database setup here, using JPA repositories and generator classes...
saveAll(
PersonGenerator.TITLE,
PersonGenerator.GENDER,
PersonGenerator.ETHNICITY,
PersonGenerator.NATIONALITY,
PersonGenerator.MAIN_ADDRESS,
PersonGenerator.MIN_PERSON,
PersonGenerator.FULL_PERSON,
*PersonGenerator.FULL_PERSON_ALIASES.toTypedArray(),
*PersonGenerator.FULL_PERSON_ADDRESSES.toTypedArray()
)
}

private fun saveAll(vararg entities: Any) = entities.forEach(entityManager::merge)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package uk.gov.justice.digital.hmpps.data.generator

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

object PersonGenerator {
val ETHNICITY = generateReferenceData("ETH")
val GENDER = generateReferenceData("GEN")
val NATIONALITY = generateReferenceData("NAT")
val TITLE = generateReferenceData("TIT")
val MAIN_ADDRESS = generateReferenceData("M", "Main Address")

val MIN_PERSON =
generatePerson("M123456", firstname = "Isabelle", surname = "Necessary", dob = LocalDate.of(1990, 3, 5))
val FULL_PERSON = generatePerson(
"F123456",
"A3349EX",
"2011/0593710D",
"89861/11W",
"FJ123456W",
"94600E",
"Frederick",
"Paul",
"Bernard",
"Johnson",
LocalDate.of(1975, 7, 15),
"No Previous",
"Freddy",
"0191 755 4789",
"07895746789",
"[email protected]",
TITLE,
GENDER,
NATIONALITY,
ETHNICITY,
"Description of ethnicity"
)

val FULL_PERSON_ALIASES = listOf(
generateAlias(
FULL_PERSON.id, "Freddy", null, null, "Banter", LocalDate.of(1974, 2, 17)
)
)

val FULL_PERSON_ADDRESSES = listOf(
generateAddress(FULL_PERSON.id, MAIN_ADDRESS, "PC1 1TS", LocalDate.now().minusDays(30))
)

fun generateReferenceData(
code: String,
description: String = "Description of $code",
id: Long = IdGenerator.getAndIncrement()
) = ReferenceData(code, description, id)

fun generatePerson(
crn: String,
nomsId: String? = null,
pnc: String? = null,
cro: String? = null,
niNumber: String? = null,
prisonerNumber: String? = null,
firstname: String,
secondName: String? = null,
thirdName: String? = null,
surname: String,
dob: LocalDate,
previousSurname: String? = null,
preferredName: String? = null,
telephoneNumber: String? = null,
mobileNumber: String? = null,
emailAddress: String? = null,
title: ReferenceData? = null,
gender: ReferenceData? = null,
nationality: ReferenceData? = null,
ethnicity: ReferenceData? = null,
ethnicityDescription: String? = null,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = Person(
crn,
nomsId,
pnc,
cro,
niNumber,
prisonerNumber,
firstname,
secondName,
thirdName,
surname,
dob,
previousSurname,
preferredName,
telephoneNumber,
mobileNumber,
emailAddress,
title,
gender,
nationality,
ethnicity,
ethnicityDescription,
softDeleted,
id
)

fun generateAlias(
personId: Long,
firstName: String,
secondName: String?,
thirdName: String?,
surname: String,
dateOfBirth: LocalDate,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = Alias(personId, firstName, secondName, thirdName, surname, dateOfBirth, softDeleted, id)

fun generateAddress(
personId: Long,
status: ReferenceData,
postcode: String,
startDate: LocalDate,
endDate: LocalDate? = null,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = PersonAddress(personId, status, postcode, startDate, endDate, softDeleted, id)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package uk.gov.justice.digital.hmpps

import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import uk.gov.justice.digital.hmpps.api.model.PersonDetail
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.integration.delius.entity.Alias
import uk.gov.justice.digital.hmpps.integration.delius.entity.PersonAddress
import uk.gov.justice.digital.hmpps.service.asAddress
import uk.gov.justice.digital.hmpps.service.asModel
import uk.gov.justice.digital.hmpps.service.detail
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

@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = RANDOM_PORT)
internal class CorePersonIntegrationTest {
@Autowired
lateinit var mockMvc: MockMvc

@MockBean
lateinit var telemetryService: TelemetryService

@Test
fun `correctly returns detail by crn`() {
val detail = mockMvc
.perform(get("/probation-cases/${PersonGenerator.MIN_PERSON.crn}").withToken())
.andExpect(status().is2xxSuccessful)
.andReturn().response.contentAsJson<PersonDetail>()

assertThat(
detail,
equalTo(PersonGenerator.MIN_PERSON.detail(listOf(), listOf()))
)
}

@Test
fun `correctly returns detail by id`() {
val detail = mockMvc
.perform(get("/probation-cases/${PersonGenerator.FULL_PERSON.id}").withToken())
.andExpect(status().is2xxSuccessful)
.andReturn().response.contentAsJson<PersonDetail>()

assertThat(
detail,
equalTo(
PersonGenerator.FULL_PERSON.detail(
PersonGenerator.FULL_PERSON_ALIASES.map(Alias::asModel),
PersonGenerator.FULL_PERSON_ADDRESSES.mapNotNull(PersonAddress::asAddress)
)
)
)
}
}

This file was deleted.

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

import java.time.LocalDate

data class PersonDetail(
val identifiers: Identifiers,
val name: Name,
val dateOfBirth: LocalDate,
val title: CodeDescription?,
val gender: CodeDescription?,
val nationality: CodeDescription?,
val ethnicity: CodeDescription?,
val ethnicityDescription: String?,
val contactDetails: ContactDetails?,
val aliases: List<Alias>,
val addresses: List<Address>
)

data class Identifiers(
val deliusId: Long,
val crn: String,
val nomsId: String?,
val prisonerNumber: String?,
val pnc: String?,
val cro: String?,
val ni: String?
)

data class Name(
val forename: String,
val middleName: String?,
val surname: String,
val previousSurname: String? = null,
val preferred: String? = null
)

data class ContactDetails(val telephone: String?, val mobile: String?, val email: String?) {
companion object {
fun of(telephone: String?, mobile: String?, email: String?): ContactDetails? =
if (telephone == null && mobile == null && email == null) {
null
} else {
ContactDetails(telephone, mobile, email)
}
}
}

data class CodeDescription(val code: String, val description: String)

data class Alias(val name: Name, val dateOfBirth: LocalDate)

data class Address(val postcode: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package uk.gov.justice.digital.hmpps.api.resource

import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import uk.gov.justice.digital.hmpps.api.model.PersonDetail
import uk.gov.justice.digital.hmpps.service.PersonService

@RestController
@RequestMapping("probation-cases")
class PersonResource(private val personService: PersonService) {
@PreAuthorize("hasRole('PROBATION_API__CORE_PERSON__CASE_DETAIL')")
@GetMapping(value = ["/{identifier}"])
fun getPersonDetails(
@PathVariable identifier: String
): PersonDetail {
val id = identifier.toLongOrNull()
return if (id == null) {
personService.getPersonDetail(identifier)
} else {
personService.getPersonDetail(id)
}
}
}

This file was deleted.

Loading

0 comments on commit 13e1f4b

Please sign in to comment.