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

Add some more tests for legacy data marshalling #142

Merged
merged 3 commits into from
Jan 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
9 changes: 9 additions & 0 deletions src/main/java/uk/gov/hmcts/opal/dto/AccountSearchDto.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package uk.gov.hmcts.opal.dto;


import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Builder;
import lombok.Data;
import java.util.Optional;

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

@JsonIgnore
public Optional<Long> getNumericCourt() {
return Optional.ofNullable(getCourt())
.filter(s -> s.matches("[0-9]+"))
.map(Long::parseLong);
}
}
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 @@ -8,19 +9,21 @@
import uk.gov.hmcts.opal.dto.AccountSearchDto;
import uk.gov.hmcts.opal.dto.ToJsonString;

import java.util.Objects;
import java.util.Optional;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class DefendantAccountSearchCriteria implements ToJsonString {

@JsonProperty("account_number")
private String accountNumber;

@JsonProperty("business_unit_id")
private Integer businessUnitId;
private Long businessUnitId;

private Boolean organisation;

Expand Down Expand Up @@ -59,11 +62,14 @@ public static DefendantAccountSearchCriteria fromAccountSearchDto(AccountSearchD
.surname(dto.getSurname())
.forenames(dto.getForename())
.initials(dto.getInitials())
.birthDate(Optional.ofNullable(dto.getDateOfBirth()).map(d -> d.toLocalDate().toString()).orElse(null))
.birthDate(Optional.ofNullable(dto.getDateOfBirth())
.map(d -> d.toLocalDate())
.map(Objects::toString)
.orElse(null))
.addressLine1(dto.getAddressLineOne())
.nationalInsuranceNumber(dto.getNiNumber())
.prosecutorCaseReference(dto.getPcr())
.businessUnitId(Optional.ofNullable(dto.getCourt()).map(s -> 1).orElse(1))
.businessUnitId(dto.getNumericCourt().orElse(null))
//.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)
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 {
Copy link
Collaborator

@sabahirfan sabahirfan Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion we don't need to write unit tests for the generated code. The config we have (lombok.addLombokGeneratedAnnotation = true) already helps tools to not include in coverage calculation.

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