Tests.kt 4.81 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * record-parser
 * Copyright (C) 2019  Memobase
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
package org.memobase

Jonas Waeber's avatar
Jonas Waeber committed
20
import com.beust.klaxon.Klaxon
Jonas Waeber's avatar
Jonas Waeber committed
21
22
23
24
25
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.kafka.common.serialization.StringSerializer
import org.apache.kafka.streams.KafkaStreams
import org.apache.kafka.streams.TopologyTestDriver
import org.apache.kafka.streams.test.ConsumerRecordFactory
Jonas Waeber's avatar
Jonas Waeber committed
26
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
27
import org.assertj.core.api.Assertions.assertThat
Jonas Waeber's avatar
Jonas Waeber committed
28
import org.junit.jupiter.api.Test
Jonas Waeber's avatar
Jonas Waeber committed
29
import org.junit.jupiter.api.TestInstance
Jonas Waeber's avatar
Jonas Waeber committed
30
31
32
import org.junit.jupiter.api.assertAll
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
Jonas Waeber's avatar
Jonas Waeber committed
33
34
import org.memobase.builder.ResourceBuilder
import org.memobase.mapping.MappingConfig
Jonas Waeber's avatar
Jonas Waeber committed
35
import org.memobase.settings.SettingsLoader
Jonas Waeber's avatar
Jonas Waeber committed
36
import java.io.File
Jonas Waeber's avatar
Jonas Waeber committed
37
import java.io.FileOutputStream
Jonas Waeber's avatar
Jonas Waeber committed
38
import java.nio.charset.Charset
Jonas Waeber's avatar
Jonas Waeber committed
39
40
import java.nio.file.Files
import java.nio.file.Paths
Jonas Waeber's avatar
Jonas Waeber committed
41
import java.util.stream.Stream
Jonas Waeber's avatar
Jonas Waeber committed
42
43
44
45
46

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class Tests {
    private val log = LogManager.getLogger("TestLogger")

Jonas Waeber's avatar
Jonas Waeber committed
47
    private val resourcePath = "src/test/resources"
Jonas Waeber's avatar
Jonas Waeber committed
48
49
50
    private fun readFile(fileName: String): String {
        return File("$resourcePath/$fileName").readText(Charset.defaultCharset())
    }
Jonas Waeber's avatar
Jonas Waeber committed
51

Jonas Waeber's avatar
Jonas Waeber committed
52
53
54
55
56
57
58
59
60
61
    @Test
    fun `test mapping config validation`() {
        val config = MappingConfig(configTestBasePath + "minimalValid")
        assertThat(config).isNotNull
            .hasFieldOrPropertyWithValue("uriField", "TestField")
            .hasFieldOrPropertyWithValue("recordType", "Foto")
        
    }

    private val configTestBasePath = "src/test/resources/configTests/"
62
   /*
Jonas Waeber's avatar
Jonas Waeber committed
63
64
    @Test
    fun `test mapping config parser`() {
Jonas Waeber's avatar
Jonas Waeber committed
65
66
        val config =
            MappingConfig("src/test/resources/multifileconfig")
Jonas Waeber's avatar
Jonas Waeber committed
67
68
69
70
71
72
        var count = 0
        Files.list(Paths.get("src/test/resources/data")).forEach { path ->
            val text = Files.newBufferedReader(path).readText()
            val values = Klaxon().parse<Map<String, String>>(text)
            val resourceBuilder = ResourceBuilder(
                config.uriField,
73
                config.recordType,
Jonas Waeber's avatar
Jonas Waeber committed
74
75
76
77
78
79
80
81
82
83
84
85
                config.recordFieldMappers,
                config.physicalObjectFieldMappers,
                values!!,
                "BAZ",
                "BAZ-B_MEI"
            )
            val string = resourceBuilder.write()


            string.forEach { s ->
                count += 1
                FileOutputStream("src/test/resources/output/data$count.json").bufferedWriter(Charset.defaultCharset()).use {
Jonas Waeber's avatar
Jonas Waeber committed
86
                    it.write(s.second)
Jonas Waeber's avatar
Jonas Waeber committed
87
88
89
                }
            }
        }
Jonas Waeber's avatar
Jonas Waeber committed
90
    }
91
    */
Jonas Waeber's avatar
Jonas Waeber committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

    @Test
    fun `test kafka topology`() {
        val settings = SettingsLoader(
            listOf(
                "institutionId",
                "recordSetId",
                "configs"
            ),
            fileName = "kafkaTest1.yml",
            useStreamsConfig = true
        )
        val topology = KafkaTopology(settings).build()
        val stream = KafkaStreams(topology, settings.kafkaStreamsSettings)

        val testDriver = TopologyTestDriver(KafkaTopology(settings).build(), settings.kafkaStreamsSettings)
        val factory = ConsumerRecordFactory(
            StringSerializer(), StringSerializer()
        )
        testDriver.pipeInput(
            factory.create(
                settings.inputTopic, "", readFile("kafkaTests/1/input.json")
            )
        )

        val record = testDriver.readOutput(
            settings.outputTopic,
            StringDeserializer(),
            StringDeserializer()
        )

        val sortedResult = record.value().lines().sorted().reduce { acc, s -> acc + "\n" + s  }

        assertThat(record)
            .isNotNull
            .hasFieldOrPropertyWithValue("key", "https://memobase.ch/record/BAZ-MEI_49884")

        // TODO: find a way to test output. the problem is, that the generated blank node ids are different each time.
        // one possibilty would be to use and sort a json document as there the keys are deterministic. maybe. or maybe not...
        //assertThat(sortedResult)
        //    .isEqualTo(readFile("kafkaTests/1/output.nt"))
Jonas Waeber's avatar
Jonas Waeber committed
133

Jonas Waeber's avatar
Jonas Waeber committed
134
135

    }
Jonas Waeber's avatar
Jonas Waeber committed
136
}