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 ...@@ -50,7 +50,7 @@ import org.apache.logging.log4j.LogManager
import org.memobase.rdf.SKOS import org.memobase.rdf.SKOS
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
class MappingConfigurationParser(directory: String) { class MappingConfigurationParser(data: ByteArray) {
private var uriField: String = "" private var uriField: String = ""
private var localRecordType: SimpleAnnotationField? = null private var localRecordType: SimpleAnnotationField? = null
private val recordType: SimpleAnnotationField private val recordType: SimpleAnnotationField
...@@ -62,7 +62,7 @@ class MappingConfigurationParser(directory: String) { ...@@ -62,7 +62,7 @@ class MappingConfigurationParser(directory: String) {
private val log = LogManager.getLogger("MappingConfigParser") private val log = LogManager.getLogger("MappingConfigParser")
init { init {
val loader = YamlLoader(directory) val loader = YamlLoader(data)
try { try {
for (item in loader.load()) { for (item in loader.load()) {
when (item) { when (item) {
......
...@@ -19,9 +19,7 @@ ...@@ -19,9 +19,7 @@
package ch.memobase.mapping package ch.memobase.mapping
import ch.memobase.mapping.exceptions.InvalidMappingException import ch.memobase.mapping.exceptions.InvalidMappingException
import java.nio.charset.Charset import java.io.ByteArrayInputStream
import java.nio.file.Files
import java.nio.file.Paths
import java.util.Optional import java.util.Optional
import org.memobase.settings.CustomEnvConfig import org.memobase.settings.CustomEnvConfig
import org.memobase.settings.MissingSettingException import org.memobase.settings.MissingSettingException
...@@ -30,22 +28,17 @@ import org.snakeyaml.engine.v2.api.LoadSettings ...@@ -30,22 +28,17 @@ import org.snakeyaml.engine.v2.api.LoadSettings
import org.snakeyaml.engine.v2.exceptions.DuplicateKeyException import org.snakeyaml.engine.v2.exceptions.DuplicateKeyException
import org.snakeyaml.engine.v2.exceptions.MissingEnvironmentVariableException import org.snakeyaml.engine.v2.exceptions.MissingEnvironmentVariableException
class YamlLoader(private val directory: String) { class YamlLoader(private val data: ByteArray) {
private fun readFiles(): String {
return Files.list(Paths.get(directory)).map { file -> file.toFile().readText(Charset.defaultCharset()) }
.reduce("") { s, s2 -> s + "\n" + s2 }.trim()
}
fun load(): Iterable<Any> { fun load(): Iterable<Any> {
val settings = val settings =
LoadSettings.builder().setEnvConfig(Optional.of(CustomEnvConfig())).build() LoadSettings.builder().setEnvConfig(Optional.of(CustomEnvConfig())).build()
val load = Load(settings) val load = Load(settings)
return try { return try {
val data = readFiles()
if (data.isEmpty()) { if (data.isEmpty()) {
throw InvalidMappingException("The mapping file may not be empty!") 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) { } catch (ex: MissingEnvironmentVariableException) {
throw MissingSettingException("env", ex.localizedMessage) throw MissingSettingException("env", ex.localizedMessage)
} catch (ex: ClassCastException) { } catch (ex: ClassCastException) {
......
import ch.memobase.mapping.MappingConfigurationParser import ch.memobase.mapping.MappingConfigurationParser
import ch.memobase.mapping.fields.FieldParsers import ch.memobase.mapping.fields.FieldParsers
import ch.memobase.mapping.mappers.RicoConceptMapper import ch.memobase.mapping.mappers.RicoConceptMapper
import java.io.File
import java.io.FileInputStream
import mapping.MapperConfiguration import mapping.MapperConfiguration
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Disabled
...@@ -12,7 +14,7 @@ class TestMappingParser { ...@@ -12,7 +14,7 @@ class TestMappingParser {
@Test @Test
fun `test minimal mapping parser`() { 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()) assertThat(mapping.get())
.isEqualTo( .isEqualTo(
MapperConfiguration( MapperConfiguration(
...@@ -28,7 +30,7 @@ class TestMappingParser { ...@@ -28,7 +30,7 @@ class TestMappingParser {
@Test @Test
fun `test sponsor field parser`() { 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()) assertThat(mapping.get())
.isEqualTo( .isEqualTo(
MapperConfiguration( MapperConfiguration(
...@@ -46,7 +48,7 @@ class TestMappingParser { ...@@ -46,7 +48,7 @@ class TestMappingParser {
@Disabled @Disabled
fun `test record parser`() { fun `test record parser`() {
// TODO: Find an easy way to compare the field mappers. // 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()) assertThat(mapping.get())
.isEqualTo( .isEqualTo(
MapperConfiguration( 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