Skip to content

Commit

Permalink
PI-2673 - move test data creation back to data loader
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Jan 6, 2025
1 parent c71147d commit 40d58af
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import java.time.LocalDate
@ConditionalOnProperty("seed.database")
class DataLoader(
private val auditUserRepository: AuditUserRepository,
private val staffUserRepository: StaffUserRepository,
private val referenceDataSetRepository: ReferenceDataSetRepository,
private val referenceDataRepository: ReferenceDataRepository,
private val registerTypeRepository: RegisterTypeRepository,
Expand All @@ -37,13 +38,15 @@ class DataLoader(
private val teamRepository: TeamRepository,
private val staffRepository: StaffRepository,
private val personRepository: PersonRepository,
private val personManagerRepository: PersonManagerRepository,
private val eventRepository: EventRepository,
private val disposalRepository: DisposalRepository,
private val custodyRepository: CustodyRepository,
private val caseAllocationRepository: CaseAllocationRepository,
private val registrationRepository: RegistrationRepository,
private val keyDateRepository: KeyDateRepository,
private val contactTypeRepository: ContactTypeRepository
private val contactTypeRepository: ContactTypeRepository,
private val entityManagerDataLoader: EntityManagerDataLoader
) : ApplicationListener<ApplicationReadyEvent> {

@PostConstruct
Expand Down Expand Up @@ -75,9 +78,18 @@ class DataLoader(

districtRepository.save(ProviderGenerator.DEFAULT_DISTRICT)
teamRepository.saveAll(PersonManagerGenerator.ALL.map { it.team } + ProviderGenerator.POM_TEAM + ProviderGenerator.UNALLOCATED_TEAM)
val staffMap = entityManagerDataLoader.loadData()

staffRepository.save(ProviderGenerator.generateStaff("Test", "Test", "Test"))

UserGenerator.DEFAULT_STAFF_USER = staffUserRepository.save(
StaffUser(
UserGenerator.DEFAULT_STAFF_USER.username,
staffMap[ProviderGenerator.DEFAULT_STAFF.code],
UserGenerator.DEFAULT_STAFF_USER.id
)
)

personRepository.saveAll(
listOf(
PersonGenerator.DEFAULT,
Expand All @@ -90,6 +102,17 @@ class DataLoader(
PersonGenerator.PERSON_MULTIPLE_CUSTODIAL
)
)
personManagerRepository.saveAll(
PersonManagerGenerator.ALL.map {
PersonManagerGenerator.generate(
team = it.team,
staff = staffMap[it.staff.code]!!,
person = it.person,
active = it.active,
softDeleted = it.softDeleted
)
}
)

eventRepository.saveAll(CaseAllocationGenerator.ALL.map { it.event })
disposalRepository.saveAll(CaseAllocationGenerator.ALL.map { it.event.disposal })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package uk.gov.justice.digital.hmpps.data

import jakarta.persistence.EntityManager
import jakarta.persistence.PersistenceContext
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import uk.gov.justice.digital.hmpps.data.generator.PersonManagerGenerator
import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.Staff

@Component
class EntityManagerDataLoader{

@PersistenceContext
private lateinit var entityManager: EntityManager

@Transactional
fun loadData():Map<String, Staff> {

val staffMap = (PersonManagerGenerator.ALL.map { it.staff } + ProviderGenerator.UNALLOCATED_STAFF).associateBy { it.code }

val savedStaffMap = staffMap.map {
entityManager.merge(it.value)
}.associateBy { it.code }

return savedStaffMap

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import uk.gov.justice.digital.hmpps.entity.PrisonStaff
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactRepository
import uk.gov.justice.digital.hmpps.integrations.delius.contact.entity.ContactType
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.PrisonManagerRepository
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.StaffRepository
import uk.gov.justice.digital.hmpps.messaging.HmppsChannelManager
import uk.gov.justice.digital.hmpps.repository.PrisonStaffRepository
import uk.gov.justice.digital.hmpps.resourceloader.ResourceLoader.notification
Expand Down Expand Up @@ -58,9 +57,6 @@ internal class AllocationMessagingIntegrationTest {
@Autowired
lateinit var contactRepository: ContactRepository

@Autowired
lateinit var staffRepo: StaffRepository

@Order(1)
@Test
fun `no change if not yet ready to allocate`() {
Expand Down Expand Up @@ -173,8 +169,6 @@ internal class AllocationMessagingIntegrationTest {
@Order(4)
@Test
fun `deallocate POM successfully`() {
staffRepo.save(ProviderGenerator.UNALLOCATED_STAFF)

val existingPom =
prisonManagerRepository.findActiveManagerAtDate(PersonGenerator.DEFAULT.id, ZonedDateTime.now())!!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ 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.*
import uk.gov.justice.digital.hmpps.data.PersonManagerRepository
import uk.gov.justice.digital.hmpps.data.StaffUserRepository
import uk.gov.justice.digital.hmpps.data.generator.*
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.ProviderGenerator
import uk.gov.justice.digital.hmpps.data.generator.ReferenceDataGenerator
import uk.gov.justice.digital.hmpps.integrations.delius.person.entity.Person
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.StaffRepository
import uk.gov.justice.digital.hmpps.integrations.delius.provider.entity.StaffUser
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.contentAsJson
import uk.gov.justice.digital.hmpps.test.MockMvcExtensions.withToken

Expand All @@ -29,52 +27,9 @@ internal class ApiIntegrationTest {
@Autowired
lateinit var mockMvc: MockMvc

@Autowired
lateinit var staffRepository: StaffRepository

@Autowired
lateinit var staffUserRepository: StaffUserRepository

@Autowired
lateinit var personManagerRepository: PersonManagerRepository

fun loadTestData() {
val staffMap = staffRepository.saveAll(
PersonManagerGenerator.ALL.map { it.staff } +
ProviderGenerator.UNALLOCATED_STAFF).associateBy { it.code }

UserGenerator.DEFAULT_STAFF_USER = staffUserRepository.save(
StaffUser(
UserGenerator.DEFAULT_STAFF_USER.username,
staffMap[ProviderGenerator.DEFAULT_STAFF.code],
UserGenerator.DEFAULT_STAFF_USER.id
)
)

personManagerRepository.saveAll(
PersonManagerGenerator.ALL.map {
PersonManagerGenerator.generate(
team = it.team,
staff = staffMap[it.staff.code]!!,
person = it.person,
active = it.active,
softDeleted = it.softDeleted
)
}
)
}

@ParameterizedTest
@MethodSource("caseIdentifiers")
fun `successful retrieval of a case record by crn or noms id`(
identifier: String,
person: Person,
loadData: Boolean
) {
if (loadData) {
loadTestData()
}

fun `successful retrieval of a case record by crn or noms id`(identifier: String, person: Person) {
val record = mockMvc
.perform(get("/case-records/$identifier").withToken())
.andExpect(status().is2xxSuccessful)
Expand Down Expand Up @@ -111,8 +66,8 @@ internal class ApiIntegrationTest {
companion object {
@JvmStatic
fun caseIdentifiers() = listOf(
Arguments.of(PersonGenerator.DEFAULT.crn, PersonGenerator.DEFAULT, true),
Arguments.of(PersonGenerator.DEFAULT.nomsId, PersonGenerator.DEFAULT, false)
Arguments.of(PersonGenerator.DEFAULT.crn, PersonGenerator.DEFAULT),
Arguments.of(PersonGenerator.DEFAULT.nomsId, PersonGenerator.DEFAULT)
)
}
}

0 comments on commit 40d58af

Please sign in to comment.