From 78f3cf8c647f226de574561ecfd194ddc3a85250 Mon Sep 17 00:00:00 2001 From: davidatkinsuk Date: Wed, 8 Jan 2025 16:46:23 +0000 Subject: [PATCH 1/3] Rename Cas1PremisesSummary to Cas1Premises MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We have several API types described as premises summary. This commit renames `Cas1PremisesSummary` to `Cas1Premises` as it’s not really a summary, and really the main describer of the premise. This should help disambiguate the various CAS1 Premises API types. --- .../controller/cas1/Cas1PremisesController.kt | 12 ++++++------ .../service/cas1/Cas1PremisesService.kt | 6 +++--- .../cas1/Cas1PremiseCapacitySummaryTransformer.kt | 4 ++-- .../transformer/cas1/Cas1PremisesTransformer.kt | 6 +++--- src/main/resources/static/cas1-api.yml | 2 +- src/main/resources/static/cas1-schemas.yml | 4 ++-- .../resources/static/codegen/built-cas1-api-spec.yml | 6 +++--- .../integration/cas1/Cas1PremisesTest.kt | 5 +++-- .../service/cas1/Cas1PremisesOverbookingRangeTest.kt | 10 +++++----- .../unit/service/cas1/Cas1PremisesServiceTest.kt | 8 ++++---- .../Cas1PremiseCapacitySummaryTransformerTest.kt | 8 ++++---- .../transformer/cas1/Cas1PremisesTransformerTest.kt | 4 ++-- 12 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/controller/cas1/Cas1PremisesController.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/controller/cas1/Cas1PremisesController.kt index a0eaa995ff..b5274f2426 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/controller/cas1/Cas1PremisesController.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/controller/cas1/Cas1PremisesController.kt @@ -5,9 +5,9 @@ import org.springframework.stereotype.Service import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.cas1.PremisesCas1Delegate import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1ApprovedPremisesGender import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremiseCapacity +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1Premises import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesBasicSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesDaySummary -import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceBookingCharacteristic import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceBookingDaySummarySortField import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.SortDirection @@ -37,14 +37,14 @@ class Cas1PremisesController( private val cas1OutOfServiceBedSummaryTransformer: Cas1OutOfServiceBedSummaryTransformer, ) : PremisesCas1Delegate { - override fun getPremisesById(premisesId: UUID): ResponseEntity { + override fun getPremisesById(premisesId: UUID): ResponseEntity { userAccessService.ensureCurrentUserHasPermission(UserPermission.CAS1_PREMISES_VIEW) return ResponseEntity .ok() .body( - cas1PremisesTransformer.toPremiseSummary( - extractEntityFromCasResult(cas1PremisesService.getPremisesSummary(premisesId)), + cas1PremisesTransformer.toPremises( + extractEntityFromCasResult(cas1PremisesService.getPremisesInfo(premisesId)), ), ) } @@ -77,7 +77,7 @@ class Cas1PremisesController( ): ResponseEntity { userAccessService.ensureCurrentUserHasPermission(UserPermission.CAS1_PREMISES_VIEW) - val premiseSummaryInfo = cas1PremisesService.getPremisesSummary(premisesId) + val premiseSummaryInfo = cas1PremisesService.getPremisesInfo(premisesId) val premiseCapacity = cas1PremisesService.getPremiseCapacity( premisesId = premisesId, startDate = startDate, @@ -103,7 +103,7 @@ class Cas1PremisesController( userAccessService.ensureCurrentUserHasPermission(UserPermission.CAS1_PREMISES_VIEW) val premiseSummaryInfo = extractEntityFromCasResult( - cas1PremisesService.getPremisesSummary(premisesId), + cas1PremisesService.getPremisesInfo(premisesId), ) return ResponseEntity.ok().body( diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1PremisesService.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1PremisesService.kt index 7f7639498a..8a8c67321e 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1PremisesService.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/service/cas1/Cas1PremisesService.kt @@ -24,7 +24,7 @@ class Cas1PremisesService( companion object { private const val OVERBOOKING_RANGE_DURATION_WEEKS = 12L } - fun getPremisesSummary(premisesId: UUID): CasResult { + fun getPremisesInfo(premisesId: UUID): CasResult { val premise = premisesRepository.findByIdOrNull(premisesId) ?: return CasResult.NotFound("premises", premisesId.toString()) @@ -33,7 +33,7 @@ class Cas1PremisesService( val overbookingSummary = premise.takeIf { it.supportsSpaceBookings }?.let { buildOverBookingSummary(it) } ?: emptyList() return CasResult.Success( - Cas1PremisesSummaryInfo( + Cas1PremisesInfo( entity = premise, bedCount = bedCount, availableBeds = bedCount - outOfServiceBedsCount, @@ -83,7 +83,7 @@ class Cas1PremisesService( ) } - data class Cas1PremisesSummaryInfo( + data class Cas1PremisesInfo( val entity: ApprovedPremisesEntity, val bedCount: Int, val availableBeds: Int, diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremiseCapacitySummaryTransformer.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremiseCapacitySummaryTransformer.kt index 89c3c93913..41dd12d194 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremiseCapacitySummaryTransformer.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremiseCapacitySummaryTransformer.kt @@ -15,10 +15,10 @@ class Cas1PremiseCapacitySummaryTransformer( ) { fun toCas1PremiseCapacitySummary( - premiseSummaryInfo: Cas1PremisesService.Cas1PremisesSummaryInfo, + premiseSummaryInfo: Cas1PremisesService.Cas1PremisesInfo, premiseCapacity: PremiseCapacitySummary, ) = Cas1PremiseCapacity( - premise = cas1PremisesTransformer.toPremiseSummary(premiseSummaryInfo), + premise = cas1PremisesTransformer.toPremises(premiseSummaryInfo), startDate = premiseCapacity.range.fromInclusive, endDate = premiseCapacity.range.toInclusive, capacity = premiseCapacity.byDay.map { it.toApiType() }, diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremisesTransformer.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremisesTransformer.kt index c8684b2833..cde8674463 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremisesTransformer.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremisesTransformer.kt @@ -1,8 +1,8 @@ package uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.cas1 import org.springframework.stereotype.Service +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1Premises import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesBasicSummary -import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.NamedId import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesBasicSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1PremisesService @@ -12,9 +12,9 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.ApAreaTransf class Cas1PremisesTransformer( val apAreaTransformer: ApAreaTransformer, ) { - fun toPremiseSummary(premisesSummaryInfo: Cas1PremisesService.Cas1PremisesSummaryInfo): Cas1PremisesSummary { + fun toPremises(premisesSummaryInfo: Cas1PremisesService.Cas1PremisesInfo): Cas1Premises { val entity = premisesSummaryInfo.entity - return Cas1PremisesSummary( + return Cas1Premises( id = entity.id, name = entity.name, apCode = entity.apCode, diff --git a/src/main/resources/static/cas1-api.yml b/src/main/resources/static/cas1-api.yml index d7ec6f6112..4ec0f418d9 100644 --- a/src/main/resources/static/cas1-api.yml +++ b/src/main/resources/static/cas1-api.yml @@ -367,7 +367,7 @@ paths: content: 'application/json': schema: - $ref: 'cas1-schemas.yml#/components/schemas/Cas1PremisesSummary' + $ref: 'cas1-schemas.yml#/components/schemas/Cas1Premises' 401: $ref: '_shared.yml#/components/responses/401Response' 403: diff --git a/src/main/resources/static/cas1-schemas.yml b/src/main/resources/static/cas1-schemas.yml index 60e8bb00d2..437b52d33d 100644 --- a/src/main/resources/static/cas1-schemas.yml +++ b/src/main/resources/static/cas1-schemas.yml @@ -77,7 +77,7 @@ components: - apArea - characteristics - fullAddress - Cas1PremisesSummary: + Cas1Premises: type: object properties: id: @@ -739,7 +739,7 @@ components: type: object properties: premise: - $ref: '#/components/schemas/Cas1PremisesSummary' + $ref: '#/components/schemas/Cas1Premises' startDate: type: string format: date diff --git a/src/main/resources/static/codegen/built-cas1-api-spec.yml b/src/main/resources/static/codegen/built-cas1-api-spec.yml index c88acc016d..67ab6add9e 100644 --- a/src/main/resources/static/codegen/built-cas1-api-spec.yml +++ b/src/main/resources/static/codegen/built-cas1-api-spec.yml @@ -369,7 +369,7 @@ paths: content: 'application/json': schema: - $ref: '#/components/schemas/Cas1PremisesSummary' + $ref: '#/components/schemas/Cas1Premises' 401: $ref: '#/components/responses/401Response' 403: @@ -6094,7 +6094,7 @@ components: - apArea - characteristics - fullAddress - Cas1PremisesSummary: + Cas1Premises: type: object properties: id: @@ -6756,7 +6756,7 @@ components: type: object properties: premise: - $ref: '#/components/schemas/Cas1PremisesSummary' + $ref: '#/components/schemas/Cas1Premises' startDate: type: string format: date diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/integration/cas1/Cas1PremisesTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/integration/cas1/Cas1PremisesTest.kt index 8ac3017b1e..134776c1f7 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/integration/cas1/Cas1PremisesTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/integration/cas1/Cas1PremisesTest.kt @@ -5,9 +5,9 @@ import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremiseCapacity +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1Premises import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesBasicSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesDaySummary -import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.FullPersonSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.PersonSummaryDiscriminator import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.ServiceName @@ -39,6 +39,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.UserRole.CAS1 import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.deliuscontext.CaseSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.asCaseSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.bodyAsListOfObjects +import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.bodyAsObject import uk.gov.justice.digital.hmpps.approvedpremisesapi.util.roundNanosToMillisToAccountForLossOfPrecisionInPostgres import java.time.LocalDate import java.time.OffsetDateTime @@ -129,7 +130,7 @@ class Cas1PremisesTest : IntegrationTestBase() { .exchange() .expectStatus() .isOk - .returnResult(Cas1PremisesSummary::class.java).responseBody.blockFirst()!! + .bodyAsObject() assertThat(summary.id).isEqualTo(premises.id) assertThat(summary.name).isEqualTo("the premises name") diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1PremisesOverbookingRangeTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1PremisesOverbookingRangeTest.kt index c0699384b5..73e2670ee5 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1PremisesOverbookingRangeTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1PremisesOverbookingRangeTest.kt @@ -80,7 +80,7 @@ class Cas1PremisesOverbookingRangeTest { every { spacePlanningService.capacity(premises, any(), null) } returns premisesCapacitySummary - val result = service.getPremisesSummary(Cas1PremisesServiceTest.CONSTANTS.PREMISES_ID) as CasResult.Success + val result = service.getPremisesInfo(Cas1PremisesServiceTest.CONSTANTS.PREMISES_ID) as CasResult.Success assertThat(result).isInstanceOf(CasResult.Success::class.java) val premisesSummaryInfo = result.value @@ -115,7 +115,7 @@ class Cas1PremisesOverbookingRangeTest { every { spacePlanningService.capacity(premises, any(), null) } returns premisesCapacitySummary - val result = service.getPremisesSummary(Cas1PremisesServiceTest.CONSTANTS.PREMISES_ID) as CasResult.Success + val result = service.getPremisesInfo(Cas1PremisesServiceTest.CONSTANTS.PREMISES_ID) as CasResult.Success assertThat(result).isInstanceOf(CasResult.Success::class.java) @@ -158,7 +158,7 @@ class Cas1PremisesOverbookingRangeTest { every { spacePlanningService.capacity(premises, any(), null) } returns premisesCapacitySummary - val result = service.getPremisesSummary(Cas1PremisesServiceTest.CONSTANTS.PREMISES_ID) as CasResult.Success + val result = service.getPremisesInfo(Cas1PremisesServiceTest.CONSTANTS.PREMISES_ID) as CasResult.Success assertThat(result).isInstanceOf(CasResult.Success::class.java) @@ -205,7 +205,7 @@ class Cas1PremisesOverbookingRangeTest { every { spacePlanningService.capacity(premises, any(), null) } returns premisesCapacitySummary - val result = service.getPremisesSummary(Cas1PremisesServiceTest.CONSTANTS.PREMISES_ID) as CasResult.Success + val result = service.getPremisesInfo(Cas1PremisesServiceTest.CONSTANTS.PREMISES_ID) as CasResult.Success assertThat(result).isInstanceOf(CasResult.Success::class.java) @@ -243,7 +243,7 @@ class Cas1PremisesOverbookingRangeTest { every { spacePlanningService.capacity(premises, any(), null) } returns premisesCapacitySummary - val result = service.getPremisesSummary(Cas1PremisesServiceTest.CONSTANTS.PREMISES_ID) as CasResult.Success + val result = service.getPremisesInfo(Cas1PremisesServiceTest.CONSTANTS.PREMISES_ID) as CasResult.Success assertThat(result).isInstanceOf(CasResult.Success::class.java) val premisesSummaryInfo = result.value diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1PremisesServiceTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1PremisesServiceTest.kt index 0a745ad91d..c3f98193c4 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1PremisesServiceTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/service/cas1/Cas1PremisesServiceTest.kt @@ -46,13 +46,13 @@ class Cas1PremisesServiceTest { } @Nested - inner class GetPremisesSummary { + inner class GetPremisesInfo { @Test fun `premises not found return error`() { every { approvedPremisesRepository.findByIdOrNull(PREMISES_ID) } returns null - val result = service.getPremisesSummary(PREMISES_ID) + val result = service.getPremisesInfo(PREMISES_ID) assertThat(result).isInstanceOf(CasResult.NotFound::class.java) } @@ -81,7 +81,7 @@ class Cas1PremisesServiceTest { every { outOfServiceBedService.getCurrentOutOfServiceBedsCountForPremisesId(PREMISES_ID) } returns 4 every { spacePlanningService.capacity(premises, any(), null) } returns premisesCapacitySummary - val result = service.getPremisesSummary(PREMISES_ID) + val result = service.getPremisesInfo(PREMISES_ID) assertThat(result).isInstanceOf(CasResult.Success::class.java) result as CasResult.Success @@ -118,7 +118,7 @@ class Cas1PremisesServiceTest { every { outOfServiceBedService.getCurrentOutOfServiceBedsCountForPremisesId(PREMISES_ID) } returns 4 every { spacePlanningService.capacity(premises, any(), null) } returns premisesCapacitySummary - val result = service.getPremisesSummary(PREMISES_ID) + val result = service.getPremisesInfo(PREMISES_ID) assertThat(result).isInstanceOf(CasResult.Success::class.java) result as CasResult.Success diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremiseCapacitySummaryTransformerTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremiseCapacitySummaryTransformerTest.kt index 6804a505e3..36c08c751a 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremiseCapacitySummaryTransformerTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremiseCapacitySummaryTransformerTest.kt @@ -8,7 +8,7 @@ import io.mockk.mockk import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.junit.jupiter.api.extension.ExtendWith -import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesSummary +import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1Premises import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceBookingCharacteristic import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.ApprovedPremisesEntityFactory import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1PremisesService @@ -33,7 +33,7 @@ class Cas1PremiseCapacitySummaryTransformerTest { fun toCas1PremiseCapacitySummary() { val premise = ApprovedPremisesEntityFactory().withDefaults().produce() - val inputPremiseSummaryInfo = mockk() + val inputPremiseSummaryInfo = mockk() val inputCapacitySummary = PremiseCapacitySummary( premise = premise, range = DateRange( @@ -63,9 +63,9 @@ class Cas1PremiseCapacitySummaryTransformerTest { ), ) - val transformedPremiseSummaryInfo = mockk() + val transformedPremiseSummaryInfo = mockk() every { - cas1PremisesTransformer.toPremiseSummary(any()) + cas1PremisesTransformer.toPremises(any()) } returns transformedPremiseSummaryInfo val result = transformer.toCas1PremiseCapacitySummary( diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremisesTransformerTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremisesTransformerTest.kt index 34c3838b73..1166accb11 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremisesTransformerTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremisesTransformerTest.kt @@ -58,8 +58,8 @@ class Cas1PremisesTransformerTest { val expectedApArea = ApArea(UUID.randomUUID(), "id", "name") every { apAreaTransformer.transformJpaToApi(apArea) } returns expectedApArea - val result = transformer.toPremiseSummary( - Cas1PremisesService.Cas1PremisesSummaryInfo( + val result = transformer.toPremises( + Cas1PremisesService.Cas1PremisesInfo( entity = premises, bedCount = 10, outOfServiceBeds = 2, From 161e82470a26f253536c020a713b860666a0b59d Mon Sep 17 00:00:00 2001 From: davidatkinsuk Date: Thu, 9 Jan 2025 08:51:10 +0000 Subject: [PATCH 2/3] Add function to build CAS1 premises full address This logic was previously in the `Cas1SpaceSearchResultsTransformer`. It has been moved to the `ApprovedPremisesEntity` to allow it be used in additional transformers --- .../jpa/entity/PremisesEntity.kt | 25 ++++++++- .../cas1/Cas1SpaceSearchResultsTransformer.kt | 13 ++--- .../factory/ApprovedPremisesEntityFactory.kt | 8 +-- .../unit/entity/ApprovedPremisesEntityTest.kt | 52 +++++++++++++++++++ 4 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/entity/ApprovedPremisesEntityTest.kt diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/jpa/entity/PremisesEntity.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/jpa/entity/PremisesEntity.kt index 42c98ba806..60b13041b4 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/jpa/entity/PremisesEntity.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/jpa/entity/PremisesEntity.kt @@ -21,6 +21,7 @@ import org.springframework.data.jpa.repository.Query import org.springframework.stereotype.Repository import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.BookingStatus import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.PropertyStatus +import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas1.CandidatePremises import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspacesEntity import java.time.LocalDate import java.util.UUID @@ -291,6 +292,8 @@ class ApprovedPremisesEntity( * Full Address, excluding postcode. When defined this should be used instead of [addressLine1], [addressLine2] and [town] * * Whilst currently nullable, once all site surveys have been imported this can be made non-null + * + * It's recommended that [resolveFullAddress] is used in the meantime when address is required */ var fullAddress: String?, ) : PremisesEntity( @@ -311,7 +314,27 @@ class ApprovedPremisesEntity( rooms, characteristics, status, -) +) { + + fun resolveFullAddress() = resolveFullAddress( + fullAddress = fullAddress, + addressLine1 = addressLine1, + addressLine2 = addressLine2, + town = town, + ) + + companion object { + fun resolveFullAddress( + fullAddress: String?, + addressLine1: String, + addressLine2: String?, + town: String?, + ) = fullAddress + ?: listOf(addressLine1, addressLine2, town) + .filter { !it.isNullOrBlank() } + .joinToString(separator = ", ") + } +} enum class ApprovedPremisesGender { MAN, diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1SpaceSearchResultsTransformer.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1SpaceSearchResultsTransformer.kt index fa61145468..6c3cd10c75 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1SpaceSearchResultsTransformer.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1SpaceSearchResultsTransformer.kt @@ -7,6 +7,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceChara import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceSearchParameters import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceSearchResults import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.NamedId +import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesEntity import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas1.CandidatePremises import uk.gov.justice.digital.hmpps.approvedpremisesapi.model.asApiType import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1SpaceSearchResult as ApiSpaceSearchResult @@ -52,10 +53,10 @@ class Cas1SpaceSearchResultsTransformer { }, ) - fun CandidatePremises.resolveFullAddress(): String { - return fullAddress - ?: listOf(addressLine1, addressLine2, town) - .filter { !it.isNullOrBlank() } - .joinToString(separator = ", ") - } + fun CandidatePremises.resolveFullAddress() = ApprovedPremisesEntity.resolveFullAddress( + fullAddress = fullAddress, + addressLine1 = addressLine1, + addressLine2 = addressLine2, + town = town, + ) } diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/factory/ApprovedPremisesEntityFactory.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/factory/ApprovedPremisesEntityFactory.kt index 17534a9648..d14a6d8f3e 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/factory/ApprovedPremisesEntityFactory.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/factory/ApprovedPremisesEntityFactory.kt @@ -31,8 +31,8 @@ class ApprovedPremisesEntityFactory : Factory { private var latitude: Yielded = { randomDouble(53.50, 54.99) } private var longitude: Yielded = { randomDouble(-1.56, 1.10) } private var addressLine1: Yielded = { randomStringUpperCase(10) } - private var addressLine2: Yielded = { randomStringUpperCase(10) } - private var town: Yielded = { randomStringUpperCase(10) } + private var addressLine2: Yielded = { randomStringUpperCase(10) } + private var town: Yielded = { randomStringUpperCase(10) } private var notes: Yielded = { randomStringUpperCase(15) } private var emailAddress: Yielded = { randomStringUpperCase(10) } private var service: Yielded = { "CAS1" } @@ -70,11 +70,11 @@ class ApprovedPremisesEntityFactory : Factory { this.addressLine1 = { addressLine1 } } - fun withAddressLine2(addressLine2: String) = apply { + fun withAddressLine2(addressLine2: String?) = apply { this.addressLine2 = { addressLine2 } } - fun withTown(town: String) = apply { + fun withTown(town: String?) = apply { this.town = { town } } diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/entity/ApprovedPremisesEntityTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/entity/ApprovedPremisesEntityTest.kt new file mode 100644 index 0000000000..9ec9a72a48 --- /dev/null +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/entity/ApprovedPremisesEntityTest.kt @@ -0,0 +1,52 @@ +package uk.gov.justice.digital.hmpps.approvedpremisesapi.unit.entity + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import uk.gov.justice.digital.hmpps.approvedpremisesapi.factory.ApprovedPremisesEntityFactory + +class ApprovedPremisesEntityTest { + + @Nested + inner class ResolveFullAddress { + + @Test + fun `uses full address on entity, if defined`() { + val approvedPremises = ApprovedPremisesEntityFactory() + .withDefaults() + .withFullAddress("The, full, address") + .withAddressLine1("line1") + .withAddressLine2("line2") + .withTown("town") + .produce() + + assertThat(approvedPremises.resolveFullAddress()).isEqualTo("The, full, address") + } + + @Test + fun `uses address parts, if no full address defined, all parts provided`() { + val approvedPremises = ApprovedPremisesEntityFactory() + .withDefaults() + .withFullAddress(null) + .withAddressLine1("line1") + .withAddressLine2("line2") + .withTown("town") + .produce() + + assertThat(approvedPremises.resolveFullAddress()).isEqualTo("line1, line2, town") + } + + @Test + fun `uses address parts, if no full address defined, mandatory parts only`() { + val approvedPremises = ApprovedPremisesEntityFactory() + .withDefaults() + .withFullAddress(null) + .withAddressLine1("line1") + .withAddressLine2(null) + .withTown(null) + .produce() + + assertThat(approvedPremises.resolveFullAddress()).isEqualTo("line1") + } + } +} From 1df3bd8c566a635f919777eb96b57e299717e12a Mon Sep 17 00:00:00 2001 From: davidatkinsuk Date: Thu, 9 Jan 2025 09:02:41 +0000 Subject: [PATCH 3/3] Add fullAddress to all CAS1 premise API types --- .../jpa/entity/PremisesEntity.kt | 15 ++++++++++++--- .../transformer/cas1/Cas1PremisesTransformer.kt | 9 +++++++++ .../cas1/Cas1SpaceSearchResultsTransformer.kt | 10 +++++----- src/main/resources/static/cas1-schemas.yml | 11 +++++++++++ .../static/codegen/built-cas1-api-spec.yml | 11 +++++++++++ .../cas1/Cas1PremisesTransformerTest.kt | 9 +++++++++ 6 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/jpa/entity/PremisesEntity.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/jpa/entity/PremisesEntity.kt index 60b13041b4..5faa3e5ee4 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/jpa/entity/PremisesEntity.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/jpa/entity/PremisesEntity.kt @@ -21,7 +21,6 @@ import org.springframework.data.jpa.repository.Query import org.springframework.stereotype.Repository import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.BookingStatus import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.PropertyStatus -import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas1.CandidatePremises import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.cas3.Cas3VoidBedspacesEntity import java.time.LocalDate import java.util.UUID @@ -189,7 +188,12 @@ interface ApprovedPremisesRepository : JpaRepository @@ -421,6 +425,11 @@ data class ApprovedPremisesBasicSummary( val apAreaName: String, val bedCount: Int, val supportsSpaceBookings: Boolean, + val fullAddress: String?, + val addressLine1: String, + val addressLine2: String?, + val town: String?, + val postcode: String, ) interface BookingSummary { diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremisesTransformer.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremisesTransformer.kt index cde8674463..3b7e13fe36 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremisesTransformer.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1PremisesTransformer.kt @@ -5,6 +5,7 @@ import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1Premises import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.Cas1PremisesBasicSummary import uk.gov.justice.digital.hmpps.approvedpremisesapi.api.model.NamedId import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesBasicSummary +import uk.gov.justice.digital.hmpps.approvedpremisesapi.jpa.entity.ApprovedPremisesEntity import uk.gov.justice.digital.hmpps.approvedpremisesapi.service.cas1.Cas1PremisesService import uk.gov.justice.digital.hmpps.approvedpremisesapi.transformer.ApAreaTransformer @@ -18,6 +19,7 @@ class Cas1PremisesTransformer( id = entity.id, name = entity.name, apCode = entity.apCode, + fullAddress = entity.resolveFullAddress(), postcode = entity.postcode, bedCount = premisesSummaryInfo.bedCount, availableBeds = premisesSummaryInfo.availableBeds, @@ -37,6 +39,13 @@ class Cas1PremisesTransformer( apArea = NamedId(entity.apAreaId, entity.apAreaName), bedCount = entity.bedCount, supportsSpaceBookings = entity.supportsSpaceBookings, + fullAddress = ApprovedPremisesEntity.resolveFullAddress( + fullAddress = entity.fullAddress, + addressLine1 = entity.addressLine1, + addressLine2 = entity.addressLine2, + town = entity.town, + ), + postcode = entity.postcode, ) } } diff --git a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1SpaceSearchResultsTransformer.kt b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1SpaceSearchResultsTransformer.kt index 6c3cd10c75..853af8ccf8 100644 --- a/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1SpaceSearchResultsTransformer.kt +++ b/src/main/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/transformer/cas1/Cas1SpaceSearchResultsTransformer.kt @@ -54,9 +54,9 @@ class Cas1SpaceSearchResultsTransformer { ) fun CandidatePremises.resolveFullAddress() = ApprovedPremisesEntity.resolveFullAddress( - fullAddress = fullAddress, - addressLine1 = addressLine1, - addressLine2 = addressLine2, - town = town, - ) + fullAddress = fullAddress, + addressLine1 = addressLine1, + addressLine2 = addressLine2, + town = town, + ) } diff --git a/src/main/resources/static/cas1-schemas.yml b/src/main/resources/static/cas1-schemas.yml index 437b52d33d..03b900b135 100644 --- a/src/main/resources/static/cas1-schemas.yml +++ b/src/main/resources/static/cas1-schemas.yml @@ -19,12 +19,19 @@ components: example: 22 supportsSpaceBookings: type: boolean + fullAddress: + description: Full address, excluding postcode + type: string + postcode: + type: string required: - id - name - apArea - bedCount - supportsSpaceBookings + - fullAddress + - postcode Cas1PremisesSearchResultSummary: type: object properties: @@ -89,6 +96,9 @@ components: apCode: type: string example: NEHOPE1 + fullAddress: + description: Full address, excluding postcode + type: string postcode: type: string example: LS1 3AD @@ -119,6 +129,7 @@ components: - id - name - apCode + - fullAddress - postcode - apArea - bedCount diff --git a/src/main/resources/static/codegen/built-cas1-api-spec.yml b/src/main/resources/static/codegen/built-cas1-api-spec.yml index 67ab6add9e..5e48de7441 100644 --- a/src/main/resources/static/codegen/built-cas1-api-spec.yml +++ b/src/main/resources/static/codegen/built-cas1-api-spec.yml @@ -6036,12 +6036,19 @@ components: example: 22 supportsSpaceBookings: type: boolean + fullAddress: + description: Full address, excluding postcode + type: string + postcode: + type: string required: - id - name - apArea - bedCount - supportsSpaceBookings + - fullAddress + - postcode Cas1PremisesSearchResultSummary: type: object properties: @@ -6106,6 +6113,9 @@ components: apCode: type: string example: NEHOPE1 + fullAddress: + description: Full address, excluding postcode + type: string postcode: type: string example: LS1 3AD @@ -6136,6 +6146,7 @@ components: - id - name - apCode + - fullAddress - postcode - apArea - bedCount diff --git a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremisesTransformerTest.kt b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremisesTransformerTest.kt index 1166accb11..d1d3f71dd1 100644 --- a/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremisesTransformerTest.kt +++ b/src/test/kotlin/uk/gov/justice/digital/hmpps/approvedpremisesapi/unit/transformer/cas1/Cas1PremisesTransformerTest.kt @@ -49,6 +49,7 @@ class Cas1PremisesTransformerTest { .withId(PREMISES_ID) .withName("the name") .withApCode("the ap code") + .withFullAddress("the full address") .withPostcode("LE11 1PO") .withProbationRegion(probationRegion) .withSupportsSpaceBookings(true) @@ -71,6 +72,7 @@ class Cas1PremisesTransformerTest { assertThat(result.id).isEqualTo(premises.id) assertThat(result.name).isEqualTo("the name") assertThat(result.apCode).isEqualTo("the ap code") + assertThat(result.fullAddress).isEqualTo("the full address") assertThat(result.postcode).isEqualTo("LE11 1PO") assertThat(result.bedCount).isEqualTo(10) assertThat(result.availableBeds).isEqualTo(8) @@ -95,6 +97,11 @@ class Cas1PremisesTransformerTest { "the ap area name", 12, supportsSpaceBookings = false, + fullAddress = "the full address", + addressLine1 = "line 1", + addressLine2 = null, + town = null, + postcode = "the postcode", ) val result = transformer.toPremiseBasicSummary(premisesSummary) @@ -106,6 +113,8 @@ class Cas1PremisesTransformerTest { assertThat(result.apArea.name).isEqualTo("the ap area name") assertThat(result.bedCount).isEqualTo(12) assertThat(result.supportsSpaceBookings).isEqualTo(false) + assertThat(result.fullAddress).isEqualTo("the full address") + assertThat(result.postcode).isEqualTo("the postcode") } } }