Test.kt 6.72 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
/*
 * search-doc-service
 * Copyright (C) 2020 Memoriav
 *
 * 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/>.
 */
Jonas Waeber's avatar
Jonas Waeber committed
18
package org.memobase
Jonas Waeber's avatar
Jonas Waeber committed
19
20
21
22
23
24
25
26
27
28
29
30
31

import java.io.File
import java.nio.charset.Charset
import java.util.stream.Stream
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
import org.apache.logging.log4j.LogManager
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
Jonas Waeber's avatar
Jonas Waeber committed
32
import org.memobase.helpers.DateFacetBuilder
Jonas Waeber's avatar
Jonas Waeber committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

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

    private val resourcePath = "src/test/resources/data"
    private fun readFile(fileName: String): String {
        return File("$resourcePath/$fileName").readText(Charset.defaultCharset())
    }

    @ParameterizedTest
    @MethodSource("testDates")
    fun `test date facet`(date: TestDate) {
        val result = when (date.type) {
            "single" ->
                DateFacetBuilder.buildFromNormalizedSingleDate(date.date)
            "range" ->
                DateFacetBuilder.buildFromNormalizedDateRange(date.date)
            else ->
                emptyList()
        }

        assertThat(result)
            .isEqualTo(date.result)
    }

    private fun testDates() = Stream.of(
        TestDate(
            "1921-09-14",
            "single",
            listOf("0~20.Jahrhundert~", "1~20.Jahrhundert~1921-1930#")
        ),
        TestDate(
            "1921",
            "range",
            listOf("0~20.Jahrhundert~", "1~20.Jahrhundert~1921-1930#")
        ),
        TestDate(
            "1921/1922",
            "range",
            listOf("0~20.Jahrhundert~", "1~20.Jahrhundert~1921-1930#")
        ),
        TestDate(
            "1921-05-01/02",
            "range",
            listOf("0~20.Jahrhundert~", "1~20.Jahrhundert~1921-1930#")
        ),
        TestDate(
            "1921-05-01/06-02",
            "range",
            listOf("0~20.Jahrhundert~", "1~20.Jahrhundert~1921-1930#")
        ),
        TestDate(
            "1921/1931",
            "range",
            listOf("0~20.Jahrhundert~", "1~20.Jahrhundert~1921-1930#", "1~20.Jahrhundert~1931-1940#")
        ),
        TestDate(
            "1921-04-01/1931-05-02",
            "range",
            listOf("0~20.Jahrhundert~", "1~20.Jahrhundert~1921-1930#", "1~20.Jahrhundert~1931-1940#")
        ),
        TestDate(
            "1921/1951",
            "range",
            listOf(
                "0~20.Jahrhundert~",
                "1~20.Jahrhundert~1921-1930#",
                "1~20.Jahrhundert~1931-1940#",
                "1~20.Jahrhundert~1941-1950#",
                "1~20.Jahrhundert~1951-1960#"
            )
        ),
        TestDate(
            "1721/1951",
            "range",
            listOf(
                "0~18.Jahrhundert~",
                "0~19.Jahrhundert~",
                "0~20.Jahrhundert~",
                "1~18.Jahrhundert~1721-1730#",
                "1~18.Jahrhundert~1731-1740#",
                "1~18.Jahrhundert~1741-1750#",
                "1~18.Jahrhundert~1751-1760#",
                "1~18.Jahrhundert~1761-1770#",
                "1~18.Jahrhundert~1771-1780#",
                "1~18.Jahrhundert~1781-1790#",
                "1~18.Jahrhundert~1791-1800#",
                "1~19.Jahrhundert~1801-1810#",
                "1~19.Jahrhundert~1811-1820#",
                "1~19.Jahrhundert~1821-1830#",
                "1~19.Jahrhundert~1831-1840#",
                "1~19.Jahrhundert~1841-1850#",
                "1~19.Jahrhundert~1851-1860#",
                "1~19.Jahrhundert~1861-1870#",
                "1~19.Jahrhundert~1871-1880#",
                "1~19.Jahrhundert~1881-1890#",
                "1~19.Jahrhundert~1891-1900#",
                "1~20.Jahrhundert~1901-1910#",
                "1~20.Jahrhundert~1911-1920#",
                "1~20.Jahrhundert~1921-1930#",
                "1~20.Jahrhundert~1931-1940#",
                "1~20.Jahrhundert~1941-1950#",
                "1~20.Jahrhundert~1951-1960#"
            )
        ),
        TestDate(
            "1721/1822",
            "range",
            listOf(
                "0~18.Jahrhundert~",
                "0~19.Jahrhundert~",
                "1~18.Jahrhundert~1721-1730#",
                "1~18.Jahrhundert~1731-1740#",
                "1~18.Jahrhundert~1741-1750#",
                "1~18.Jahrhundert~1751-1760#",
                "1~18.Jahrhundert~1761-1770#",
                "1~18.Jahrhundert~1771-1780#",
                "1~18.Jahrhundert~1781-1790#",
                "1~18.Jahrhundert~1791-1800#",
                "1~19.Jahrhundert~1801-1810#",
                "1~19.Jahrhundert~1811-1820#",
                "1~19.Jahrhundert~1821-1830#"
            )
        )
    )

    @ParameterizedTest
    @MethodSource("testParams")
    fun `integration tests`(params: TestParam) {
        val service = Service(params.settingsFileName)
        val testDriver = TopologyTestDriver(service.topology, service.settings.kafkaStreamsSettings)
        val factory = ConsumerRecordFactory(
            StringSerializer(), StringSerializer()
        )
        testDriver.pipeInput(
            factory.create(
                service.settings.inputTopic, params.inputKey, readFile("${params.count}/input.json")
            )
        )

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

        assertThat(record.value())
            .isEqualTo(readFile("${params.count}/output.json"))
    }

    private fun testParams() = Stream.of(
        TestParam(
            "test-1",
            1,
            "kafkaTest1.yml",
            "key"
Jonas Waeber's avatar
Jonas Waeber committed
190
        ),
Jonas Waeber's avatar
Jonas Waeber committed
191
192
193
194
195
        TestParam(
            "test-2",
            2,
            "kafkaTest1.yml",
            "key"
Jonas Waeber's avatar
Jonas Waeber committed
196
        )/*,
197
198
199
200
201
        TestParam(
            "test-3",
            3,
            "kafkaTest1.yml",
            "key"
202
        )*/
Jonas Waeber's avatar
Jonas Waeber committed
203
204
    )
}