Tests.kt 6.33 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.settings.SettingsLoader
Jonas Waeber's avatar
Jonas Waeber committed
33
34
import java.io.File
import java.nio.charset.Charset
Jonas Waeber's avatar
Jonas Waeber committed
35
import java.util.stream.Stream
Jonas Waeber's avatar
Jonas Waeber committed
36
37
38
39
40

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

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

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

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

    @ParameterizedTest
    @MethodSource("kafkaTests")
    fun `test kafka topology`(params: KafkaTestParams) {
Jonas Waeber's avatar
Jonas Waeber committed
91
92
93
94
95
96
        val settings = SettingsLoader(
            listOf(
                "institutionId",
                "recordSetId",
                "configs"
            ),
Jonas Waeber's avatar
Jonas Waeber committed
97
            fileName = "kafkaTest${params.count}.yml",
Jonas Waeber's avatar
Jonas Waeber committed
98
99
100
101
102
103
104
105
            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
106
                settings.inputTopic, params.key, readFile("kafkaTests/${params.count}/input.json")
Jonas Waeber's avatar
Jonas Waeber committed
107
108
109
            )
        )

Jonas Waeber's avatar
Jonas Waeber committed
110
111
112


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

Jonas Waeber's avatar
Jonas Waeber committed
118
119
120
121
122
123
124
125
126
        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
127

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

Jonas Waeber's avatar
Jonas Waeber committed
132
133
134
135
136
137
138
139
140
141
            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
142
    }
Jonas Waeber's avatar
Jonas Waeber committed
143

Jonas Waeber's avatar
Jonas Waeber committed
144
145
146
147
148
    private fun kafkaTests() = Stream.of(
        KafkaTestParams(
            1,
            "MEI_49884",
            listOf("https://memobase.ch/record/BAZ-MEI_49884",
149
                "https://memobase.ch/instantiation/physical/BAZ-MEI_49884-0")
Jonas Waeber's avatar
Jonas Waeber committed
150
151
152
153
154
155
156
        ),
        KafkaTestParams(
            2,
            "Sig Han 1293",
            listOf(
                "https://memobase.ch/record/TEST-Sig_Han_1293",
                "https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0")
157
158
159
160
161
162
163
164
165
        ),
        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"
            )
Jonas Waeber's avatar
Jonas Waeber committed
166
167
        )
    )
Jonas Waeber's avatar
Jonas Waeber committed
168
169
170
171
172
173
174
175
176
177

    @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
178
179
        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
180
        Pair(" oisadi  ", "oisadi"),
Jonas Waeber's avatar
Jonas Waeber committed
181
        Pair("Test      mit vielen  spaces   . jal", "Test_mit_vielen_spaces_jal")
Jonas Waeber's avatar
Jonas Waeber committed
182
    )
Jonas Waeber's avatar
Jonas Waeber committed
183
}