Skip to content

Commit

Permalink
PI-2673 - move test data creation back to data loader. Use entityMana…
Browse files Browse the repository at this point in the history
…ger.merge to resolve issue with detached entities.
  • Loading branch information
achimber-moj committed Jan 3, 2025
1 parent cf5d89d commit 97e02ee
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ class DataLoader(
private val lduRepository: LduRepository,
private val staffUserRepository: StaffUserRepository,
private val boroughRepository: BoroughRepository,
private val referralBookingDataLoader: ReferralBookingDataLoader,
private val documentDataLoader: DocumentDataLoader,
private val entityManagerDataLoader: EntityManagerDataLoader

) : ApplicationListener<ApplicationReadyEvent> {

Expand Down Expand Up @@ -166,6 +168,7 @@ class DataLoader(
)
)

entityManagerDataLoader.loadData()
eventRepository.save(PersonGenerator.EVENT)
eventRepository.save(PersonGenerator.INACTIVE_EVENT)
registrationRepository.save(
Expand Down Expand Up @@ -211,8 +214,9 @@ class DataLoader(
)

eventRepository.save(ANOTHER_EVENT)
personRepository.save(PersonGenerator.PERSON_WITH_BOOKING)

probationCaseDataLoader.loadData()
referralBookingDataLoader.loadData()
documentDataLoader.loadData()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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.AddressGenerator
import uk.gov.justice.digital.hmpps.data.generator.ReferralGenerator

@Component
class EntityManagerDataLoader {

@PersistenceContext
private lateinit var entityManager: EntityManager

@Transactional
fun loadData() {
AddressGenerator.PERSON_ADDRESS_ID = entityManager.merge(AddressGenerator.PERSON_ADDRESS).id
AddressGenerator.INACTIVE_PERSON_ADDRESS_ID = entityManager.merge(AddressGenerator.INACTIVE_PERSON_ADDRESS).id
entityManager.merge(ReferralGenerator.EXISTING_REFERRAL)
entityManager.merge(ReferralGenerator.BOOKING_WITHOUT_ARRIVAL)
ReferralGenerator.BOOKING_ARRIVED_DB_RECORD = entityManager.merge(ReferralGenerator.BOOKING_ARRIVED)
ReferralGenerator.BOOKING_DEPARTED_DB_RECORD = entityManager.merge(ReferralGenerator.BOOKING_DEPARTED)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package uk.gov.justice.digital.hmpps.data

import org.springframework.stereotype.Component
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.ReferralGenerator
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.ResidenceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.person.PersonRepository

@Component
class ReferralBookingDataLoader(
private val personRepository: PersonRepository,
private val residenceRepository: ResidenceRepository
) {
fun loadData() {
personRepository.save(PersonGenerator.PERSON_WITH_BOOKING)
ReferralGenerator.ARRIVAL = residenceRepository.save(
ReferralGenerator.generateResidence(
PersonGenerator.PERSON_WITH_BOOKING,
ReferralGenerator.BOOKING_ARRIVED_DB_RECORD!!,
arrivalDateTime = ReferralGenerator.ARRIVAL.arrivalDate,
)
)
ReferralGenerator.DEPARTURE = residenceRepository.save(
ReferralGenerator.generateResidence(
PersonGenerator.PERSON_WITH_BOOKING,
ReferralGenerator.BOOKING_DEPARTED_DB_RECORD!!,
arrivalDateTime = ReferralGenerator.DEPARTURE.arrivalDate,
departureDateTime = ReferralGenerator.DEPARTURE.departureDate
)
)
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ object AddressGenerator {
postcode = "MB01 3TD"
)

var PERSON_ADDRESS_ID: Long? = null

var INACTIVE_PERSON_ADDRESS = generatePersonAddress(
personId = PersonGenerator.PERSON_INACTIVE_EVENT.id,
addressNumber = "12",
Expand All @@ -21,6 +23,8 @@ object AddressGenerator {
postcode = "MB01 3TD"
)

var INACTIVE_PERSON_ADDRESS_ID: Long? = null

val Q001 = generateAddress(
addressNumber = "1",
streetName = "Promise Street",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ object ReferralGenerator {
expectedArrivalDate = LocalDate.now(),
expectedDepartureDate = LocalDate.now().plusDays(7),
)
var BOOKING_ARRIVED_DB_RECORD:Referral? = null

var ARRIVAL = generateResidence(PersonGenerator.PERSON_WITH_BOOKING, BOOKING_ARRIVED)

val DEPARTED_ID = UUID.randomUUID().toString()
Expand All @@ -41,6 +43,9 @@ object ReferralGenerator {
expectedArrivalDate = LocalDate.now().minusDays(8),
expectedDepartureDate = LocalDate.now().minusDays(1)
)

var BOOKING_DEPARTED_DB_RECORD:Referral? = null

var DEPARTURE = generateResidence(
PersonGenerator.PERSON_WITH_BOOKING, BOOKING_DEPARTED,
departureDateTime = ZonedDateTime.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import uk.gov.justice.digital.hmpps.datetime.EuropeLondon
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.EventDetails
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.PersonArrived
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.PersonDeparted
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.PreferredResidenceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.ReferralRepository
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.ResidenceRepository
import uk.gov.justice.digital.hmpps.integrations.delius.contact.ContactRepository
Expand Down Expand Up @@ -73,16 +72,9 @@ internal class MessagingIntegrationInactiveTest {
@Autowired
private lateinit var residenceRepository: ResidenceRepository

@Autowired
private lateinit var preferredResidenceRepository: PreferredResidenceRepository

@Autowired
private lateinit var staffRepository: StaffRepository

fun setUpTestSpecificData() {
personAddressRepository.save(AddressGenerator.INACTIVE_PERSON_ADDRESS)
}

@Test
fun `application submission with an inactive event creates an alert contact`() {
// Given an application-submitted event
Expand Down Expand Up @@ -133,8 +125,6 @@ internal class MessagingIntegrationInactiveTest {
@Test
@Order(1)
fun `booking made with inactive event creates referral and contact`() {
setUpTestSpecificData()

// Given a booking-made event
val event = prepEvent("booking-made-inactive", wireMockServer.port())

Expand Down Expand Up @@ -245,7 +235,7 @@ internal class MessagingIntegrationInactiveTest {
// And the main address is updated to be that of the approved premises - consequently any existing main address is made previous
val addresses =
personAddressRepository.findAll().filter { it.personId == PersonGenerator.PERSON_INACTIVE_EVENT.id }
.associateBy { it.id == AddressGenerator.INACTIVE_PERSON_ADDRESS.id }
.associateBy { it.id == AddressGenerator.INACTIVE_PERSON_ADDRESS_ID }
assertThat(addresses.size, equalTo(2))
val previous = addresses[true]!!
assertThat(previous.endDate, equalTo(details.arrivedAt.toLocalDate()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ internal class MessagingIntegrationTest {
} while (message != null)
}

fun setUpTestSpecificData() {
personAddressRepository.save(AddressGenerator.PERSON_ADDRESS)
}

@Test
fun `application submission creates an alert contact`() {
// Given an application-submitted event
Expand Down Expand Up @@ -149,8 +145,6 @@ internal class MessagingIntegrationTest {
@Test
@Order(1)
fun `booking made creates referral and contact`() {
setUpTestSpecificData()

// Given a booking-made event
val event = prepEvent("booking-made", wireMockServer.port())

Expand Down Expand Up @@ -301,7 +295,7 @@ internal class MessagingIntegrationTest {

// And the main address is updated to be that of the approved premises - consequently any existing main address is made previous
val addresses = personAddressRepository.findAll().filter { it.personId == PersonGenerator.DEFAULT.id }
.associateBy { it.id == AddressGenerator.PERSON_ADDRESS.id }
.associateBy { it.id == AddressGenerator.PERSON_ADDRESS_ID }
assertThat(addresses.size, equalTo(2))
val previous = addresses[true]!!
assertThat(previous.endDate, equalTo(details.arrivedAt.toLocalDate()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import uk.gov.justice.digital.hmpps.data.generator.ApprovedPremisesGenerator
import uk.gov.justice.digital.hmpps.data.generator.PersonGenerator
import uk.gov.justice.digital.hmpps.data.generator.ReferralGenerator
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.Referral
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.ReferralRepository
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.ResidenceRepository
import uk.gov.justice.digital.hmpps.model.ApReferral
import uk.gov.justice.digital.hmpps.model.ApprovedPremises
import uk.gov.justice.digital.hmpps.model.ExistingReferrals
Expand All @@ -32,15 +30,8 @@ class ReferralControllerIntegrationTest {
@Autowired
lateinit var mockMvc: MockMvc

@Autowired
lateinit var referralRepository: ReferralRepository

@Autowired
lateinit var residenceRepository: ResidenceRepository

@Test
fun `existing referrals for a crn are returned successfully`() {
referralRepository.save(ReferralGenerator.EXISTING_REFERRAL)
val person = PersonGenerator.DEFAULT
val res = mockMvc
.perform(get("/probation-case/${person.crn}/referrals").withToken())
Expand All @@ -50,30 +41,9 @@ class ReferralControllerIntegrationTest {
assertThat(res.referrals.size, equalTo(1))
}

@ParameterizedTest()
@ParameterizedTest
@MethodSource("bookingDetails")
fun `referral detail is returned correctly`(bookingId: String, detail: ReferralDetail, loadData: Boolean) {
if (loadData) {
referralRepository.save(ReferralGenerator.BOOKING_WITHOUT_ARRIVAL)
ReferralGenerator.BOOKING_ARRIVED = referralRepository.save(ReferralGenerator.BOOKING_ARRIVED)
ReferralGenerator.BOOKING_DEPARTED = referralRepository.save(ReferralGenerator.BOOKING_DEPARTED)
ReferralGenerator.ARRIVAL = residenceRepository.save(
ReferralGenerator.generateResidence(
PersonGenerator.PERSON_WITH_BOOKING,
ReferralGenerator.BOOKING_ARRIVED,
arrivalDateTime = ReferralGenerator.ARRIVAL.arrivalDate,
)
)
ReferralGenerator.DEPARTURE = residenceRepository.save(
ReferralGenerator.generateResidence(
PersonGenerator.PERSON_WITH_BOOKING,
ReferralGenerator.BOOKING_DEPARTED,
arrivalDateTime = ReferralGenerator.DEPARTURE.arrivalDate,
departureDateTime = ReferralGenerator.DEPARTURE.departureDate
)
)
}

fun `referral detail is returned correctly`(bookingId: String, detail: ReferralDetail) {
val person = PersonGenerator.PERSON_WITH_BOOKING
val res = mockMvc
.perform(get("/probation-case/${person.crn}/referrals/$bookingId").withToken())
Expand All @@ -99,19 +69,19 @@ class ReferralControllerIntegrationTest {

@JvmStatic
fun bookingDetails() = listOf(
Arguments.of(ReferralGenerator.BOOKING_ID, ReferralGenerator.BOOKING_WITHOUT_ARRIVAL.detail(), true),
Arguments.of(ReferralGenerator.BOOKING_ID, ReferralGenerator.BOOKING_WITHOUT_ARRIVAL.detail()),
Arguments.of(
ReferralGenerator.ARRIVED_ID,
ReferralGenerator.BOOKING_ARRIVED.detail(
ReferralGenerator.ARRIVAL.arrivalDate
), false
)
),
Arguments.of(
ReferralGenerator.DEPARTED_ID,
ReferralGenerator.BOOKING_DEPARTED.detail(
ReferralGenerator.DEPARTURE.arrivalDate,
ReferralGenerator.DEPARTURE.departureDate
), false
)
)
)
}
Expand Down

0 comments on commit 97e02ee

Please sign in to comment.