Tests.kt 5.32 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
import org.memobase.builder.ResourceBuilder
Jonas Waeber's avatar
Jonas Waeber committed
34
import org.memobase.helpers.StringHelpers
Jonas Waeber's avatar
Jonas Waeber committed
35
import org.memobase.mapping.MappingConfig
Jonas Waeber's avatar
Jonas Waeber committed
36
import org.memobase.settings.SettingsLoader
Jonas Waeber's avatar
Jonas Waeber committed
37
import java.io.File
Jonas Waeber's avatar
Jonas Waeber committed
38
import java.io.FileOutputStream
Jonas Waeber's avatar
Jonas Waeber committed
39
import java.nio.charset.Charset
Jonas Waeber's avatar
Jonas Waeber committed
40
41
import java.nio.file.Files
import java.nio.file.Paths
Jonas Waeber's avatar
Jonas Waeber committed
42
import java.util.stream.Stream
Jonas Waeber's avatar
Jonas Waeber committed
43
44
45
46
47

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

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

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

Jonas Waeber's avatar
Jonas Waeber committed
93
    private val regex = Regex("(_:B[A-Za-z0-9]+)")
Jonas Waeber's avatar
Jonas Waeber committed
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
    @Test
    fun `test kafka topology`() {
        val settings = SettingsLoader(
            listOf(
                "institutionId",
                "recordSetId",
                "configs"
            ),
            fileName = "kafkaTest1.yml",
            useStreamsConfig = true
        )
        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()
        )

Jonas Waeber's avatar
Jonas Waeber committed
121
122
123
124
125
126
127
        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
129
130
131
132

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

Jonas Waeber's avatar
Jonas Waeber committed
133
134
135
136


        assertThat(sortedResult)
            .isEqualTo(readFile("kafkaTests/1/output.nt"))
Jonas Waeber's avatar
Jonas Waeber committed
137

Jonas Waeber's avatar
Jonas Waeber committed
138
139

    }
Jonas Waeber's avatar
Jonas Waeber committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155


    @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(
        Pair("J2.143#1996/386#414-3#1*", "J2.143-1996-386-414-3-1*"),
        Pair(" IB Becker Audiovisuals / 2", "IB_Becker_Audiovisuals-2"),
        Pair(" oisadi  ", "oisadi"),
        Pair("Test      mit vielen  spaces   . jal", "Test_mit_vielen_spaces_._jal")
    )
Jonas Waeber's avatar
Jonas Waeber committed
156
}