Skip to content

Commit

Permalink
Enhance test app seed job to include bookings
Browse files Browse the repository at this point in the history
This commit updates the create test application seed job to support seeding applications with a space bookings. This is required to support load testing.

Example config:

```
creator_username,crn,count,state,premises_qcode
JIMSNOWLDAP,X320741,50,AUTHORISED,
JIMSNOWLDAP,X320741,50,BOOKED,Q713
```
  • Loading branch information
davidatkinsuk committed Jan 22, 2025
1 parent e470164 commit 0695490
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.ServiceName
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.SituationOption
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.SubmitApprovedPremisesApplication
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesApplicationEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.AssessmentRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.BookingEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.BookingRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas1SpaceBookingEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas1SpaceBookingEntity.Companion.CHARACTERISTICS_OF_INTEREST
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.Cas1SpaceBookingRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.CharacteristicEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.CharacteristicRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.OfflineApplicationEntity
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.PlacementRequestRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.PostcodeDistrictRepository
import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.PersonInfoResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.problem.ForbiddenProblem
Expand All @@ -38,6 +42,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.EnvironmentServi
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.OffenderService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.UserService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.UserService.GetUserResponse
import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1SpaceBookingService
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.ensureEntityFromCasResultIsSuccess
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.extractEntityFromCasResult
import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.extractEntityFromNestedAuthorisableValidatableActionResult
Expand All @@ -46,6 +51,7 @@ import java.time.LocalDate
import java.time.OffsetDateTime
import java.util.UUID
import java.util.concurrent.TimeUnit
import kotlin.random.Random

@SuppressWarnings("MagicNumber", "TooGenericExceptionCaught")
@Service
Expand All @@ -61,6 +67,10 @@ class Cas1ApplicationSeedService(
private val assessmentRepository: AssessmentRepository,
private val postcodeDistrictRepository: PostcodeDistrictRepository,
private val cache: Cas1ApplicationSeedServiceCaches,
private val spaceBookingService: Cas1SpaceBookingService,
private val placementRequestRepository: PlacementRequestRepository,
private val premisesRepository: ApprovedPremisesRepository,
private val characteristicsRepository: CharacteristicRepository,
) {
companion object {
private val log = LoggerFactory.getLogger(this::class.java)
Expand All @@ -85,6 +95,7 @@ class Cas1ApplicationSeedService(
enum class ApplicationState {
PENDING_SUBMISSION,
AUTHORISED,
BOOKED,
}

@SuppressWarnings("TooGenericExceptionCaught")
Expand All @@ -93,12 +104,13 @@ class Cas1ApplicationSeedService(
crn: String,
createIfExistingApplicationForCrn: Boolean = false,
state: ApplicationState,
premisesQCode: String? = null,
) {
if (environmentService.isNotATestEnvironment()) {
error("Cannot create test applications as not in a test environment")
}

createApplicationInternal(deliusUserName, crn, createIfExistingApplicationForCrn, state)
createApplicationInternal(deliusUserName, crn, createIfExistingApplicationForCrn, state, premisesQCode)
}

fun createOfflineApplicationWithBooking(deliusUserName: String, crn: String) {
Expand All @@ -114,6 +126,7 @@ class Cas1ApplicationSeedService(
crn: String,
createIfExistingApplicationForCrn: Boolean,
state: ApplicationState,
premisesQCode: String?,
) {
if (!createIfExistingApplicationForCrn && applicationService.getApplicationsForCrn(crn, ServiceName.approvedPremises).isNotEmpty()) {
log.info("Already have CAS1 application for $crn, not seeding a new application")
Expand All @@ -131,6 +144,16 @@ class Cas1ApplicationSeedService(
submitApplication(application)
assessAndAcceptApplication(application)
}
ApplicationState.BOOKED -> {
val application = createApplicationPendingSubmission(deliusUserName, crn)
submitApplication(application)
assessAndAcceptApplication(application)
val premises = premisesRepository.findByQCode(premisesQCode!!)!!
createSpaceBooking(
application,
premises,
)
}
}
}

Expand Down Expand Up @@ -253,6 +276,27 @@ class Cas1ApplicationSeedService(
)
}

@SuppressWarnings("MagicNumber")
private fun createSpaceBooking(
application: ApprovedPremisesApplicationEntity,
premises: ApprovedPremisesEntity,
) {
val arrivalDate = LocalDate.now().minusDays(Random.nextLong(0, 7))
val departureDate = arrivalDate.plusDays(Random.nextLong(1, 365))
val characteristics = CHARACTERISTICS_OF_INTEREST.shuffled().take(Random.nextInt(0, CHARACTERISTICS_OF_INTEREST.size - 1))

ensureEntityFromCasResultIsSuccess(
spaceBookingService.createNewBooking(
premisesId = premises.id,
placementRequestId = placementRequestRepository.findByApplication(application).first().id,
arrivalDate = arrivalDate,
departureDate = departureDate,
createdBy = application.createdByUser,
characteristics = characteristicsRepository.findAllWherePropertyNameIn(characteristics, "approved-premises"),
),
)
}

private fun getAssessmentId(application: ApprovedPremisesApplicationEntity) = assessmentRepository.findByApplicationIdAndReallocatedAtNull(applicationId = application.id)!!.id

private fun createOfflineApplicationInternal(deliusUserName: String, crn: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Cas1CreateTestApplicationsSeedJob(
"crn",
"count",
"state",
"premises_qcode",
),
runInTransaction = false,
) {
Expand All @@ -34,6 +35,7 @@ class Cas1CreateTestApplicationsSeedJob(
crn = seedColumns.getStringOrNull("crn")!!,
count = seedColumns.getIntOrNull("count")!!,
state = Cas1ApplicationSeedService.ApplicationState.valueOf(seedColumns.getStringOrNull("state")!!),
premisesQCode = seedColumns.getStringOrNull("premises_qcode"),
)
}

Expand All @@ -56,6 +58,7 @@ class Cas1CreateTestApplicationsSeedJob(
crn = crn,
createIfExistingApplicationForCrn = true,
state = row.state,
premisesQCode = row.premisesQCode,
)
}
}
Expand All @@ -71,4 +74,5 @@ data class Cas1CreateTestApplicationsSeedCsvRow(
val crn: String,
val count: Int,
val state: Cas1ApplicationSeedService.ApplicationState,
val premisesQCode: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test
import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.SeedFileType
import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.NeedsDetailsFactory
import uk.gov.justice.digital.hmpps.approvedpremisesapi.integration.givens.givenAUser
import uk.gov.justice.digital.hmpps.approvedpremisesapi.integration.givens.givenAnApprovedPremises
import uk.gov.justice.digital.hmpps.approvedpremisesapi.integration.givens.givenAnOffender
import uk.gov.justice.digital.hmpps.approvedpremisesapi.integration.httpmocks.apDeliusContextMockSuccessfulTeamsManagingCaseCall
import uk.gov.justice.digital.hmpps.approvedpremisesapi.integration.httpmocks.apOASysContextMockSuccessfulNeedsDetailsCall
Expand All @@ -29,6 +30,8 @@ class SeedCas1CreateTestApplicationSeedTest : SeedTestBase() {
apOASysContextMockSuccessfulNeedsDetailsCall(crn, NeedsDetailsFactory().produce())
govUKBankHolidaysAPIMockSuccessfullCallWithEmptyResponse()

val premises = givenAnApprovedPremises(supportsSpaceBookings = true)

postCodeDistrictFactory.produceAndPersist()

withCsv(
Expand All @@ -38,7 +41,8 @@ class SeedCas1CreateTestApplicationSeedTest : SeedTestBase() {
creatorUsername = user.deliusUsername,
crn = crn,
count = 1,
state = Cas1ApplicationSeedService.ApplicationState.AUTHORISED,
state = Cas1ApplicationSeedService.ApplicationState.BOOKED,
premisesQCode = premises.qCode,
),
).toCsv(),
)
Expand All @@ -56,6 +60,7 @@ class SeedCas1CreateTestApplicationSeedTest : SeedTestBase() {
"crn",
"count",
"state",
"premises_qcode",
)
.newRow()

Expand All @@ -65,6 +70,7 @@ class SeedCas1CreateTestApplicationSeedTest : SeedTestBase() {
.withQuotedField(it.crn)
.withQuotedField(it.count)
.withQuotedField(it.state)
.withQuotedField(it.premisesQCode ?: "")
.newRow()
}

Expand Down

0 comments on commit 0695490

Please sign in to comment.