From 72bc54cd72ded6a56da78f9301aca2d3ee1e68b9 Mon Sep 17 00:00:00 2001 From: davidatkinsuk Date: Thu, 26 Sep 2024 12:54:18 +0100 Subject: [PATCH] Fix premises seed and throw exception on failure This commit fixes teh approved premises seed CSV file, adding a missing column name. It also changes the auto seed behaviour to throw an exception if seeding fails, instead of logging an error and carrying on. This will help us spot seeding errors instead of relying on e2e test failure to determine seeding failed. --- .../service/SeedService.kt | 317 +++++++++--------- .../local+dev+test/1__approved_premises.csv | 2 +- 2 files changed, 162 insertions(+), 157 deletions(-) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/SeedService.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/SeedService.kt index 17c295f6a04..2d3b3c88131 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/SeedService.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/SeedService.kt @@ -116,7 +116,7 @@ class SeedService( csv.file.path } - seedData(seedFileType, seedFileType.value) { filePath } + seedDataThrowExceptions(seedFileType, seedFileType.value) { filePath } } } } @@ -146,167 +146,172 @@ class SeedService( fun seedData(seedFileType: SeedFileType, filename: String) = seedData(seedFileType, filename) { "${seedConfig.filePrefix}/${this.fileName}" } private fun seedData(seedFileType: SeedFileType, filename: String, resolveCsvPath: SeedJob<*>.() -> String) { - seedLogger.info("Starting seed request: $seedFileType - $filename") - try { - val job: SeedJob<*> = when (seedFileType) { - SeedFileType.approvedPremises -> ApprovedPremisesSeedJob( - filename, - getBean(PremisesRepository::class), - getBean(ProbationRegionRepository::class), - getBean(LocalAuthorityAreaRepository::class), - getBean(CharacteristicRepository::class), - ) - SeedFileType.approvedPremisesRooms -> ApprovedPremisesRoomsSeedJob( - filename, - getBean(PremisesRepository::class), - getBean(RoomRepository::class), - getBean(BedRepository::class), - getBean(CharacteristicRepository::class), - ) - SeedFileType.user -> UsersSeedJob( - filename, - ServiceName.values().toList(), - getBean(UserService::class), - ) - SeedFileType.approvedPremisesApStaffUsers -> ApStaffUsersSeedJob( - filename, - getBean(UserService::class), - seedLogger, - ) - SeedFileType.nomisUsers -> NomisUsersSeedJob( - filename, - getBean(NomisUserRepository::class), - ) - SeedFileType.externalUsers -> ExternalUsersSeedJob( - filename, - getBean(ExternalUserRepository::class), - ) - SeedFileType.cas2Applications -> Cas2ApplicationsSeedJob( - filename, - getBean(Cas2ApplicationRepository::class), - getBean(NomisUserRepository::class), - getBean(ExternalUserRepository::class), - getBean(Cas2StatusUpdateRepository::class), - getBean(Cas2AssessmentRepository::class), - getBean(JsonSchemaService::class), - getBean(Cas2PersistedApplicationStatusFinder::class), - ) - SeedFileType.approvedPremisesUsers -> UsersSeedJob( - filename, - listOf(ServiceName.approvedPremises), - getBean(UserService::class), - ) - SeedFileType.temporaryAccommodationUsers -> UsersSeedJob( - filename, - listOf(ServiceName.temporaryAccommodation), - getBean(UserService::class), - ) - SeedFileType.characteristics -> CharacteristicsSeedJob( - filename, - getBean(CharacteristicRepository::class), - ) - SeedFileType.updateNomsNumber -> Cas1UpdateNomsNumberSeedJob( - filename, - getBean(ApplicationRepository::class), - getBean(ApplicationTimelineNoteService::class), - getBean(BookingRepository::class), - ) - SeedFileType.temporaryAccommodationPremises -> TemporaryAccommodationPremisesSeedJob( - filename, - getBean(PremisesRepository::class), - getBean(ProbationRegionRepository::class), - getBean(LocalAuthorityAreaRepository::class), - getBean(ProbationDeliveryUnitRepository::class), - getBean(CharacteristicService::class), - ) - SeedFileType.temporaryAccommodationBedspace -> TemporaryAccommodationBedspaceSeedJob( - filename, - getBean(PremisesRepository::class), - getBean(CharacteristicService::class), - getBean(RoomService::class), - ) - - SeedFileType.approvedPremisesCancelBookings -> ApprovedPremisesBookingCancelSeedJob( - filename, - getBean(BookingService::class), - getBean(BookingRepository::class), - ) - - SeedFileType.approvedPremisesApAreaEmailAddresses -> Cas1ApAreaEmailAddressSeedJob( - filename, - getBean(ApAreaRepository::class), - ) - - SeedFileType.approvedPremisesBookingAdhocProperty -> Cas1BookingAdhocPropertySeedJob( - filename, - getBean(BookingRepository::class), - ) - - SeedFileType.approvedPremisesAssessmentMoreInfoBugFix -> Cas1FurtherInfoBugFixSeedJob( - filename, - getBean(AssessmentRepository::class), - ) - - SeedFileType.approvedPremisesRedactAssessmentDetails -> Cas1RemoveAssessmentDetailsSeedJob( - filename, - getBean(AssessmentRepository::class), - getBean(ObjectMapper::class), - getBean(ApplicationService::class), - ) - - SeedFileType.approvedPremisesWithdrawPlacementRequest -> Cas1WithdrawPlacementRequestSeedJob( - filename, - getBean(PlacementRequestService::class), - getBean(ApplicationService::class), - ) - - SeedFileType.approvedPremisesReplayDomainEvents -> Cas1DomainEventReplaySeedJob( - filename, - getBean(DomainEventService::class), - ) - - SeedFileType.approvedPremisesDuplicateApplication -> Cas1DuplicateApplicationSeedJob( - filename, - getBean(ApplicationService::class), - getBean(OffenderService::class), - ) - - SeedFileType.approvedPremisesUpdateEventNumber -> Cas1UpdateEventNumberSeedJob( - filename, - getBean(ApplicationService::class), - getBean(ApplicationRepository::class), - getBean(DomainEventRepository::class), - getBean(ObjectMapper::class), - ) - - SeedFileType.approvedPremisesLinkBookingToPlacementRequest -> Cas1LinkedBookingToPlacementRequestSeedJob( - filename, - getBean(PlacementRequestRepository::class), - getBean(BookingRepository::class), - getBean(ApplicationTimelineNoteService::class), - ) - - SeedFileType.approvedPremisesOutOfServiceBeds -> Cas1OutOfServiceBedSeedJob( - filename, - getBean(Cas1OutOfServiceBedService::class), - getBean(PremisesService::class), - ) - - SeedFileType.updateUsersFromApi -> UpdateUsersFromApiSeedJob( - filename, - getBean(UserService::class), - ) - } - - transactionTemplate.executeWithoutResult { processJob(job, resolveCsvPath) } - - seedLogger.info("Seed request complete") + seedDataThrowExceptions(seedFileType, filename, resolveCsvPath) } catch (exception: Exception) { seedLogger.error("Unable to complete Seed Job", exception) } } + @SuppressWarnings("CyclomaticComplexMethod") + private fun seedDataThrowExceptions(seedFileType: SeedFileType, filename: String, resolveCsvPath: SeedJob<*>.() -> String) { + seedLogger.info("Starting seed request: $seedFileType - $filename") + + val job: SeedJob<*> = when (seedFileType) { + SeedFileType.approvedPremises -> ApprovedPremisesSeedJob( + filename, + getBean(PremisesRepository::class), + getBean(ProbationRegionRepository::class), + getBean(LocalAuthorityAreaRepository::class), + getBean(CharacteristicRepository::class), + ) + SeedFileType.approvedPremisesRooms -> ApprovedPremisesRoomsSeedJob( + filename, + getBean(PremisesRepository::class), + getBean(RoomRepository::class), + getBean(BedRepository::class), + getBean(CharacteristicRepository::class), + ) + SeedFileType.user -> UsersSeedJob( + filename, + ServiceName.values().toList(), + getBean(UserService::class), + ) + SeedFileType.approvedPremisesApStaffUsers -> ApStaffUsersSeedJob( + filename, + getBean(UserService::class), + seedLogger, + ) + SeedFileType.nomisUsers -> NomisUsersSeedJob( + filename, + getBean(NomisUserRepository::class), + ) + SeedFileType.externalUsers -> ExternalUsersSeedJob( + filename, + getBean(ExternalUserRepository::class), + ) + SeedFileType.cas2Applications -> Cas2ApplicationsSeedJob( + filename, + getBean(Cas2ApplicationRepository::class), + getBean(NomisUserRepository::class), + getBean(ExternalUserRepository::class), + getBean(Cas2StatusUpdateRepository::class), + getBean(Cas2AssessmentRepository::class), + getBean(JsonSchemaService::class), + getBean(Cas2PersistedApplicationStatusFinder::class), + ) + SeedFileType.approvedPremisesUsers -> UsersSeedJob( + filename, + listOf(ServiceName.approvedPremises), + getBean(UserService::class), + ) + SeedFileType.temporaryAccommodationUsers -> UsersSeedJob( + filename, + listOf(ServiceName.temporaryAccommodation), + getBean(UserService::class), + ) + SeedFileType.characteristics -> CharacteristicsSeedJob( + filename, + getBean(CharacteristicRepository::class), + ) + SeedFileType.updateNomsNumber -> Cas1UpdateNomsNumberSeedJob( + filename, + getBean(ApplicationRepository::class), + getBean(ApplicationTimelineNoteService::class), + getBean(BookingRepository::class), + ) + SeedFileType.temporaryAccommodationPremises -> TemporaryAccommodationPremisesSeedJob( + filename, + getBean(PremisesRepository::class), + getBean(ProbationRegionRepository::class), + getBean(LocalAuthorityAreaRepository::class), + getBean(ProbationDeliveryUnitRepository::class), + getBean(CharacteristicService::class), + ) + SeedFileType.temporaryAccommodationBedspace -> TemporaryAccommodationBedspaceSeedJob( + filename, + getBean(PremisesRepository::class), + getBean(CharacteristicService::class), + getBean(RoomService::class), + ) + + SeedFileType.approvedPremisesCancelBookings -> ApprovedPremisesBookingCancelSeedJob( + filename, + getBean(BookingService::class), + getBean(BookingRepository::class), + ) + + SeedFileType.approvedPremisesApAreaEmailAddresses -> Cas1ApAreaEmailAddressSeedJob( + filename, + getBean(ApAreaRepository::class), + ) + + SeedFileType.approvedPremisesBookingAdhocProperty -> Cas1BookingAdhocPropertySeedJob( + filename, + getBean(BookingRepository::class), + ) + + SeedFileType.approvedPremisesAssessmentMoreInfoBugFix -> Cas1FurtherInfoBugFixSeedJob( + filename, + getBean(AssessmentRepository::class), + ) + + SeedFileType.approvedPremisesRedactAssessmentDetails -> Cas1RemoveAssessmentDetailsSeedJob( + filename, + getBean(AssessmentRepository::class), + getBean(ObjectMapper::class), + getBean(ApplicationService::class), + ) + + SeedFileType.approvedPremisesWithdrawPlacementRequest -> Cas1WithdrawPlacementRequestSeedJob( + filename, + getBean(PlacementRequestService::class), + getBean(ApplicationService::class), + ) + + SeedFileType.approvedPremisesReplayDomainEvents -> Cas1DomainEventReplaySeedJob( + filename, + getBean(DomainEventService::class), + ) + + SeedFileType.approvedPremisesDuplicateApplication -> Cas1DuplicateApplicationSeedJob( + filename, + getBean(ApplicationService::class), + getBean(OffenderService::class), + ) + + SeedFileType.approvedPremisesUpdateEventNumber -> Cas1UpdateEventNumberSeedJob( + filename, + getBean(ApplicationService::class), + getBean(ApplicationRepository::class), + getBean(DomainEventRepository::class), + getBean(ObjectMapper::class), + ) + + SeedFileType.approvedPremisesLinkBookingToPlacementRequest -> Cas1LinkedBookingToPlacementRequestSeedJob( + filename, + getBean(PlacementRequestRepository::class), + getBean(BookingRepository::class), + getBean(ApplicationTimelineNoteService::class), + ) + + SeedFileType.approvedPremisesOutOfServiceBeds -> Cas1OutOfServiceBedSeedJob( + filename, + getBean(Cas1OutOfServiceBedService::class), + getBean(PremisesService::class), + ) + + SeedFileType.updateUsersFromApi -> UpdateUsersFromApiSeedJob( + filename, + getBean(UserService::class), + ) + } + + transactionTemplate.executeWithoutResult { processJob(job, resolveCsvPath) } + + seedLogger.info("Seed request complete") + } + private fun getBean(clazz: KClass) = applicationContext.getBean(clazz.java) private fun processJob(job: SeedJob, resolveCsvPath: SeedJob.() -> String) { diff --git a/src/main/resources/db/seed/local+dev+test/1__approved_premises.csv b/src/main/resources/db/seed/local+dev+test/1__approved_premises.csv index 155aefece40..500f3de8c84 100644 --- a/src/main/resources/db/seed/local+dev+test/1__approved_premises.csv +++ b/src/main/resources/db/seed/local+dev+test/1__approved_premises.csv @@ -1,4 +1,4 @@ -name,addressLine1,addressLine2,town,postcode,latitude,longitude,notes,emailAddress,probationRegion,localAuthorityArea,characteristics,isIAP,isPIPE,isESAP,isSemiSpecialistMentalHealth,isRecoveryFocussed,isSuitableForVulnerable,acceptsSexOffenders,acceptsChildSexOffenders,acceptsNonSexualChildOffenders,acceptsHateCrimeOffenders,isCatered,hasWideStepFreeAccess,hasWideAccessToCommunalAreas,hasStepFreeAccessToCommunalAreas,hasWheelChairAccessibleBathrooms,hasLift,hasTactileFlooring,hasBrailleSignage,hasHearingLoop,status,apCode,qCode +name,addressLine1,addressLine2,town,postcode,latitude,longitude,notes,emailAddress,probationRegion,localAuthorityArea,characteristics,isIAP,isPIPE,isESAP,isSemiSpecialistMentalHealth,isRecoveryFocussed,isSuitableForVulnerable,acceptsSexOffenders,acceptsChildSexOffenders,acceptsNonSexualChildOffenders,acceptsHateCrimeOffenders,isCatered,hasWideStepFreeAccess,hasWideAccessToCommunalAreas,hasStepFreeAccessToCommunalAreas,hasWheelChairAccessibleBathrooms,hasLift,hasTactileFlooring,hasBrailleSignage,hasHearingLoop,status,apCode,qCode,gender Test AP 1,454 Nader Port,,Quitzonview,OX26 2EQ,51.904735,-1.1635,No,some@emailaddress.com,Greater Manchester,Malvern Hills,,YES,NO,NO,NO,NO,YES,YES,YES,YES,YES,YES,NO,NO,YES,NO,NO,NO,NO,YES,active,TEST1,Q095,MAN Test AP 2,7525 Alba Trail,,Theronborough,WA6 9BQ,53.266459,-2.768905,,some@emailaddress.com,North East,Hambleton,,NO,NO,NO,NO,NO,YES,YES,YES,YES,YES,YES,YES,NO,YES,YES,NO,NO,NO,YES,active,TEST2,Q100,MAN Test AP 3,8527 Bell Rapids,,South Myah,ML8 5EP,55.751711,-3.851103,restrictions on offences against sex workers due to location of the AP,some@emailaddress.com,Wales,Tamworth,,NO,YES,NO,NO,NO,YES,YES,YES,YES,YES,YES,YES,YES,YES,YES,NO,NO,NO,NO,active,TEST3,Q057,MAN