Skip to content

Commit

Permalink
Added support for custom separators.
Browse files Browse the repository at this point in the history
Fixes #17.
  • Loading branch information
morazow committed Oct 6, 2021
1 parent cd920ca commit 9202d4b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/main/scala/com/exasol/common/PropertiesParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final case class PropertiesParser(private val propertySeparator: String, private
}
string
.split(propertySeparator)
.map(stringToTuple)
.map(splitStringToTuple)
.toMap
}

Expand All @@ -41,14 +41,14 @@ final case class PropertiesParser(private val propertySeparator: String, private
.map { case (k, v) => s"$k$keyValueAssignment$v" }
.mkString(propertySeparator)

private[this] def stringToTuple(string: String): Tuple2[String, String] = {
private[this] def splitStringToTuple(string: String): Tuple2[String, String] = {
val idx = string.indexOf(keyValueAssignment)
if (idx < 0) {
throw new IllegalArgumentException(
s"Properties input string does not contain key value assignment '$keyValueAssignment'."
)
}
stripAndReplace(string.substring(0, idx)) -> stripAndReplace(string.substring(idx + 1))
stripAndReplace(string.substring(0, idx)) -> stripAndReplace(string.substring(idx + keyValueAssignment.length()))
}

private[this] def stripAndReplace(string: String): String =
Expand Down
35 changes: 28 additions & 7 deletions src/test/scala/com/exasol/common/AbstractPropertiesTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,39 @@ class AbstractPropertiesTest extends AnyFunSuite with BeforeAndAfterEach with Mo

test("parseConnectionInfo returns key value pairs from password") {
properties = Map("CONNECTION_NAME" -> "connection_info")
val metadata = mockMetadata("", "a=secret1;b=secret2")
val result = BaseProperties(properties).parseConnectionInfo("username", Option(metadata))
assert(result === Map("a" -> "secret1", "b" -> "secret2"))
verify(metadata, times(1)).getConnection("connection_info")
val expected = Map("a" -> "secret1", "b" -> "secret2")
assertParseConnectionInfo("", "a=secret1;b=secret2", properties, expected)
}

test("parseConnectionInfo returns key value pairs with updated username") {
properties = Map("CONNECTION_NAME" -> "connection_info")
val metadata = mockMetadata("John", "a=secret1;b=secret2")
val result = BaseProperties(properties).parseConnectionInfo("username", Option(metadata))
assert(result === Map("username" -> "John", "a" -> "secret1", "b" -> "secret2"))
val expected = Map("userKey" -> "John", "a" -> "secret1", "b" -> "secret2")
assertParseConnectionInfo("John", "a=secret1;b=secret2", properties, expected)
}

test("parseConnectionInfo returns key value pairs with custom separators") {
properties = Map("CONNECTION_NAME" -> "connection_info", "CONNECTION_SEPARATOR" -> "#")
val expected = Map("k1" -> "v1", "k2" -> "v2", "k3" -> "v3;k4=v4")
assertParseConnectionInfo("", "k1=v1#k2=v2#k3=v3;k4=v4", properties, expected)
}

test("parseConnectionInfo returns key value pairs with custom key-value assignment") {
properties = Map("CONNECTION_NAME" -> "connection_info", "CONNECTION_KEYVALUE_ASSIGNMENT" -> "@@")
val expected = Map("k1" -> "v1", "k2" -> "v2", "k3" -> "v3", "k4" -> "v4")
assertParseConnectionInfo("", "k1@@v1;k2@@v2;k3@@v3;k4@@v4", properties, expected)
}

private[this] def assertParseConnectionInfo(
user: String,
password: String,
props: Map[String, String],
expected: Map[String, String]
): Unit = {
val metadata = mockMetadata(user, password)
val result = BaseProperties(properties).parseConnectionInfo("userKey", Option(metadata))
assert(result === expected)
verify(metadata, times(1)).getConnection("connection_info")
()
}

test("parseConnectionInfo throws if password does not contain key value pairs") {
Expand Down

0 comments on commit 9202d4b

Please sign in to comment.