Skip to content

Commit

Permalink
PI-2673 - move test variables to entityManagerDataLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
achimber-moj committed Jan 6, 2025
1 parent e3f6fa1 commit 0888f9b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@ 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
import uk.gov.justice.digital.hmpps.integrations.delius.approvedpremises.referral.entity.Referral

@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() {
AddressGenerator.PERSON_ADDRESS_ID = entityManager.merge(AddressGenerator.PERSON_ADDRESS).id
AddressGenerator.INACTIVE_PERSON_ADDRESS_ID = entityManager.merge(AddressGenerator.INACTIVE_PERSON_ADDRESS).id
personAddressId = entityManager.merge(AddressGenerator.PERSON_ADDRESS).id
inactivePersonAddressId = 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)
bookingArrivedDbRecord = entityManager.merge(ReferralGenerator.BOOKING_ARRIVED)
bookingDepartedDbRecord = entityManager.merge(ReferralGenerator.BOOKING_DEPARTED)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ import uk.gov.justice.digital.hmpps.integrations.delius.person.PersonRepository
@Component
class ReferralBookingDataLoader(
private val personRepository: PersonRepository,
private val residenceRepository: ResidenceRepository
private val residenceRepository: ResidenceRepository,
private val entityManagerDataLoader: EntityManagerDataLoader
) {
fun loadData() {
personRepository.save(PersonGenerator.PERSON_WITH_BOOKING)
ReferralGenerator.ARRIVAL = residenceRepository.save(
ReferralGenerator.generateResidence(
PersonGenerator.PERSON_WITH_BOOKING,
ReferralGenerator.BOOKING_ARRIVED_DB_RECORD!!,
entityManagerDataLoader.bookingArrivedDbRecord!!,
arrivalDateTime = ReferralGenerator.ARRIVAL.arrivalDate,
)
)
ReferralGenerator.DEPARTURE = residenceRepository.save(
ReferralGenerator.generateResidence(
PersonGenerator.PERSON_WITH_BOOKING,
ReferralGenerator.BOOKING_DEPARTED_DB_RECORD!!,
entityManagerDataLoader.bookingDepartedDbRecord!!,
arrivalDateTime = ReferralGenerator.DEPARTURE.arrivalDate,
departureDateTime = ReferralGenerator.DEPARTURE.departureDate
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ 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 @@ -23,8 +21,6 @@ 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,7 +31,6 @@ 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)

Expand All @@ -44,8 +43,6 @@ 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,6 +17,7 @@ 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.datetime.EuropeLondon
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.EventDetails
Expand Down Expand Up @@ -75,6 +76,9 @@ 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 @@ -235,7 +239,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 == entityManagerDataLoader.inactivePersonAddressId }
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,6 +18,7 @@ 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.datetime.EuropeLondon
import uk.gov.justice.digital.hmpps.integrations.approvedpremises.EventDetails
Expand Down Expand Up @@ -86,6 +87,9 @@ 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 @@ -295,7 +299,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 == entityManagerDataLoader.personAddressId }
assertThat(addresses.size, equalTo(2))
val previous = addresses[true]!!
assertThat(previous.endDate, equalTo(details.arrivedAt.toLocalDate()))
Expand Down

0 comments on commit 0888f9b

Please sign in to comment.