Skip to content

Commit

Permalink
Add Business Unit reference data endpoint & infrastructure code
Browse files Browse the repository at this point in the history
  • Loading branch information
RustyHMCTS committed May 3, 2024
1 parent 30900f0 commit eaab572
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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}")
Expand All @@ -56,5 +63,15 @@ public ResponseEntity<List<BusinessUnitEntity>> 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<BusinessUnitReferenceDataResults> getBusinessUnitRefData(
@PathVariable Optional<String> filter) {
log.info(":GET:getBusinessUnitRefData: query: \n{}", filter);

List<BusinessUnitEntity> refData = opalBusinessUnitService.getReferenceData(filter);

log.info(":GET:getBusinessUnitRefData: business unit reference data count: {}", refData.size());
return ResponseEntity.ok(BusinessUnitReferenceDataResults.builder().refData(refData).build());
}
}
Original file line number Diff line number Diff line change
@@ -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<BusinessUnitEntity> refData;

public static class BusinessUnitReferenceDataResultsBuilder {
public BusinessUnitReferenceDataResults.BusinessUnitReferenceDataResultsBuilder refData(
List<BusinessUnitEntity> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public static class OffenceReferenceDataResultsBuilder {
public OffenceReferenceDataResults.OffenceReferenceDataResultsBuilder refData(
List<OffenceReferenceData> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<BusinessUnitEntity> {

public Specification<BusinessUnitEntity> findBySearchCriteria(BusinessUnitSearchDto criteria) {
Expand All @@ -21,6 +23,12 @@ public Specification<BusinessUnitEntity> findBySearchCriteria(BusinessUnitSearch
));
}

public Specification<BusinessUnitEntity> referenceDataFilter(Optional<String> filter) {
return Specification.allOf(specificationList(
filter.filter(s -> !s.isBlank()).map(BusinessUnitSpecs::likeAnyBusinessUnit)
));
}

public static Specification<BusinessUnitEntity> equalsBusinessUnitId(Short businessUnitId) {
return (root, query, builder) -> equalsBusinessUnitIdPredicate(root, builder, businessUnitId);
}
Expand Down Expand Up @@ -71,4 +79,11 @@ public static Predicate equalsParentBusinessUnitIdPredicate(
return builder.equal(from.get(BusinessUnitEntity_.parentBusinessUnitId), parentBusinessUnitId);
}

public static Specification<BusinessUnitEntity> likeAnyBusinessUnit(String filter) {
return Specification.anyOf(
likeBusinessUnitName(filter),
likeBusinessUnitCode(filter),
likeBusinessUnitType(filter)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,4 +40,16 @@ public List<BusinessUnitEntity> searchBusinessUnits(BusinessUnitSearchDto criter
return page.getContent();
}

public List<BusinessUnitEntity> getReferenceData(Optional<String> filter) {

Sort nameSort = Sort.by(Sort.Direction.ASC, BusinessUnitEntity_.BUSINESS_UNIT_NAME);

Page<BusinessUnitEntity> page = businessUnitRepository
.findBy(specs.referenceDataFilter(filter),
ffq -> ffq
.sortBy(nameSort)
.page(Pageable.unpaged()));

return page.getContent();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -63,4 +64,24 @@ void testSearchBusinessUnits_Success() {
verify(businessUnitService, times(1)).searchBusinessUnits(any());
}

@Test
void testGetBusinessUnitsRefData_Success() {
// Arrange
BusinessUnitEntity entity = BusinessUnitEntity.builder().build();
List<BusinessUnitEntity> businessUnitList = List.of(entity);

when(businessUnitService.getReferenceData(any())).thenReturn(businessUnitList);

// Act
Optional<String> filter = Optional.empty();
ResponseEntity<BusinessUnitReferenceDataResults> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BusinessUnitEntity> 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<BusinessUnitEntity> result = businessUnitService.getReferenceData(Optional.empty());

// Assert
assertEquals(List.of(businessUnitEntity), result);

}

}

0 comments on commit eaab572

Please sign in to comment.