Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj committed Nov 9, 2023
1 parent ba1aa4a commit c318511
Show file tree
Hide file tree
Showing 14 changed files with 294 additions and 61 deletions.
2 changes: 0 additions & 2 deletions projects/prisoner-profile-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: prisoner-profile-and-delius-dev.hmpps.service.justice.gov.uk
Expand Down
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: prisoner-profile-and-delius-preprod.hmpps.service.justice.gov.uk
Expand Down
2 changes: 0 additions & 2 deletions projects/prisoner-profile-and-delius/deploy/values-prod.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: prisoner-profile-and-delius.hmpps.service.justice.gov.uk
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
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.CommunityManagerGenerator.ALLOCATED_PERSON
import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.STAFF
import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.TEAM
import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.UNALLOCATED_PERSON
import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.UNALLOCATED_STAFF
import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.generateCommunityManager
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...
listOf(
TEAM,
UNALLOCATED_STAFF,
STAFF,
UNALLOCATED_PERSON,
ALLOCATED_PERSON,
generateCommunityManager(
ALLOCATED_PERSON,
STAFF
),
generateCommunityManager(
UNALLOCATED_PERSON,
UNALLOCATED_STAFF
)
).saveAll()
}

fun List<Any>.saveAll() = forEach { entityManager.persist(it) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package uk.gov.justice.digital.hmpps.data.generator

import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.CommunityManager
import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.Staff
import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.Team

object CommunityManagerGenerator {
val TEAM = generateTeam("N01PPT")
val UNALLOCATED_STAFF = generateStaff("N01PPTU", "Unallocated", "Staff")
val STAFF = generateStaff("N01PPTA", "James", "Brown")
val ALLOCATED_PERSON = generatePerson("A123456", "A1234TD")
val UNALLOCATED_PERSON = generatePerson("U123456", "U1234TD")

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

fun generateStaff(code: String, forename: String, surname: String, id: Long = IdGenerator.getAndIncrement()) =
Staff(code, forename, surname, id)

fun generatePerson(
crn: String,
nomsId: String,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = Person(crn, nomsId, softDeleted, id)

fun generateCommunityManager(
person: Person,
staff: Staff,
team: Team = TEAM,
active: Boolean = true,
softDeleted: Boolean = false,
id: Long = IdGenerator.getAndIncrement()
) = CommunityManager(person, team, staff, active, softDeleted, id)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDo3hw1/oChbttEOxEH4NUDrH+Y
n2x0DavAmDjMbhcSiQ6+/t8Nz/N03BauWzFOGBtftnQrHfnF+O7RAKj8zMjcbIq4
QrYeXEpnaFCGEwTtOBpxvSEWPrLEpr1gCarBQZDp67ag+SYqrDgkn2Vme/dMvMUQ
xUO3DT6jg9921J6TlwIDAQAB
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDju2wS/xXRfjdRI9thM2yJ7xO4
cy5o8eJ1tljIrqJvKcdsmuflNv7IXGZP4OIQBhALkS3CQxIhBl9gNAtyu2AdYUdn
bSlBS5qLtLg2EoPn2GyGSwRiZCmZcmDsHdn8DX0zNCgZN9i/B/yCL0gXgvHRGEqP
QCtNM3Urqkvn97LVjQIDAQAB
-----END PUBLIC KEY-----
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJwcm9iYXRpb24taW50ZWdyYXRpb24tZGV2IiwiZ3JhbnRfdHlwZSI6ImNsaWVudF9jcmVkZW50aWFscyIsInVzZXJfbmFtZSI6InByb2JhdGlvbi1pbnRlZ3JhdGlvbi1kZXYiLCJzY29wZSI6WyJyZWFkIiwid3JpdGUiXSwiYXV0aF9zb3VyY2UiOiJub25lIiwiaXNzIjoiaHR0cHM6Ly9zaWduLWluLWRldi5obXBwcy5zZXJ2aWNlLmp1c3RpY2UuZ292LnVrL2F1dGgvaXNzdWVyIiwiZXhwIjo5OTk5OTk5OTk5LCJhdXRob3JpdGllcyI6WyJST0xFX0VYQU1QTEUiLCJST0xFX1dPUktMT0FEX1JFQUQiLCJST0xFX0FMTE9DQVRJ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er",
"expires_in": 9999999999,
"scope": "read write",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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.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
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
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.Manager
import uk.gov.justice.digital.hmpps.api.Team
import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.ALLOCATED_PERSON
import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.STAFF
import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.TEAM
import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.UNALLOCATED_PERSON
import uk.gov.justice.digital.hmpps.data.generator.CommunityManagerGenerator.UNALLOCATED_STAFF
import uk.gov.justice.digital.hmpps.integrations.delius.manager.name
import uk.gov.justice.digital.hmpps.security.withOAuth2Token

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

@Autowired
lateinit var wireMockServer: WireMockServer

@Autowired
lateinit var objectMapper: ObjectMapper

@ParameterizedTest
@MethodSource("communityManagers")
fun `returns the community manager correctly`(nomsId: String, expected: Manager) {
val res = mockMvc
.perform(
get("/probation-cases/$nomsId/community-manager")
.withOAuth2Token(wireMockServer)
)
.andExpect(status().is2xxSuccessful)
.andReturn().response.contentAsString

val actual = objectMapper.readValue<Manager>(res)
assertThat(actual, equalTo(expected))
}

@Test
fun `noms id not found throws 404`() {
mockMvc
.perform(
get("/probation-cases/invalid/community-manager")
.withOAuth2Token(wireMockServer)
)
.andExpect(status().isNotFound)
}

companion object {
@JvmStatic
fun communityManagers() = listOf(
Arguments.of(ALLOCATED_PERSON.nomsId, Manager(STAFF.code, STAFF.name(), Team(TEAM.code, TEAM.description))),
Arguments.of(
UNALLOCATED_PERSON.nomsId,
Manager(UNALLOCATED_STAFF.code, UNALLOCATED_STAFF.name(), Team(TEAM.code, TEAM.description))
)
)
}
}

This file was deleted.

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

data class Manager(
val code: String,
val name: Name,
val team: Team
) {
val unallocated = code.endsWith("U")
}

data class Name(val forename: String, val surname: String)

data class Team(val code: String, val description: String)
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.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import uk.gov.justice.digital.hmpps.integrations.delius.manager.ManagerService

@RestController
@RequestMapping("probation-cases/{nomsId}")
class ManagerResource(private val managerService: ManagerService) {
@PreAuthorize("hasRole('PROBATION_API__PRISONER_PROFILE__CASE_DETAIL')")
@RequestMapping("community-manager", method = [RequestMethod.GET])
fun findCommunityManager(@PathVariable nomsId: String): Manager = managerService.findCommunityManager(nomsId)
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package uk.gov.justice.digital.hmpps.integrations.delius.manager

import org.springframework.stereotype.Service
import uk.gov.justice.digital.hmpps.api.Manager
import uk.gov.justice.digital.hmpps.api.Name
import uk.gov.justice.digital.hmpps.api.Team
import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.CommunityManager
import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.CommunityManagerRepository
import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.Staff
import uk.gov.justice.digital.hmpps.integrations.delius.manager.entity.getByNomsId

@Service
class ManagerService(private val communityManagerRepository: CommunityManagerRepository) {
fun findCommunityManager(nomsId: String) = communityManagerRepository.getByNomsId(nomsId).asManager()
}

fun CommunityManager.asManager() = Manager(staff.code, staff.name(), Team(team.code, team.description))
fun Staff.name() = Name(forename, surname)
Loading

0 comments on commit c318511

Please sign in to comment.