Skip to content

Commit

Permalink
PI-2673 - move variables back to generators
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Jan 10, 2025
1 parent 1cac6a2 commit 31a4365
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class DataLoader(
private val referralBookingDataLoader: ReferralBookingDataLoader,
private val documentDataLoader: DocumentDataLoader,
private val entityManagerDataLoader: EntityManagerDataLoader

) : ApplicationListener<ApplicationReadyEvent> {

@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,25 @@ 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.AddressGenerator.INACTIVE_PERSON_ADDRESS_ID
import uk.gov.justice.digital.hmpps.data.generator.AddressGenerator.PERSON_ADDRESS_ID
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.data.generator.ReferralGenerator.BOOKING_ARRIVED_DB_RECORD
import uk.gov.justice.digital.hmpps.data.generator.ReferralGenerator.BOOKING_DEPARTED_DB_RECORD

@Component
class EntityManagerDataLoader {

@PersistenceContext
private lateinit var entityManager: EntityManager

var personAddressId: Long? = null

var inactivePersonAddressId: Long? = null

var bookingArrivedDbRecord: Referral? = null

var bookingDepartedDbRecord: Referral? = null

@Transactional
fun loadData() {
personAddressId = entityManager.merge(AddressGenerator.PERSON_ADDRESS).id
inactivePersonAddressId = entityManager.merge(AddressGenerator.INACTIVE_PERSON_ADDRESS).id
PERSON_ADDRESS_ID = entityManager.merge(AddressGenerator.PERSON_ADDRESS).id
INACTIVE_PERSON_ADDRESS_ID = entityManager.merge(AddressGenerator.INACTIVE_PERSON_ADDRESS).id
entityManager.merge(ReferralGenerator.EXISTING_REFERRAL)
entityManager.merge(ReferralGenerator.BOOKING_WITHOUT_ARRIVAL)
bookingArrivedDbRecord = entityManager.merge(ReferralGenerator.BOOKING_ARRIVED)
bookingDepartedDbRecord = entityManager.merge(ReferralGenerator.BOOKING_DEPARTED)
BOOKING_ARRIVED_DB_RECORD = entityManager.merge(ReferralGenerator.BOOKING_ARRIVED)
BOOKING_DEPARTED_DB_RECORD = entityManager.merge(ReferralGenerator.BOOKING_DEPARTED)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@ 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.data.generator.ReferralGenerator.BOOKING_ARRIVED_DB_RECORD
import uk.gov.justice.digital.hmpps.data.generator.ReferralGenerator.BOOKING_DEPARTED_DB_RECORD
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,
private val entityManagerDataLoader: EntityManagerDataLoader
private val residenceRepository: ResidenceRepository
) {
fun loadData() {
personRepository.save(PersonGenerator.PERSON_WITH_BOOKING)
ReferralGenerator.ARRIVAL = residenceRepository.save(
ReferralGenerator.generateResidence(
PersonGenerator.PERSON_WITH_BOOKING,
entityManagerDataLoader.bookingArrivedDbRecord!!,
BOOKING_ARRIVED_DB_RECORD!!,
arrivalDateTime = ReferralGenerator.ARRIVAL.arrivalDate,
)
)
ReferralGenerator.DEPARTURE = residenceRepository.save(
ReferralGenerator.generateResidence(
PersonGenerator.PERSON_WITH_BOOKING,
entityManagerDataLoader.bookingDepartedDbRecord!!,
BOOKING_DEPARTED_DB_RECORD!!,
arrivalDateTime = ReferralGenerator.DEPARTURE.arrivalDate,
departureDateTime = ReferralGenerator.DEPARTURE.departureDate
)
Expand Down
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 @@ -32,6 +32,8 @@ object ReferralGenerator {
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 @@ -43,6 +45,8 @@ object ReferralGenerator {
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 @@ -17,8 +17,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.test.context.bean.override.mockito.MockitoBean
import uk.gov.justice.digital.hmpps.data.EntityManagerDataLoader
import uk.gov.justice.digital.hmpps.data.generator.*
import uk.gov.justice.digital.hmpps.data.generator.AddressGenerator.INACTIVE_PERSON_ADDRESS_ID
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
Expand Down Expand Up @@ -76,9 +76,6 @@ internal class MessagingIntegrationInactiveTest {
@Autowired
private lateinit var staffRepository: StaffRepository

@Autowired
private lateinit var entityManagerDataLoader: EntityManagerDataLoader

@Test
fun `application submission with an inactive event creates an alert contact`() {
// Given an application-submitted event
Expand Down Expand Up @@ -239,7 +236,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 == entityManagerDataLoader.inactivePersonAddressId }
.associateBy { it.id == 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 @@ -18,8 +18,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT
import org.springframework.test.context.bean.override.mockito.MockitoBean
import uk.gov.justice.digital.hmpps.data.EntityManagerDataLoader
import uk.gov.justice.digital.hmpps.data.generator.*
import uk.gov.justice.digital.hmpps.data.generator.AddressGenerator.PERSON_ADDRESS_ID
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
Expand Down Expand Up @@ -87,9 +87,6 @@ internal class MessagingIntegrationTest {
@Autowired
private lateinit var staffRepository: StaffRepository

@Autowired
private lateinit var entityManagerDataLoader: EntityManagerDataLoader

@BeforeEach
fun clearTopic() {
val topic = channelManager.getChannel(topicName)
Expand Down Expand Up @@ -299,7 +296,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 == entityManagerDataLoader.personAddressId }
.associateBy { it.id == PERSON_ADDRESS_ID }
assertThat(addresses.size, equalTo(2))
val previous = addresses[true]!!
assertThat(previous.endDate, equalTo(details.arrivedAt.toLocalDate()))
Expand Down

0 comments on commit 31a4365

Please sign in to comment.