Implemented that all separators get masked not only DataElementsSeparator

This commit is contained in:
dankl 2019-10-26 00:17:27 +02:00 committed by dankito
parent 267a7db8d8
commit 30baacb54c
3 changed files with 95 additions and 4 deletions

View File

@ -8,7 +8,7 @@ abstract class Nachrichtenteil(protected val messageUtils: MessageUtils = Messag
abstract fun format(): String
open fun maskMessagePart(messagePart: String, separator: String): String {
open fun maskMessagePart(messagePart: String, separators: List<String>): String {
var maskedMessagePart = messagePart
val binaryDataRanges = messageUtils.findBinaryDataRanges(messagePart)
@ -20,9 +20,24 @@ abstract class Nachrichtenteil(protected val messageUtils: MessageUtils = Messag
.maskCharacterAtIndices(maskedMessagePart, Separators.MaskingCharacter, unmaskedMaskingCharacterIndices)
val separatorIndices = messageUtils.findSeparatorIndices(maskedMessagePart, separator, binaryDataRanges)
separators.forEach { separator ->
maskedMessagePart = maskSeparators(maskedMessagePart, separator, binaryDataRanges)
}
return messageUtils.maskCharacterAtIndices(maskedMessagePart, separator, separatorIndices)
maskedMessagePart = maskSeparators(maskedMessagePart, Separators.DataElementGroupsSeparator, binaryDataRanges)
return maskedMessagePart
}
protected open fun maskSeparators(unmaskedMessagePart: String, separator: String, binaryDataRanges: List<IntRange>): String {
val separatorIndices = messageUtils.findSeparatorIndices(unmaskedMessagePart, separator, binaryDataRanges)
if (separatorIndices.isNotEmpty()) {
return messageUtils.maskCharacterAtIndices(unmaskedMessagePart, separator, separatorIndices)
}
return unmaskedMessagePart
}
}

View File

@ -25,7 +25,7 @@ abstract class TextDatenelement(val value: String?, existenzstatus: Existenzstat
}
protected open fun formatValue(value: String): String {
return maskMessagePart(value, Separators.DataElementsSeparator) // may overwritten in sub classes
return maskMessagePart(value, Separators.AllSeparators) // may overwritten in sub classes
}

View File

@ -1,6 +1,8 @@
package net.dankito.fints.messages.datenelemente.basisformate
import net.dankito.fints.messages.Existenzstatus
import net.dankito.fints.messages.datenelementgruppen.implementierte.signatur.SicherheitsidentifikationDetails
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
@ -59,6 +61,80 @@ class TextDatenelementTest {
}
@Test
fun maskDataElementsSeparator() {
// given
val customerSystemIdWithDataElementsSeparator = "f8Clj:x3BG4BAACI/459llkXrAQA"
val expected = "1::" + customerSystemIdWithDataElementsSeparator.replace(":", "?:")
val underTest = SicherheitsidentifikationDetails(customerSystemIdWithDataElementsSeparator)
// when
val result = underTest.format()
// then
assertThat(result).isEqualTo(expected)
}
@Test
fun maskDataElementGroupsSeparator() {
// given
val customerSystemIdWithDataElementGroupsSeparator = "f8Clj+x3BG4BAACI/459llkXrAQA"
val expected = "1::" + customerSystemIdWithDataElementGroupsSeparator.replace("+", "?+")
val underTest = SicherheitsidentifikationDetails(customerSystemIdWithDataElementGroupsSeparator)
// when
val result = underTest.format()
// then
assertThat(result).isEqualTo(expected)
}
@Test
fun maskSegmentsSeparator() {
// given
val customerSystemIdWithSegmentsSeparator = "f8Clj\'x3BG4BAACI/459llkXrAQA"
val expected = "1::" + customerSystemIdWithSegmentsSeparator.replace("'", "?'")
val underTest = SicherheitsidentifikationDetails(customerSystemIdWithSegmentsSeparator)
// when
val result = underTest.format()
// then
assertThat(result).isEqualTo(expected)
}
@Test
fun maskMaskingCharacter() {
// given
val customerSystemIdWithMaskingCharacter = "f8Clj?x3BG4BAACI/459llkXrAQA"
val expected = "1::" + customerSystemIdWithMaskingCharacter.replace("?", "??")
val underTest = SicherheitsidentifikationDetails(customerSystemIdWithMaskingCharacter)
// when
val result = underTest.format()
// then
assertThat(result).isEqualTo(expected)
}
private fun createTextDatenelement(text: String): TextDatenelement {
return object : TextDatenelement(text, Existenzstatus.Mandatory) { }
}