TestMappingParser.kt 2.36 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
package ch.memobase.test

Jonas Waeber's avatar
Jonas Waeber committed
3
4
import ch.memobase.mapping.MappingConfigurationParser
import ch.memobase.mapping.fields.FieldParsers
Jonas Waeber's avatar
Jonas Waeber committed
5
import com.beust.klaxon.Klaxon
6
7
import java.io.File
import java.io.FileInputStream
Jonas Waeber's avatar
Jonas Waeber committed
8
import java.nio.charset.Charset
Jonas Waeber's avatar
Jonas Waeber committed
9
10
11
12
13
14
15
16
import mapping.MapperConfiguration
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestMappingParser {

Jonas Waeber's avatar
Jonas Waeber committed
17
18
19
20
21
22
    private fun readFile(fileName: String): String {
        return FileInputStream("src/test/resources/output/$fileName.json").readBytes().toString(Charset.defaultCharset())
    }

    private val klaxon = Klaxon()

Jonas Waeber's avatar
Jonas Waeber committed
23
24
    @Test
    fun `test minimal mapping parser`() {
Jonas Waeber's avatar
Jonas Waeber committed
25
        val mapping = MappingConfigurationParser(FileInputStream(File("src/test/resources/mapping1.yml")).readBytes())
Jonas Waeber's avatar
Jonas Waeber committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        assertThat(mapping.get())
            .isEqualTo(
                MapperConfiguration(
                    "TestField",
                    false,
                    FieldParsers.parseSimpleAnnotationField("type", mapOf(Pair("const", "Foto"))),
                    emptyList(),
                    emptyList(),
                    emptyList()
                )
            )
    }

    @Test
    fun `test sponsor field parser`() {
Jonas Waeber's avatar
Jonas Waeber committed
41
        val mapping = MappingConfigurationParser(FileInputStream(File("src/test/resources/mapping2.yml")).readBytes())
Jonas Waeber's avatar
Jonas Waeber committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
        assertThat(mapping.get())
            .isEqualTo(
                MapperConfiguration(
                    "TestField",
                    true,
                    FieldParsers.parseSimpleAnnotationField("type", mapOf(Pair("const", "Foto"))),
                    emptyList(),
                    emptyList(),
                    emptyList()
                )
            )
    }

    @Test
    fun `test record parser`() {
Jonas Waeber's avatar
Jonas Waeber committed
57
58
59
60
61
62
63
64
65
66
67
68
        val mapping = MappingConfigurationParser(FileInputStream(File("src/test/resources/mapping3.yml")).readBytes())
        val string = klaxon.toJsonString(mapping.get())
        assertThat(string)
            .isEqualTo(readFile("output3"))
    }

    @Test
    fun `test skos field parser`() {
        val mapping = MappingConfigurationParser(FileInputStream(File("src/test/resources/mapping4.yml")).readBytes())
        val string = klaxon.toJsonString(mapping.get())
        assertThat(string)
            .isEqualTo(readFile("output4"))
Jonas Waeber's avatar
Jonas Waeber committed
69
70
    }
}