From a654c13d9869a12ce3177aaacc37289f59dfb344 Mon Sep 17 00:00:00 2001 From: Krish Date: Fri, 25 Sep 2020 13:24:29 +0100 Subject: [PATCH] PP-6988 Fix ledger refund transaction mapping - Fixed mapping for refunded_by and refunded_by_user_email fields from ledger --- .../ledger/model/LedgerTransaction.java | 20 +++++++------- .../connector/refund/model/domain/Refund.java | 4 +-- .../tasks/service/RefundParityChecker.java | 4 +-- .../ledger/service/LedgerServiceTest.java | 27 ++++++++++++++++--- .../connector/expunge/service/LedgerStub.java | 4 +-- .../domain/LedgerTransactionFixture.java | 20 +++++++------- .../service/RefundParityCheckerTest.java | 4 +-- .../util/TestTemplateResourceLoader.java | 3 ++- ...nsaction.json => payment_transaction.json} | 0 .../templates/ledger/refund_transaction.json | 15 +++++++++++ 10 files changed, 69 insertions(+), 32 deletions(-) rename src/test/resources/templates/ledger/{transaction.json => payment_transaction.json} (100%) create mode 100644 src/test/resources/templates/ledger/refund_transaction.json diff --git a/src/main/java/uk/gov/pay/connector/client/ledger/model/LedgerTransaction.java b/src/main/java/uk/gov/pay/connector/client/ledger/model/LedgerTransaction.java index 25083e78a1..35b6fd654f 100644 --- a/src/main/java/uk/gov/pay/connector/client/ledger/model/LedgerTransaction.java +++ b/src/main/java/uk/gov/pay/connector/client/ledger/model/LedgerTransaction.java @@ -45,8 +45,8 @@ public class LedgerTransaction { private String walletType; @JsonProperty("metadata") private Map externalMetaData; - private String userEmail; - private String userExternalId; + private String refundedBy; + private String refundedByUserEmail; private String parentTransactionId; public LedgerTransaction() { @@ -257,20 +257,20 @@ public void setExternalMetaData(Map externalMetaData) { this.externalMetaData = externalMetaData; } - public String getUserEmail() { - return userEmail; + public String getRefundedBy() { + return refundedBy; } - public void setUserEmail(String userEmail) { - this.userEmail = userEmail; + public void setRefundedBy(String refundedBy) { + this.refundedBy = refundedBy; } - public String getUserExternalId() { - return userExternalId; + public String getRefundedByUserEmail() { + return refundedByUserEmail; } - public void setUserExternalId(String userExternalId) { - this.userExternalId = userExternalId; + public void setRefundedByUserEmail(String refundedByUserEmail) { + this.refundedByUserEmail = refundedByUserEmail; } public String getParentTransactionId() { diff --git a/src/main/java/uk/gov/pay/connector/refund/model/domain/Refund.java b/src/main/java/uk/gov/pay/connector/refund/model/domain/Refund.java index 04327d96f3..e34e401438 100644 --- a/src/main/java/uk/gov/pay/connector/refund/model/domain/Refund.java +++ b/src/main/java/uk/gov/pay/connector/refund/model/domain/Refund.java @@ -49,8 +49,8 @@ public static Refund from(LedgerTransaction ledgerTransaction) { ledgerTransaction.getTransactionId(), ledgerTransaction.getAmount(), ExternalRefundStatus.fromPublicStatusLabel(ledgerTransaction.getState().getStatus()), - ledgerTransaction.getUserExternalId(), - ledgerTransaction.getUserEmail(), + ledgerTransaction.getRefundedBy(), + ledgerTransaction.getRefundedByUserEmail(), ledgerTransaction.getParentTransactionId(), ledgerTransaction.getGatewayTransactionId(), true diff --git a/src/main/java/uk/gov/pay/connector/tasks/service/RefundParityChecker.java b/src/main/java/uk/gov/pay/connector/tasks/service/RefundParityChecker.java index b0a81ed9bc..e7db022676 100644 --- a/src/main/java/uk/gov/pay/connector/tasks/service/RefundParityChecker.java +++ b/src/main/java/uk/gov/pay/connector/tasks/service/RefundParityChecker.java @@ -49,8 +49,8 @@ public ParityCheckStatus checkParity(RefundEntity refundEntity, LedgerTransactio fieldsMatch = fieldsMatch && isEquals(refundEntity.getChargeExternalId(), transaction.getParentTransactionId(), "parent_transaction_id"); fieldsMatch = fieldsMatch && isEquals(refundEntity.getAmount(), transaction.getAmount(), "amount"); fieldsMatch = fieldsMatch && isEquals(refundEntity.getGatewayTransactionId(), transaction.getGatewayTransactionId(), "gateway_transaction_id"); - fieldsMatch = fieldsMatch && isEquals(refundEntity.getUserExternalId(), transaction.getUserExternalId(), "user_external_id"); - fieldsMatch = fieldsMatch && isEquals(refundEntity.getUserEmail(), transaction.getUserEmail(), "user_email"); + fieldsMatch = fieldsMatch && isEquals(refundEntity.getUserExternalId(), transaction.getRefundedBy(), "refunded_by"); + fieldsMatch = fieldsMatch && isEquals(refundEntity.getUserEmail(), transaction.getRefundedByUserEmail(), "refunded_by_user_email"); String refundCreatedEventDate = getRefundCreatedDate(refundEntity.getExternalId()) .map(ISO_INSTANT_MILLISECOND_PRECISION::format).orElse(null); diff --git a/src/test/java/uk/gov/pay/connector/client/ledger/service/LedgerServiceTest.java b/src/test/java/uk/gov/pay/connector/client/ledger/service/LedgerServiceTest.java index 9cd35cdf36..98835ac9dd 100644 --- a/src/test/java/uk/gov/pay/connector/client/ledger/service/LedgerServiceTest.java +++ b/src/test/java/uk/gov/pay/connector/client/ledger/service/LedgerServiceTest.java @@ -31,7 +31,8 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static uk.gov.pay.connector.util.TestTemplateResourceLoader.LEDGER_GET_REFUNDS_FOR_PAYMENT; -import static uk.gov.pay.connector.util.TestTemplateResourceLoader.LEDGER_GET_TRANSACTION; +import static uk.gov.pay.connector.util.TestTemplateResourceLoader.LEDGER_PAYMENT_TRANSACTION; +import static uk.gov.pay.connector.util.TestTemplateResourceLoader.LEDGER_REFUND_TRANSACTION; import static uk.gov.pay.connector.util.TestTemplateResourceLoader.load; @RunWith(MockitoJUnitRunner.class) @@ -60,8 +61,8 @@ public void setUp() { } @Test - public void getTransaction_shouldSerialiseLedgerTransaction() throws JsonProcessingException { - when(mockResponse.readEntity(LedgerTransaction.class)).thenReturn(objectMapper.readValue(load(LEDGER_GET_TRANSACTION), LedgerTransaction.class)); + public void getTransaction_shouldSerialiseLedgerPaymentTransactionCorrectly() throws JsonProcessingException { + when(mockResponse.readEntity(LedgerTransaction.class)).thenReturn(objectMapper.readValue(load(LEDGER_PAYMENT_TRANSACTION), LedgerTransaction.class)); String externalId = "external-id"; Optional mayBeTransaction = ledgerService.getTransaction("external-id"); @@ -74,6 +75,26 @@ public void getTransaction_shouldSerialiseLedgerTransaction() throws JsonProcess assertThat(transaction.getExternalMetaData(), is(notNullValue())); } + @Test + public void getTransaction_shouldSerialiseLedgerRefundTransactionCorrectly() throws JsonProcessingException { + when(mockResponse.readEntity(LedgerTransaction.class)) + .thenReturn(objectMapper.readValue(load(LEDGER_REFUND_TRANSACTION), LedgerTransaction.class)); + + String externalId = "external-id"; + Optional mayBeTransaction = ledgerService.getTransaction("external-id"); + + assertThat(mayBeTransaction.isPresent(), is(true)); + LedgerTransaction transaction = mayBeTransaction.get(); + assertThat(transaction.getTransactionId(), is(externalId)); + assertThat(transaction.getAmount(), is(1000L)); + assertThat(transaction.getGatewayTransactionId(), is("re_1H6zDTHj08j2jFuBtG6maiHK11")); + assertThat(transaction.getParentTransactionId(), is("64pcdagc9c13vgi7n904aio3n9")); + assertThat(transaction.getRefundedBy(), is("refunded-by-external-id")); + assertThat(transaction.getRefundedByUserEmail(), is("test@example.org")); + assertThat(transaction.getCreatedDate(), is("2020-07-20T13:39:38.940Z")); + assertThat(transaction.getState().getStatus(), is("success")); + } + @Test public void getRefundsFromLedgerShouldSerialiseResponseCorrectly() throws JsonProcessingException { when(mockResponse.readEntity(RefundTransactionsForPayment.class)). diff --git a/src/test/java/uk/gov/pay/connector/expunge/service/LedgerStub.java b/src/test/java/uk/gov/pay/connector/expunge/service/LedgerStub.java index 497eff889e..b8354aa3b1 100644 --- a/src/test/java/uk/gov/pay/connector/expunge/service/LedgerStub.java +++ b/src/test/java/uk/gov/pay/connector/expunge/service/LedgerStub.java @@ -183,8 +183,8 @@ private Map refundEntityToLedgerTransaction(RefundEntity refund, Optional.ofNullable(refund.getChargeExternalId()).ifPresent(value -> map.put("parent_transaction_id", value)); Optional.of(refund.getAmount()).ifPresent(value -> map.put("amount", String.valueOf(value))); Optional.ofNullable(refund.getGatewayTransactionId()).ifPresent(value -> map.put("gateway_transaction_id", value)); - Optional.ofNullable(refund.getUserExternalId()).ifPresent(value -> map.put("user_external_id", value)); - Optional.ofNullable(refund.getUserEmail()).ifPresent(value -> map.put("user_email", value)); + Optional.ofNullable(refund.getUserExternalId()).ifPresent(value -> map.put("refunded_by", value)); + Optional.ofNullable(refund.getUserEmail()).ifPresent(value -> map.put("refunded_by_user_email", value)); Optional.ofNullable(refundCreatedEventDate).ifPresent(value -> map.put("created_date", ISO_INSTANT_MILLISECOND_PRECISION.format(value))); diff --git a/src/test/java/uk/gov/pay/connector/model/domain/LedgerTransactionFixture.java b/src/test/java/uk/gov/pay/connector/model/domain/LedgerTransactionFixture.java index e518fb8f26..e80d2562dd 100644 --- a/src/test/java/uk/gov/pay/connector/model/domain/LedgerTransactionFixture.java +++ b/src/test/java/uk/gov/pay/connector/model/domain/LedgerTransactionFixture.java @@ -63,8 +63,8 @@ public class LedgerTransactionFixture { private ZonedDateTime capturedDate; private ChargeResponse.RefundSummary refundSummary; private String parentTransactionId; - private String userEmail; - private String userExternalId; + private String refundedBy; + private String refundedByUserEmail; public static LedgerTransactionFixture aValidLedgerTransaction() { return new LedgerTransactionFixture(); @@ -149,8 +149,8 @@ public static LedgerTransactionFixture from(RefundEntity refundEntity) { .withExternalId(refundEntity.getExternalId()) .withGatewayTransactionId(refundEntity.getGatewayTransactionId()) .withParentTransactionId(refundEntity.getChargeExternalId()) - .withUserEmail(refundEntity.getUserEmail()) - .withUserExternalId(refundEntity.getUserExternalId()); + .withRefundedByUserEmail(refundEntity.getUserEmail()) + .withRefundedBy(refundEntity.getUserExternalId()); return ledgerTransactionFixture; } @@ -204,8 +204,8 @@ public LedgerTransaction build() { ledgerTransaction.setRefundSummary(refundSummary); ledgerTransaction.setParentTransactionId(parentTransactionId); - ledgerTransaction.setUserEmail(userEmail); - ledgerTransaction.setUserExternalId(userExternalId); + ledgerTransaction.setRefundedBy(refundedBy); + ledgerTransaction.setRefundedByUserEmail(refundedByUserEmail); return ledgerTransaction; } @@ -340,13 +340,13 @@ public LedgerTransactionFixture withParentTransactionId(String chargeExternalId) return this; } - public LedgerTransactionFixture withUserEmail(String userEmail) { - this.userEmail = userEmail; + public LedgerTransactionFixture withRefundedBy(String userExternalId) { + this.refundedBy = userExternalId; return this; } - public LedgerTransactionFixture withUserExternalId(String userExternalId) { - this.userExternalId = userExternalId; + public LedgerTransactionFixture withRefundedByUserEmail(String userEmail) { + this.refundedByUserEmail = userEmail; return this; } } diff --git a/src/test/java/uk/gov/pay/connector/tasks/service/RefundParityCheckerTest.java b/src/test/java/uk/gov/pay/connector/tasks/service/RefundParityCheckerTest.java index e4e5d35c48..1ce837fb5d 100644 --- a/src/test/java/uk/gov/pay/connector/tasks/service/RefundParityCheckerTest.java +++ b/src/test/java/uk/gov/pay/connector/tasks/service/RefundParityCheckerTest.java @@ -68,13 +68,13 @@ public void parityCheck_shouldReturnDataMismatchIfRefundDoesNotMatchWithLedger() @Test public void parityCheck_shouldReturnDataMismatchIfUserEmailDoesNotMatchWithLedger() { - LedgerTransaction transaction = from(refundEntity).withUserEmail(null).build(); + LedgerTransaction transaction = from(refundEntity).withRefundedByUserEmail(null).build(); assertParityCheckStatus(transaction, DATA_MISMATCH); } @Test public void parityCheck_shouldReturnDataMismatchIfUserExternalIdDoesNotMatchWithLedger() { - LedgerTransaction transaction = from(refundEntity).withUserExternalId(null).build(); + LedgerTransaction transaction = from(refundEntity).withRefundedBy(null).build(); assertParityCheckStatus(transaction, DATA_MISMATCH); } diff --git a/src/test/java/uk/gov/pay/connector/util/TestTemplateResourceLoader.java b/src/test/java/uk/gov/pay/connector/util/TestTemplateResourceLoader.java index 6a247e1eda..1db57abc0c 100644 --- a/src/test/java/uk/gov/pay/connector/util/TestTemplateResourceLoader.java +++ b/src/test/java/uk/gov/pay/connector/util/TestTemplateResourceLoader.java @@ -158,7 +158,8 @@ public class TestTemplateResourceLoader { public static final String SQS_SEND_MESSAGE_RESPONSE = TEMPLATE_BASE_NAME + "/sqs/send-message-response.xml"; public static final String SQS_ERROR_RESPONSE = TEMPLATE_BASE_NAME + "/sqs/error-response.xml"; - public static final String LEDGER_GET_TRANSACTION = TEMPLATE_BASE_NAME + "/ledger/transaction.json"; + public static final String LEDGER_PAYMENT_TRANSACTION = TEMPLATE_BASE_NAME + "/ledger/payment_transaction.json"; + public static final String LEDGER_REFUND_TRANSACTION = TEMPLATE_BASE_NAME + "/ledger/refund_transaction.json"; public static final String LEDGER_GET_REFUNDS_FOR_PAYMENT = TEMPLATE_BASE_NAME + "/ledger/refunds_for_payment.json"; public static String load(String location) { diff --git a/src/test/resources/templates/ledger/transaction.json b/src/test/resources/templates/ledger/payment_transaction.json similarity index 100% rename from src/test/resources/templates/ledger/transaction.json rename to src/test/resources/templates/ledger/payment_transaction.json diff --git a/src/test/resources/templates/ledger/refund_transaction.json b/src/test/resources/templates/ledger/refund_transaction.json new file mode 100644 index 0000000000..3e37334f03 --- /dev/null +++ b/src/test/resources/templates/ledger/refund_transaction.json @@ -0,0 +1,15 @@ +{ + "gateway_account_id": "1821", + "amount": 1000, + "state": { + "finished": true, + "status": "success" + }, + "created_date": "2020-07-20T13:39:38.940Z", + "gateway_transaction_id": "re_1H6zDTHj08j2jFuBtG6maiHK11", + "transaction_type": "REFUND", + "transaction_id": "external-id", + "parent_transaction_id": "64pcdagc9c13vgi7n904aio3n9", + "refunded_by": "refunded-by-external-id", + "refunded_by_user_email": "test@example.org" +}