Commit dc795777 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Refactor yaml loader to accept byte array instead of directory.

parent 2f3e6e88
Pipeline #15777 passed with stage
in 2 minutes
......@@ -50,7 +50,7 @@ import org.apache.logging.log4j.LogManager
import org.memobase.rdf.SKOS
@Suppress("UNCHECKED_CAST")
class MappingConfigurationParser(directory: String) {
class MappingConfigurationParser(data: ByteArray) {
private var uriField: String = ""
private var localRecordType: SimpleAnnotationField? = null
private val recordType: SimpleAnnotationField
......@@ -62,7 +62,7 @@ class MappingConfigurationParser(directory: String) {
private val log = LogManager.getLogger("MappingConfigParser")
init {
val loader = YamlLoader(directory)
val loader = YamlLoader(data)
try {
for (item in loader.load()) {
when (item) {
......
......@@ -19,9 +19,7 @@
package ch.memobase.mapping
import ch.memobase.mapping.exceptions.InvalidMappingException
import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.Paths
import java.io.ByteArrayInputStream
import java.util.Optional
import org.memobase.settings.CustomEnvConfig
import org.memobase.settings.MissingSettingException
......@@ -30,22 +28,17 @@ import org.snakeyaml.engine.v2.api.LoadSettings
import org.snakeyaml.engine.v2.exceptions.DuplicateKeyException
import org.snakeyaml.engine.v2.exceptions.MissingEnvironmentVariableException
class YamlLoader(private val directory: String) {
private fun readFiles(): String {
return Files.list(Paths.get(directory)).map { file -> file.toFile().readText(Charset.defaultCharset()) }
.reduce("") { s, s2 -> s + "\n" + s2 }.trim()
}
class YamlLoader(private val data: ByteArray) {
fun load(): Iterable<Any> {
val settings =
LoadSettings.builder().setEnvConfig(Optional.of(CustomEnvConfig())).build()
val load = Load(settings)
return try {
val data = readFiles()
if (data.isEmpty()) {
throw InvalidMappingException("The mapping file may not be empty!")
}
load.loadAllFromString(data) as Iterable<Any>
load.loadAllFromInputStream(ByteArrayInputStream(data)) as Iterable<Any>
} catch (ex: MissingEnvironmentVariableException) {
throw MissingSettingException("env", ex.localizedMessage)
} catch (ex: ClassCastException) {
......
import ch.memobase.mapping.MappingConfigurationParser
import ch.memobase.mapping.fields.FieldParsers
import ch.memobase.mapping.mappers.RicoConceptMapper
import java.io.File
import java.io.FileInputStream
import mapping.MapperConfiguration
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Disabled
......@@ -12,7 +14,7 @@ class TestMappingParser {
@Test
fun `test minimal mapping parser`() {
val mapping = MappingConfigurationParser("src/test/resources/mappings1")
val mapping = MappingConfigurationParser(FileInputStream(File("src/test/resources/mappings1/test_mapping.yml")).readBytes())
assertThat(mapping.get())
.isEqualTo(
MapperConfiguration(
......@@ -28,7 +30,7 @@ class TestMappingParser {
@Test
fun `test sponsor field parser`() {
val mapping = MappingConfigurationParser("src/test/resources/mappings2")
val mapping = MappingConfigurationParser(FileInputStream(File("src/test/resources/mappings2/test_mapping.yml")).readBytes())
assertThat(mapping.get())
.isEqualTo(
MapperConfiguration(
......@@ -46,7 +48,7 @@ class TestMappingParser {
@Disabled
fun `test record parser`() {
// TODO: Find an easy way to compare the field mappers.
val mapping = MappingConfigurationParser("src/test/resources/mappings3")
val mapping = MappingConfigurationParser(FileInputStream(File("src/test/resources/mappings3/test_mapping.yml")).readBytes())
assertThat(mapping.get())
.isEqualTo(
MapperConfiguration(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment