Skip to content

Commit

Permalink
Add some more tests for legacy data marshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
RustyHMCTS committed Jan 22, 2024
1 parent 2c6bf46 commit 380bb00
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 41 deletions.
8 changes: 8 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/dto/AccountSearchDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import lombok.Builder;
import lombok.Data;
import java.util.Optional;

@Data
@Builder
Expand All @@ -29,4 +30,11 @@ public class AccountSearchDto implements ToJsonString {
private String majorCreditor;
/** Unsure. */
private String tillNumber;

public Long getNumericCourt() {
return Optional.ofNullable(getCourt())
.filter(s -> s.matches("[0-9]+"))
.map(Long::parseLong)
.orElse(null);
}
}
18 changes: 8 additions & 10 deletions src/main/java/uk/gov/hmcts/opal/dto/ToJsonString.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

public interface ToJsonString {

static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.registerModule(new JavaTimeModule());

default String toJsonString() throws JsonProcessingException {
return newObjectMapper()
.writeValueAsString(this);
return OBJECT_MAPPER.writeValueAsString(this);
}

default String toJson() {
Expand All @@ -21,9 +23,7 @@ default String toJson() {
}

default String toPrettyJsonString() throws JsonProcessingException {
return newObjectMapper()
.writerWithDefaultPrettyPrinter()
.writeValueAsString(this);
return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(this);
}

default String toPrettyJson() {
Expand All @@ -35,12 +35,10 @@ default String toPrettyJson() {
}

default JsonNode toJsonNode() throws JsonProcessingException {
return newObjectMapper()
.readTree(this.toJsonString());
return OBJECT_MAPPER.readTree(this.toJsonString());
}

static ObjectMapper newObjectMapper() {
return new ObjectMapper()
.registerModule(new JavaTimeModule());
static ObjectMapper getObjectMapper() {
return OBJECT_MAPPER;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.hmcts.opal.service.legacy.dto;
package uk.gov.hmcts.opal.dto.legacy;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -16,37 +17,50 @@
@AllArgsConstructor
public class DefendantAccountSearchCriteria implements ToJsonString {

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("account_number")
private String accountNumber;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("business_unit_id")
private Integer businessUnitId;
private Long businessUnitId;

@JsonInclude(JsonInclude.Include.NON_NULL)
private Boolean organisation;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("organisation_name")
private String organisationName;

@JsonInclude(JsonInclude.Include.NON_NULL)
private String surname;

@JsonInclude(JsonInclude.Include.NON_NULL)
private String forenames;

@JsonInclude(JsonInclude.Include.NON_NULL)
private String initials;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("birth_date")
private String birthDate;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("national_insurance_number")
private String nationalInsuranceNumber;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("prosecutor_case_reference")
private String prosecutorCaseReference;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty("address_line_1")
private String addressLine1;

@JsonInclude(JsonInclude.Include.NON_NULL)
private Boolean searchAliases;

@JsonInclude(JsonInclude.Include.NON_NULL)
private Boolean liveOnly;

private Integer firstRowNumber;
Expand All @@ -63,7 +77,7 @@ public static DefendantAccountSearchCriteria fromAccountSearchDto(AccountSearchD
.addressLine1(dto.getAddressLineOne())
.nationalInsuranceNumber(dto.getNiNumber())
.prosecutorCaseReference(dto.getPcr())
.businessUnitId(Optional.ofNullable(dto.getCourt()).map(s -> 1).orElse(1))
.businessUnitId(dto.getNumericCourt())
//.organisation_name(no organisation name)
//.searchAliases( dunno )
//.liveOnly( dunno )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.hmcts.opal.service.legacy.dto;
package uk.gov.hmcts.opal.dto.legacy;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package uk.gov.hmcts.opal.service.legacy.dto;
package uk.gov.hmcts.opal.dto.legacy;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import uk.gov.hmcts.opal.dto.AccountSearchResultsDto;
import uk.gov.hmcts.opal.dto.ToJsonString;

import java.util.Collections;
import java.util.List;
Expand All @@ -15,7 +16,7 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DefendantAccountsSearchResults {
public class DefendantAccountsSearchResults implements ToJsonString {

List<DefendantAccountSearchResult> defendantAccountsSearchResult;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public static Specification<DefendantAccountEntity> findByAccountSearch(AccountS
notBlank(accountSearchDto.getNiNumber()).map(DefendantAccountSpecs::likeNiNumber),
notBlank(accountSearchDto.getAddressLineOne()).map(DefendantAccountSpecs::likeAddressLine1),
notNullLocalDate(accountSearchDto.getDateOfBirth()).map(DefendantAccountSpecs::equalsDateOfBirth),
Optional.ofNullable(accountSearchDto.getCourt()).filter(s -> s.matches("[0-9]+")).map(Long::parseLong)
.map(DefendantAccountSpecs::equalsAnyCourtId)
Optional.ofNullable(accountSearchDto.getNumericCourt()).map(DefendantAccountSpecs::equalsAnyCourtId)
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import static uk.gov.hmcts.opal.dto.ToJsonString.newObjectMapper;
import static uk.gov.hmcts.opal.dto.ToJsonString.getObjectMapper;

@Service
@Transactional
Expand Down Expand Up @@ -87,7 +87,7 @@ public AccountSearchResultsDto searchDefendantAccounts(AccountSearchDto accountS

try (InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("tempSearchData.json")) {
ObjectMapper mapper = newObjectMapper();
ObjectMapper mapper = getObjectMapper();
AccountSearchResultsDto dto = mapper.readValue(in, AccountSearchResultsDto.class);
log.info(":searchDefendantAccounts: temporary Hack for Front End testing. Read JSON file: \n{}",
dto.toPrettyJsonString());
Expand Down Expand Up @@ -121,7 +121,7 @@ public AccountDetailsDto getAccountDetailsByDefendantAccountId(Long defendantAcc
try (InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("tempDetailsData.json")) {

ObjectMapper mapper = newObjectMapper();
ObjectMapper mapper = getObjectMapper();
AccountDetailsDto dto = mapper.readValue(in, AccountDetailsDto.class);
log.info(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import uk.gov.hmcts.opal.dto.legacy.LegacyAccountDetailsRequestDto;
import uk.gov.hmcts.opal.dto.legacy.LegacyAccountDetailsResponseDto;
import uk.gov.hmcts.opal.entity.DefendantAccountEntity;
import uk.gov.hmcts.opal.service.legacy.dto.DefendantAccountSearchCriteria;
import uk.gov.hmcts.opal.service.legacy.dto.DefendantAccountsSearchResults;
import uk.gov.hmcts.opal.dto.legacy.DefendantAccountSearchCriteria;
import uk.gov.hmcts.opal.dto.legacy.DefendantAccountsSearchResults;

import java.util.Collections;
import java.util.List;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/uk/gov/hmcts/opal/service/LegacyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public <T> T extractResponse(ResponseEntity<String> responseEntity, Class<T> clz
getLog().info("extractResponse: Raw JSON response: {}", rawJson);

try {
ObjectMapper objectMapper = ToJsonString.newObjectMapper();
ObjectMapper objectMapper = ToJsonString.getObjectMapper();
JsonNode root = objectMapper.readTree(rawJson);

return objectMapper.treeToValue(root, clzz);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package uk.gov.hmcts.opal.dto.legacy;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import uk.gov.hmcts.opal.dto.ToJsonString;
import uk.gov.hmcts.opal.dto.legacy.DefendantAccountSearchCriteria;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static uk.gov.hmcts.opal.dto.legacy.DefendantAccountSearchCriteria.DefendantAccountSearchCriteriaBuilder;

@Slf4j
public class DefendantAccountSearchCriteriaTest {

@Test
public void testBuilder() throws InterruptedException {
DefendantAccountSearchCriteria criteria = constructDefendantAccountSearchCriteria();

assertEquals("accountNo", criteria.getAccountNumber());
assertEquals("John", criteria.getForenames());
assertEquals("D", criteria.getInitials());
assertEquals("Smith", criteria.getSurname());
assertEquals("1977-06-26", criteria.getBirthDate());
assertEquals("Glasgow", criteria.getAddressLine1());
assertEquals("XX123456C", criteria.getNationalInsuranceNumber());
assertEquals(1L, criteria.getBusinessUnitId());
assertNull(criteria.getOrganisationName());
assertNull(criteria.getProsecutorCaseReference());
}

@Test
public void testNullBusinessUnit() {
DefendantAccountSearchCriteriaBuilder criteriaBuilder = constructDefendantAccountSearchCriteriaBuilder();
DefendantAccountSearchCriteria criteria = criteriaBuilder.businessUnitId(null).build();
assertNull(criteria.getBusinessUnitId());
assertEquals(getJsonRepresentation(), criteria.toPrettyJson());
}

@Test
public void testJsonString() throws Exception {
DefendantAccountSearchCriteria model = constructDefendantAccountSearchCriteria();
assertNotNull(model.toJsonString());

DefendantAccountSearchCriteria parsed = ToJsonString.getObjectMapper()
.readValue(getJsonRepresentation(), DefendantAccountSearchCriteria.class);
assertNotNull(parsed);
}

private DefendantAccountSearchCriteria constructDefendantAccountSearchCriteria() {
return constructDefendantAccountSearchCriteriaBuilder().build();
}

private DefendantAccountSearchCriteriaBuilder constructDefendantAccountSearchCriteriaBuilder() {
return DefendantAccountSearchCriteria.builder()
.accountNumber("accountNo")
.addressLine1("Glasgow")
.businessUnitId(1L)
.firstRowNumber(4)
.lastRowNumber(44)
.surname("Smith")
.forenames("John")
.initials("D")
.birthDate("1977-06-26")
.nationalInsuranceNumber("XX123456C");
}

private String getJsonRepresentation() {
return """
{
"surname" : "Smith",
"forenames" : "John",
"initials" : "D",
"firstRowNumber" : 4,
"lastRowNumber" : 44,
"account_number" : "accountNo",
"birth_date" : "1977-06-26",
"national_insurance_number" : "XX123456C",
"address_line_1" : "Glasgow"
}""";
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package uk.gov.hmcts.opal.service.legacy.dto;
package uk.gov.hmcts.opal.dto.legacy;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import uk.gov.hmcts.opal.dto.AccountEnquiryDto;
import uk.gov.hmcts.opal.dto.AccountSummaryDto;
import uk.gov.hmcts.opal.dto.ToJsonString;
import uk.gov.hmcts.opal.dto.legacy.DefendantAccountSearchResult;
import uk.gov.hmcts.opal.dto.legacy.DefendantAccountsSearchResults;

import java.math.BigDecimal;
import java.time.LocalDate;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@Slf4j
public class DefendantAccountSearchResultTest {

@Test
public void testBuilder() {
DefendantAccountSearchResult accountEnquiryDto = constructTestDefendantAccountSearchResult();

assertEquals("accountNameNo", accountEnquiryDto.getAccountNumber());
assertEquals("accountNo", accountEnquiryDto.getAccountNumber());
assertEquals("Mr John Smith", accountEnquiryDto.getFullName());
assertEquals("Scotland", accountEnquiryDto.getAddressLine1());
assertEquals("1977-06-26", accountEnquiryDto.getBirthDate());
Expand All @@ -27,12 +32,16 @@ public void testBuilder() {
}

@Test
public void testToJsonString() throws Exception {
DefendantAccountSearchResult accountEnquiryDto = constructTestDefendantAccountSearchResult();
public void testJsonString() throws Exception {
DefendantAccountSearchResult model = constructTestDefendantAccountSearchResult();
assertNotNull(model.toJsonString());

assertNotNull(accountEnquiryDto.toJsonString());
DefendantAccountsSearchResults parsed = ToJsonString.getObjectMapper()
.readValue(getJsonRepresentation(), DefendantAccountsSearchResults.class);
assertNotNull(parsed);
}


@Test
public void testControllerModelEqualsAndHashCode() {
// Arrange
Expand All @@ -59,7 +68,7 @@ public void testControllerModelToAccountSummaryDto() {

AccountSummaryDto dto = model2.toAccountSummaryDto();
assertEquals("Mr John Smith", dto.getName());
assertEquals("accountNameNo", dto.getAccountNo());
assertEquals("accountNo", dto.getAccountNo());
assertEquals("Cardiff", dto.getCourt());
assertEquals(12345L, dto.getDefendantAccountId());
assertEquals(BigDecimal.valueOf(1000), dto.getBalance());
Expand All @@ -68,9 +77,9 @@ public void testControllerModelToAccountSummaryDto() {

}

private DefendantAccountSearchResult constructTestDefendantAccountSearchResult() {
public static DefendantAccountSearchResult constructTestDefendantAccountSearchResult() {
return DefendantAccountSearchResult.builder()
.accountNumber("accountNameNo")
.accountNumber("accountNo")
.defendantAccountId(12345L)
.surname("Smith")
.forenames("John")
Expand All @@ -82,4 +91,28 @@ private DefendantAccountSearchResult constructTestDefendantAccountSearchResult()
.businessUnitName("Cardiff")
.build();
}

private String getJsonRepresentation() {
return """
{
"defendantAccountsSearchResult" : [ {
"accountNumber" : "accountNo",
"organisation" : null,
"title" : "Mr",
"surname" : "Smith",
"forenames" : "John",
"initials" : null,
"rowNumber" : null,
"defendant_account_id" : 12345,
"business_unit_id" : 9,
"business_unit_name" : "Cardiff",
"organisation_name" : null,
"birth_date" : "1977-06-26",
"address_line_1" : "Scotland",
"account_balance" : 1000
} ],
"totalCount" : 1
}
""";
}
}
Loading

0 comments on commit 380bb00

Please sign in to comment.