-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGetTransactionsTest.java
More file actions
193 lines (158 loc) · 8.45 KB
/
GetTransactionsTest.java
File metadata and controls
193 lines (158 loc) · 8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package ai.pluggy.client.integration;
import static ai.pluggy.client.integration.helper.AccountHelper.retrieveFirstAccountId;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import ai.pluggy.client.request.TransactionsSearchRequest;
import ai.pluggy.client.response.Transaction;
import ai.pluggy.client.response.TransactionsResponse;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import retrofit2.Response;
public class GetTransactionsTest extends BaseApiIntegrationTest {
@Disabled("currently, dont work correctly - re-enable once it's working again")
@SneakyThrows
@Test
void getTransactions_byExistingAccountId_ok() {
// precondition: retrieve accounts data
String firstAccountId = retrieveFirstAccountId(client, this.getItemsIdCreated());
// get first account transactions
Response<TransactionsResponse> transactionsResponse = client.service()
.getTransactions(firstAccountId)
.execute();
TransactionsResponse transactions = transactionsResponse.body();
// expect transactions response to contain 1 or more results
assertNotNull(transactions);
assertNotNull(transactions.getResults());
assertTrue(transactions.getResults().size() > 0);
}
@Disabled("currently, dont work correctly - re-enable once it's working again")
@SneakyThrows
@Test
void getTransactions_byExistingAccountId_withDateFilters_ok() {
// precondition: retrieve accounts data
String firstAccountId = retrieveFirstAccountId(client, this.getItemsIdCreated());
// precondition: get account transactions (all results)
Response<TransactionsResponse> allTransactionsResponse = client.service()
.getTransactions(firstAccountId, new TransactionsSearchRequest().pageSize(100))
.execute();
TransactionsResponse allTransactionsResults = allTransactionsResponse.body();
// expect transactions response to include 3 or more results
assertNotNull(allTransactionsResults);
List<Transaction> allTransactions = allTransactionsResults.getResults();
assertNotNull(allTransactions);
// build error string in case of no transactions filtered result.
String allTxsString = transactionsToIdAndDateStrings(allTransactionsResults);
int allTransactionsCount = allTransactions.size();
assertTrue(allTransactionsCount >= 3,
String.format("Expected at least 3 txs for account id '%s', got tx(s): ['%s']",
firstAccountId, allTxsString));
// sort transactions result by date string, and extract a sub-range of dates
// from the total txs results
allTransactions.sort(Comparator.comparing(Transaction::getDate));
String minDate = allTransactions.get(0).getDate();
String middleDate = allTransactions.get(Math.floorDiv(allTransactionsCount, 2)).getDate();
String maxDate = allTransactions.get(allTransactionsCount - 1).getDate();
assertTrue(minDate.compareTo(maxDate) < 0,
String.format("expected min date '%s' of txs to be earlier than max date '%s'",
minDate, maxDate));
assertTrue(minDate.compareTo(middleDate) < 0,
String.format("expected min date '%s' of txs to be earlier than middle date '%s'",
minDate, middleDate));
assertTrue(middleDate.compareTo(maxDate) < 0,
String.format("expected middle date '%s' of txs to be earlier than max date '%s'",
minDate, middleDate));
// get account transactions with sub-range date filters
String fromDateFilter = "2019-11-15";
String toDateFilter = middleDate.substring(0, 10);
TransactionsSearchRequest dateFilters = new TransactionsSearchRequest()
.from(fromDateFilter)
.to(toDateFilter)
.pageSize(100);
Response<TransactionsResponse> transactionsFilteredResponse = client.service()
.getTransactions(firstAccountId, dateFilters)
.execute();
TransactionsResponse transactionsFiltered = transactionsFilteredResponse.body();
// expect filtered transactions response to include 1 or more results
assertNotNull(transactionsFiltered);
assertNotNull(transactionsFiltered.getResults());
int transactionsFilteredCount = transactionsFiltered.getResults().size();
String expectedTransactionsFilteredMsg = String.format(
"Expected at least 1 tx between '%s' (out of total '%d' txs) for account id '%s', all txs: '%s'",
dateFilters,
allTransactionsCount, firstAccountId, allTxsString);
assertTrue(transactionsFilteredCount > 0, expectedTransactionsFilteredMsg);
// expect filtered transactions count to be less than total transactions count
assertTrue(transactionsFilteredCount < allTransactionsCount,
String.format(
"Transactions filtered result: %d should be less than all transactions result: %d, using date filters '%s'",
transactionsFilteredCount, allTransactionsCount, dateFilters));
}
@SneakyThrows
@Test
void getTransactions_byExistingAccountId_withPageFilters_ok() {
// precondition: retrieve accounts data
String firstAccountId = retrieveFirstAccountId(client, this.getItemsIdCreated());
// fetch first page
int pageSize = 1;
int firstPage = 1;
TransactionsSearchRequest firstPageParams = new TransactionsSearchRequest()
.page(firstPage)
.pageSize(pageSize);
Response<TransactionsResponse> firstPageResponse = client.service()
.getTransactions(firstAccountId, firstPageParams)
.execute();
// precondition: expect to have more txs present in the next page
TransactionsResponse transactionsFirstPage = firstPageResponse.body();
assertNotNull(transactionsFirstPage);
List<Transaction> firstPageTransactions = transactionsFirstPage.getResults();
int firstPageTxsCount = firstPageTransactions.size();
Integer allTxsCount = transactionsFirstPage.getTotal();
// skip if the sandbox account doesn't have enough txs to paginate meaningfully
assumeTrue(allTxsCount >= 2,
String.format("skipping: need at least 2 txs to validate page 2 (got '%d') for account id '%s'",
allTxsCount, firstAccountId));
// expect first page to be complete (ie. to equal page size param)
assertEquals(firstPageTxsCount, pageSize,
String.format("expected first page txs response count '%d' to equal the page size param '%d'",
firstPageTxsCount, pageSize));
// fetch next page
TransactionsSearchRequest nextPageParams = new TransactionsSearchRequest()
.page(firstPageParams.getPage() + 1)
.pageSize(firstPageParams.getPageSize());
Response<TransactionsResponse> nextPageResponse = client.service()
.getTransactions(firstAccountId, nextPageParams)
.execute();
// expect second page to include results, and to be different than first page.
TransactionsResponse transactionsNextPage = nextPageResponse.body();
assertNotNull(transactionsNextPage);
List<Transaction> nextPageTransactions = transactionsNextPage.getResults();
// skip if page 2 came back empty even though total reported >= 2 — sandbox
// metadata can be inconsistent with what pagination actually returns.
assumeTrue(nextPageTransactions.size() > 0,
String.format("skipping: page 2 returned empty for account id '%s' (total reported '%d')",
firstAccountId, allTxsCount));
// fetch transactions by ids
Response<TransactionsResponse> transactionsbyIds = client.service()
.getTransactions(firstAccountId,
new TransactionsSearchRequest().ids(Arrays.asList(nextPageTransactions.get(0).getId())))
.execute();
assertEquals(transactionsbyIds.body().getResults().size(), 1);
// expect first tx of next page to be different from the first page.
assertNotEquals(nextPageTransactions.get(0).getId(),
firstPageTransactions.get(firstPageTxsCount - 1).getId());
}
private String transactionsToIdAndDateStrings(TransactionsResponse allTransactions) {
return allTransactions.getResults().stream()
.map(transaction -> String.format(
"{id=%s, date=%s}", transaction.getId(), transaction.getDate().substring(0, 10)))
.collect(Collectors.joining(", "));
}
}