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 19, 2024
1 parent 1fea577 commit 35e7793
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 27 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);
}
}
14 changes: 8 additions & 6 deletions src/main/java/uk/gov/hmcts/opal/dto/ToJsonString.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

public interface ToJsonString {

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

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

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

default String toPrettyJsonString() throws JsonProcessingException {
return newObjectMapper()
return getObjectMapper()
.writerWithDefaultPrettyPrinter()
.writeValueAsString(this);
}
Expand All @@ -35,12 +38,11 @@ default String toPrettyJson() {
}

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

static ObjectMapper newObjectMapper() {
return new ObjectMapper()
.registerModule(new JavaTimeModule());
static ObjectMapper getObjectMapper() {
return objectMapper;
}
}
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 @@ -37,7 +37,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 @@ -85,7 +85,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 @@ -119,7 +119,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
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
@@ -1,5 +1,6 @@
package uk.gov.hmcts.opal.service.legacy.dto;

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
Expand Up @@ -5,6 +5,7 @@
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 @@ -39,7 +39,7 @@ void putDefendantAccount_SuccessfulResponse() throws Exception {

DefendantAccountEntity expectedAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();

String jsonBody = ToJsonString.newObjectMapper().writeValueAsString(inputAccountEntity);
String jsonBody = ToJsonString.getObjectMapper().writeValueAsString(inputAccountEntity);

ResponseEntity<String> successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK);
when(restTemplate.postForEntity(any(String.class), any(DefendantAccountEntity.class), any(Class.class)))
Expand Down Expand Up @@ -77,7 +77,7 @@ void putDefendantAccount_FailureCodeResponse() throws Exception {
// Arrange
final DefendantAccountEntity inputAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();

String jsonBody = ToJsonString.newObjectMapper().writeValueAsString(inputAccountEntity);
String jsonBody = ToJsonString.getObjectMapper().writeValueAsString(inputAccountEntity);

ResponseEntity<String> unsuccessfulResponseEntity = new ResponseEntity<>(
jsonBody, HttpStatus.INTERNAL_SERVER_ERROR);
Expand Down Expand Up @@ -121,7 +121,7 @@ void getParty_SuccessfulResponse() throws Exception {

DefendantAccountEntity expectedAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();

String jsonBody = ToJsonString.newObjectMapper().writeValueAsString(inputAccountEntity);
String jsonBody = ToJsonString.getObjectMapper().writeValueAsString(inputAccountEntity);

ResponseEntity<String> successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK);
when(restTemplate.getForEntity(any(String.class), any(Class.class), any(AccountEnquiryDto.class)))
Expand Down Expand Up @@ -163,7 +163,7 @@ void getParty_FailureCodeResponse() throws Exception {
final DefendantAccountEntity inputAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity();


String jsonBody = ToJsonString.newObjectMapper().writeValueAsString(inputAccountEntity);
String jsonBody = ToJsonString.getObjectMapper().writeValueAsString(inputAccountEntity);

ResponseEntity<String> unsuccessfulResponseEntity = new ResponseEntity<>(
jsonBody, HttpStatus.INTERNAL_SERVER_ERROR);
Expand Down Expand Up @@ -206,7 +206,7 @@ void searchForParty_SuccessfulResponse() throws Exception {
// Arrange
DefendantAccountsSearchResults resultsDto = DefendantAccountsSearchResults.builder()
.totalCount(9L).build();
String jsonBody = ToJsonString.newObjectMapper().writeValueAsString(resultsDto);
String jsonBody = ToJsonString.getObjectMapper().writeValueAsString(resultsDto);

ResponseEntity<String> successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK);
when(restTemplate.postForEntity(any(String.class), any(DefendantAccountSearchCriteria.class), any(Class.class)))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package uk.gov.hmcts.opal.service.legacy.dto;

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

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.service.legacy.dto.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"
}""";
}
}
Loading

0 comments on commit 35e7793

Please sign in to comment.