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.
PI-1473 add detail url for probation-case.engagement.created
- Loading branch information
1 parent
4a36d35
commit 93a4dce
Showing
12 changed files
with
215 additions
and
3 deletions.
There are no files selected for viewing
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
11 changes: 9 additions & 2 deletions
11
...s/domain-events-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,22 +1,29 @@ | ||
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 em: EntityManager | ||
) : ApplicationListener<ApplicationReadyEvent> { | ||
@PostConstruct | ||
fun saveAuditUser() { | ||
auditUserRepository.save(UserGenerator.AUDIT_USER) | ||
} | ||
|
||
override fun onApplicationEvent(event: ApplicationReadyEvent) {} | ||
@Transactional | ||
override fun onApplicationEvent(event: ApplicationReadyEvent) { | ||
em.persist(PersonGenerator.ENGAGEMENT_CREATED) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...-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,21 @@ | ||
package uk.gov.justice.digital.hmpps.data.generator | ||
|
||
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person | ||
import java.time.LocalDate | ||
|
||
object PersonGenerator { | ||
val ENGAGEMENT_CREATED = | ||
generate("X789654", "Bernard", "Shepherd", LocalDate.of(1977, 10, 9), pnc = "1977/9999748M") | ||
|
||
fun generate( | ||
crn: String, | ||
forename: String, | ||
surname: String, | ||
dateOfBirth: LocalDate, | ||
secondName: String? = null, | ||
thirdName: String? = null, | ||
pnc: String? = null, | ||
softDeleted: Boolean = false, | ||
id: Long = IdGenerator.getAndIncrement() | ||
) = Person(crn, pnc, forename, secondName, thirdName, surname, dateOfBirth, softDeleted, id) | ||
} |
6 changes: 6 additions & 0 deletions
6
projects/domain-events-and-delius/src/dev/resources/local-public-key.pub
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,6 @@ | ||
-----BEGIN PUBLIC KEY----- | ||
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDju2wS/xXRfjdRI9thM2yJ7xO4 | ||
cy5o8eJ1tljIrqJvKcdsmuflNv7IXGZP4OIQBhALkS3CQxIhBl9gNAtyu2AdYUdn | ||
bSlBS5qLtLg2EoPn2GyGSwRiZCmZcmDsHdn8DX0zNCgZN9i/B/yCL0gXgvHRGEqP | ||
QCtNM3Urqkvn97LVjQIDAQAB | ||
-----END PUBLIC KEY----- |
10 changes: 10 additions & 0 deletions
10
...domain-events-and-delius/src/dev/resources/simulations/__files/hmpps-auth-token-body.json
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,10 @@ | ||
{ | ||
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJwcm9iYXRpb24taW50ZWdyYXRpb24tZGV2IiwiZ3JhbnRfdHlwZSI6ImNsaWVudF9jcmVkZW50aWFscyIsInVzZXJfbmFtZSI6InByb2JhdGlvbi1pbnRlZ3JhdGlvbi1kZXYiLCJzY29wZSI6WyJyZWFkIl0sImF1dGhfc291cmNlIjoibm9uZSIsImlzcyI6Imh0dHBzOi8vc2lnbi1pbi1kZXYuaG1wcHMuc2VydmljZS5qdXN0aWNlLmdvdi51ay9hdXRoL2lzc3VlciIsImV4cCI6OTk5OTk5OTk5OSwiYXV0aG9yaXRpZXMiOlsiUk9MRV9WSUVXX1BST0JBVElPTl9DQVNFX0VOR0FHRU1FTlRfQ1JFQVRFRCJdLCJqdGkiOiIyNUR1Um4xLWh5SFpld0xjZEpKeHdWTDAzS1UiLCJjbGllbnRfaWQiOiJwcm9iYXRpb24taW50ZWdyYXRpb24tZGV2IiwiaWF0IjoxNjYzNzU3MzExfQ.w-fJ016upnUJKTPCyGVuIupXRESglhmdD3cvhCyeCU67c8bOdb6gVgpjCMsCxuP-Z5EkIMjLmIe_XvxZQPuLtExgpBbfQ596JMWqco5E6r53Fp8n7BMqR54xhxVoTJRk8em8lrIwGgNnRv9Ee2zhidtxlP2f3GS1G7F48oSTPuE", | ||
"token_type": "bearer", | ||
"expires_in": 9999999999, | ||
"scope": "read write", | ||
"sub": "probation-integration-dev", | ||
"auth_source": "none", | ||
"jti": "fN29JHJy1N7gcYvqe-8B_k5T0mA", | ||
"iss": "https://sign-in-dev.hmpps.service.justice.gov.uk/auth/issuer" | ||
} |
13 changes: 13 additions & 0 deletions
13
projects/domain-events-and-delius/src/dev/resources/simulations/mappings/hmpps-auth.json
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,13 @@ | ||
{ | ||
"request": { | ||
"method": "POST", | ||
"urlPath": "/auth/oauth/token" | ||
}, | ||
"response": { | ||
"headers": { | ||
"Content-Type": "application/json" | ||
}, | ||
"status": 200, | ||
"bodyFileName": "hmpps-auth-token-body.json" | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...d-delius/src/integrationTest/kotlin/uk/gov/justice/digital/hmpps/EngagementCreatedTest.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 | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import com.github.tomakehurst.wiremock.WireMockServer | ||
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.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers | ||
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator | ||
import uk.gov.justice.digital.hmpps.security.withOAuth2Token | ||
import uk.gov.justice.digital.hmpps.service.Engagement | ||
import uk.gov.justice.digital.hmpps.service.Identifiers | ||
import uk.gov.justice.digital.hmpps.service.Name | ||
|
||
@AutoConfigureMockMvc | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
internal class EngagementCreatedTest { | ||
@Autowired | ||
internal lateinit var mockMvc: MockMvc | ||
|
||
@Autowired | ||
internal lateinit var wireMockServer: WireMockServer | ||
|
||
@Autowired | ||
internal lateinit var objectMapper: ObjectMapper | ||
|
||
@Test | ||
fun `engagement details returned from detail url`() { | ||
val person = PersonGenerator.ENGAGEMENT_CREATED | ||
val response = mockMvc | ||
.perform(get("/probation-case.engagement.created/${person.crn}").withOAuth2Token(wireMockServer)) | ||
.andExpect(MockMvcResultMatchers.status().is2xxSuccessful).andReturn().response.contentAsString | ||
|
||
val engagement = objectMapper.readValue<Engagement>(response) | ||
assertThat( | ||
engagement, | ||
equalTo( | ||
Engagement( | ||
Identifiers(person.crn, person.pnc), | ||
Name(person.forename, person.surname, listOf()), | ||
person.dateOfBirth | ||
) | ||
) | ||
) | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
...-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/api/engagement/EngagementResource.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,17 @@ | ||
package uk.gov.justice.digital.hmpps.api.engagement | ||
|
||
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.Engagement | ||
import uk.gov.justice.digital.hmpps.service.PersonService | ||
|
||
@RestController | ||
@RequestMapping | ||
class EngagementResource(private val personService: PersonService) { | ||
@PreAuthorize("hasRole('VIEW_PROBATION_CASE_ENGAGEMENT_CREATED')") | ||
@GetMapping("probation-case.engagement.created/{crn}") | ||
fun getEngagement(@PathVariable crn: String): Engagement = personService.findEngagement(crn) | ||
} |
49 changes: 49 additions & 0 deletions
49
.../src/main/kotlin/uk/gov/justice/digital/hmpps/integrations/delius/person/entity/Person.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,49 @@ | ||
package uk.gov.justice.digital.hmpps.integrations.delius.person.entity | ||
|
||
import jakarta.persistence.Column | ||
import jakarta.persistence.Entity | ||
import jakarta.persistence.Id | ||
import jakarta.persistence.Table | ||
import org.hibernate.annotations.Immutable | ||
import org.hibernate.annotations.Where | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import java.time.LocalDate | ||
|
||
@Immutable | ||
@Entity | ||
@Table(name = "offender") | ||
@Where(clause = "soft_deleted = 0") | ||
class Person( | ||
|
||
@Column(columnDefinition = "char(7)") | ||
val crn: String, | ||
|
||
@Column(name = "pnc_number", columnDefinition = "char(13)") | ||
val pnc: String?, | ||
|
||
@Column(name = "first_name") | ||
val forename: String, | ||
|
||
@Column(name = "second_name") | ||
val secondName: String?, | ||
|
||
@Column(name = "third_name") | ||
val thirdName: String?, | ||
|
||
@Column(name = "surname") | ||
val surname: String, | ||
|
||
@Column(name = "date_of_birth_date") | ||
val dateOfBirth: LocalDate, | ||
|
||
@Column(columnDefinition = "number") | ||
val softDeleted: Boolean, | ||
|
||
@Id | ||
@Column(name = "offender_id") | ||
val id: Long, | ||
) | ||
|
||
interface PersonRepository : JpaRepository<Person, Long> { | ||
fun findByCrn(crn: String): Person? | ||
} |
33 changes: 33 additions & 0 deletions
33
...n-events-and-delius/src/main/kotlin/uk/gov/justice/digital/hmpps/service/PersonService.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,33 @@ | ||
package uk.gov.justice.digital.hmpps.service | ||
|
||
import org.springframework.stereotype.Service | ||
import uk.gov.justice.digital.hmpps.exception.NotFoundException | ||
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person | ||
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.PersonRepository | ||
import java.time.LocalDate | ||
|
||
@Service | ||
class PersonService(private val personRepository: PersonRepository) { | ||
fun findEngagement(crn: String): Engagement = personRepository.findByCrn(crn)?.asEngagement() | ||
?: throw NotFoundException("Engagement", "crn", crn) | ||
} | ||
|
||
private fun Person.asEngagement() = Engagement( | ||
Identifiers(crn, pnc), | ||
Name(forename, surname, listOfNotNull(secondName, thirdName)), | ||
dateOfBirth | ||
) | ||
|
||
data class Engagement( | ||
val identifiers: Identifiers, | ||
val name: Name, | ||
val dateOfBirth: LocalDate | ||
) | ||
|
||
data class Identifiers(val crn: String, val pnc: String?) | ||
|
||
data class Name( | ||
val forename: String, | ||
val surname: String, | ||
val otherNames: List<String> | ||
) |
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