Skip to content

Commit

Permalink
ENH: Add functionality for reading cusom namespaces from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkleiven committed Mar 9, 2024
1 parent 9e36552 commit e4de93f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/main/Config.kt
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("\"", "") }
}
5 changes: 4 additions & 1 deletion src/main/ExternalTripleStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ data class ParsedSparqlQuery(
fun parseQuery(sparqlQueryResource: String): ParsedSparqlQuery {
val catalog = QueryCatalog(sparqlQueryResource)

val namespaces: MutableMap<String, String> = mutableMapOf()
namespaces.putAll(Config.namespaces)
namespaces.putAll(extractPrefixes(catalog))
return ParsedSparqlQuery(
extractPrefixes(catalog),
namespaces,
extractPredicates(catalog),
)
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/TestConfig.kt
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())
}
}

0 comments on commit e4de93f

Please sign in to comment.