IntegrationTests.kt 9.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
/*
 * 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

19
import com.beust.klaxon.Klaxon
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.TestInstance
Jonas Waeber's avatar
Jonas Waeber committed
27
28
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
Jonas Waeber's avatar
Jonas Waeber committed
29
import org.memobase.params.IntegrationTestParams
Jonas Waeber's avatar
Jonas Waeber committed
30
import org.memobase.settings.SettingsLoader
Jonas Waeber's avatar
Jonas Waeber committed
31
32
import java.io.File
import java.nio.charset.Charset
Jonas Waeber's avatar
Jonas Waeber committed
33
import java.util.stream.Stream
Jonas Waeber's avatar
Jonas Waeber committed
34
35

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Jonas Waeber's avatar
Jonas Waeber committed
36
class IntegrationTests {
Jonas Waeber's avatar
Jonas Waeber committed
37
38
    private val log = LogManager.getLogger("TestLogger")

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

44
45
    private fun reportingTopic(value: String) = "$value-reporting"

Jonas Waeber's avatar
Jonas Waeber committed
46
    private val regex = Regex("(_:B[A-Za-z0-9]+)")
Jonas Waeber's avatar
Jonas Waeber committed
47
48
49

    @ParameterizedTest
    @MethodSource("kafkaTests")
Jonas Waeber's avatar
Jonas Waeber committed
50
    fun `test kafka topology`(params: IntegrationTestParams) {
51
52
        val service = Service("kafkaTest${params.count}.yml")
        val settings = service.settings
Jonas Waeber's avatar
Jonas Waeber committed
53
        val testDriver = TopologyTestDriver(KafkaTopology(settings).prepare().build(), settings.kafkaStreamsSettings)
Jonas Waeber's avatar
Jonas Waeber committed
54
55
56
57
58
        val factory = ConsumerRecordFactory(
            StringSerializer(), StringSerializer()
        )
        testDriver.pipeInput(
            factory.create(
Jonas Waeber's avatar
Jonas Waeber committed
59
                settings.inputTopic, params.key, readFile("kafkaTests/${params.count}/input.json")
Jonas Waeber's avatar
Jonas Waeber committed
60
61
62
            )
        )

Jonas Waeber's avatar
Jonas Waeber committed
63
        var record = testDriver.readOutput(
Jonas Waeber's avatar
Jonas Waeber committed
64
65
66
67
68
            settings.outputTopic,
            StringDeserializer(),
            StringDeserializer()
        )

Jonas Waeber's avatar
Jonas Waeber committed
69
70
71
72
73
74
75
76
        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
77
            }.sorted().reduce { acc, s -> acc + "\n" + s }
Jonas Waeber's avatar
Jonas Waeber committed
78

Jonas Waeber's avatar
Jonas Waeber committed
79
80
81
            assertThat(record)
                .isNotNull
                .hasFieldOrPropertyWithValue("key", params.expectedKeys[recordCount - 1])
Jonas Waeber's avatar
Jonas Waeber committed
82

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

86
87
88
89
90
91
92
93
94
            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
95
96
97
98
99
100
101
            record = testDriver.readOutput(
                settings.outputTopic,
                StringDeserializer(),
                StringDeserializer()
            )
            recordCount += 1
        }
Jonas Waeber's avatar
Jonas Waeber committed
102
    }
Jonas Waeber's avatar
Jonas Waeber committed
103

Jonas Waeber's avatar
Jonas Waeber committed
104
    private fun kafkaTests() = Stream.of(
Jonas Waeber's avatar
Jonas Waeber committed
105
        IntegrationTestParams(
Jonas Waeber's avatar
Jonas Waeber committed
106
107
            1,
            "MEI_49884",
Jonas Waeber's avatar
Jonas Waeber committed
108
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
109
110
                "https://memobase.ch/record/BAZ-B_MEI-MEI_49884",
                "https://memobase.ch/instantiation/physical/BAZ-MEI_49884-1"
111
112
113
            ),
            listOf(
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
114
                    "https://memobase.ch/record/BAZ-B_MEI-MEI_49884",
115
116
117
                    "SUCCESS",
                    "Successfully created a record from source."
                ),
Jonas Waeber's avatar
Jonas Waeber committed
118
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
119
                    "https://memobase.ch/record/BAZ-B_MEI-MEI_49884-1",
120
121
                    "SUCCESS",
                    "Successfully created a physical instantiation from source."
Jonas Waeber's avatar
Jonas Waeber committed
122
123
                )
            )
Jonas Waeber's avatar
Jonas Waeber committed
124
        ),
Jonas Waeber's avatar
Jonas Waeber committed
125
        IntegrationTestParams(
Jonas Waeber's avatar
Jonas Waeber committed
126
127
128
            2,
            "Sig Han 1293",
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
129
130
                "https://memobase.ch/record/TEST_RECORD_SET-Sig_Han_1293",
                "https://memobase.ch/digital/TEST_RECORD_SET-Sig_Han_1293-1"
Jonas Waeber's avatar
Jonas Waeber committed
131
            ),
132
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
133
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
134
                    "https://memobase.ch/record/TEST_RECORD_SET-Sig_Han_1293",
Jonas Waeber's avatar
Jonas Waeber committed
135
136
137
138
                    "SUCCESS",
                    "Successfully created a record from source."
                ),
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
139
                    "https://memobase.ch/digital/TEST_RECORD_SET-Sig_Han_1293-1",
Jonas Waeber's avatar
Jonas Waeber committed
140
141
142
                    "SUCCESS",
                    "Successfully created a digital instantiation from source."
                )
143
            )
144
        ),
Jonas Waeber's avatar
Jonas Waeber committed
145
        IntegrationTestParams(
146
147
148
            3,
            "Sig Han 1293",
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
149
150
151
                "https://memobase.ch/record/TEST_RECORD_SET-Sig_Han_1293",
                "https://memobase.ch/physical/TEST_RECORD_SET-Sig_Han_1293-1",
                "https://memobase.ch/digital/TEST_RECORD_SET-Sig_Han_1293-1"
152
153
            ),
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
154
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
155
                    "https://memobase.ch/record/TEST_RECORD_SET-Sig_Han_1293",
Jonas Waeber's avatar
Jonas Waeber committed
156
157
158
159
                    "SUCCESS",
                    "Successfully created a record from source."
                ),
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
160
                    "https://memobase.ch/physical/TEST_RECORD_SET-Sig_Han_1293-1",
Jonas Waeber's avatar
Jonas Waeber committed
161
162
163
164
                    "SUCCESS",
                    "Successfully created a physical instantiation from source."
                ),
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
165
                    "https://memobase.ch/digital/TEST_RECORD_SET-Sig_Han_1293-1",
Jonas Waeber's avatar
Jonas Waeber committed
166
167
168
                    "SUCCESS",
                    "Successfully created a digital instantiation from source."
                )
169
            )
170
        ),
Jonas Waeber's avatar
Jonas Waeber committed
171
        IntegrationTestParams(
172
173
174
            4,
            "Sig Han 1293",
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
175
176
                "https://memobase.ch/record/TEST_RECORD_SET-sigantur-example",
                "https://memobase.ch/physical/TEST_RECORD_SET-sigantur-example-1"
177
178
            ),
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
179
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
180
                    "https://memobase.ch/record/TEST_RECORD_SET-sigantur-example",
Jonas Waeber's avatar
Jonas Waeber committed
181
182
183
184
                    "SUCCESS",
                    "Successfully created a record from source."
                ),
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
185
                    "https://memobase.ch/physical/TEST_RECORD_SET-sigantur-example-1",
Jonas Waeber's avatar
Jonas Waeber committed
186
187
188
                    "SUCCESS",
                    "Successfully created a physical instantiation from source."
                )
189
            )
190
        ),
Jonas Waeber's avatar
Jonas Waeber committed
191
        IntegrationTestParams(
192
193
194
            5,
            "ID_1",
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
195
                "https://memobase.ch/record/TEST_RECORD_SET-ID_1"
196
197
198
            ),
            listOf(
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
199
                    "https://memobase.ch/record/TEST_RECORD_SET-ID_1",
200
201
202
203
                    "SUCCESS",
                    "Successfully created a record from source."
                )
            )
Jonas Waeber's avatar
Jonas Waeber committed
204
        ),
Jonas Waeber's avatar
Jonas Waeber committed
205
        IntegrationTestParams(
Jonas Waeber's avatar
Jonas Waeber committed
206
207
208
            6,
            "ID_1",
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
209
                "https://memobase.ch/record/TEST_RECORD_SET-ID_1"
Jonas Waeber's avatar
Jonas Waeber committed
210
211
212
            ),
            listOf(
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
213
                    "https://memobase.ch/record/TEST_RECORD_SET-ID_1",
Jonas Waeber's avatar
Jonas Waeber committed
214
215
216
217
                    "SUCCESS",
                    "Successfully created a record from source."
                )
            )
Jonas Waeber's avatar
Jonas Waeber committed
218
        ),
Jonas Waeber's avatar
Jonas Waeber committed
219
        IntegrationTestParams(
Jonas Waeber's avatar
Jonas Waeber committed
220
221
222
            7,
            "ID_1",
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
223
                "https://memobase.ch/record/TEST_RECORD_SET-ID_1"
Jonas Waeber's avatar
Jonas Waeber committed
224
225
226
            ),
            listOf(
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
227
                    "https://memobase.ch/record/TEST_RECORD_SET-ID_1",
Jonas Waeber's avatar
Jonas Waeber committed
228
229
230
231
                    "SUCCESS",
                    "Successfully created a record from source."
                )
            )
232
        ),
Jonas Waeber's avatar
Jonas Waeber committed
233
        IntegrationTestParams(
234
235
236
            8,
            "ID_1",
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
237
                "https://memobase.ch/record/TEST_RECORD_SET-ID_1"
238
239
240
            ),
            listOf(
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
241
                    "https://memobase.ch/record/TEST_RECORD_SET-ID_1",
242
243
244
245
                    "SUCCESS",
                    "Successfully created a record from source."
                )
            )
Jonas Waeber's avatar
Jonas Waeber committed
246
        ),
Jonas Waeber's avatar
Jonas Waeber committed
247
        IntegrationTestParams(
Jonas Waeber's avatar
Jonas Waeber committed
248
249
250
            9,
            "ID_1",
            listOf(
Jonas Waeber's avatar
Jonas Waeber committed
251
                "https://memobase.ch/record/TEST_RECORD_SET-ID_1"
Jonas Waeber's avatar
Jonas Waeber committed
252
253
254
            ),
            listOf(
                Report(
Jonas Waeber's avatar
Jonas Waeber committed
255
                    "https://memobase.ch/record/TEST_RECORD_SET-ID_1",
Jonas Waeber's avatar
Jonas Waeber committed
256
257
258
259
                    "SUCCESS",
                    "Successfully created a record from source."
                )
            )
Jonas Waeber's avatar
Jonas Waeber committed
260
261
        )
    )
Jonas Waeber's avatar
Jonas Waeber committed
262
}