-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Add functionality for reading cusom namespaces from a file
- Loading branch information
1 parent
9e36552
commit e4de93f
Showing
3 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.github.statnett.loadflowservice | ||
|
||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.jsonObject | ||
import java.io.File | ||
|
||
private val logger = KotlinLogging.logger {} | ||
|
||
class Environment { | ||
companion object { | ||
var namespaceFile: String? = System.getenv("NAMESPACE_FILE") | ||
} | ||
} | ||
|
||
class Config { | ||
companion object { | ||
var namespaces: Map<String, String> = loadNamespaces(Environment.namespaceFile) | ||
} | ||
} | ||
|
||
fun loadNamespaces(filename: String?): Map<String, String> { | ||
if (filename == null) { | ||
return mapOf() | ||
} | ||
|
||
val namespaceFile = File(filename) | ||
val jsonString = namespaceFile.readText() | ||
return Json.parseToJsonElement(jsonString).jsonObject.mapValues { item -> item.value.toString().replace("\"", "") } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import com.github.statnett.loadflowservice.loadNamespaces | ||
import org.junit.jupiter.api.Test | ||
import java.io.File | ||
import kotlin.test.assertEquals | ||
|
||
class TestConfig { | ||
@Test | ||
fun `test load namespaces from file`() { | ||
val filename = "temp_namespaces" | ||
val jsonString = "{\"cim\":\"c\",\"md\":\"m\"}" | ||
|
||
val file = File.createTempFile(filename, "json") | ||
file.deleteOnExit() | ||
file.writeText(jsonString) | ||
|
||
val result = loadNamespaces(file.path) | ||
|
||
assertEquals(mapOf("cim" to "c", "md" to "m"), result) | ||
} | ||
|
||
@Test | ||
fun `test load namespaces null`() { | ||
assertEquals(loadNamespaces(null), mapOf()) | ||
} | ||
} |