generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d876af
commit 13e1f4b
Showing
14 changed files
with
545 additions
and
52 deletions.
There are no files selected for viewing
21 changes: 19 additions & 2 deletions
21
...e-person-record-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/DataLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
128 changes: 128 additions & 0 deletions
128
...-and-delius/src/dev/kotlin/uk/gov/justice/digital/hmpps/data/generator/PersonGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
64 changes: 64 additions & 0 deletions
64
...lius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/CorePersonIntegrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
) | ||
) | ||
} | ||
} |
30 changes: 0 additions & 30 deletions
30
...ord-and-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/IntegrationTest.kt
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
...-record-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/model/PersonDetail.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
26 changes: 26 additions & 0 deletions
26
...rd-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/resource/PersonResource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
...ecord-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/controller/ApiController.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.