Skip to content

Commit

Permalink
Add Major Creditor reference data endpoint & associated code
Browse files Browse the repository at this point in the history
  • Loading branch information
RustyHMCTS committed May 21, 2024
1 parent 729b53e commit 91a1592
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
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.entity.projection.BusinessUnitReferenceData;
import uk.gov.hmcts.opal.service.opal.BusinessUnitService;

import static java.util.Collections.singletonList;
Expand Down Expand Up @@ -49,6 +49,7 @@ void testGetBusinessUnitById() throws Exception {
.andExpect(jsonPath("$.businessUnitCode").value("AAAA"))
.andExpect(jsonPath("$.businessUnitType").value("LARGE UNIT"))
.andExpect(jsonPath("$.accountNumberPrefix").value("XX"))
.andExpect(jsonPath("$.opalDomain").value("Fines"))
.andExpect(jsonPath("$.parentBusinessUnit.businessUnitId").value(99));
}

Expand Down Expand Up @@ -78,6 +79,7 @@ void testPostBusinessUnitsSearch() throws Exception {
.andExpect(jsonPath("$[0].businessUnitCode").value("AAAA"))
.andExpect(jsonPath("$[0].businessUnitType").value("LARGE UNIT"))
.andExpect(jsonPath("$[0].accountNumberPrefix").value("XX"))
.andExpect(jsonPath("$[0].opalDomain").value("Fines"))
.andExpect(jsonPath("$[0].parentBusinessUnit.businessUnitId").value(99));
}

Expand All @@ -89,6 +91,26 @@ void testPostBusinessUnitsSearch_WhenBusinessUnitDoesNotExist() throws Exception
.andExpect(status().isNoContent());
}

@Test
void testGetCourtRefData() throws Exception {
BusinessUnitReferenceData refData = createBusinessUnitRefData();

when(businessUnitService.getReferenceData(any())).thenReturn(singletonList(refData));

mockMvc.perform(get("/api/business-unit/ref-data")
.header("authorization", "Bearer some_value"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.count").value(1))
.andExpect(jsonPath("$.refData[0].businessUnitId").value(1))
.andExpect(jsonPath("$.refData[0].businessUnitName").value("Business Unit 001"))
.andExpect(jsonPath("$.refData[0].businessUnitCode").value("AAAA"))
.andExpect(jsonPath("$.refData[0].businessUnitType").value("LARGE UNIT"))
.andExpect(jsonPath("$.refData[0].accountNumberPrefix").value("XX"))
.andExpect(jsonPath("$.refData[0].opalDomain").value("Fines"));
}


private BusinessUnitEntity createBusinessUnitEntity() {
return BusinessUnitEntity.builder()
.businessUnitId((short)1)
Expand All @@ -97,6 +119,42 @@ private BusinessUnitEntity createBusinessUnitEntity() {
.businessUnitType("LARGE UNIT")
.accountNumberPrefix("XX")
.parentBusinessUnit(BusinessUnitEntity.builder().businessUnitId((short)99).build())
.opalDomain("Fines")
.build();
}

private BusinessUnitReferenceData createBusinessUnitRefData() {
return new BusinessUnitReferenceData() {

@Override
public Short getBusinessUnitId() {
return (short)1;
}

@Override
public String getBusinessUnitName() {
return "Business Unit 001";
}

@Override
public String getBusinessUnitCode() {
return "AAAA";
}

@Override
public String getBusinessUnitType() {
return "LARGE UNIT";
}

@Override
public String getAccountNumberPrefix() {
return "XX";
}

@Override
public String getOpalDomain() {
return "Fines";
}
};
}
}
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 @@ -12,6 +12,7 @@
import uk.gov.hmcts.opal.dto.search.MajorCreditorSearchDto;
import uk.gov.hmcts.opal.entity.BusinessUnitEntity;
import uk.gov.hmcts.opal.entity.MajorCreditorEntity;
import uk.gov.hmcts.opal.entity.projection.MajorCreditorReferenceData;
import uk.gov.hmcts.opal.service.opal.MajorCreditorService;

import static java.util.Collections.singletonList;
Expand Down Expand Up @@ -91,6 +92,24 @@ void testPostMajorCreditorsSearch_WhenMajorCreditorDoesNotExist() throws Excepti
.andExpect(status().isNoContent());
}

@Test
void testGetMajorCreditorRefData() throws Exception {
MajorCreditorReferenceData refData = new MajorCreditorReferenceData(1L, "MC_001",
"Major Credit Card Ltd", "MN12 4TT");

when(majorCreditorService.getReferenceData(any())).thenReturn(singletonList(refData));

mockMvc.perform(get("/api/major-creditor/ref-data")
.header("authorization", "Bearer some_value"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.count").value(1))
.andExpect(jsonPath("$.refData[0].majorCreditorId").value(1))
.andExpect(jsonPath("$.refData[0].majorCreditorCode").value("MC_001"))
.andExpect(jsonPath("$.refData[0].name").value("Major Credit Card Ltd"))
.andExpect(jsonPath("$.refData[0].postcode").value("MN12 4TT"));
}

private MajorCreditorEntity createMajorCreditorEntity() {
return MajorCreditorEntity.builder()
.majorCreditorId(1L)
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,15 @@
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.MajorCreditorReferenceDataResults;
import uk.gov.hmcts.opal.dto.search.MajorCreditorSearchDto;
import uk.gov.hmcts.opal.entity.MajorCreditorEntity;
import uk.gov.hmcts.opal.entity.projection.MajorCreditorReferenceData;
import uk.gov.hmcts.opal.service.MajorCreditorServiceInterface;
import uk.gov.hmcts.opal.service.opal.MajorCreditorService;

import java.util.List;
import java.util.Optional;

import static uk.gov.hmcts.opal.util.HttpUtil.buildResponse;

Expand All @@ -29,9 +33,12 @@ public class MajorCreditorController {

private final MajorCreditorServiceInterface majorCreditorService;

public MajorCreditorController(@Qualifier("majorCreditorService")
MajorCreditorServiceInterface majorCreditorService) {
private final MajorCreditorService opalMajorCreditorService;

public MajorCreditorController(@Qualifier("majorCreditorService") MajorCreditorServiceInterface
majorCreditorService, MajorCreditorService opalMajorCreditorService) {
this.majorCreditorService = majorCreditorService;
this.opalMajorCreditorService = opalMajorCreditorService;
}

@GetMapping(value = "/{majorCreditorId}")
Expand All @@ -56,5 +63,15 @@ public ResponseEntity<List<MajorCreditorEntity>> postMajorCreditorsSearch(
return buildResponse(response);
}

@GetMapping(value = {"/ref-data", "/ref-data/", "/ref-data/{filter}"})
@Operation(summary = "Returns MajorCreditors as reference data with an optional filter applied")
public ResponseEntity<MajorCreditorReferenceDataResults> getMajorCreditorRefData(
@PathVariable Optional<String> filter) {
log.info(":GET:getMajorCreditorRefData: query: \n{}", filter);

List<MajorCreditorReferenceData> refData = opalMajorCreditorService.getReferenceData(filter);

log.info(":GET:getMajorCreditorRefData: major creditor reference data count: {}", refData.size());
return ResponseEntity.ok(MajorCreditorReferenceDataResults.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.projection.MajorCreditorReferenceData;

import java.util.List;
import java.util.Optional;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class MajorCreditorReferenceDataResults {
private Integer count;
private List<MajorCreditorReferenceData> refData;

public static class MajorCreditorReferenceDataResultsBuilder {
public MajorCreditorReferenceDataResults.MajorCreditorReferenceDataResultsBuilder refData(
List<MajorCreditorReferenceData> refData) {
this.refData = refData;
return this.count(Optional.ofNullable(refData).map(List::size).orElse(0));
}

private MajorCreditorReferenceDataResults.MajorCreditorReferenceDataResultsBuilder count(Integer count) {
this.count = count;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ public class MajorCreditorEntity extends AddressEntity {
@Column(name = "major_creditor_code", length = 4)
private String majorCreditorCode;

@Column(name = "contact_name", length = 35)
private String contactName;

@Column(name = "contact_telephone", length = 35)
private String contactTelephone;

@Column(name = "contact_email", length = 80)
private String contactEmail;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package uk.gov.hmcts.opal.entity.projection;

public record MajorCreditorReferenceData(Long majorCreditorId, String majorCreditorCode, String name, String postcode) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import uk.gov.hmcts.opal.entity.MajorCreditorEntity;
import uk.gov.hmcts.opal.entity.MajorCreditorEntity_;

import java.util.Optional;

import static uk.gov.hmcts.opal.repository.jpa.BusinessUnitSpecs.equalsBusinessUnitIdPredicate;
import static uk.gov.hmcts.opal.repository.jpa.BusinessUnitSpecs.likeBusinessUnitNamePredicate;

Expand All @@ -21,7 +23,13 @@ public Specification<MajorCreditorEntity> findBySearchCriteria(MajorCreditorSear
numericLong(criteria.getMajorCreditorId()).map(MajorCreditorSpecs::equalsMajorCreditorId),
numericShort(criteria.getBusinessUnitId()).map(MajorCreditorSpecs::equalsBusinessUnitId),
notBlank(criteria.getBusinessUnitName()).map(MajorCreditorSpecs::likeBusinessUnitName),
numericShort(criteria.getMajorCreditorCode()).map(MajorCreditorSpecs::equalsMajorCreditorCode)
notBlank(criteria.getMajorCreditorCode()).map(MajorCreditorSpecs::likeMajorCreditorCode)
));
}

public Specification<MajorCreditorEntity> referenceDataFilter(Optional<String> filter) {
return Specification.allOf(specificationList(
filter.filter(s -> !s.isBlank()).map(this::likeAnyMajorCreditor)
));
}

Expand All @@ -44,13 +52,20 @@ public static Specification<MajorCreditorEntity> likeBusinessUnitName(String bus
likeBusinessUnitNamePredicate(joinBusinessUnit(root), builder, businessUnitName);
}

public static Specification<MajorCreditorEntity> equalsMajorCreditorCode(Short majorCreditorCode) {
return (root, query, builder) -> equalsMajorCreditorCodePredicate(root, builder, majorCreditorCode);
public static Specification<MajorCreditorEntity> likeMajorCreditorCode(String majorCreditorCode) {
return (root, query, builder) -> likeMajorCreditorCodePredicate(root, builder, majorCreditorCode);
}

public static Predicate likeMajorCreditorCodePredicate(
From<?, MajorCreditorEntity> from, CriteriaBuilder builder, String majorCreditorCode) {
return likeWildcardPredicate(from.get(MajorCreditorEntity_.majorCreditorCode), builder, majorCreditorCode);
}

public static Predicate equalsMajorCreditorCodePredicate(
From<?, MajorCreditorEntity> from, CriteriaBuilder builder, Short businessUnitId) {
return builder.equal(from.get(MajorCreditorEntity_.majorCreditorCode), businessUnitId);
public Specification<MajorCreditorEntity> likeAnyMajorCreditor(String filter) {
return Specification.anyOf(
likeName(filter),
likeMajorCreditorCode(filter)
);
}

public static Join<MajorCreditorEntity, BusinessUnitEntity> joinBusinessUnit(From<?, MajorCreditorEntity> from) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
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.MajorCreditorSearchDto;
import uk.gov.hmcts.opal.entity.MajorCreditorEntity;
import uk.gov.hmcts.opal.entity.MajorCreditorEntity_;
import uk.gov.hmcts.opal.entity.projection.MajorCreditorReferenceData;
import uk.gov.hmcts.opal.repository.MajorCreditorRepository;
import uk.gov.hmcts.opal.repository.jpa.MajorCreditorSpecs;
import uk.gov.hmcts.opal.service.MajorCreditorServiceInterface;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand All @@ -37,4 +41,25 @@ public List<MajorCreditorEntity> searchMajorCreditors(MajorCreditorSearchDto cri
return page.getContent();
}

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

Sort nameSort = Sort.by(Sort.Direction.ASC, MajorCreditorEntity_.NAME);

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

return page.getContent().stream().map(this::toRefData).toList();
}

private MajorCreditorReferenceData toRefData(MajorCreditorEntity entity) {
return new MajorCreditorReferenceData(
entity.getMajorCreditorId(),
entity.getMajorCreditorCode(),
entity.getName(),
entity.getPostcode()
);
}
}
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,11 +7,14 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import uk.gov.hmcts.opal.dto.reference.MajorCreditorReferenceDataResults;
import uk.gov.hmcts.opal.dto.search.MajorCreditorSearchDto;
import uk.gov.hmcts.opal.entity.MajorCreditorEntity;
import uk.gov.hmcts.opal.entity.projection.MajorCreditorReferenceData;
import uk.gov.hmcts.opal.service.opal.MajorCreditorService;

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 +66,26 @@ void testSearchMajorCreditors_Success() {
verify(majorCreditorService, times(1)).searchMajorCreditors(any());
}

@Test
void testGetMajorCreditorRefData_Success() {
// Arrange
MajorCreditorReferenceData refData = new MajorCreditorReferenceData(1L, "MC_001",
"Major Credit Card Ltd", "MN12 4TT");
List<MajorCreditorReferenceData> refDataList = List.of(refData);

when(majorCreditorService.getReferenceData(any())).thenReturn(refDataList);

// Act
Optional<String> filter = Optional.empty();
ResponseEntity<MajorCreditorReferenceDataResults> response = majorCreditorController
.getMajorCreditorRefData(filter);

// Assert
assertEquals(HttpStatus.OK, response.getStatusCode());
MajorCreditorReferenceDataResults refDataResults = response.getBody();
assertEquals(1, refDataResults.getCount());
assertEquals(refDataList, refDataResults.getRefData());
verify(majorCreditorService, times(1)).getReferenceData(any());
}

}
Loading

0 comments on commit 91a1592

Please sign in to comment.