Skip to content

Commit

Permalink
PI-1579 CAS3 Referral submitted processing (#2433)
Browse files Browse the repository at this point in the history
Co-authored-by: Marcus Aspin <[email protected]>
  • Loading branch information
stevomcallister and marcus-bcl authored Oct 24, 2023
1 parent 1d27890 commit 4003bec
Show file tree
Hide file tree
Showing 31 changed files with 589 additions and 108 deletions.
3 changes: 3 additions & 0 deletions projects/cas3-and-delius/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {
implementation(project(":libs:audit"))
implementation(project(":libs:commons"))
implementation(project(":libs:messaging"))
implementation(project(":libs:oauth-client"))

implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
Expand All @@ -15,6 +16,8 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation(libs.openfeign)
implementation(libs.springdoc)

dev(project(":libs:dev-tools"))
dev("com.h2database:h2")
Expand Down
5 changes: 4 additions & 1 deletion projects/cas3-and-delius/deploy/database/access.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ database:
access:
username_key: /cas3-and-delius/db-username
password_key: /cas3-and-delius/db-password
tables:
- audited_interaction
- contact

audit:
username: Cas3AndDelius
forename: Probation Integration # TODO change this to something meaningful for your service
forename: Transitional Accommodation (CAS3)
surname: Service
5 changes: 3 additions & 2 deletions projects/cas3-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: cas3-and-delius-dev.hmpps.service.justice.gov.uk
Expand All @@ -9,6 +7,9 @@ generic-service:

env:
SENTRY_ENVIRONMENT: dev
SPRING_SECURITY_OAUTH2_CLIENT_PROVIDER_HMPPS-AUTH_TOKEN-URI: https://sign-in-dev.hmpps.service.justice.gov.uk/auth/oauth/token
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_JWK_SET_URI: https://sign-in-dev.hmpps.service.justice.gov.uk/auth/.well-known/jwks.json
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI: https://sign-in-dev.hmpps.service.justice.gov.uk/auth/issuer
LOGGING_LEVEL_UK_GOV_DIGITAL_JUSTICE_HMPPS: DEBUG

generic-prometheus-alerts:
Expand Down
3 changes: 3 additions & 0 deletions projects/cas3-and-delius/deploy/values-preprod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ generic-service:

env:
SENTRY_ENVIRONMENT: preprod
SPRING_SECURITY_OAUTH2_CLIENT_PROVIDER_HMPPS-AUTH_TOKEN-URI: https://sign-in-preprod.hmpps.service.justice.gov.uk/auth/oauth/token
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_JWK_SET_URI: https://sign-in-preprod.hmpps.service.justice.gov.uk/auth/.well-known/jwks.json
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI: https://sign-in-preprod.hmpps.service.justice.gov.uk/auth/issuer

generic-prometheus-alerts:
businessHoursOnly: true
3 changes: 3 additions & 0 deletions projects/cas3-and-delius/deploy/values-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ generic-service:

env:
SENTRY_ENVIRONMENT: prod
SPRING_SECURITY_OAUTH2_CLIENT_PROVIDER_HMPPS-AUTH_TOKEN-URI: https://sign-in.hmpps.service.justice.gov.uk/auth/oauth/token
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_JWK_SET_URI: https://sign-in.hmpps.service.justice.gov.uk/auth/.well-known/jwks.json
SPRING_SECURITY_OAUTH2_RESOURCESERVER_JWT_ISSUER_URI: https://sign-in.hmpps.service.justice.gov.uk/auth/issuer
4 changes: 3 additions & 1 deletion projects/cas3-and-delius/deploy/values.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Common values
generic-service:
nameOverride: cas3-and-delius
serviceAccountName: cas3-and-delius
Expand All @@ -12,6 +11,9 @@ generic-service:
namespace_secrets:
common:
SPRING_DATASOURCE_URL: DB_URL
cas3-and-delius-client-credentials:
OAUTH2_CLIENT-ID: CLIENT_ID
OAUTH2_CLIENT-SECRET: CLIENT_SECRET
cas3-and-delius-database:
SPRING_DATASOURCE_USERNAME: DB_USERNAME
SPRING_DATASOURCE_PASSWORD: DB_PASSWORD
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
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.BusinessInteractionGenerator
import uk.gov.justice.digital.hmpps.data.generator.ContactTypeGenerator
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)
}

@Transactional
override fun onApplicationEvent(are: ApplicationReadyEvent) {
// Perform dev/test database setup here, using JPA repositories and generator classes...
em.saveAll(
BusinessInteractionGenerator.UPDATE_CONTACT,
ContactTypeGenerator.CONTACT_TYPE,
PersonGenerator.PERSON_CRN,
PersonGenerator.generatePersonManager(PersonGenerator.PERSON_CRN)
)
}

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

import uk.gov.justice.digital.hmpps.audit.BusinessInteraction
import uk.gov.justice.digital.hmpps.integrations.delius.audit.BusinessInteractionCode
import java.time.ZonedDateTime

object BusinessInteractionGenerator {
val UPDATE_CONTACT = BusinessInteraction(
IdGenerator.getAndIncrement(),
BusinessInteractionCode.UPDATE_CONTACT.code,
ZonedDateTime.now().minusMonths(6)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.integrations.delius.entity.ContactType

object ContactTypeGenerator {
val CONTACT_TYPE = ContactType(
IdGenerator.getAndIncrement(),
"EARS",
false
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import uk.gov.justice.digital.hmpps.message.HmppsDomainEvent
import uk.gov.justice.digital.hmpps.resourceloader.ResourceLoader

object MessageGenerator {
val EXAMPLE = ResourceLoader.message<HmppsDomainEvent>("example-message")
val REFERRAL_SUBMITTED = ResourceLoader.message<HmppsDomainEvent>("referral-submitted")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.integrations.delius.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.entity.PersonManager

object PersonGenerator {
val PERSON_CRN = generate("A000001", null)

fun generate(crn: String, noms: String?, softDeleted: Boolean = false, id: Long = IdGenerator.getAndIncrement()) =
Person(crn, noms, softDeleted, id)

fun generatePersonManager(person: Person) =
PersonManager(IdGenerator.getAndIncrement(), person.id, 1, 1, 1, 1)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCc2Dlk/NOvXrjSs83W+Nj4bMfz
6svg2ulfcqTcEoA/Vy6FWK3pOX50VZph3V0Sbh8OTRyHgTw3BRQKrxE/TdsUWw8A
QxgbjBWypmm6I/gUGeiSgYwATZpdVbqmuNI0BRg5l/vgJki6K5cg+4fRazZXaHvN
ldzA6bUDdyt73u7qSwIDAQAB
-----END PUBLIC KEY-----

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"eventType": "accommodation.cas3.referral.submitted",
"version": 1,
"description": "A cas3 referral has been submitted",
"detailUrl": "http://localhost:{wiremock.port}/cas3-api/events/referral-submitted/1234",
"occurredAt": "2022-12-04T10:42:43+00:00",
"additionalInformation": {
"applicationId": "68df9f6c-3fcb-4ec6-8fcf-96551cd9b080"
},
"personReference": {
"identifiers": [
{
"type": "CRN",
"value": "A000001"
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"id": "364145f9-0af8-488e-9901-b4c46cd9ba37",
"timestamp": "2022-11-30T14:53:44",
"eventType": "accommodation.cas3.referral.submitted",
"eventDetails": {
"applicationId": "68df9f6c-3fcb-4ec6-8fcf-96551cd9b080",
"applicationUrl": "https://approved-premises-dev.hmpps.service.justice.gov.uk/application/68df9f6c-3fcb-4ec6-8fcf-96551cd9b080",
"personReference": {
"crn": "A000001",
"noms": "A0001AA"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJwcm9iYXRpb24taW50ZWdyYXRpb24tZGV2IiwiZ3JhbnRfdHlwZSI6ImNsaWVudF9jcmVkZW50aWFscyIsInVzZXJfbmFtZSI6InByb2JhdGlvbi1pbnRlZ3JhdGlvbi1kZXYiLCJzY29wZSI6WyJyZWFkIiwid3JpdGUiXSwiYXV0aF9zb3VyY2UiOiJub25lIiwiaXNzIjoiaHR0cHM6Ly9zaWduLWluLWRldi5obXBwcy5zZXJ2aWNlLmp1c3RpY2UuZ292LnVrL2F1dGgvaXNzdWVyIiwiZXhwIjo5OTk5OTk5OTk5LCJhdXRob3JpdGllcyI6WyJST0xFX0FQUFJPVkVEX1BSRU1JU0VTX1NUQUZGIl0sImp0aSI6IjI1RHVSbjEtaHlIWmV3TGNkSkp4d1ZMMDNLVSIsImNsaWVudF9pZCI6InByb2JhdGlvbi1pbnRlZ3JhdGlvbi1kZXYiLCJpYXQiOjE2NjM3NTczMTF9.HmAw0LBKPSHHyDh1egCb1i2ubjzDQ9x43XKDt-Qg09GsS7RuroBUm2BmRoCXPIapSve-BaUBWGa_pPopsaX6VBlzHBOWZPu68HaCkzBa82fwvyVPI3s88eJBUemEOZWQ0RmCx8KiPjK53-rZEhx_aEMJSQoHJIrFK86TjLwphk4",
"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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"mappings": [
{
"request": {
"method": "GET",
"urlPath": "/cas3-api/events/referral-submitted/1234"
},
"response": {
"headers": {
"Content-Type": "application/json"
},
"status": 200,
"bodyFileName": "cas3-referral-submitted.json"
}
}
]
}
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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package uk.gov.justice.digital.hmpps

import com.github.tomakehurst.wiremock.WireMockServer
import org.hamcrest.MatcherAssert
import org.hamcrest.Matchers
import org.junit.jupiter.api.MethodOrderer
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestMethodOrder
import org.mockito.Mockito
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
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 uk.gov.justice.digital.hmpps.integrations.delius.entity.ContactRepository
import uk.gov.justice.digital.hmpps.messaging.HmppsChannelManager
import uk.gov.justice.digital.hmpps.telemetry.TelemetryService
import uk.gov.justice.digital.hmpps.telemetry.notificationReceived

@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
internal class CASIntegrationTest {
@Value("\${messaging.consumer.queue}")
lateinit var queueName: String

@Autowired
lateinit var channelManager: HmppsChannelManager

@Autowired
lateinit var wireMockServer: WireMockServer

@Autowired
lateinit var contactRepository: ContactRepository

@MockBean
lateinit var telemetryService: TelemetryService

@Test
fun `message is processed correctly`() {
// Given an application-submitted event
val event = prepEvent("referral-submitted", wireMockServer.port())

// When it is received
channelManager.getChannel(queueName).publishAndWait(event)

// Then it is logged to telemetry
Mockito.verify(telemetryService).notificationReceived(event)

val contact =
contactRepository.getByExternalReference(event.message.additionalInformation["applicationId"] as String)

MatcherAssert.assertThat(contact!!.type.code, Matchers.equalTo("EARS"))
}
}

This file was deleted.

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

import org.springframework.cloud.openfeign.EnableFeignClients
import org.springframework.context.annotation.Configuration
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager
import uk.gov.justice.digital.hmpps.config.feign.FeignConfig
import uk.gov.justice.digital.hmpps.integrations.approvedpremesis.Cas3ApiClient

@Configuration
@EnableFeignClients(clients = [Cas3ApiClient::class])
class FeignOAuth2Config(
authorizedClientManager: OAuth2AuthorizedClientManager
) : FeignConfig(authorizedClientManager) {
override fun registrationId() = "cas3-and-delius"
}
Loading

0 comments on commit 4003bec

Please sign in to comment.