Tests.kt 6.66 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
21
22
23
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.kafka.common.serialization.StringSerializer
import org.apache.kafka.streams.TopologyTestDriver
import org.apache.kafka.streams.test.ConsumerRecordFactory
Jonas Waeber's avatar
Jonas Waeber committed
24
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
25
import org.assertj.core.api.Assertions.assertThat
Jonas Waeber's avatar
Jonas Waeber committed
26
import org.junit.jupiter.api.Test
Jonas Waeber's avatar
Jonas Waeber committed
27
import org.junit.jupiter.api.TestInstance
Jonas Waeber's avatar
Jonas Waeber committed
28
29
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
Jonas Waeber's avatar
Jonas Waeber committed
30
import org.memobase.helpers.StringHelpers
Jonas Waeber's avatar
Jonas Waeber committed
31
import org.memobase.mapping.MappingConfig
Jonas Waeber's avatar
Jonas Waeber committed
32
import org.memobase.mapping.fields.ConstantField
Jonas Waeber's avatar
Jonas Waeber committed
33
import org.memobase.settings.SettingsLoader
Jonas Waeber's avatar
Jonas Waeber committed
34
35
import java.io.File
import java.nio.charset.Charset
Jonas Waeber's avatar
Jonas Waeber committed
36
import java.util.stream.Stream
Jonas Waeber's avatar
Jonas Waeber committed
37
38
39
40
41

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

Jonas Waeber's avatar
Jonas Waeber committed
42
    private val resourcePath = "src/test/resources"
Jonas Waeber's avatar
Jonas Waeber committed
43
44
45
    private fun readFile(fileName: String): String {
        return File("$resourcePath/$fileName").readText(Charset.defaultCharset())
    }
Jonas Waeber's avatar
Jonas Waeber committed
46

Jonas Waeber's avatar
Jonas Waeber committed
47
48
49
50
51
    @Test
    fun `test mapping config validation`() {
        val config = MappingConfig(configTestBasePath + "minimalValid")
        assertThat(config).isNotNull
            .hasFieldOrPropertyWithValue("uriField", "TestField")
Jonas Waeber's avatar
Jonas Waeber committed
52
            .hasFieldOrPropertyWithValue("recordType", ConstantField("type", "Foto"))
Jonas Waeber's avatar
Jonas Waeber committed
53
54
55
56
        
    }

    private val configTestBasePath = "src/test/resources/configTests/"
Jonas Waeber's avatar
Jonas Waeber committed
57
    /*
Jonas Waeber's avatar
Jonas Waeber committed
58
    @Test
Jonas Waeber's avatar
Jonas Waeber committed
59
    fun `json-ld data export for record sets`() {
Jonas Waeber's avatar
Jonas Waeber committed
60
61
        val config =
            MappingConfig("src/test/resources/multifileconfig")
Jonas Waeber's avatar
Jonas Waeber committed
62
63
64
65
66
67
        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,
68
                config.recordType,
Jonas Waeber's avatar
Jonas Waeber committed
69
70
71
72
73
74
75
76
77
78
79
80
                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
81
                    it.write(s.second)
Jonas Waeber's avatar
Jonas Waeber committed
82
83
84
                }
            }
        }
Jonas Waeber's avatar
Jonas Waeber committed
85
    }*/
Jonas Waeber's avatar
Jonas Waeber committed
86

Jonas Waeber's avatar
Jonas Waeber committed
87
    private val regex = Regex("(_:B[A-Za-z0-9]+)")
Jonas Waeber's avatar
Jonas Waeber committed
88
89
90
91

    @ParameterizedTest
    @MethodSource("kafkaTests")
    fun `test kafka topology`(params: KafkaTestParams) {
Jonas Waeber's avatar
Jonas Waeber committed
92
93
94
95
96
97
        val settings = SettingsLoader(
            listOf(
                "institutionId",
                "recordSetId",
                "configs"
            ),
Jonas Waeber's avatar
Jonas Waeber committed
98
            fileName = "kafkaTest${params.count}.yml",
Jonas Waeber's avatar
Jonas Waeber committed
99
100
101
102
103
104
105
106
            useStreamsConfig = true
        )
        val testDriver = TopologyTestDriver(KafkaTopology(settings).build(), settings.kafkaStreamsSettings)
        val factory = ConsumerRecordFactory(
            StringSerializer(), StringSerializer()
        )
        testDriver.pipeInput(
            factory.create(
Jonas Waeber's avatar
Jonas Waeber committed
107
                settings.inputTopic, params.key, readFile("kafkaTests/${params.count}/input.json")
Jonas Waeber's avatar
Jonas Waeber committed
108
109
110
            )
        )

Jonas Waeber's avatar
Jonas Waeber committed
111
112
113


        var record = testDriver.readOutput(
Jonas Waeber's avatar
Jonas Waeber committed
114
115
116
117
118
            settings.outputTopic,
            StringDeserializer(),
            StringDeserializer()
        )

Jonas Waeber's avatar
Jonas Waeber committed
119
120
121
122
123
124
125
126
127
        var recordCount = 1
        while (record != null) {
            val sortedResult = record.value().lines().map {
                var replacedString = it
                for (matchResult in regex.findAll(it)) {
                    replacedString = replacedString.replace(matchResult.groups[0]?.value.orEmpty(), "_:B")
                }
                replacedString
            }.sorted().reduce { acc, s -> acc + "\n" + s  }
Jonas Waeber's avatar
Jonas Waeber committed
128

Jonas Waeber's avatar
Jonas Waeber committed
129
130
131
            assertThat(record)
                .isNotNull
                .hasFieldOrPropertyWithValue("key", params.expectedKeys[recordCount - 1])
Jonas Waeber's avatar
Jonas Waeber committed
132

Jonas Waeber's avatar
Jonas Waeber committed
133
134
135
136
137
138
139
140
141
142
            assertThat(sortedResult)
                .isEqualTo(readFile("kafkaTests/${params.count}/output$recordCount.nt"))

            record = testDriver.readOutput(
                settings.outputTopic,
                StringDeserializer(),
                StringDeserializer()
            )
            recordCount += 1
        }
Jonas Waeber's avatar
Jonas Waeber committed
143
    }
Jonas Waeber's avatar
Jonas Waeber committed
144

Jonas Waeber's avatar
Jonas Waeber committed
145
146
147
148
149
    private fun kafkaTests() = Stream.of(
        KafkaTestParams(
            1,
            "MEI_49884",
            listOf("https://memobase.ch/record/BAZ-MEI_49884",
150
                "https://memobase.ch/instantiation/physical/BAZ-MEI_49884-0")
Jonas Waeber's avatar
Jonas Waeber committed
151
152
153
154
155
156
157
        ),
        KafkaTestParams(
            2,
            "Sig Han 1293",
            listOf(
                "https://memobase.ch/record/TEST-Sig_Han_1293",
                "https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0")
158
159
160
161
162
163
164
165
166
        ),
        KafkaTestParams(
            3,
            "Sig Han 1293",
            listOf(
                "https://memobase.ch/record/TEST-Sig_Han_1293",
                "https://memobase.ch/instantiation/physical/TEST-Sig_Han_1293-0",
                "https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-1"
            )
167
168
169
170
171
172
173
174
        ),
        KafkaTestParams(
            4,
            "Sig Han 1293",
            listOf(
                "https://memobase.ch/record/TEST-sigantur-example",
                "https://memobase.ch/instantiation/physical/TEST-sigantur-example-0"
            )
Jonas Waeber's avatar
Jonas Waeber committed
175
176
        )
    )
Jonas Waeber's avatar
Jonas Waeber committed
177
178
179
180
181
182
183
184
185
186

    @ParameterizedTest
    @MethodSource("idExamples")
    fun `test id normalization`(params: Pair<String, String>) {
        val result = StringHelpers.normalizeId(params.first)
        assertThat(result).isEqualTo(params.second)
    }


    private fun idExamples() = Stream.of(
Jonas Waeber's avatar
Jonas Waeber committed
187
188
        Pair("J2.143#1996/386#414-3#1*", "J2_143_1996_386_414-3_1*"),
        Pair(" IB Becker Audiovisuals-2", "IB_Becker_Audiovisuals-2"),
Jonas Waeber's avatar
Jonas Waeber committed
189
        Pair(" oisadi  ", "oisadi"),
Jonas Waeber's avatar
Jonas Waeber committed
190
        Pair("Test      mit vielen  spaces   . jal", "Test_mit_vielen_spaces_jal")
Jonas Waeber's avatar
Jonas Waeber committed
191
    )
Jonas Waeber's avatar
Jonas Waeber committed
192
}