Skip to content
This repository was archived by the owner on Jan 15, 2021. It is now read-only.

Commit 95a448f

Browse files
atamanromanwandi34
authored andcommitted
Enable withBalance for transactions
1 parent a26659a commit 95a448f

File tree

4 files changed

+82
-28
lines changed

4 files changed

+82
-28
lines changed

service/src/main/java/de/adorsys/psd2/sandbox/xs2a/service/ais/AccountSpiImpl.java

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Optional;
2424
import java.util.stream.Collectors;
25+
import org.apache.commons.lang3.tuple.Pair;
2526
import org.jetbrains.annotations.NotNull;
2627
import org.springframework.stereotype.Service;
2728

@@ -107,9 +108,22 @@ public SpiResponse<SpiTransactionReport> requestTransactionsForAccount(
107108
.map(testDataMapper::mapTransactionToSpiTransaction)
108109
.collect(Collectors.toList());
109110

111+
List<SpiAccountBalance> balances = null;
112+
if (withBalance) {
113+
Pair<List<SpiAccountBalance>, SpiResponse<SpiTransactionReport>> rslt = getBalancesForAccount(
114+
spiAccountReference,
115+
spiAccountConsent,
116+
aspspConsentData
117+
);
118+
if (rslt.getRight() != null) {
119+
return rslt.getRight();
120+
}
121+
balances = rslt.getLeft();
122+
}
123+
110124
SpiTransactionReport spiTransactionReport = new SpiTransactionReport(
111125
spiTransactions,
112-
null,
126+
balances,
113127
SpiTransactionReport.RESPONSE_TYPE_JSON,
114128
null);
115129

@@ -156,30 +170,18 @@ public SpiResponse<List<SpiAccountBalance>> requestBalancesForAccount(
156170
@NotNull SpiAccountConsent spiAccountConsent,
157171
@NotNull AspspConsentData aspspConsentData) {
158172

159-
Optional<TestPsu> psu = testDataService.getPsuByIban(spiAccountReference.getIban());
160-
161-
if (!psu.isPresent()) {
162-
return SpiResponse.<List<SpiAccountBalance>>builder()
163-
.aspspConsentData(aspspConsentData)
164-
.fail(SpiResponseStatus.TECHNICAL_FAILURE);
165-
}
166-
167-
Optional<Account> account = testDataService.getDistinctAccount(
168-
psu.get().getPsuId(),
169-
spiAccountReference.getResourceId());
170-
171-
if (!account.isPresent()) {
172-
return SpiResponse.<List<SpiAccountBalance>>builder()
173-
.aspspConsentData(aspspConsentData)
174-
.fail(SpiResponseStatus.TECHNICAL_FAILURE);
173+
Pair<List<SpiAccountBalance>, SpiResponse<List<SpiAccountBalance>>> res = getBalancesForAccount(
174+
spiAccountReference,
175+
spiAccountConsent,
176+
aspspConsentData
177+
);
178+
if (res.getRight() != null) {
179+
return res.getRight();
175180
}
176181

177-
List<SpiAccountBalance> balances = testDataMapper
178-
.mapBalanceListToSpiBalanceList(account.get(), spiAccountConsent.getAccess().getBalances());
179-
180182
return SpiResponse.<List<SpiAccountBalance>>builder()
181183
.aspspConsentData(aspspConsentData)
182-
.payload(balances)
184+
.payload(res.getLeft())
183185
.success();
184186
}
185187

@@ -224,4 +226,39 @@ private Optional<List<Transaction>> getTransactionsByIbanAndAccountId(String iba
224226
return testDataService
225227
.getTransactions(psu.get().getPsuId(), accountId);
226228
}
229+
230+
/*
231+
* @return Pair<Result, Error> with either Result or Error set
232+
*/
233+
private <T> Pair<List<SpiAccountBalance>, SpiResponse<T>> getBalancesForAccount(
234+
SpiAccountReference accountReference, SpiAccountConsent spiAccountConsent,
235+
AspspConsentData aspspConsentData
236+
) {
237+
Optional<TestPsu> psu = testDataService.getPsuByIban(accountReference.getIban());
238+
239+
if (!psu.isPresent()) {
240+
return Pair.of(null, SpiResponse.<T>builder()
241+
.aspspConsentData(aspspConsentData)
242+
.fail(SpiResponseStatus.TECHNICAL_FAILURE)
243+
);
244+
}
245+
246+
Optional<Account> account = testDataService.getDistinctAccount(
247+
psu.get().getPsuId(),
248+
accountReference.getResourceId()
249+
);
250+
251+
if (!account.isPresent()) {
252+
return Pair.of(null, SpiResponse.<T>builder()
253+
.aspspConsentData(aspspConsentData)
254+
.fail(SpiResponseStatus.TECHNICAL_FAILURE));
255+
}
256+
257+
return Pair.of(
258+
testDataMapper.mapBalanceListToSpiBalanceList(
259+
account.get(), spiAccountConsent.getAccess().getBalances()
260+
),
261+
null
262+
);
263+
}
227264
}

service/src/main/resources/bank_profile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ setting:
1818
consentLifetime: 0
1919
transactionLifetime: 0
2020
allPsd2Support: false
21-
transactionsWithoutBalancesSupported: false
21+
transactionsWithoutBalancesSupported: true
2222
signingBasketSupported: false
2323
paymentCancellationAuthorizationMandated: true
2424
piisConsentSupported: false

service/src/test/java/de/adorsys/psd2/sandbox/xs2a/ais/AIS.feature

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,13 @@ Feature: AIS
108108
Given PSU created a consent on dedicated accounts for account information <accounts>, balances <balances> and transactions <transactions>
109109
And PSU authorised the consent with psu-id <psu-id>, password <password>, sca-method <sca-method> and tan <tan>
110110
And PSU accesses the account list withBalances <withBalance>
111-
When PSU accesses the transaction list
111+
When PSU accesses the transaction list withBalances <withBalance>
112112
Then the transaction list data are received
113113
Examples:
114114
| accounts | balances | transactions | psu-id | password | sca-method | tan | withBalance |
115115
| DE11760365688833114935 | DE11760365688833114935 | DE11760365688833114935 | PSU-Successful | 12345 | SMS_OTP | 54321 | false |
116+
| DE11760365688833114935 | DE11760365688833114935 | DE11760365688833114935 | PSU-Successful | 12345 | SMS_OTP | 54321 | true |
117+
116118

117119
Scenario Outline: Get Single Transaction
118120
Given PSU created a consent on dedicated accounts for account information <accounts>, balances <balances> and transactions <transactions>

service/src/test/java/de/adorsys/psd2/sandbox/xs2a/ais/AisSteps.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.hamcrest.CoreMatchers.containsString;
44
import static org.hamcrest.CoreMatchers.equalTo;
5+
import static org.hamcrest.CoreMatchers.nullValue;
56
import static org.hamcrest.MatcherAssert.assertThat;
67
import static org.junit.Assert.assertEquals;
78
import static org.junit.Assert.assertNotNull;
@@ -293,7 +294,7 @@ public void deleteConsent() {
293294
}
294295

295296
@When("PSU accesses the account list withBalances (.*)")
296-
public void getAccountList(String withBalance) {
297+
public void getAccountListWithBalance(String withBalance) {
297298
HashMap<String, String> headers = TestUtils.createSession();
298299
headers.put("Consent-ID", context.getConsentId());
299300
Request<?> request = Request.emptyRequest(headers);
@@ -312,15 +313,23 @@ public void getAccountList(String withBalance) {
312313
}
313314

314315
@When("PSU accesses the transaction list")
315-
public void getTransactionList() {
316+
public void getAccountList() {
317+
getTransactionList("true");
318+
}
319+
320+
@When("PSU accesses the transaction list withBalances (.*)")
321+
public void getTransactionList(String withBalance) {
316322
ResponseEntity<AccountList> actualResponse = context.getActualResponse();
317323
HashMap<String, String> headers = TestUtils.createSession();
318324
headers.put("Consent-ID", context.getConsentId());
319325
Request<?> request = Request.emptyRequest(headers);
320326
context.setAccountId(actualResponse.getBody().getAccounts().get(0).getResourceId());
321-
String queryParams = "?bookingStatus=both&dateFrom="
322-
+ LocalDate.now().minusYears(1) + "&dateTo="
323-
+ LocalDate.now();
327+
String queryParams = String.format(
328+
"?bookingStatus=both&dateFrom=%s&dateTo=%s&withBalance=%s",
329+
LocalDate.now().minusYears(1), LocalDate.now(), withBalance
330+
);
331+
332+
context.setWithBalance(Boolean.parseBoolean(withBalance));
324333

325334
ResponseEntity<TransactionsResponse200Json> response = template.exchange(
326335
"accounts/" + context.getAccountId() + "/transactions" + queryParams,
@@ -400,6 +409,12 @@ public void receiveTransactionListData() {
400409
ResponseEntity<TransactionsResponse200Json> actualResponse = context.getActualResponse();
401410

402411
assertThat(actualResponse.getBody().getTransactions().getBooked().size(), equalTo(5));
412+
413+
if (context.isWithBalance()) {
414+
assertThat(actualResponse.getBody().getBalances().size(), equalTo(2));
415+
} else {
416+
assertNull(actualResponse.getBody().getBalances());
417+
}
403418
}
404419

405420
@Then("the transaction data are received")

0 commit comments

Comments
 (0)