Tests.kt 10.2 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

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

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

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

48
49
    private fun reportingTopic(value: String) = "$value-reporting"

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

Jonas Waeber's avatar
Jonas Waeber committed
57
58
59
60
    }

    private val configTestBasePath = "src/test/resources/configTests/"

Jonas Waeber's avatar
Jonas Waeber committed
61
    private val regex = Regex("(_:B[A-Za-z0-9]+)")
Jonas Waeber's avatar
Jonas Waeber committed
62
63
64
65

    @ParameterizedTest
    @MethodSource("kafkaTests")
    fun `test kafka topology`(params: KafkaTestParams) {
Jonas Waeber's avatar
Jonas Waeber committed
66
67
68
69
70
71
        val settings = SettingsLoader(
            listOf(
                "institutionId",
                "recordSetId",
                "configs"
            ),
Jonas Waeber's avatar
Jonas Waeber committed
72
            fileName = "kafkaTest${params.count}.yml",
Jonas Waeber's avatar
Jonas Waeber committed
73
74
75
76
77
78
79
80
            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
81
                settings.inputTopic, params.key, readFile("kafkaTests/${params.count}/input.json")
Jonas Waeber's avatar
Jonas Waeber committed
82
83
84
            )
        )

Jonas Waeber's avatar
Jonas Waeber committed
85
        var record = testDriver.readOutput(
Jonas Waeber's avatar
Jonas Waeber committed
86
87
88
89
90
            settings.outputTopic,
            StringDeserializer(),
            StringDeserializer()
        )

Jonas Waeber's avatar
Jonas Waeber committed
91
92
93
94
95
96
97
98
        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
Jonas Waeber's avatar
Jonas Waeber committed
99
            }.sorted().reduce { acc, s -> acc + "\n" + s }
Jonas Waeber's avatar
Jonas Waeber committed
100

Jonas Waeber's avatar
Jonas Waeber committed
101
102
103
            assertThat(record)
                .isNotNull
                .hasFieldOrPropertyWithValue("key", params.expectedKeys[recordCount - 1])
Jonas Waeber's avatar
Jonas Waeber committed
104

Jonas Waeber's avatar
Jonas Waeber committed
105
106
107
            assertThat(sortedResult)
                .isEqualTo(readFile("kafkaTests/${params.count}/output$recordCount.nt"))

108
109
110
111
112
113
114
115
116
            val recordReport =
                testDriver.readOutput(
                    reportingTopic(settings.outputTopic),
                    StringDeserializer(),
                    StringDeserializer()
                )
            assertThat(Klaxon().parse<Report>(recordReport.value()))
                .isEqualTo(params.reports[recordCount - 1])

Jonas Waeber's avatar
Jonas Waeber committed
117
118
119
120
121
122
123
            record = testDriver.readOutput(
                settings.outputTopic,
                StringDeserializer(),
                StringDeserializer()
            )
            recordCount += 1
        }
Jonas Waeber's avatar
Jonas Waeber committed
124
    }
Jonas Waeber's avatar
Jonas Waeber committed
125

Jonas Waeber's avatar
Jonas Waeber committed
126
127
128
129
    private fun kafkaTests() = Stream.of(
        KafkaTestParams(
            1,
            "MEI_49884",
Jonas Waeber's avatar
Jonas Waeber committed
130
            listOf(
131
132
133
134
135
136
137
138
139
                "https://memobase.ch/record/BAZ-MEI_49884",
                "https://memobase.ch/instantiation/physical/BAZ-MEI_49884-0"
            ),
            listOf(
                Report(
                    "https://memobase.ch/record/BAZ-MEI_49884",
                    "SUCCESS",
                    "Successfully created a record from source."
                ),
Jonas Waeber's avatar
Jonas Waeber committed
140
                Report(
141
142
143
                    "https://memobase.ch/instantiation/physical/BAZ-MEI_49884-0",
                    "SUCCESS",
                    "Successfully created a physical instantiation from source."
Jonas Waeber's avatar
Jonas Waeber committed
144
145
                )
            )
Jonas Waeber's avatar
Jonas Waeber committed
146
147
148
149
150
151
        ),
        KafkaTestParams(
            2,
            "Sig Han 1293",
            listOf(
                "https://memobase.ch/record/TEST-Sig_Han_1293",
Jonas Waeber's avatar
Jonas Waeber committed
152
153
                "https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0"
            ),
154
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
155
156
157
158
159
160
161
162
163
164
                Report(
                    "https://memobase.ch/record/TEST-Sig_Han_1293",
                    "SUCCESS",
                    "Successfully created a record from source."
                ),
                Report(
                    "https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0",
                    "SUCCESS",
                    "Successfully created a digital instantiation from source."
                )
165
            )
166
167
168
169
170
171
172
173
        ),
        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"
174
175
            ),
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
                Report(
                    "https://memobase.ch/record/TEST-Sig_Han_1293",
                    "SUCCESS",
                    "Successfully created a record from source."
                ),
                Report(
                    "https://memobase.ch/instantiation/physical/TEST-Sig_Han_1293-0",
                    "SUCCESS",
                    "Successfully created a physical instantiation from source."
                ),
                Report(
                    "https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-1",
                    "SUCCESS",
                    "Successfully created a digital instantiation from source."
                )
191
            )
192
193
194
195
196
197
198
        ),
        KafkaTestParams(
            4,
            "Sig Han 1293",
            listOf(
                "https://memobase.ch/record/TEST-sigantur-example",
                "https://memobase.ch/instantiation/physical/TEST-sigantur-example-0"
199
200
            ),
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
201
202
203
204
205
206
207
208
209
210
                Report(
                    "https://memobase.ch/record/TEST-sigantur-example",
                    "SUCCESS",
                    "Successfully created a record from source."
                ),
                Report(
                    "https://memobase.ch/instantiation/physical/TEST-sigantur-example-0",
                    "SUCCESS",
                    "Successfully created a physical instantiation from source."
                )
211
            )
212
213
214
215
216
217
218
219
220
221
222
223
224
225
        ),
        KafkaTestParams(
            5,
            "ID_1",
            listOf(
                "https://memobase.ch/record/TEST-ID_1"
            ),
            listOf(
                Report(
                    "https://memobase.ch/record/TEST-ID_1",
                    "SUCCESS",
                    "Successfully created a record from source."
                )
            )
Jonas Waeber's avatar
Jonas Waeber committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
        ),
        KafkaTestParams(
            6,
            "ID_1",
            listOf(
                "https://memobase.ch/record/TEST-ID_1"
            ),
            listOf(
                Report(
                    "https://memobase.ch/record/TEST-ID_1",
                    "SUCCESS",
                    "Successfully created a record from source."
                )
            )
Jonas Waeber's avatar
Jonas Waeber committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
        ),
        KafkaTestParams(
            7,
            "ID_1",
            listOf(
                "https://memobase.ch/record/TEST-ID_1"
            ),
            listOf(
                Report(
                    "https://memobase.ch/record/TEST-ID_1",
                    "SUCCESS",
                    "Successfully created a record from source."
                )
            )
254
255
256
257
258
259
260
261
262
263
264
265
266
267
        ),
        KafkaTestParams(
            8,
            "ID_1",
            listOf(
                "https://memobase.ch/record/TEST-ID_1"
            ),
            listOf(
                Report(
                    "https://memobase.ch/record/TEST-ID_1",
                    "SUCCESS",
                    "Successfully created a record from source."
                )
            )
Jonas Waeber's avatar
Jonas Waeber committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
        ),
        KafkaTestParams(
            9,
            "ID_1",
            listOf(
                "https://memobase.ch/record/TEST-ID_1"
            ),
            listOf(
                Report(
                    "https://memobase.ch/record/TEST-ID_1",
                    "SUCCESS",
                    "Successfully created a record from source."
                )
            )
Jonas Waeber's avatar
Jonas Waeber committed
282
283
        )
    )
Jonas Waeber's avatar
Jonas Waeber committed
284
285
286
287
288
289
290
291
292
293

    @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
294
295
        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
296
        Pair(" oisadi  ", "oisadi"),
Jonas Waeber's avatar
Jonas Waeber committed
297
        Pair("Test      mit vielen  spaces   . jal", "Test_mit_vielen_spaces_jal")
Jonas Waeber's avatar
Jonas Waeber committed
298
    )
Jonas Waeber's avatar
Jonas Waeber committed
299
}