From 291db44f4690735897fc4a7e194f3796be84fa5f Mon Sep 17 00:00:00 2001 From: DeclanClarkeCGI <142809814+DeclanClarkeCGI@users.noreply.github.com> Date: Wed, 3 Jul 2024 12:24:18 +0100 Subject: [PATCH] Po 252 xml to json conversion for legacy response (#393) * update schemas, update defendantaccount details service to retrieve notes * jaxb util classes * Add xml annotations to response classes * Add xml annotations to response classes * Add unmarshaller to response handler * Fix tests * Add xml names for fields in dto layer * Tests for utils and tidy-up --- .../java/uk/gov/hmcts/opal/dto/NoteDto.java | 8 ++ .../java/uk/gov/hmcts/opal/dto/PartyDto.java | 10 ++ .../opal/dto/legacy/AccountActivitiesDto.java | 5 + .../opal/dto/legacy/AccountActivityDto.java | 14 +++ .../opal/dto/legacy/DefendantAccountDto.java | 63 +++++++++++- .../DefendantAccountsSearchResults.java | 2 + .../hmcts/opal/dto/legacy/ImpositionDto.java | 14 +++ .../hmcts/opal/dto/legacy/ImpositionsDto.java | 5 + .../LegacyAccountDetailsResponseDto.java | 7 ++ .../dto/legacy/LegacySaveNoteResponseDto.java | 9 +- .../gov/hmcts/opal/dto/legacy/PartiesDto.java | 5 + .../gov/hmcts/opal/dto/legacy/PartyDto.java | 27 ++++++ .../opal/dto/legacy/PaymentTermsDto.java | 14 +++ .../opal/entity/DefendantAccountEntity.java | 17 ++++ .../opal/service/legacy/LegacyService.java | 14 ++- .../gov/hmcts/opal/util/LocalDateAdapter.java | 17 ++++ .../hmcts/opal/util/LocalDateTimeAdapter.java | 18 ++++ .../java/uk/gov/hmcts/opal/util/XmlUtil.java | 37 ++++++++ .../LegacyDefendantAccountServiceTest.java | 31 +++--- .../service/legacy/LegacyNoteServiceTest.java | 46 +++++---- .../legacy/LegacyPartyServiceTest.java | 95 +++++++++---------- .../service/legacy/LegacyServiceTest.java | 23 +++-- .../opal/service/legacy/LegacyTestsBase.java | 6 ++ .../hmcts/opal/util/LocalDateAdapterTest.java | 30 ++++++ .../opal/util/LocalDateTimeAdapterTest.java | 30 ++++++ 25 files changed, 442 insertions(+), 105 deletions(-) create mode 100644 src/main/java/uk/gov/hmcts/opal/util/LocalDateAdapter.java create mode 100644 src/main/java/uk/gov/hmcts/opal/util/LocalDateTimeAdapter.java create mode 100644 src/main/java/uk/gov/hmcts/opal/util/XmlUtil.java create mode 100644 src/test/java/uk/gov/hmcts/opal/util/LocalDateAdapterTest.java create mode 100644 src/test/java/uk/gov/hmcts/opal/util/LocalDateTimeAdapterTest.java diff --git a/src/main/java/uk/gov/hmcts/opal/dto/NoteDto.java b/src/main/java/uk/gov/hmcts/opal/dto/NoteDto.java index e3d2c8550..2fe1fe23b 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/NoteDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/NoteDto.java @@ -2,11 +2,16 @@ import com.fasterxml.jackson.annotation.JsonFormat; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import lombok.extern.jackson.Jacksonized; +import uk.gov.hmcts.opal.util.LocalDateTimeAdapter; import java.time.LocalDateTime; @@ -15,6 +20,8 @@ @NoArgsConstructor @AllArgsConstructor @Jacksonized +@XmlRootElement(name = "note") +@XmlAccessorType(XmlAccessType.FIELD) public class NoteDto implements ToJsonString { private Long noteId; @@ -30,6 +37,7 @@ public class NoteDto implements ToJsonString { private String noteText; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @XmlJavaTypeAdapter(LocalDateTimeAdapter.class) private LocalDateTime postedDate; private String postedBy; diff --git a/src/main/java/uk/gov/hmcts/opal/dto/PartyDto.java b/src/main/java/uk/gov/hmcts/opal/dto/PartyDto.java index 54ab01a24..f37a71383 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/PartyDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/PartyDto.java @@ -1,11 +1,17 @@ package uk.gov.hmcts.opal.dto; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import lombok.extern.jackson.Jacksonized; +import uk.gov.hmcts.opal.util.LocalDateAdapter; +import uk.gov.hmcts.opal.util.LocalDateTimeAdapter; import java.time.LocalDate; import java.time.LocalDateTime; @@ -15,6 +21,8 @@ @NoArgsConstructor @AllArgsConstructor @Jacksonized +@XmlRootElement(name = "party") +@XmlAccessorType(XmlAccessType.FIELD) public class PartyDto implements ToJsonString { private Long partyId; private boolean organisation; @@ -30,8 +38,10 @@ public class PartyDto implements ToJsonString { private String addressLine5; private String postcode; private String accountType; + @XmlJavaTypeAdapter(LocalDateAdapter.class) private LocalDate dateOfBirth; private Short age; private String niNumber; + @XmlJavaTypeAdapter(LocalDateTimeAdapter.class) private LocalDateTime lastChangedDate; } diff --git a/src/main/java/uk/gov/hmcts/opal/dto/legacy/AccountActivitiesDto.java b/src/main/java/uk/gov/hmcts/opal/dto/legacy/AccountActivitiesDto.java index 727e378c8..cd4765b48 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/legacy/AccountActivitiesDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/legacy/AccountActivitiesDto.java @@ -1,6 +1,9 @@ package uk.gov.hmcts.opal.dto.legacy; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -12,9 +15,11 @@ @Data @AllArgsConstructor @NoArgsConstructor +@XmlAccessorType(XmlAccessType.FIELD) public class AccountActivitiesDto { @JsonProperty(value = "account_activity") + @XmlElement(name = "account_activity") private List accountActivity; } diff --git a/src/main/java/uk/gov/hmcts/opal/dto/legacy/AccountActivityDto.java b/src/main/java/uk/gov/hmcts/opal/dto/legacy/AccountActivityDto.java index c0f551af4..8a9ff9f80 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/legacy/AccountActivityDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/legacy/AccountActivityDto.java @@ -3,10 +3,15 @@ import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonRootName; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import uk.gov.hmcts.opal.util.LocalDateTimeAdapter; import java.time.LocalDateTime; @@ -15,27 +20,36 @@ @AllArgsConstructor @NoArgsConstructor @JsonRootName(value = "account_activity") +@XmlAccessorType(XmlAccessType.FIELD) public class AccountActivityDto { @JsonProperty("activity_id") + @XmlElement(name = "activity_id") private Integer activityId; @JsonProperty("activity_type") + @XmlElement(name = "activity_type") private String activityType; @JsonProperty("activity_type_code") + @XmlElement(name = "activity_type_code") private String activityTypeCode; @JsonProperty("activity_text") + @XmlElement(name = "activity_text") private String activityText; @JsonProperty("posted_date") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @XmlJavaTypeAdapter(LocalDateTimeAdapter.class) + @XmlElement(name = "posted_date") private LocalDateTime postedDate; @JsonProperty("posted_by") + @XmlElement(name = "posted_by") private String postedBy; @JsonProperty("amount") + @XmlElement(name = "amount") private Double amount; } diff --git a/src/main/java/uk/gov/hmcts/opal/dto/legacy/DefendantAccountDto.java b/src/main/java/uk/gov/hmcts/opal/dto/legacy/DefendantAccountDto.java index 670a32839..bb0e48eba 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/legacy/DefendantAccountDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/legacy/DefendantAccountDto.java @@ -2,10 +2,15 @@ import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import uk.gov.hmcts.opal.util.LocalDateAdapter; import java.math.BigDecimal; import java.time.LocalDate; @@ -14,64 +19,120 @@ @Data @AllArgsConstructor @NoArgsConstructor +@XmlAccessorType(XmlAccessType.FIELD) public class DefendantAccountDto { @JsonProperty("defendant_account_id") + @XmlElement(name = "defendant_account_id") private Long defendantAccountId; + @JsonProperty("account_number") + @XmlElement(name = "account_number") private String accountNumber; + @JsonProperty("amount_imposed") + @XmlElement(name = "amount_imposed") private BigDecimal amountImposed; + @JsonProperty("amount_paid") + @XmlElement(name = "amount_paid") private BigDecimal amountPaid; + @JsonProperty("account_balance") + @XmlElement(name = "account_balance") private BigDecimal accountBalance; + @JsonProperty("business_unit_id") + @XmlElement(name = "business_unit_id") private int businessUnitId; + @JsonProperty("business_unit_name") + @XmlElement(name = "business_unit_name") private String businessUnitName; + @JsonProperty("account_status") + @XmlElement(name = "account_status") private String accountStatus; + @JsonProperty("originator_name") + @XmlElement(name = "originator_name") private String originatorName; + @JsonProperty("imposed_hearing_date") @JsonFormat(pattern = "yyyy-MM-dd") + @XmlJavaTypeAdapter(LocalDateAdapter.class) + @XmlElement(name = "imposed_hearing_date") private LocalDate imposedHearingDate; + @JsonProperty("imposing_court_code") + @XmlElement(name = "imposing_court_code") private int imposingCourtCode; + @JsonProperty("last_hearing_date") + @XmlElement(name = "last_hearing_date") private String lastHearingDate; + @JsonProperty("last_hearing_court_code") + @XmlElement(name = "last_hearing_court_code") private int lastHearingCourtCode; + @JsonProperty("last_changed_date") @JsonFormat(pattern = "yyyy-MM-dd") + @XmlJavaTypeAdapter(LocalDateAdapter.class) + @XmlElement(name = "last_changed_date") private LocalDate lastChangedDate; + @JsonProperty("last_movement_date") @JsonFormat(pattern = "yyyy-MM-dd") + @XmlJavaTypeAdapter(LocalDateAdapter.class) + @XmlElement(name = "last_movement_date") private LocalDate lastMovementDate; + @JsonProperty("collection_order") + @XmlElement(name = "collection_order") private boolean collectionOrder; + @JsonProperty("enforcing_court_code") + @XmlElement(name = "enforcing_court_code") private int enforcingCourtCode; + @JsonProperty("last_enforcement") + @XmlElement(name = "last_enforcement") private String lastEnforcement; + @JsonProperty("enf_override_result_id") + @XmlElement(name = "enf_override_result_id") private String enfOverrideResultId; + @JsonProperty("enf_override_enforcer_code") + @XmlElement(name = "enf_override_enforcer_code") private Short enfOverrideEnforcerCode; + @JsonProperty("enf_override_tfo_lja_code") + @XmlElement(name = "enf_override_tfo_lja_code") private int enfOverrideTfoLjaCode; + @JsonProperty("prosecutor_case_reference") + @XmlElement(name = "prosecutor_case_reference") private String prosecutorCaseReference; + @JsonProperty("account_comments") + @XmlElement(name = "account_comments") private String accountComments; + @JsonProperty("payment_terms") + @XmlElement(name = "payment_terms") private PaymentTermsDto paymentTerms; + @JsonProperty("parties") + @XmlElement(name = "parties") private PartiesDto parties; + @JsonProperty("impositions") + @XmlElement(name = "impositions") private ImpositionsDto impositions; + @JsonProperty("account_activities") + @XmlElement(name = "account_activities") private AccountActivitiesDto accountActivities; - } diff --git a/src/main/java/uk/gov/hmcts/opal/dto/legacy/DefendantAccountsSearchResults.java b/src/main/java/uk/gov/hmcts/opal/dto/legacy/DefendantAccountsSearchResults.java index c2d330fed..57e4ab9f1 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/legacy/DefendantAccountsSearchResults.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/legacy/DefendantAccountsSearchResults.java @@ -1,5 +1,6 @@ package uk.gov.hmcts.opal.dto.legacy; +import jakarta.xml.bind.annotation.XmlRootElement; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -16,6 +17,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor +@XmlRootElement public class DefendantAccountsSearchResults implements ToJsonString { List defendantAccountsSearchResult; diff --git a/src/main/java/uk/gov/hmcts/opal/dto/legacy/ImpositionDto.java b/src/main/java/uk/gov/hmcts/opal/dto/legacy/ImpositionDto.java index 1c40fa97a..a74ea612e 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/legacy/ImpositionDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/legacy/ImpositionDto.java @@ -2,6 +2,9 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonRootName; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -12,35 +15,46 @@ @AllArgsConstructor @NoArgsConstructor @JsonRootName(value = "imposition") +@XmlAccessorType(XmlAccessType.FIELD) public class ImpositionDto { @JsonProperty("imposition_id") + @XmlElement(name = "imposition_id") private Integer impositionId; @JsonProperty("posted_date") + @XmlElement(name = "posted_date") private String postedDate; @JsonProperty("result_id") + @XmlElement(name = "result_id") private String resultId; @JsonProperty("imposed_date") + @XmlElement(name = "imposed_date") private String imposedDate; @JsonProperty("imposing_court_code") + @XmlElement(name = "imposing_court_code") private Integer imposingCourtCode; @JsonProperty("imposed_amount") + @XmlElement(name = "imposed_amount") private Double imposedAmount; @JsonProperty("paid_amount") + @XmlElement(name = "paid_amount") private Double paidAmount; @JsonProperty("offence_title") + @XmlElement(name = "offence_title") private String offenceTitle; @JsonProperty("creditor_account_number") + @XmlElement(name = "creditor_account_number") private String creditorAccountNumber; @JsonProperty("creditor_name") + @XmlElement(name = "creditor_name") private String creditorName; } diff --git a/src/main/java/uk/gov/hmcts/opal/dto/legacy/ImpositionsDto.java b/src/main/java/uk/gov/hmcts/opal/dto/legacy/ImpositionsDto.java index a9d1d50d0..7dcfcf008 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/legacy/ImpositionsDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/legacy/ImpositionsDto.java @@ -1,6 +1,9 @@ package uk.gov.hmcts.opal.dto.legacy; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -12,9 +15,11 @@ @Data @AllArgsConstructor @NoArgsConstructor +@XmlAccessorType(XmlAccessType.FIELD) public class ImpositionsDto { @JsonProperty(value = "imposition") + @XmlElement(name = "imposition") private List imposition; } diff --git a/src/main/java/uk/gov/hmcts/opal/dto/legacy/LegacyAccountDetailsResponseDto.java b/src/main/java/uk/gov/hmcts/opal/dto/legacy/LegacyAccountDetailsResponseDto.java index 9277e45a1..4f106265c 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/legacy/LegacyAccountDetailsResponseDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/legacy/LegacyAccountDetailsResponseDto.java @@ -1,6 +1,10 @@ package uk.gov.hmcts.opal.dto.legacy; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlRootElement; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -18,9 +22,12 @@ @NoArgsConstructor @AllArgsConstructor @Jacksonized +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) public class LegacyAccountDetailsResponseDto implements ToJsonString { @JsonProperty("defendant_account") + @XmlElement(name = "defendant_account") private DefendantAccountDto defendantAccount; diff --git a/src/main/java/uk/gov/hmcts/opal/dto/legacy/LegacySaveNoteResponseDto.java b/src/main/java/uk/gov/hmcts/opal/dto/legacy/LegacySaveNoteResponseDto.java index d435f49ee..bbe15c19c 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/legacy/LegacySaveNoteResponseDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/legacy/LegacySaveNoteResponseDto.java @@ -3,6 +3,9 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlRootElement; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -16,14 +19,16 @@ @AllArgsConstructor @Jacksonized @JsonInclude(JsonInclude.Include.NON_NULL) +@XmlRootElement(name = "LegacySaveNoteResponse") +@XmlAccessorType(XmlAccessType.FIELD) public class LegacySaveNoteResponseDto { @JsonProperty("note_id") - private Long nodeId; + private Long noteId; public NoteDto createClonedAndUpdatedDto(NoteDto noteDto) { return noteDto.toBuilder() - .noteId(nodeId) + .noteId(noteId) .build(); } } diff --git a/src/main/java/uk/gov/hmcts/opal/dto/legacy/PartiesDto.java b/src/main/java/uk/gov/hmcts/opal/dto/legacy/PartiesDto.java index d93db6508..cb033c5fb 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/legacy/PartiesDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/legacy/PartiesDto.java @@ -1,6 +1,9 @@ package uk.gov.hmcts.opal.dto.legacy; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -12,8 +15,10 @@ @Data @AllArgsConstructor @NoArgsConstructor +@XmlAccessorType(XmlAccessType.FIELD) public class PartiesDto { @JsonProperty(value = "party") + @XmlElement(name = "party") private List party; } diff --git a/src/main/java/uk/gov/hmcts/opal/dto/legacy/PartyDto.java b/src/main/java/uk/gov/hmcts/opal/dto/legacy/PartyDto.java index a308f110c..4f853f19e 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/legacy/PartyDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/legacy/PartyDto.java @@ -3,10 +3,15 @@ import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonRootName; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import uk.gov.hmcts.opal.util.LocalDateAdapter; import java.time.LocalDate; @@ -15,67 +20,89 @@ @AllArgsConstructor @NoArgsConstructor @JsonRootName(value = "party") +@XmlAccessorType(XmlAccessType.FIELD) public class PartyDto { @JsonProperty("party_id") + @XmlElement(name = "party_id") private Integer partyId; @JsonProperty("association_type") + @XmlElement(name = "association_type") private String associationType; @JsonProperty("debtor") + @XmlElement(name = "debtor") private Boolean debtor; @JsonProperty("organisation") + @XmlElement(name = "organisation") private Boolean organisation; @JsonProperty("organisation_name") + @XmlElement(name = "organisation_name") private String organisationName; @JsonProperty("surname") + @XmlElement(name = "surname") private String surname; @JsonProperty("forenames") + @XmlElement(name = "forenames") private String forenames; @JsonProperty("initials") + @XmlElement(name = "initials") private String initials; @JsonProperty("title") + @XmlElement(name = "title") private String title; @JsonProperty("birth_date") @JsonFormat(pattern = "yyyy-MM-dd") + @XmlJavaTypeAdapter(LocalDateAdapter.class) + @XmlElement(name = "birth_date") private LocalDate birthDate; @JsonProperty("age") + @XmlElement(name = "age") private Integer age; @JsonProperty("national_insurance_number") + @XmlElement(name = "national_insurance_number") private String nationalInsuranceNumber; @JsonProperty("last_changed_date") + @XmlElement(name = "last_changed_date") private String lastChangedDate; @JsonProperty("full_name") + @XmlElement(name = "full_name") private String fullName; @JsonProperty("address_line_1") + @XmlElement(name = "address_line_1") private String addressLine1; @JsonProperty("address_line_2") + @XmlElement(name = "address_line_2") private String addressLine2; @JsonProperty("address_line_3") + @XmlElement(name = "address_line_3") private String addressLine3; @JsonProperty("address_line_4") + @XmlElement(name = "address_line_4") private String addressLine4; @JsonProperty("address_line_5") + @XmlElement(name = "address_line_5") private String addressLine5; @JsonProperty("postcode") + @XmlElement(name = "postcode") private String postcode; } diff --git a/src/main/java/uk/gov/hmcts/opal/dto/legacy/PaymentTermsDto.java b/src/main/java/uk/gov/hmcts/opal/dto/legacy/PaymentTermsDto.java index 411fc06fc..b0cc60006 100644 --- a/src/main/java/uk/gov/hmcts/opal/dto/legacy/PaymentTermsDto.java +++ b/src/main/java/uk/gov/hmcts/opal/dto/legacy/PaymentTermsDto.java @@ -2,10 +2,15 @@ import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonProperty; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import uk.gov.hmcts.opal.util.LocalDateAdapter; import java.math.BigDecimal; import java.time.LocalDate; @@ -14,28 +19,37 @@ @Data @AllArgsConstructor @NoArgsConstructor +@XmlAccessorType(XmlAccessType.FIELD) public class PaymentTermsDto { @JsonProperty("terms_date") @JsonFormat(pattern = "yyyy-MM-dd") + @XmlJavaTypeAdapter(LocalDateAdapter.class) + @XmlElement(name = "terms_date") private LocalDate termsDate; @JsonProperty("terms_type_code") + @XmlElement(name = "terms_type_code") private String termsTypeCode; @JsonProperty("instalment_amount") + @XmlElement(name = "instalment_amount") private BigDecimal instalmentAmount; @JsonProperty("instalment_period") + @XmlElement(name = "instalment_period") private String instalmentPeriod; @JsonProperty("instalment_lump_sum") + @XmlElement(name = "instalment_lump_sum") private BigDecimal instalmentLumpSum; @JsonProperty("jail_days") + @XmlElement(name = "jail_days") private Integer jailDays; @JsonProperty("wording") + @XmlElement(name = "wording") private String wording; } diff --git a/src/main/java/uk/gov/hmcts/opal/entity/DefendantAccountEntity.java b/src/main/java/uk/gov/hmcts/opal/entity/DefendantAccountEntity.java index 925ab22ee..53296fb0e 100644 --- a/src/main/java/uk/gov/hmcts/opal/entity/DefendantAccountEntity.java +++ b/src/main/java/uk/gov/hmcts/opal/entity/DefendantAccountEntity.java @@ -17,10 +17,15 @@ import jakarta.persistence.Table; import jakarta.persistence.Temporal; import jakarta.persistence.TemporalType; +import jakarta.xml.bind.annotation.XmlAccessType; +import jakarta.xml.bind.annotation.XmlAccessorType; +import jakarta.xml.bind.annotation.XmlRootElement; +import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import uk.gov.hmcts.opal.util.LocalDateAdapter; import java.math.BigDecimal; import java.time.LocalDate; @@ -34,6 +39,8 @@ @AllArgsConstructor @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "defendantAccountId") +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) public class DefendantAccountEntity { @Id @@ -51,6 +58,7 @@ public class DefendantAccountEntity { @Column(name = "imposed_hearing_date") @Temporal(TemporalType.DATE) + @XmlJavaTypeAdapter(LocalDateAdapter.class) private LocalDate imposedHearingDate; @Column(name = "imposing_court_id") @@ -70,6 +78,7 @@ public class DefendantAccountEntity { @Column(name = "completed_date") @Temporal(TemporalType.DATE) + @XmlJavaTypeAdapter(LocalDateAdapter.class) private LocalDate completedDate; @ManyToOne @@ -82,10 +91,12 @@ public class DefendantAccountEntity { @Column(name = "last_hearing_date") @Temporal(TemporalType.DATE) + @XmlJavaTypeAdapter(LocalDateAdapter.class) private LocalDate lastHearingDate; @Column(name = "last_movement_date") @Temporal(TemporalType.DATE) + @XmlJavaTypeAdapter(LocalDateAdapter.class) private LocalDate lastMovementDate; @Column(name = "last_enforcement", length = 6) @@ -93,6 +104,7 @@ public class DefendantAccountEntity { @Column(name = "last_changed_date") @Temporal(TemporalType.DATE) + @XmlJavaTypeAdapter(LocalDateAdapter.class) private LocalDate lastChangedDate; @Column(name = "originator_name", length = 100) @@ -136,18 +148,22 @@ public class DefendantAccountEntity { @Column(name = "collection_order_date") @Temporal(TemporalType.DATE) + @XmlJavaTypeAdapter(LocalDateAdapter.class) private LocalDate collectionOrderEffectiveDate; @Column(name = "further_steps_notice_date") @Temporal(TemporalType.DATE) + @XmlJavaTypeAdapter(LocalDateAdapter.class) private LocalDate furtherStepsNoticeDate; @Column(name = "confiscation_order_date") @Temporal(TemporalType.DATE) + @XmlJavaTypeAdapter(LocalDateAdapter.class) private LocalDate confiscationOrderDate; @Column(name = "fine_registration_date") @Temporal(TemporalType.DATE) + @XmlJavaTypeAdapter(LocalDateAdapter.class) private LocalDate fineRegistrationDate; @Column(name = "consolidated_account_type", length = 1) @@ -158,6 +174,7 @@ public class DefendantAccountEntity { @Column(name = "payment_card_requested_date") @Temporal(TemporalType.DATE) + @XmlJavaTypeAdapter(LocalDateAdapter.class) private LocalDate paymentCardRequestedDate; @Column(name = "payment_card_requested_by", length = 20) diff --git a/src/main/java/uk/gov/hmcts/opal/service/legacy/LegacyService.java b/src/main/java/uk/gov/hmcts/opal/service/legacy/LegacyService.java index a57751496..1bfdc1dcb 100644 --- a/src/main/java/uk/gov/hmcts/opal/service/legacy/LegacyService.java +++ b/src/main/java/uk/gov/hmcts/opal/service/legacy/LegacyService.java @@ -1,8 +1,6 @@ package uk.gov.hmcts.opal.service.legacy; import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.helpers.MessageFormatter; @@ -13,6 +11,7 @@ import org.springframework.web.util.UriComponentsBuilder; import uk.gov.hmcts.opal.config.properties.LegacyGatewayProperties; import uk.gov.hmcts.opal.dto.ToJsonString; +import uk.gov.hmcts.opal.util.XmlUtil; import java.util.Base64; import java.util.Map; @@ -29,18 +28,17 @@ public abstract class LegacyService { protected abstract Logger getLog(); + @SuppressWarnings("unchecked") public T extractResponse(ResponseEntity responseEntity, Class clzz) { if (responseEntity.getStatusCode().is2xxSuccessful()) { if (responseEntity.getBody() != null) { - String rawJson = responseEntity.getBody(); - getLog().info("extractResponse: Raw JSON response: {}", rawJson); + String rawXml = responseEntity.getBody(); + getLog().info("extractResponse: Raw XML response: {}", rawXml); try { - ObjectMapper objectMapper = ToJsonString.getObjectMapper(); - JsonNode root = objectMapper.readTree(rawJson); - - return objectMapper.treeToValue(root, clzz); + XmlUtil xmlUtil = new XmlUtil(); + return (T) xmlUtil.unmarshalXmlString(rawXml, clzz); } catch (Exception e) { getLog().error("extractResponse: Error deserializing response: {}", e.getMessage(), e); diff --git a/src/main/java/uk/gov/hmcts/opal/util/LocalDateAdapter.java b/src/main/java/uk/gov/hmcts/opal/util/LocalDateAdapter.java new file mode 100644 index 000000000..ba04dca43 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/util/LocalDateAdapter.java @@ -0,0 +1,17 @@ +package uk.gov.hmcts.opal.util; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; +import java.time.LocalDate; + +public class LocalDateAdapter extends XmlAdapter { + + @Override + public LocalDate unmarshal(String v) throws Exception { + return LocalDate.parse(v); + } + + @Override + public String marshal(LocalDate v) throws Exception { + return v.toString(); + } +} diff --git a/src/main/java/uk/gov/hmcts/opal/util/LocalDateTimeAdapter.java b/src/main/java/uk/gov/hmcts/opal/util/LocalDateTimeAdapter.java new file mode 100644 index 000000000..138938d8f --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/util/LocalDateTimeAdapter.java @@ -0,0 +1,18 @@ +package uk.gov.hmcts.opal.util; + +import jakarta.xml.bind.annotation.adapters.XmlAdapter; + +import java.time.LocalDateTime; + +public class LocalDateTimeAdapter extends XmlAdapter { + + @Override + public LocalDateTime unmarshal(String v) throws Exception { + return LocalDateTime.parse(v); + } + + @Override + public String marshal(LocalDateTime v) throws Exception { + return v.toString(); + } +} diff --git a/src/main/java/uk/gov/hmcts/opal/util/XmlUtil.java b/src/main/java/uk/gov/hmcts/opal/util/XmlUtil.java new file mode 100644 index 000000000..ed146f419 --- /dev/null +++ b/src/main/java/uk/gov/hmcts/opal/util/XmlUtil.java @@ -0,0 +1,37 @@ +package uk.gov.hmcts.opal.util; + +import jakarta.xml.bind.JAXBContext; +import jakarta.xml.bind.JAXBException; +import jakarta.xml.bind.Marshaller; + +import java.io.StringReader; +import java.io.StringWriter; + +public class XmlUtil { + + public String marshalXmlString(T object, Class clzz) { + try { + JAXBContext jaxbContext = JAXBContext.newInstance(clzz); + Marshaller marshaller = jaxbContext.createMarshaller(); + + // To format the XML output + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + + StringWriter sw = new StringWriter(); + marshaller.marshal(object, sw); + return sw.toString(); + } catch (JAXBException e) { + throw new RuntimeException("Error serializing object to XML", e); + } + } + + @SuppressWarnings("unchecked") + public T unmarshalXmlString(String xmlString, Class clzz) { + try { + JAXBContext jaxbContext = JAXBContext.newInstance(clzz); + return (T) jaxbContext.createUnmarshaller().unmarshal(new StringReader(xmlString)); + } catch (JAXBException e) { + throw new RuntimeException("Error deserializing XML to object", e); + } + } +} diff --git a/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyDefendantAccountServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyDefendantAccountServiceTest.java index 1110ef304..b7cae58b1 100644 --- a/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyDefendantAccountServiceTest.java +++ b/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyDefendantAccountServiceTest.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.github.fge.jackson.JsonLoader; import com.github.fge.jsonschema.core.exceptions.ProcessingException; @@ -71,9 +70,9 @@ void putDefendantAccount_SuccessfulResponse() throws Exception { final DefendantAccountEntity expectedAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity(); - String jsonBody = ToJsonString.getObjectMapper().writeValueAsString(inputAccountEntity); + String xml = marshalXmlString(inputAccountEntity, DefendantAccountEntity.class); - ResponseEntity successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK); + ResponseEntity successfulResponseEntity = new ResponseEntity<>(xml, HttpStatus.OK); when(requestBodySpec.header(anyString(), anyString())).thenReturn(requestBodySpec); when(requestBodySpec.body(any(DefendantAccountEntity.class))).thenReturn(requestBodySpec); when(responseSpec.toEntity(any(Class.class))).thenReturn(successfulResponseEntity); @@ -120,10 +119,10 @@ void putDefendantAccount_FailureCodeResponse() throws Exception { mockRestClientPost(); final DefendantAccountEntity inputAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity(); - String jsonBody = ToJsonString.getObjectMapper().writeValueAsString(inputAccountEntity); + String xml = marshalXmlString(inputAccountEntity, DefendantAccountEntity.class); ResponseEntity unsuccessfulResponseEntity = new ResponseEntity<>( - jsonBody, HttpStatus.INTERNAL_SERVER_ERROR); + xml, HttpStatus.INTERNAL_SERVER_ERROR); when(requestBodySpec.header(anyString(), anyString())).thenReturn(requestBodySpec); when(requestBodySpec.body(any(DefendantAccountEntity.class))).thenReturn(requestBodySpec); when(responseSpec.toEntity(any(Class.class))).thenReturn(unsuccessfulResponseEntity); @@ -170,7 +169,7 @@ void putDefendantAccount_ErrorResponse() { assertNotNull(lgre); Throwable cause = lgre.getCause(); assertNotNull(cause); - assertEquals(UnrecognizedPropertyException.class, cause.getClass()); + assertEquals(RuntimeException.class, cause.getClass()); } @Test @@ -182,13 +181,14 @@ void getParty_SuccessfulResponse() throws Exception { final DefendantAccountEntity expectedAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity(); - String jsonBody = ToJsonString.getObjectMapper().writeValueAsString(inputAccountEntity); + String xml = marshalXmlString(inputAccountEntity, DefendantAccountEntity.class); - ResponseEntity successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK); + ResponseEntity successfulResponseEntity = new ResponseEntity<>(xml, HttpStatus.OK); when(requestBodySpec.header(anyString(), anyString())).thenReturn(requestBodySpec); when(requestBodySpec.body(any(AccountEnquiryDto.class))).thenReturn(requestBodySpec); when(responseSpec.toEntity(any(Class.class))).thenReturn(successfulResponseEntity); + // Act AccountEnquiryDto enquiry = AccountEnquiryDto.builder().build(); DefendantAccountEntity resultAccountEntity = legacyDefendantAccountService.getDefendantAccount(enquiry); @@ -233,10 +233,10 @@ void getParty_FailureCodeResponse() throws Exception { final DefendantAccountEntity inputAccountEntity = DefendantAccountServiceTest.buildDefendantAccountEntity(); - String jsonBody = ToJsonString.getObjectMapper().writeValueAsString(inputAccountEntity); + String xml = marshalXmlString(inputAccountEntity, DefendantAccountEntity.class); ResponseEntity unsuccessfulResponseEntity = new ResponseEntity<>( - jsonBody, HttpStatus.INTERNAL_SERVER_ERROR); + xml, HttpStatus.INTERNAL_SERVER_ERROR); when(requestBodySpec.header(anyString(), anyString())).thenReturn(requestBodySpec); when(requestBodySpec.body(any(AccountEnquiryDto.class))).thenReturn(requestBodySpec); when(responseSpec.toEntity(any(Class.class))).thenReturn(unsuccessfulResponseEntity); @@ -283,7 +283,7 @@ void getParty_ErrorResponse() { assertNotNull(lgre); Throwable cause = lgre.getCause(); assertNotNull(cause); - assertEquals(UnrecognizedPropertyException.class, cause.getClass()); + assertEquals(RuntimeException.class, cause.getClass()); } @@ -351,9 +351,10 @@ void getAccountDetailsByDefendantAccountId_Success() { // Arrange mockRestClientPost(); - String jsonBody = ToJsonString.getObjectMapper().writeValueAsString(buildLegacyAccountDto()); - ResponseEntity successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK); + String xml = marshalXmlString(buildLegacyAccountDto(), LegacyAccountDetailsResponseDto.class); + + ResponseEntity successfulResponseEntity = new ResponseEntity<>(xml, HttpStatus.OK); when(requestBodySpec.header(anyString(), anyString())).thenReturn(requestBodySpec); when(requestBodySpec.body(any(LegacyAccountDetailsRequestDto.class))).thenReturn(requestBodySpec); when(responseSpec.toEntity(any(Class.class))).thenReturn(successfulResponseEntity); @@ -375,9 +376,9 @@ void searchForDefendantAccounts_SuccessfulResponse() throws Exception { // Arrange DefendantAccountsSearchResults resultsDto = DefendantAccountsSearchResults.builder() .totalCount(9L).build(); - String jsonBody = ToJsonString.getObjectMapper().writeValueAsString(resultsDto); + String xml = marshalXmlString(resultsDto, DefendantAccountsSearchResults.class); - ResponseEntity successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK); + ResponseEntity successfulResponseEntity = new ResponseEntity<>(xml, HttpStatus.OK); when(requestBodySpec.header(anyString(), anyString())).thenReturn(requestBodySpec); when(requestBodySpec.body(any(DefendantAccountSearchCriteria.class))).thenReturn(requestBodySpec); when(responseSpec.toEntity(any(Class.class))).thenReturn(successfulResponseEntity); diff --git a/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyNoteServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyNoteServiceTest.java index d1300b90a..c0757908d 100644 --- a/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyNoteServiceTest.java +++ b/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyNoteServiceTest.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.github.fge.jackson.JsonLoader; import com.github.fge.jsonschema.core.exceptions.ProcessingException; @@ -61,13 +60,13 @@ void saveNote_SuccessfulResponse() { final NoteDto expectedNoteDto = inputNoteDto.toBuilder().noteId(12345L).build(); - String jsonBody = """ - { - "note_id": 12345 - } + String xml = """ + + 12345 + """; - ResponseEntity successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK); + ResponseEntity successfulResponseEntity = new ResponseEntity<>(xml, HttpStatus.OK); when(requestBodySpec.header(anyString(), anyString())).thenReturn(requestBodySpec); when(requestBodySpec.body(any(LegacySaveNoteRequestDto.class))).thenReturn(requestBodySpec); when(responseSpec.toEntity(any(Class.class))).thenReturn(successfulResponseEntity); @@ -97,7 +96,8 @@ void saveNote_FailureBodyResponse() { LegacyGatewayResponseException lgre = assertThrows( LegacyGatewayResponseException.class, - () -> legacyNoteService.saveNote(inputNoteDto)); + () -> legacyNoteService.saveNote(inputNoteDto) + ); // Assert @@ -113,14 +113,14 @@ void saveNote_FailureCodeResponse() { final NoteDto inputNoteDto = new NoteDto(); - String jsonBody = """ - { - "note_id": 123456, - } + String xml = """ + + + """; ResponseEntity unsuccessfulResponseEntity = new ResponseEntity<>( - jsonBody, HttpStatus.INTERNAL_SERVER_ERROR); + xml, HttpStatus.INTERNAL_SERVER_ERROR); when(requestBodySpec.header(anyString(), anyString())).thenReturn(requestBodySpec); when(requestBodySpec.body(any(LegacySaveNoteRequestDto.class))).thenReturn(requestBodySpec); when(responseSpec.toEntity(any(Class.class))).thenReturn(unsuccessfulResponseEntity); @@ -129,13 +129,16 @@ void saveNote_FailureCodeResponse() { LegacyGatewayResponseException lgre = assertThrows( LegacyGatewayResponseException.class, - () -> legacyNoteService.saveNote(inputNoteDto)); + () -> legacyNoteService.saveNote(inputNoteDto) + ); // Assert assertNotNull(lgre); - assertEquals("Received a non-2xx response from the Legacy Gateway: 500 INTERNAL_SERVER_ERROR", - lgre.getMessage()); + assertEquals( + "Received a non-2xx response from the Legacy Gateway: 500 INTERNAL_SERVER_ERROR", + lgre.getMessage() + ); } @Test @@ -157,7 +160,8 @@ void saveNote_ValidateRequest() throws IOException, ProcessingException { String content = Files.readString( Paths.get("src/test/resources/schemas/AccountNotes/of_create_note_in.json"), - StandardCharsets.UTF_8); + StandardCharsets.UTF_8 + ); // Parse the JSON schema JsonSchemaFactory schemaFactory = JsonSchemaFactory.byDefault(); @@ -175,7 +179,7 @@ void saveNote_ValidateRequest() throws IOException, ProcessingException { void saveNote_ValidateResponse() throws IOException, ProcessingException { LegacySaveNoteResponseDto responseDto = LegacySaveNoteResponseDto.builder() - .nodeId(1234567L) + .noteId(1234567L) .build(); // Serialize the DTO to JSON using Jackson ObjectMapper objectMapper = new ObjectMapper(); @@ -185,7 +189,8 @@ void saveNote_ValidateResponse() throws IOException, ProcessingException { String content = Files.readString( Paths.get("src/test/resources/schemas/AccountNotes/of_create_note_out.json"), - StandardCharsets.UTF_8); + StandardCharsets.UTF_8 + ); // Parse the JSON schema JsonSchemaFactory schemaFactory = JsonSchemaFactory.byDefault(); @@ -226,14 +231,15 @@ void saveNote_ErrorResponse() { // Act LegacyGatewayResponseException lgre = assertThrows( LegacyGatewayResponseException.class, - () -> legacyNoteService.saveNote(inputNoteDto)); + () -> legacyNoteService.saveNote(inputNoteDto) + ); // Assert assertNotNull(lgre); Throwable cause = lgre.getCause(); assertNotNull(cause); - assertEquals(UnrecognizedPropertyException.class, cause.getClass()); + assertEquals(RuntimeException.class, cause.getClass()); } } diff --git a/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyPartyServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyPartyServiceTest.java index c72f25da4..1c26c2c5b 100644 --- a/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyPartyServiceTest.java +++ b/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyPartyServiceTest.java @@ -1,6 +1,5 @@ package uk.gov.hmcts.opal.service.legacy; -import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -43,7 +42,7 @@ void saveParty_SuccessfulResponse() throws Exception { final PartyDto expectedPartyDto = PartyServiceTest.buildPartyDto(); - String jsonBody = createJsonBody1(); + String jsonBody = createXml(); ResponseEntity successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK); when(requestBodySpec.header(anyString(), anyString())).thenReturn(requestBodySpec); @@ -94,7 +93,7 @@ void saveParty_FailureCodeResponse() { final PartyDto inputPartyDto = new PartyDto(); - String jsonBody = createJsonBody1(); + String jsonBody = createXml(); ResponseEntity unsuccessfulResponseEntity = new ResponseEntity<>( jsonBody, HttpStatus.INTERNAL_SERVER_ERROR); @@ -125,7 +124,7 @@ void saveParty_ErrorResponse() { final PartyDto inputPartyDto = new PartyDto(); - String jsonBody = createBrokenJson(); + String jsonBody = createBrokenXml(); ResponseEntity unsuccessfulResponseEntity = new ResponseEntity<>( jsonBody, HttpStatus.OK); @@ -144,7 +143,7 @@ void saveParty_ErrorResponse() { assertNotNull(lgre); Throwable cause = lgre.getCause(); assertNotNull(cause); - assertEquals(UnrecognizedPropertyException.class, cause.getClass()); + assertEquals(RuntimeException.class, cause.getClass()); } @Test @@ -156,7 +155,7 @@ void getParty_SuccessfulResponse() throws Exception { final PartyDto expectedPartyDto = PartyServiceTest.buildPartyDto(); - String jsonBody = createJsonBody1(); + String jsonBody = createXml(); ResponseEntity successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK); when(requestBodySpec.header(anyString(), anyString())).thenReturn(requestBodySpec); @@ -205,7 +204,7 @@ void getParty_FailureCodeResponse() { // Arrange mockRestClientPost(); - String jsonBody = createJsonBody1(); + String jsonBody = createXml(); ResponseEntity unsuccessfulResponseEntity = new ResponseEntity<>( jsonBody, HttpStatus.INTERNAL_SERVER_ERROR); @@ -233,7 +232,7 @@ void getParty_FailureCodeResponse() { void getParty_ErrorResponse() { // Arrange mockRestClientPost(); - String jsonBody = createBrokenJson(); + String jsonBody = createBrokenXml(); ResponseEntity unsuccessfulResponseEntity = new ResponseEntity<>( jsonBody, HttpStatus.OK); @@ -252,7 +251,7 @@ void getParty_ErrorResponse() { assertNotNull(lgre); Throwable cause = lgre.getCause(); assertNotNull(cause); - assertEquals(UnrecognizedPropertyException.class, cause.getClass()); + assertEquals(RuntimeException.class, cause.getClass()); } @Test @@ -285,51 +284,49 @@ void testSearchCourts() { } - private static String createJsonBody1() { + private static String createXml() { return """ - { - "organisation" : false, - "organisationName" : null, - "surname" : "Smith", - "forenames" : "John James", - "initials" : "JJ", - "title" : "Mr", - "addressLine1" : "22 Acacia Avenue", - "addressLine2" : "Hammersmith", - "addressLine3" : "Birmingham", - "addressLine4" : "Cornwall", - "addressLine5" : "Scotland", - "postcode" : "SN15 9TT", - "accountType" : "TFO", - "dateOfBirth" : [ 2001, 8, 16 ], - "age" : 21, - "niNumber" : "FF22446688", - "lastChangedDate" : [ 2023, 12, 5, 15, 45 ] - } + + false + Smith + John James + JJ + Mr + 22 Acacia Avenue + Hammersmith + Birmingham + Cornwall + Scotland + SN15 9TT + TFO + 2001-08-16 + 21 + FF22446688 + 2023-12-05T15:45 + """; } - private static String createBrokenJson() { + private static String createBrokenXml() { return """ - { - "organisation" : false, - "organisationName" : null, - "surname" : "Smith", - "forenames" : "John James", - "initials" : "JJ", - "title" : "Mr", - "FOOBAR 1" : "22 Acacia Avenue", - "FOOBAR 2" : "Hammersmith", - "FOOBAR 3" : "Birmingham", - "FOOBAR 4" : "Cornwall", - "FOOBAR 5" : "Scotland", - "postcode" : "SN15 9TT", - "accountType" : "TFO", - "dateOfBirth" : [ 2001, 8, 16 ], - "age" : 21, - "niNumber" : "FF22446688", - "lastChangedDate" : [ 2023, 12, 5, 15, 45 ] - } + + false + Smith + John James + JJ + Mr + 22 Acacia Avenue + Hammersmith + Birmingham + Cornwall + Scotland + SN15 9TT + TFO + 2001-08-16 + 21 + FF22446688 + 2023-12-05T15:45 + """; } diff --git a/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyServiceTest.java b/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyServiceTest.java index 054a1efd5..cb79443bf 100644 --- a/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyServiceTest.java +++ b/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyServiceTest.java @@ -51,18 +51,21 @@ void testGetFromGateway_success() { // Arrange mockRestClientGet(); - String jsonBody = """ - { - "noteId": 1, - "noteType": "AC", - "associatedRecordType": "defendants_accounts", - "associatedRecordId": "123456", - "noteText": "This is a sample note text.", - "postedBy": "user123" - } + String xml = """ + + 1 + AC + defendants_accounts + 123456 + 10 + This is a sample note text. + 2022-12-01T12:00:00 + user123 + 1001 + """; - ResponseEntity successfulResponseEntity = new ResponseEntity<>(jsonBody, HttpStatus.OK); + ResponseEntity successfulResponseEntity = new ResponseEntity<>(xml, HttpStatus.OK); when(requestHeaderSpec.header(anyString(), anyString())).thenReturn(requestHeaderSpec); when(responseSpec.toEntity(any(Class.class))).thenReturn(successfulResponseEntity); diff --git a/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyTestsBase.java b/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyTestsBase.java index 98f6033aa..d6eccd585 100644 --- a/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyTestsBase.java +++ b/src/test/java/uk/gov/hmcts/opal/service/legacy/LegacyTestsBase.java @@ -4,6 +4,7 @@ import org.springframework.http.MediaType; import org.springframework.web.client.RestClient; import uk.gov.hmcts.opal.config.properties.LegacyGatewayProperties; +import uk.gov.hmcts.opal.util.XmlUtil; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -40,6 +41,8 @@ public abstract class LegacyTestsBase { @Mock ResponseSpec responseSpec; + XmlUtil xmlUtil = new XmlUtil(); + @SuppressWarnings("unchecked") void mockRestClientGet() { @@ -56,4 +59,7 @@ void mockRestClientPost() { when(requestBodySpec.retrieve()).thenReturn(responseSpec); } + String marshalXmlString(T object, Class clzz) { + return xmlUtil.marshalXmlString(object, clzz); + } } diff --git a/src/test/java/uk/gov/hmcts/opal/util/LocalDateAdapterTest.java b/src/test/java/uk/gov/hmcts/opal/util/LocalDateAdapterTest.java new file mode 100644 index 000000000..34c096a71 --- /dev/null +++ b/src/test/java/uk/gov/hmcts/opal/util/LocalDateAdapterTest.java @@ -0,0 +1,30 @@ +package uk.gov.hmcts.opal.util; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class LocalDateAdapterTest { + + private final LocalDateAdapter adapter = new LocalDateAdapter(); + + @Test + void shouldUnmarshalValidStringToLocalDate() throws Exception { + LocalDate result = adapter.unmarshal("2022-04-01"); + assertEquals(LocalDate.of(2022, 4, 1), result); + } + + @Test + void shouldThrowExceptionWhenUnmarshalInvalidString() { + assertThrows(Exception.class, () -> adapter.unmarshal("invalid")); + } + + @Test + void shouldMarshalLocalDateToValidString() throws Exception { + String result = adapter.marshal(LocalDate.of(2022, 4, 1)); + assertEquals("2022-04-01", result); + } +} diff --git a/src/test/java/uk/gov/hmcts/opal/util/LocalDateTimeAdapterTest.java b/src/test/java/uk/gov/hmcts/opal/util/LocalDateTimeAdapterTest.java new file mode 100644 index 000000000..b368809cb --- /dev/null +++ b/src/test/java/uk/gov/hmcts/opal/util/LocalDateTimeAdapterTest.java @@ -0,0 +1,30 @@ +package uk.gov.hmcts.opal.util; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class LocalDateTimeAdapterTest { + + private final LocalDateTimeAdapter adapter = new LocalDateTimeAdapter(); + + @Test + void shouldUnmarshalValidStringToLocalDateTime() throws Exception { + LocalDateTime result = adapter.unmarshal("2022-04-01T12:00:00"); + assertEquals(LocalDateTime.of(2022, 4, 1, 12, 0), result); + } + + @Test + void shouldThrowExceptionWhenUnmarshalInvalidString() { + assertThrows(Exception.class, () -> adapter.unmarshal("invalid")); + } + + @Test + void shouldMarshalLocalDateTimeToValidString() throws Exception { + String result = adapter.marshal(LocalDateTime.of(2022, 4, 1, 12, 0)); + assertEquals("2022-04-01T12:00", result); + } +}