diff --git a/src/main/Config.kt b/src/main/Config.kt new file mode 100644 index 0000000..04cc871 --- /dev/null +++ b/src/main/Config.kt @@ -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 = loadNamespaces(Environment.namespaceFile) + } +} + +fun loadNamespaces(filename: String?): Map { + if (filename == null) { + return mapOf() + } + + val namespaceFile = File(filename) + val jsonString = namespaceFile.readText() + return Json.parseToJsonElement(jsonString).jsonObject.mapValues { item -> item.value.toString().replace("\"", "") } +} diff --git a/src/main/ExternalTripleStore.kt b/src/main/ExternalTripleStore.kt index 72577fa..5ab556f 100644 --- a/src/main/ExternalTripleStore.kt +++ b/src/main/ExternalTripleStore.kt @@ -10,8 +10,11 @@ data class ParsedSparqlQuery( fun parseQuery(sparqlQueryResource: String): ParsedSparqlQuery { val catalog = QueryCatalog(sparqlQueryResource) + val namespaces: MutableMap = mutableMapOf() + namespaces.putAll(Config.namespaces) + namespaces.putAll(extractPrefixes(catalog)) return ParsedSparqlQuery( - extractPrefixes(catalog), + namespaces, extractPredicates(catalog), ) } diff --git a/src/test/TestConfig.kt b/src/test/TestConfig.kt new file mode 100644 index 0000000..31f4ed0 --- /dev/null +++ b/src/test/TestConfig.kt @@ -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()) + } +}