Skip to content

Commit

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

* update before install in format action
  • Loading branch information
anthony-britton-moj authored Feb 28, 2024
1 parent 90b09b4 commit 39552ef
Show file tree
Hide file tree
Showing 16 changed files with 510 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/actions/format-code/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ runs:
steps:
- name: Install dependencies
shell: bash
run: sudo apt-get install -y libxml2-utils
run: sudo apt update && sudo apt-get install -y libxml2-utils

- name: Check for the latest version of IntelliJ IDEA
id: latest
Expand Down
2 changes: 0 additions & 2 deletions projects/oasys-and-delius/deploy/values-dev.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
enabled: false # TODO set this to true when you're ready to deploy your service

generic-service:
ingress:
host: oasys-and-delius-dev.hmpps.service.justice.gov.uk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +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.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator.REGISTERED_PERSON
import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator.DEFAULT_PROVIDER
import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator.DEFAULT_TEAM
Expand All @@ -18,7 +19,9 @@ import uk.gov.justice.digital.hmpps.data.generator.RegistrationGenerator.CATEGOR
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 @@ -39,6 +42,7 @@ class DataLoader(
referenceData()
providerData()
registrationData()
custodialData()
}

fun referenceData() {
Expand Down Expand Up @@ -76,5 +80,24 @@ class DataLoader(
entityManager.saveAll(person, registration1, review1, registration2, review2)
}

fun custodialData() {
entityManager.saveAll(
SentenceGenerator.INSTITUTION_TYPE,
SentenceGenerator.DEFAULT_INSTITUTION,
SentenceGenerator.CUSTODY_STATUS,
SentenceGenerator.RELEASE_TYPE,
SentenceGenerator.RECALL_REASON,
PersonGenerator.CUSTODY_PERSON,
PersonGenerator.RELEASED_PERSON
)
persistCustody(SentenceGenerator.CUSTODIAL_SENTENCE)
persistCustody(SentenceGenerator.RELEASED_SENTENCE)
entityManager.saveAll(SentenceGenerator.RELEASE, SentenceGenerator.RECALL)
}

fun persistCustody(custody: Custody) {
entityManager.saveAll(custody.disposal.event, custody.disposal, custody)
}

fun EntityManager.saveAll(vararg any: Any) = any.forEach(::persist)
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.integration.delius.person.Person
import uk.gov.justice.digital.hmpps.integration.delius.person.entity.Person

object PersonGenerator {
val REGISTERED_PERSON = generate("R123456")
val RELEASED_PERSON = generate("B123456")
val CUSTODY_PERSON = generate("C123456")

fun generate(
crn: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.integration.delius.person.Person
import uk.gov.justice.digital.hmpps.integration.delius.person.entity.Person
import uk.gov.justice.digital.hmpps.integration.delius.provider.entity.Staff
import uk.gov.justice.digital.hmpps.integration.delius.provider.entity.Team
import uk.gov.justice.digital.hmpps.integration.delius.reference.entity.ReferenceData
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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.provider.entity.Institution
import uk.gov.justice.digital.hmpps.integration.delius.reference.entity.ReferenceData
import uk.gov.justice.digital.hmpps.integration.delius.sentence.entity.*
import java.time.ZonedDateTime

object SentenceGenerator {
val INSTITUTION_TYPE = ReferenceDataGenerator.generate("INST1")
val DEFAULT_INSTITUTION = generateInstitution("HMPDEF", nomisCdeCode = "DEF")
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 RELEASE =
generateRelease(RELEASED_SENTENCE, date = ZonedDateTime.now().minusDays(1), institution = DEFAULT_INSTITUTION)
val RECALL = generateRecall(RELEASE)

fun generateCustodialSentence(
person: Person,
status: ReferenceData = CUSTODY_STATUS,
institution: Institution? = DEFAULT_INSTITUTION,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = Custody(
generateDisposal(generateEvent(person)),
status,
institution,
emptyList(),
softDeleted,
id
)

fun generateDisposal(
event: Event,
active: Boolean = true,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = Disposal(event, active, softDeleted, id)

fun generateEvent(
person: Person,
active: Boolean = true,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = Event(person, null, active, softDeleted, id)

fun generateInstitution(
code: String,
description: String = "Description of $code",
type: ReferenceData? = INSTITUTION_TYPE,
name: String? = "Name of $code",
nomisCdeCode: String? = "NOM$code",
establishment: Boolean = true,
private: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = Institution(code, description, type, name, nomisCdeCode, establishment, private, id)

fun generateRelease(
custody: Custody,
type: ReferenceData = RELEASE_TYPE,
date: ZonedDateTime = ZonedDateTime.now(),
institution: Institution? = null,
notes: String? = null,
softDeleted: Boolean = false,
createdDateTime: ZonedDateTime = ZonedDateTime.now(),
id: Long = IdGenerator.getAndIncrement()
) = Release(custody, type, date, institution, notes, null, softDeleted, createdDateTime, id)

fun generateRecall(
release: Release,
date: ZonedDateTime = ZonedDateTime.now(),
reason: RecallReason = RECALL_REASON,
notes: String? = null,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = Recall(release, date, reason, notes, softDeleted, id)

fun generateRecallReason(
code: String,
description: String = "Description of $code",
id: Long = IdGenerator.getAndIncrement()
) = RecallReason(code, description, id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import org.hamcrest.Matchers.equalTo
import org.hamcrest.Matchers.hasSize
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
Expand All @@ -13,9 +16,10 @@ 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.CodeDescription
import uk.gov.justice.digital.hmpps.api.model.Registrations
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.telemetry.TelemetryService
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken
Expand Down Expand Up @@ -67,4 +71,54 @@ internal class IntegrationTest {
assertFalse(reg2.warnUser)
assertTrue(reg2.active)
}

@ParameterizedTest
@MethodSource("custodialSentences")
fun `releases are correctly returned`(person: Person, releaseRecall: ReleaseRecall) {
val res = mockMvc
.perform(get("/probation-cases/${person.crn}/release").withToken())
.andExpect(status().is2xxSuccessful)
.andReturn().response.contentAsJson<ReleaseRecall>()

assertThat(res, equalTo(releaseRecall))
}

companion object {
private val institution = SentenceGenerator.DEFAULT_INSTITUTION

@JvmStatic
fun custodialSentences() = listOf(
Arguments.of(PersonGenerator.CUSTODY_PERSON, ReleaseRecall(null, null)),
Arguments.of(
PersonGenerator.RELEASED_PERSON,
ReleaseRecall(
Release(
SentenceGenerator.RELEASE.date.toLocalDate(),
null,
Institution(
institution.id,
institution.establishment,
institution.code,
institution.description,
institution.name,
CodeDescription(
institution.type!!.code, institution.type!!.description
),
false,
institution.nomisCdeCode,
),
CodeDescription(SentenceGenerator.RELEASE_TYPE.code, SentenceGenerator.RELEASE_TYPE.description)
),
Recall(
SentenceGenerator.RECALL.date.toLocalDate(),
CodeDescription(
SentenceGenerator.RECALL_REASON.code,
SentenceGenerator.RECALL_REASON.description
),
null
)
)
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import uk.gov.justice.digital.hmpps.service.RegistrationService
class RegistrationResource(private val registrationService: RegistrationService) {
@PreAuthorize("hasRole('PROBATION_API__OASYS__CASE_DETAIL')")
@GetMapping(value = ["/registrations"])
fun handle(@PathVariable("crn") crn: String): Registrations = registrationService.findActiveRegistrations(crn)
fun getActiveRegistrations(@PathVariable("crn") crn: String): Registrations =
registrationService.findActiveRegistrations(crn)
}


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

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.service.SentenceService

@RestController
@RequestMapping("probation-cases/{crn}")
class SentenceResource(private val sentenceService: SentenceService) {
@PreAuthorize("hasRole('PROBATION_API__OASYS__CASE_DETAIL')")
@GetMapping(value = ["/release"])
fun getReleaseRecall(@PathVariable("crn") crn: String) = sentenceService.findLatestReleaseRecall(crn)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package uk.gov.justice.digital.hmpps.api.model

import java.time.LocalDate

data class ReleaseRecall(
val lastRelease: Release?,
val lastRecall: Recall?
)

data class Release(
val date: LocalDate,
val notes: String?,
val institution: Institution?,
val reason: CodeDescription
)

data class Recall(
val date: LocalDate,
val reason: CodeDescription,
val notes: String?
)

data class Institution(
val institutionId: Long,
val isEstablishment: Boolean,
val code: String,
val description: String,
val institutionName: String?,
val establishmentType: CodeDescription?,
val isPrivate: Boolean?,
val nomsPrisonInstitutionCode: String?
)

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.digital.hmpps.integration.delius.person
package uk.gov.justice.digital.hmpps.integration.delius.person.entity

import jakarta.persistence.Column
import jakarta.persistence.Entity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uk.gov.justice.digital.hmpps.integration.delius.provider.entity

import jakarta.persistence.*
import org.hibernate.annotations.Immutable
import org.hibernate.type.YesNoConverter
import uk.gov.justice.digital.hmpps.integration.delius.reference.entity.ReferenceData

@Immutable
@Entity
@Table(name = "r_institution")
class Institution(

@Column(columnDefinition = "char(6)")
val code: String,
val description: String,

@ManyToOne
@JoinColumn(name = "establishment_type_id")
val type: ReferenceData?,

@Column(name = "institution_name")
val name: String?,
val nomisCdeCode: String?,

@Convert(converter = YesNoConverter::class)
val establishment: Boolean,

@Convert(converter = YesNoConverter::class)
val private: Boolean?,

@Id
@Column(name = "institution_id")
val id: Long
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.hibernate.annotations.SQLRestriction
import org.hibernate.type.YesNoConverter
import org.springframework.data.jpa.repository.EntityGraph
import org.springframework.data.jpa.repository.JpaRepository
import uk.gov.justice.digital.hmpps.integration.delius.person.Person
import uk.gov.justice.digital.hmpps.integration.delius.person.entity.Person
import uk.gov.justice.digital.hmpps.integration.delius.provider.entity.Staff
import uk.gov.justice.digital.hmpps.integration.delius.provider.entity.Team
import uk.gov.justice.digital.hmpps.integration.delius.reference.entity.ReferenceData
Expand Down
Loading

0 comments on commit 39552ef

Please sign in to comment.