From 75e77eb84a60ffe2f9b5fe37080fc6c52562854d Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 9 Jun 2020 00:25:39 +0200 Subject: [PATCH] Fixed that balance of pre booked transactions may be empty --- .../banking/fints/response/ResponseParser.kt | 8 +++-- .../fints/response/ResponseParserTest.kt | 29 ++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/response/ResponseParser.kt b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/response/ResponseParser.kt index 34f8b6b8..76db415c 100644 --- a/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/response/ResponseParser.kt +++ b/fints4k/src/commonMain/kotlin/net/dankito/banking/fints/response/ResponseParser.kt @@ -599,7 +599,7 @@ open class ResponseParser( // dataElementGroups[1] is account details val balance = parseBalance(dataElementGroups[4]) - val balanceOfPreBookedTransactions = if (dataElementGroups.size > 5) parseBalanceToNullIfZero(dataElementGroups[5]) else null + val balanceOfPreBookedTransactions = if (dataElementGroups.size > 5) parseBalanceToNullIfZeroOrNotSet(dataElementGroups[5]) else null return BalanceSegment( balance.amount, @@ -611,7 +611,11 @@ open class ResponseParser( ) } - protected open fun parseBalanceToNullIfZero(dataElementGroup: String): Balance? { + protected open fun parseBalanceToNullIfZeroOrNotSet(dataElementGroup: String): Balance? { + if (dataElementGroup.isEmpty()) { + return null + } + val parsedBalance = parseBalance(dataElementGroup) if (BigDecimal.ZERO.equals(parsedBalance.amount)) { diff --git a/fints4k/src/commonTest/kotlin/net/dankito/banking/fints/response/ResponseParserTest.kt b/fints4k/src/commonTest/kotlin/net/dankito/banking/fints/response/ResponseParserTest.kt index e547e4fe..004daa27 100644 --- a/fints4k/src/commonTest/kotlin/net/dankito/banking/fints/response/ResponseParserTest.kt +++ b/fints4k/src/commonTest/kotlin/net/dankito/banking/fints/response/ResponseParserTest.kt @@ -989,7 +989,7 @@ class ResponseParserTest : FinTsTestBase() { } @Test - fun parseBalance_BalanceOfPreBookedTransactionsIsOmitted() { + fun parseBalance_BalanceOfPreBookedTransactionsIsZero() { // given val balance = BigDecimal.ZERO @@ -1015,6 +1015,33 @@ class ResponseParserTest : FinTsTestBase() { ?: run { fail("No segment of type BalanceSegment found in ${result.receivedSegments}") } } + @Test + fun parseBalance_BalanceOfPreBookedTransactionsIsEmpty() { + + // given + val balance = BigDecimal.ZERO + val date = com.soywiz.klock.Date(2020, 6, 11) + val bankCode = "12345678" + val accountId = "0987654321" + val accountProductName = "Girokonto" + + // when + // "HISAL:7:5:3+0987654321:Girokonto:280:12345678+Girokonto+EUR++0,:EUR'" + val result = underTest.parse("HISAL:7:5:3+$accountId:$accountProductName:280:$bankCode+$accountProductName+EUR+C:0,:EUR:${convertDate(date)}:204204++0,:EUR'") + + // then + assertSuccessfullyParsedSegment(result, InstituteSegmentId.Balance, 7, 5, 3) + + result.getFirstSegmentById(InstituteSegmentId.Balance)?.let { segment -> + expect(segment.balance).toBe(balance) + expect(segment.currency).toBe("EUR") + expect(segment.date).toBe(date) + expect(segment.accountProductName).toBe(accountProductName) + expect(segment.balanceOfPreBookedTransactions).toBe(null) + } + ?: run { fail("No segment of type BalanceSegment found in ${result.receivedSegments}") } + } + private fun assertSuccessfullyParsedSegment(result: Response, segmentId: ISegmentId, segmentNumber: Int, segmentVersion: Int, referenceSegmentNumber: Int? = null) {