From b7ed5aa0154cd8638d5a6ed61758b670a0503c7f Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 12 May 2020 12:39:46 +0200 Subject: [PATCH] Fixed that balance of prebooked transactions is optional --- .../dankito/fints/response/ResponseParser.kt | 14 ++++++-- .../fints/response/ResponseParserTest.kt | 32 +++++++++++++++++-- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/fints4k/src/main/kotlin/net/dankito/fints/response/ResponseParser.kt b/fints4k/src/main/kotlin/net/dankito/fints/response/ResponseParser.kt index f86fa3b6..c0e9f29c 100644 --- a/fints4k/src/main/kotlin/net/dankito/fints/response/ResponseParser.kt +++ b/fints4k/src/main/kotlin/net/dankito/fints/response/ResponseParser.kt @@ -547,18 +547,28 @@ open class ResponseParser @JvmOverloads constructor( // dataElementGroups[1] is account details val balance = parseBalance(dataElementGroups[4]) - val balanceOfPreBookedTransactions = parseBalance(dataElementGroups[5]) + val balanceOfPreBookedTransactions = if (dataElementGroups.size > 5) parseBalanceToNullIfZero(dataElementGroups[5]) else null return BalanceSegment( balance.amount, parseString(dataElementGroups[3]), balance.date, parseString(dataElementGroups[2]), - if (balanceOfPreBookedTransactions.amount.equals(BigDecimal.ZERO)) null else balanceOfPreBookedTransactions.amount, + balanceOfPreBookedTransactions?.amount, segment ) } + protected open fun parseBalanceToNullIfZero(dataElementGroup: String): Balance? { + val parsedBalance = parseBalance(dataElementGroup) + + if (BigDecimal.ZERO.equals(parsedBalance.amount)) { + return null + } + + return parsedBalance + } + protected open fun parseBalance(dataElementGroup: String): Balance { val dataElements = getDataElements(dataElementGroup) diff --git a/fints4k/src/test/kotlin/net/dankito/fints/response/ResponseParserTest.kt b/fints4k/src/test/kotlin/net/dankito/fints/response/ResponseParserTest.kt index a6054d62..51c89709 100644 --- a/fints4k/src/test/kotlin/net/dankito/fints/response/ResponseParserTest.kt +++ b/fints4k/src/test/kotlin/net/dankito/fints/response/ResponseParserTest.kt @@ -11,11 +11,10 @@ import net.dankito.fints.messages.datenelementgruppen.implementierte.signatur.Si import net.dankito.fints.messages.segmente.id.ISegmentId import net.dankito.fints.messages.segmente.id.MessageSegmentId import net.dankito.fints.response.segments.* -import net.dankito.utils.datetime.asUtilDate import org.assertj.core.api.Assertions.assertThat import org.junit.Assert import org.junit.Test -import java.time.LocalDate +import java.math.BigDecimal class ResponseParserTest : FinTsTestBase() { @@ -918,7 +917,7 @@ class ResponseParserTest : FinTsTestBase() { // given val balance = 1234.56.toBigDecimal() - val date = LocalDate.of(1988, 3, 27).asUtilDate() + val date = java.util.Date(88, 2, 27) val bankCode = "12345678" val accountId = "0987654321" val accountProductName = "Sichteinlagen" @@ -940,6 +939,33 @@ class ResponseParserTest : FinTsTestBase() { ?: run { Assert.fail("No segment of type Balance found in ${result.receivedSegments}") } } + @Test + fun parseBalance_BalanceOfPreBookedTransactionsIsOmitted() { + + // given + val balance = BigDecimal.ZERO + val date = java.util.Date(2020, 5, 11) + val bankCode = "12345678" + val accountId = "0987654321" + val accountProductName = "Girokonto" + + // when + // "HISAL:7:5:3+0987654321:Girokonto:280:12345678+Girokonto+EUR+C:0,:EUR:20200511:204204'" + val result = underTest.parse("HISAL:7:5:3+$accountId:$accountProductName:280:$bankCode+$accountProductName+EUR+C:0,:EUR:${convertDate(date)}:204204'") + + // then + assertSuccessfullyParsedSegment(result, InstituteSegmentId.Balance, 7, 5, 3) + + result.getFirstSegmentById(InstituteSegmentId.Balance)?.let { segment -> + assertThat(segment.balance).isEqualTo(balance) + assertThat(segment.currency).isEqualTo("EUR") + assertThat(segment.date).isEqualTo(date) + assertThat(segment.accountProductName).isEqualTo(accountProductName) + assertThat(segment.balanceOfPreBookedTransactions).isNull() + } + ?: run { Assert.fail("No segment of type Balance found in ${result.receivedSegments}") } + } + private fun assertSuccessfullyParsedSegment(result: Response, segmentId: ISegmentId, segmentNumber: Int, segmentVersion: Int, referenceSegmentNumber: Int? = null) {