From eaab5728b0d76f0dc42f71021a4405ee3a8f3153 Mon Sep 17 00:00:00 2001 From: Russell Dodd Date: Wed, 1 May 2024 12:21:03 +0100 Subject: [PATCH] Add Business Unit reference data endpoint & infrastructure code --- ...BusinessUnitControllerIntegrationTest.java | 3 +- .../{develop => }/BusinessUnitController.java | 21 ++++++++++-- .../BusinessUnitReferenceDataResults.java | 32 +++++++++++++++++++ .../OffenceReferenceDataResults.java | 3 +- .../repository/jpa/BusinessUnitSpecs.java | 15 +++++++++ .../service/opal/BusinessUnitService.java | 15 +++++++++ .../BusinessUnitControllerTest.java | 25 +++++++++++++-- .../service/opal/BusinessUnitServiceTest.java | 22 +++++++++++++ 8 files changed, 129 insertions(+), 7 deletions(-) rename src/integrationTest/java/uk/gov/hmcts/opal/controllers/{develop => }/BusinessUnitControllerIntegrationTest.java (97%) rename src/main/java/uk/gov/hmcts/opal/controllers/{develop => }/BusinessUnitController.java (67%) create mode 100644 src/main/java/uk/gov/hmcts/opal/dto/reference/BusinessUnitReferenceDataResults.java rename src/test/java/uk/gov/hmcts/opal/controllers/{develop => }/BusinessUnitControllerTest.java (69%) diff --git a/src/integrationTest/java/uk/gov/hmcts/opal/controllers/develop/BusinessUnitControllerIntegrationTest.java b/src/integrationTest/java/uk/gov/hmcts/opal/controllers/BusinessUnitControllerIntegrationTest.java similarity index 97% rename from src/integrationTest/java/uk/gov/hmcts/opal/controllers/develop/BusinessUnitControllerIntegrationTest.java rename to src/integrationTest/java/uk/gov/hmcts/opal/controllers/BusinessUnitControllerIntegrationTest.java index 06e97024b..4488c8d47 100644 --- a/src/integrationTest/java/uk/gov/hmcts/opal/controllers/develop/BusinessUnitControllerIntegrationTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/opal/controllers/BusinessUnitControllerIntegrationTest.java @@ -1,4 +1,4 @@ -package uk.gov.hmcts.opal.controllers.develop; +package uk.gov.hmcts.opal.controllers; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -9,6 +9,7 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.web.servlet.MockMvc; +import uk.gov.hmcts.opal.controllers.BusinessUnitController; import uk.gov.hmcts.opal.dto.search.BusinessUnitSearchDto; import uk.gov.hmcts.opal.entity.BusinessUnitEntity; import uk.gov.hmcts.opal.service.opal.BusinessUnitService; diff --git a/src/main/java/uk/gov/hmcts/opal/controllers/develop/BusinessUnitController.java b/src/main/java/uk/gov/hmcts/opal/controllers/BusinessUnitController.java similarity index 67% rename from src/main/java/uk/gov/hmcts/opal/controllers/develop/BusinessUnitController.java rename to src/main/java/uk/gov/hmcts/opal/controllers/BusinessUnitController.java index f47fa5e5b..66e7b6103 100644 --- a/src/main/java/uk/gov/hmcts/opal/controllers/develop/BusinessUnitController.java +++ b/src/main/java/uk/gov/hmcts/opal/controllers/BusinessUnitController.java @@ -1,4 +1,4 @@ -package uk.gov.hmcts.opal.controllers.develop; +package uk.gov.hmcts.opal.controllers; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; @@ -12,11 +12,14 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import uk.gov.hmcts.opal.dto.reference.BusinessUnitReferenceDataResults; import uk.gov.hmcts.opal.dto.search.BusinessUnitSearchDto; import uk.gov.hmcts.opal.entity.BusinessUnitEntity; import uk.gov.hmcts.opal.service.BusinessUnitServiceInterface; +import uk.gov.hmcts.opal.service.opal.BusinessUnitService; import java.util.List; +import java.util.Optional; import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse; @@ -29,9 +32,13 @@ public class BusinessUnitController { private final BusinessUnitServiceInterface businessUnitService; + private final BusinessUnitService opalBusinessUnitService; + public BusinessUnitController( - @Qualifier("businessUnitService") BusinessUnitServiceInterface businessUnitService) { + @Qualifier("businessUnitService") BusinessUnitServiceInterface businessUnitService, + BusinessUnitService opalBusinessUnitService) { this.businessUnitService = businessUnitService; + this.opalBusinessUnitService = opalBusinessUnitService; } @GetMapping(value = "/{businessUnitId}") @@ -56,5 +63,15 @@ public ResponseEntity> postBusinessUnitsSearch( return buildResponse(response); } + @GetMapping(value = {"/ref-data", "/ref-data/", "/ref-data/{filter}"}) + @Operation(summary = "Returns Business Units as reference data with an option filter applied") + public ResponseEntity getBusinessUnitRefData( + @PathVariable Optional filter) { + log.info(":GET:getBusinessUnitRefData: query: \n{}", filter); + + List refData = opalBusinessUnitService.getReferenceData(filter); + log.info(":GET:getBusinessUnitRefData: business unit reference data count: {}", refData.size()); + return ResponseEntity.ok(BusinessUnitReferenceDataResults.builder().refData(refData).build()); + } } diff --git a/src/main/java/uk/gov/hmcts/opal/dto/reference/BusinessUnitReferenceDataResults.java b/src/main/java/uk/gov/hmcts/opal/dto/reference/BusinessUnitReferenceDataResults.java new file mode 100644 index 000000000..f02e6ec55 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/dto/reference/BusinessUnitReferenceDataResults.java @@ -0,0 +1,32 @@ +package uk.gov.hmcts.opal.dto.reference; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import uk.gov.hmcts.opal.entity.BusinessUnitEntity; + +import java.util.List; +import java.util.Optional; + +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class BusinessUnitReferenceDataResults { + private Integer count; + private List refData; + + public static class BusinessUnitReferenceDataResultsBuilder { + public BusinessUnitReferenceDataResults.BusinessUnitReferenceDataResultsBuilder refData( + List refData) { + this.refData = refData; + return this.count(Optional.ofNullable(refData).map(List::size).orElse(0)); + } + + private BusinessUnitReferenceDataResults.BusinessUnitReferenceDataResultsBuilder count(Integer count) { + this.count = count; + return this; + } + } +} diff --git a/src/main/java/uk/gov/hmcts/opal/dto/reference/OffenceReferenceDataResults.java b/src/main/java/uk/gov/hmcts/opal/dto/reference/OffenceReferenceDataResults.java index 94ffb2151..97984358c 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/reference/OffenceReferenceDataResults.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/reference/OffenceReferenceDataResults.java @@ -21,8 +21,7 @@ public static class OffenceReferenceDataResultsBuilder { public OffenceReferenceDataResults.OffenceReferenceDataResultsBuilder refData( List refData) { this.refData = refData; - this.count(Optional.ofNullable(refData).map(List::size).orElse(0)); - return this; + return this.count(Optional.ofNullable(refData).map(List::size).orElse(0)); } private OffenceReferenceDataResults.OffenceReferenceDataResultsBuilder count(Integer count) { diff --git a/src/main/java/uk/gov/hmcts/opal/repository/jpa/BusinessUnitSpecs.java b/src/main/java/uk/gov/hmcts/opal/repository/jpa/BusinessUnitSpecs.java index b1c65d68d..c53dc1c6b 100644 --- a/src/main/java/uk/gov/hmcts/opal/repository/jpa/BusinessUnitSpecs.java +++ b/src/main/java/uk/gov/hmcts/opal/repository/jpa/BusinessUnitSpecs.java @@ -8,6 +8,8 @@ import uk.gov.hmcts.opal.entity.BusinessUnitEntity; import uk.gov.hmcts.opal.entity.BusinessUnitEntity_; +import java.util.Optional; + public class BusinessUnitSpecs extends EntitySpecs { public Specification findBySearchCriteria(BusinessUnitSearchDto criteria) { @@ -21,6 +23,12 @@ public Specification findBySearchCriteria(BusinessUnitSearch )); } + public Specification referenceDataFilter(Optional filter) { + return Specification.allOf(specificationList( + filter.filter(s -> !s.isBlank()).map(BusinessUnitSpecs::likeAnyBusinessUnit) + )); + } + public static Specification equalsBusinessUnitId(Short businessUnitId) { return (root, query, builder) -> equalsBusinessUnitIdPredicate(root, builder, businessUnitId); } @@ -71,4 +79,11 @@ public static Predicate equalsParentBusinessUnitIdPredicate( return builder.equal(from.get(BusinessUnitEntity_.parentBusinessUnitId), parentBusinessUnitId); } + public static Specification likeAnyBusinessUnit(String filter) { + return Specification.anyOf( + likeBusinessUnitName(filter), + likeBusinessUnitCode(filter), + likeBusinessUnitType(filter) + ); + } } diff --git a/src/main/java/uk/gov/hmcts/opal/service/opal/BusinessUnitService.java b/src/main/java/uk/gov/hmcts/opal/service/opal/BusinessUnitService.java index cb2e24e74..f9eb1bb2e 100644 --- a/src/main/java/uk/gov/hmcts/opal/service/opal/BusinessUnitService.java +++ b/src/main/java/uk/gov/hmcts/opal/service/opal/BusinessUnitService.java @@ -5,14 +5,17 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import uk.gov.hmcts.opal.dto.search.BusinessUnitSearchDto; import uk.gov.hmcts.opal.entity.BusinessUnitEntity; +import uk.gov.hmcts.opal.entity.BusinessUnitEntity_; import uk.gov.hmcts.opal.repository.BusinessUnitRepository; import uk.gov.hmcts.opal.repository.jpa.BusinessUnitSpecs; import uk.gov.hmcts.opal.service.BusinessUnitServiceInterface; import java.util.List; +import java.util.Optional; @Service @RequiredArgsConstructor @@ -37,4 +40,16 @@ public List searchBusinessUnits(BusinessUnitSearchDto criter return page.getContent(); } + public List getReferenceData(Optional filter) { + + Sort nameSort = Sort.by(Sort.Direction.ASC, BusinessUnitEntity_.BUSINESS_UNIT_NAME); + + Page page = businessUnitRepository + .findBy(specs.referenceDataFilter(filter), + ffq -> ffq + .sortBy(nameSort) + .page(Pageable.unpaged())); + + return page.getContent(); + } } diff --git a/src/test/java/uk/gov/hmcts/opal/controllers/develop/BusinessUnitControllerTest.java b/src/test/java/uk/gov/hmcts/opal/controllers/BusinessUnitControllerTest.java similarity index 69% rename from src/test/java/uk/gov/hmcts/opal/controllers/develop/BusinessUnitControllerTest.java rename to src/test/java/uk/gov/hmcts/opal/controllers/BusinessUnitControllerTest.java index cb5e0dc80..2740f2504 100644 --- a/src/test/java/uk/gov/hmcts/opal/controllers/develop/BusinessUnitControllerTest.java +++ b/src/test/java/uk/gov/hmcts/opal/controllers/BusinessUnitControllerTest.java @@ -1,4 +1,4 @@ -package uk.gov.hmcts.opal.controllers.develop; +package uk.gov.hmcts.opal.controllers; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -7,12 +7,13 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import uk.gov.hmcts.opal.controllers.develop.BusinessUnitController; +import uk.gov.hmcts.opal.dto.reference.BusinessUnitReferenceDataResults; import uk.gov.hmcts.opal.dto.search.BusinessUnitSearchDto; import uk.gov.hmcts.opal.entity.BusinessUnitEntity; import uk.gov.hmcts.opal.service.opal.BusinessUnitService; import java.util.List; +import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; @@ -63,4 +64,24 @@ void testSearchBusinessUnits_Success() { verify(businessUnitService, times(1)).searchBusinessUnits(any()); } + @Test + void testGetBusinessUnitsRefData_Success() { + // Arrange + BusinessUnitEntity entity = BusinessUnitEntity.builder().build(); + List businessUnitList = List.of(entity); + + when(businessUnitService.getReferenceData(any())).thenReturn(businessUnitList); + + // Act + Optional filter = Optional.empty(); + ResponseEntity response = businessUnitController + .getBusinessUnitRefData(filter); + + // Assert + assertEquals(HttpStatus.OK, response.getStatusCode()); + BusinessUnitReferenceDataResults refDataResults = response.getBody(); + assertEquals(1, refDataResults.getCount()); + assertEquals(businessUnitList, refDataResults.getRefData()); + verify(businessUnitService, times(1)).getReferenceData(any()); + } } diff --git a/src/test/java/uk/gov/hmcts/opal/service/opal/BusinessUnitServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/opal/BusinessUnitServiceTest.java index a27d61aa0..549c59832 100644 --- a/src/test/java/uk/gov/hmcts/opal/service/opal/BusinessUnitServiceTest.java +++ b/src/test/java/uk/gov/hmcts/opal/service/opal/BusinessUnitServiceTest.java @@ -16,6 +16,7 @@ import uk.gov.hmcts.opal.repository.BusinessUnitRepository; import java.util.List; +import java.util.Optional; import java.util.function.Function; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -69,5 +70,26 @@ void testSearchBusinessUnits() { } + @SuppressWarnings("unchecked") + @Test + void testBusinessUnitsReferenceData() { + // Arrange + FluentQuery.FetchableFluentQuery ffq = Mockito.mock(FluentQuery.FetchableFluentQuery.class); + when(ffq.sortBy(any())).thenReturn(ffq); + + BusinessUnitEntity businessUnitEntity = BusinessUnitEntity.builder().build(); + Page mockPage = new PageImpl<>(List.of(businessUnitEntity), Pageable.unpaged(), 999L); + when(businessUnitRepository.findBy(any(Specification.class), any())).thenAnswer(iom -> { + iom.getArgument(1, Function.class).apply(ffq); + return mockPage; + }); + + // Act + List result = businessUnitService.getReferenceData(Optional.empty()); + + // Assert + assertEquals(List.of(businessUnitEntity), result); + + } }