Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PO-349: Add Major Creditor reference data endpoint & associated code #363

Merged
merged 6 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public class Constants {
public static final String COURTS_REF_DATA_URI = "/api/court/ref-data/";
public static final String LJA_REF_DATA_URI = "/api/local-justice-area/ref-data/";
public static final String OFFENCES_REF_DATA_URI = "/api/offence/ref-data/";

public static final String MAJOR_CREDITORS_URI = "/api/major-creditor/";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static uk.gov.hmcts.opal.config.Constants.COURTS_REF_DATA_URI;
import static uk.gov.hmcts.opal.steps.BearerTokenStepDef.getToken;

public class Courts extends BaseStepDef {
public class CourtsStepDef extends BaseStepDef {

@When("I make a request to the court ref data api with")
public void getRequestToCourtsRefData() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package uk.gov.hmcts.opal.steps;

import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import net.serenitybdd.rest.SerenityRest;

import org.apache.http.HttpStatus;



import static net.serenitybdd.rest.SerenityRest.then;
import static org.hamcrest.Matchers.equalTo;
import static uk.gov.hmcts.opal.config.Constants.MAJOR_CREDITORS_URI;

import static uk.gov.hmcts.opal.steps.BearerTokenStepDef.getToken;

public class MajorCreditorsStepDef extends BaseStepDef {


@When("I make a request to the major creditors ref data api filter by major creditor id {int}")
public void getRequestToMajorCreditorsBy(int majorCreditorId) {

SerenityRest
.given()
.accept("*/*")
.header("Authorization", "Bearer " + getToken())
.contentType("application/json")
.when()
.get(getTestUrl() + MAJOR_CREDITORS_URI + majorCreditorId);

}

@Then("the major creditors ref data matching to result")
public void theMajorCreditorsRefDataMatchingToResult() {
then().assertThat()
.statusCode(HttpStatus.SC_OK)
.body("name",equalTo("CHESTERFIELD BOROUGH COUNCIL"))
.body("majorCreditorId",equalTo(15));



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import static uk.gov.hmcts.opal.steps.BearerTokenStepDef.getToken;


public class Offences extends BaseStepDef {
public class OffencesStepDef extends BaseStepDef {

static Logger log = LoggerFactory.getLogger(Offences.class.getName());
static Logger log = LoggerFactory.getLogger(OffencesStepDef.class.getName());

@When("I make a request to the offence ref data api filtering by cjs code {string}")
public void getRequestToOffencesRefData(String filter) {
Expand All @@ -28,6 +28,7 @@ public void getRequestToOffencesRefData(String filter) {

}


@Then("the LJA ref data matching to result")
@Then("the court ref data matching to result")
@Then("the offence ref data matching to result")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: Verifying the end points for the Major Creditors

@PO-349 @Opal
Scenario: Verifying the end points for the major creditors
Given I am testing as the "opal-test@hmcts.net" user
When I make a request to the major creditors ref data api filter by major creditor id 15
Then the major creditors ref data matching to result
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
Loading