Skip to content

Commit 071c721

Browse files
authored
Merge pull request #943 from alphagov/PP-7211-refactor_settled_date_search
PP-7211 refactor search by settled dates
2 parents f8d46f9 + 17153d6 commit 071c721

7 files changed

Lines changed: 145 additions & 21 deletions

File tree

src/main/java/uk/gov/pay/ledger/transaction/search/common/TransactionSearchParams.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
import javax.ws.rs.DefaultValue;
99
import javax.ws.rs.QueryParam;
10+
import java.time.LocalDate;
1011
import java.time.ZonedDateTime;
12+
import java.time.format.DateTimeFormatter;
1113
import java.util.ArrayList;
1214
import java.util.HashMap;
1315
import java.util.List;
@@ -16,6 +18,7 @@
1618
import java.util.Optional;
1719
import java.util.stream.Collectors;
1820

21+
import static java.time.ZoneOffset.UTC;
1922
import static org.apache.commons.lang3.StringUtils.isNotBlank;
2023

2124
public class TransactionSearchParams extends SearchParams {
@@ -41,6 +44,8 @@ public class TransactionSearchParams extends SearchParams {
4144
private static final long DEFAULT_MAX_DISPLAY_SIZE = 500L;
4245
private static final Long DEFAULT_LIMIT_TOTAL_SIZE = 10000L;
4346

47+
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
48+
4449
private long maxDisplaySize = DEFAULT_MAX_DISPLAY_SIZE;
4550

4651
@DefaultValue("2")
@@ -309,10 +314,10 @@ public Map<String, Object> getQueryMap() {
309314
queryMap.put(GATEWAY_PAYOUT_ID, gatewayPayoutId);
310315
}
311316
if (isNotBlank(fromSettledDate)) {
312-
queryMap.put(FROM_SETTLED_DATE_FIELD, ZonedDateTime.parse(fromSettledDate));
317+
queryMap.put(FROM_SETTLED_DATE_FIELD, parseFromSettledDate());
313318
}
314319
if (isNotBlank(toSettledDate)) {
315-
queryMap.put(TO_SETTLED_DATE_FIELD, ZonedDateTime.parse(toSettledDate));
320+
queryMap.put(TO_SETTLED_DATE_FIELD, parseToSettledDate());
316321
}
317322
}
318323
return queryMap;
@@ -486,4 +491,12 @@ public void setExactReferenceMatch(boolean exactReferenceMatch) {
486491
public void setGatewayTransactionId(String gatewayTransactionId) {
487492
this.gatewayTransactionId = gatewayTransactionId;
488493
}
494+
495+
private ZonedDateTime parseFromSettledDate() {
496+
return LocalDate.parse(fromSettledDate, DATE_TIME_FORMATTER).atStartOfDay().atZone(UTC);
497+
}
498+
499+
private ZonedDateTime parseToSettledDate() {
500+
return LocalDate.parse(toSettledDate, DATE_TIME_FORMATTER).plusDays(1L).atStartOfDay().atZone(UTC);
501+
}
489502
}

src/main/java/uk/gov/pay/ledger/transaction/search/common/TransactionSearchParamsValidator.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import uk.gov.pay.ledger.util.CommaDelimitedSetParameter;
66

77
import java.time.ZonedDateTime;
8+
import java.time.format.DateTimeFormatter;
89
import java.time.format.DateTimeParseException;
910

1011
import static org.apache.commons.lang3.StringUtils.isNotBlank;
@@ -39,10 +40,10 @@ private static void validateDates(TransactionSearchParams searchParams) {
3940
validateDate(TO_DATE_FIELD, searchParams.getToDate());
4041
}
4142
if (isNotBlank(searchParams.getFromSettledDate())) {
42-
validateDate(FROM_SETTLED_DATE_FIELD, searchParams.getFromSettledDate());
43+
validSettledDate(FROM_SETTLED_DATE_FIELD, searchParams.getFromSettledDate());
4344
}
4445
if (isNotBlank(searchParams.getToSettledDate())) {
45-
validateDate(TO_SETTLED_DATE_FIELD, searchParams.getToSettledDate());
46+
validSettledDate(TO_SETTLED_DATE_FIELD, searchParams.getToSettledDate());
4647
}
4748
}
4849

@@ -53,4 +54,12 @@ private static void validateDate(String fieldName, String dateToParse) {
5354
throw new UnparsableDateException(fieldName, dateToParse);
5455
}
5556
}
57+
58+
private static void validSettledDate(String fieldName, String settledDateToParse) {
59+
try {
60+
DateTimeFormatter.ofPattern("yyyy-MM-dd").parse(settledDateToParse);
61+
} catch (DateTimeParseException e) {
62+
throw new UnparsableDateException(fieldName, settledDateToParse);
63+
}
64+
}
5665
}

src/test/java/uk/gov/pay/ledger/pact/ContractTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,64 @@ public void createAPaymentWithPayoutDate(Map<String, String> params) {
418418
.build().insert(app.getJdbi());
419419
}
420420

421+
@State("three payments with payout dates exists")
422+
public void createThreePaymentsWithPaidoutDates(Map<String, String> params) {
423+
String gatewayAccountId = params.get("account_id");
424+
if (isBlank(gatewayAccountId)) {
425+
gatewayAccountId = "123456";
426+
}
427+
String gatewayPayoutId1 = randomAlphanumeric(20);
428+
String gatewayPayoutId2 = randomAlphanumeric(20);
429+
String gatewayPayoutId3 = randomAlphanumeric(20);
430+
String gatewayPayoutId4 = randomAlphanumeric(20);
431+
432+
aTransactionFixture()
433+
.withTransactionType("PAYMENT")
434+
.withGatewayAccountId(gatewayAccountId)
435+
.withGatewayPayoutId(gatewayPayoutId1)
436+
.insert(app.getJdbi());
437+
aTransactionFixture()
438+
.withTransactionType("PAYMENT")
439+
.withGatewayAccountId(gatewayAccountId)
440+
.withGatewayPayoutId(gatewayPayoutId2)
441+
.insert(app.getJdbi());
442+
aTransactionFixture()
443+
.withTransactionType("PAYMENT")
444+
.withGatewayAccountId(gatewayAccountId)
445+
.withGatewayPayoutId(gatewayPayoutId3)
446+
.insert(app.getJdbi());
447+
aTransactionFixture()
448+
.withTransactionType("REFUND")
449+
.withGatewayAccountId(gatewayAccountId)
450+
.withGatewayPayoutId(gatewayPayoutId4)
451+
.insert(app.getJdbi());
452+
453+
aPayoutFixture()
454+
.withGatewayPayoutId(gatewayPayoutId1)
455+
.withGatewayAccountId(gatewayAccountId)
456+
.withPaidOutDate(ZonedDateTime.parse("2020-09-19T10:15:30Z"))
457+
.build()
458+
.insert(app.getJdbi());
459+
aPayoutFixture()
460+
.withGatewayPayoutId(gatewayPayoutId2)
461+
.withGatewayAccountId(gatewayAccountId)
462+
.withPaidOutDate(ZonedDateTime.parse("2020-09-18T23:59:59.999Z"))
463+
.build()
464+
.insert(app.getJdbi());
465+
aPayoutFixture()
466+
.withGatewayPayoutId(gatewayPayoutId3)
467+
.withGatewayAccountId(gatewayAccountId)
468+
.withPaidOutDate(ZonedDateTime.parse("2020-09-21T00:00:00Z"))
469+
.build()
470+
.insert(app.getJdbi());
471+
aPayoutFixture()
472+
.withGatewayPayoutId(gatewayPayoutId4)
473+
.withGatewayAccountId(gatewayAccountId)
474+
.withPaidOutDate(ZonedDateTime.parse("2020-09-19T19:05:00Z"))
475+
.build()
476+
.insert(app.getJdbi());
477+
}
478+
421479
private void createARefundTransaction(String parentExternalId, String gatewayAccountId,
422480
String externalId, Long amount,
423481
String reference, String description,

src/test/java/uk/gov/pay/ledger/transaction/dao/TransactionDaoSearchIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ public void shouldReturn1Records_WhenSearchingBySettledDate() {
830830

831831

832832
TransactionSearchParams searchParams = new TransactionSearchParams();
833-
searchParams.setFromSettledDate(paidoutDate);
833+
searchParams.setFromSettledDate("2020-09-08");
834834

835835
List<TransactionEntity> transactionList = transactionDao.searchTransactions(searchParams);
836836
assertThat(transactionList.size(), is(1));
@@ -839,7 +839,7 @@ public void shouldReturn1Records_WhenSearchingBySettledDate() {
839839
assertThat(total, is(1L));
840840

841841
searchParams = new TransactionSearchParams();
842-
searchParams.setToSettledDate(paidoutDate);
842+
searchParams.setToSettledDate("2020-09-08");
843843

844844
transactionList = transactionDao.searchTransactions(searchParams);
845845
assertThat(transactionList.size(), is(1));
@@ -848,8 +848,8 @@ public void shouldReturn1Records_WhenSearchingBySettledDate() {
848848
assertThat(total, is(1L));
849849

850850
searchParams = new TransactionSearchParams();
851-
searchParams.setFromSettledDate("2020-09-08T00:25:00.000Z");
852-
searchParams.setToSettledDate("2020-09-10T00:00:25.000Z");
851+
searchParams.setFromSettledDate("2020-09-08");
852+
searchParams.setToSettledDate("2020-09-10");
853853

854854
transactionList = transactionDao.searchTransactions(searchParams);
855855
assertThat(transactionList.size(), is(1));

src/test/java/uk/gov/pay/ledger/transaction/resource/TransactionResourceSearchIT.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,11 @@ public void shouldReturnSettledDateForSearch() {
451451

452452
@Test
453453
public void shouldReturnSettledDateForSearchBySettledDate() {
454-
String gatewayAccountId = "12345678";
454+
String gatewayAccountId = randomAlphanumeric(10);
455455
String gatewayPayoutId1 = randomAlphanumeric(20);
456456
String gatewayPayoutId2 = randomAlphanumeric(20);
457457
String gatewayPayoutId3 = randomAlphanumeric(20);
458+
String gatewayPayoutId4 = randomAlphanumeric(20);
458459

459460
aTransactionFixture()
460461
.withTransactionType("PAYMENT")
@@ -471,23 +472,34 @@ public void shouldReturnSettledDateForSearchBySettledDate() {
471472
.withGatewayAccountId(gatewayAccountId)
472473
.withGatewayPayoutId(gatewayPayoutId3)
473474
.insert(rule.getJdbi());
475+
aTransactionFixture()
476+
.withTransactionType("REFUND")
477+
.withGatewayAccountId(gatewayAccountId)
478+
.withGatewayPayoutId(gatewayPayoutId4)
479+
.insert(rule.getJdbi());
474480

475481
aPayoutFixture()
476482
.withGatewayPayoutId(gatewayPayoutId1)
477483
.withGatewayAccountId(gatewayAccountId)
478-
.withPaidOutDate(ZonedDateTime.parse("2020-09-07T10:15:30Z"))
484+
.withPaidOutDate(ZonedDateTime.parse("2020-09-07T00:00:01Z"))
479485
.build()
480486
.insert(rule.getJdbi());
481487
aPayoutFixture()
482488
.withGatewayPayoutId(gatewayPayoutId2)
483489
.withGatewayAccountId(gatewayAccountId)
484-
.withPaidOutDate(ZonedDateTime.parse("2020-09-05T10:15:30Z"))
490+
.withPaidOutDate(ZonedDateTime.parse("2020-09-06T23:59:59.999Z"))
485491
.build()
486492
.insert(rule.getJdbi());
487493
aPayoutFixture()
488494
.withGatewayPayoutId(gatewayPayoutId3)
489495
.withGatewayAccountId(gatewayAccountId)
490-
.withPaidOutDate(ZonedDateTime.parse("2020-09-09T10:15:30Z"))
496+
.withPaidOutDate(ZonedDateTime.parse("2020-09-09T00:00:00Z"))
497+
.build()
498+
.insert(rule.getJdbi());
499+
aPayoutFixture()
500+
.withGatewayPayoutId(gatewayPayoutId4)
501+
.withGatewayAccountId(gatewayAccountId)
502+
.withPaidOutDate(ZonedDateTime.parse("2020-09-08T23:59:59Z"))
491503
.build()
492504
.insert(rule.getJdbi());
493505

@@ -496,19 +508,20 @@ public void shouldReturnSettledDateForSearchBySettledDate() {
496508
.accept(JSON)
497509
.get("/v1/transaction?" +
498510
"account_id=" + gatewayAccountId +
499-
"&from_settled_date=2020-09-07T09:30:00Z" +
500-
"&to_settled_date=2020-09-08T10:30:00Z" +
511+
"&from_settled_date=2020-09-07" +
512+
"&to_settled_date=2020-09-08" +
501513
"&page=1" +
502514
"&display_size=5"
503515
)
504516
.then()
505517
.statusCode(Response.Status.OK.getStatusCode())
506518
.contentType(JSON)
507-
.body("count", is(1))
508-
.body("results[0].settlement_summary.settled_date", is(notNullValue()))
509-
.body("_links.self.href", containsString("v1/transaction?account_id=" + gatewayAccountId + "&from_settled_date=2020-09-07T09%3A30%3A00Z&to_settled_date=2020-09-08T10%3A30%3A00Z&page=1&display_size=5"))
510-
.body("_links.first_page.href", containsString("v1/transaction?account_id=" + gatewayAccountId + "&from_settled_date=2020-09-07T09%3A30%3A00Z&to_settled_date=2020-09-08T10%3A30%3A00Z&page=1&display_size=5"))
511-
.body("_links.last_page.href", containsString("v1/transaction?account_id=" + gatewayAccountId + "&from_settled_date=2020-09-07T09%3A30%3A00Z&to_settled_date=2020-09-08T10%3A30%3A00Z&page=1&display_size=5"))
519+
.body("count", is(2))
520+
.body("results[0].settlement_summary.settled_date", is("2020-09-08"))
521+
.body("results[1].settlement_summary.settled_date", is("2020-09-07"))
522+
.body("_links.self.href", containsString("v1/transaction?account_id=" + gatewayAccountId + "&from_settled_date=2020-09-07&to_settled_date=2020-09-08&page=1&display_size=5"))
523+
.body("_links.first_page.href", containsString("v1/transaction?account_id=" + gatewayAccountId + "&from_settled_date=2020-09-07&to_settled_date=2020-09-08&page=1&display_size=5"))
524+
.body("_links.last_page.href", containsString("v1/transaction?account_id=" + gatewayAccountId + "&from_settled_date=2020-09-07&to_settled_date=2020-09-08&page=1&display_size=5"))
512525
.body("_links.prev_page.href", is(nullValue()))
513526
.body("_links.next_page.href", is(nullValue()));
514527
}

src/test/java/uk/gov/pay/ledger/transaction/search/common/TransactionSearchParamsTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.jupiter.api.BeforeEach;
44
import org.junit.jupiter.api.Test;
55

6+
import java.time.ZonedDateTime;
67
import java.util.List;
78

89
import static org.hamcrest.MatcherAssert.assertThat;
@@ -155,4 +156,18 @@ public void getLimitTotalSizeShouldReturnDefaultValueIfBelowDisplaySize() {
155156

156157
assertThat(transactionSearchParams.getLimitTotalSize(), is(10000L));
157158
}
159+
160+
@Test
161+
public void shouldApplyFromSettledDateCorrectly() {
162+
transactionSearchParams.setFromSettledDate("2020-09-25");
163+
assertThat(transactionSearchParams.getQueryMap().get("from_settled_date"), is(ZonedDateTime.parse("2020-09-25T00:00:00.000Z")));
164+
assertThat(transactionSearchParams.buildQueryParamString(1L), containsString("from_settled_date=2020-09-25"));
165+
}
166+
167+
@Test
168+
public void shouldApplyToSettledDateCorrectly() {
169+
transactionSearchParams.setToSettledDate("2020-09-26");
170+
assertThat(transactionSearchParams.getQueryMap().get("to_settled_date"), is(ZonedDateTime.parse("2020-09-27T00:00:00.000Z")));
171+
assertThat(transactionSearchParams.buildQueryParamString(1L), containsString("to_settled_date=2020-09-26"));
172+
}
158173
}

src/test/java/uk/gov/pay/ledger/transaction/search/common/TransactionSearchParamsValidatorTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public void shouldNotThrowException_whenValidDateFormats() {
6262

6363
@Test
6464
public void shouldNotThrowException_whenValidSettledDateFormats() {
65-
searchParams.setFromSettledDate("2020-09-10T10:15:30Z");
66-
searchParams.setToSettledDate("2020-09-10T10:15:30Z");
65+
searchParams.setFromSettledDate("2020-09-10");
66+
searchParams.setToSettledDate("2020-09-10");
6767
TransactionSearchParamsValidator.validateSearchParams(searchParams, null);
6868
}
6969

@@ -99,4 +99,20 @@ public void validateSearchParamsForCsvShouldThrowExceptionForInvalidDates(String
9999
assertThrows(UnparsableDateException.class, () -> TransactionSearchParamsValidator.validateSearchParamsForCsv(
100100
searchParams, new CommaDelimitedSetParameter("1")));
101101
}
102+
103+
@Test
104+
public void validateFromSettledDateSearchParamsShouldThrowExceptionForInvalidDates() {
105+
searchParams.setFromSettledDate("25/09/2020");
106+
UnparsableDateException unparsableDateException = assertThrows(UnparsableDateException.class,
107+
() -> TransactionSearchParamsValidator.validateSearchParams(searchParams, null));
108+
assertThat(unparsableDateException.getMessage(), is("Input from_settled_date (25/09/2020) is wrong format"));
109+
}
110+
111+
@Test
112+
public void validateToSettledDateSearchParamsShouldThrowExceptionForInvalidDates() {
113+
searchParams.setToSettledDate("2020.09.25");
114+
UnparsableDateException unparsableDateException = assertThrows(UnparsableDateException.class,
115+
() -> TransactionSearchParamsValidator.validateSearchParams(searchParams, null));
116+
assertThat(unparsableDateException.getMessage(), is("Input to_settled_date (2020.09.25) is wrong format"));
117+
}
102118
}

0 commit comments

Comments
 (0)