Fixed that balance of prebooked transactions is optional

This commit is contained in:
dankito 2020-05-12 12:39:46 +02:00
parent 737061f606
commit b7ed5aa015
2 changed files with 41 additions and 5 deletions

View File

@ -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)

View File

@ -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<BalanceSegment>(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) {