Due to a scheduled upgrade to version 14.10, GitLab will be unavailabe on Monday 30.05., from 19:00 until 20:00.

TestPipeline.kt 6.96 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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package org.memobase

import ch.memobase.reporting.Report
import ch.memobase.testing.EmbeddedSftpServer
import com.beust.klaxon.Klaxon
import org.apache.kafka.common.header.internals.RecordHeader
import org.apache.kafka.common.header.internals.RecordHeaders
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.kafka.common.serialization.StringSerializer
import org.apache.kafka.streams.TestOutputTopic
import org.apache.kafka.streams.TopologyTestDriver
import org.apache.kafka.streams.test.TestRecord
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.assertAll
import java.io.File
import java.nio.charset.Charset

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestPipeline {
    private val resourcePath = "src/test/resources/data"
    private fun readFile(fileName: String, count: Int): String {
        return File("$resourcePath/$count/$fileName").readText(Charset.defaultCharset())
    }
    private val klaxon = Klaxon()

    private val regex = Regex("(_:B[A-Za-z0-9]+)")
    private val regexTime = Regex("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}")

    private fun sort(source: String): String {
        return source.lines().map {
            var replacedString = it
            for (matchResult in regex.findAll(it)) {
                replacedString = replacedString.replace(matchResult.groups[0]?.value.orEmpty(), "_:B")
            }
            for (matchResult in regexTime.findAll(it)) {
                replacedString = replacedString.replace(matchResult.groups[0]?.value.orEmpty(), "2020-10-10T09:10:22")
            }
            replacedString
        }.sorted().reduce { acc, s -> acc + "\n" + s }.trim()
    }

    private fun setupTest(
        count: Int,
        inputKey: String
    ): Pair<TestOutputTopic<String, String>, TestOutputTopic<String, String>> {
48
        val service = Service("data/$count/app.yml")
Jonas Waeber's avatar
Jonas Waeber committed
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
        val testDriver =
            TopologyTestDriver(KafkaTopology(service.settings).prepare().build(), service.settings.kafkaStreamsSettings)
        val inputValue = readFile("input.json", count)
        val headers = RecordHeaders()
        headers.add(RecordHeader("sessionId", "test-session-id".toByteArray()))
        headers.add(RecordHeader("recordSetId", "rst-001".toByteArray()))
        headers.add(RecordHeader("institutionId", "ins".toByteArray()))
        val inputTopic =
            testDriver.createInputTopic(service.settings.inputTopic, StringSerializer(), StringSerializer())
        val inputRecord = TestRecord<String, String>(inputKey, inputValue, headers)
        inputTopic.pipeInput(inputRecord)

        val configTopic = testDriver.createInputTopic(
            service.settings.appSettings.getProperty("configTopic"),
            StringSerializer(),
            StringSerializer()
        )
        configTopic.pipeInput("rst-001#mapping", readFile("mapping.yml", count))

        val outputTopic =
            testDriver.createOutputTopic(service.settings.outputTopic, StringDeserializer(), StringDeserializer())
        val outputReportTopic =
            testDriver.createOutputTopic(
                service.settings.processReportTopic,
                StringDeserializer(),
                StringDeserializer()
            )
        return Pair(outputTopic, outputReportTopic)
    }


    private fun parseReport(data: String): Report {
        return klaxon.parse<Report>(data)!!
    }


    @Test
    fun `test 1 minimal configuration`() {
87
        val topics = setupTest(1, "test-1")
88
89
90
91
92
93
        while (topics.first.isEmpty) {

        }
        assertThat(topics.second.isEmpty)
            .withFailMessage("Reports topic is empty.")
            .isFalse()
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

        val record = topics.first.readRecord()
        val recordValue = record.value
        val recordKey = record.key

        assertAll(
            {
                assertThat(sort(recordValue))
                    .isEqualTo(sort(readFile("output.nt", 1)))
            },
            {
                assertThat(recordKey)
                    .isEqualTo("https://memobase.ch/record/rst-001-1")
            }

        )
    }

    @Test
    fun `test 2 minimal configuration with digital and physical object`() {
114
        val topics = setupTest(2, "test-2")
115
116
117
118
119
120
        while (topics.first.isEmpty) {

        }
        assertThat(topics.second.isEmpty)
            .withFailMessage("Reports topic is empty.")
            .isFalse()
Jonas Waeber's avatar
Jonas Waeber committed
121

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
        val record = topics.first.readRecord()
        val recordValue = record.value
        val recordKey = record.key

        assertAll(
            {
                assertThat(sort(recordValue))
                    .isEqualTo(sort(readFile("output.nt", 2)))
            },
            {
                assertThat(recordKey)
                    .isEqualTo("https://memobase.ch/record/rst-001-1")
            }

        )
    }

    @Test
    fun `test 3 test list input`() {
        val topics = setupTest(3, "test-3")
142
        while (topics.first.isEmpty) {
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
        assertThat(topics.second.isEmpty)
            .withFailMessage("Reports topic is empty.")
            .isFalse()

        val report = topics.second.readRecord()
        val reportKey = report.key
        val reportValue = report.value


        assertAll( "Assert reports.",
            {
                assertThat(reportKey)
                    .isEqualTo("https://memobase.ch/record/rst-001-22861")
            },
            {
                assertThat(parseReport(reportValue))
                    .isEqualTo(Report("https://memobase.ch/record/rst-001-22861", "SUCCESS", "Generated RDF document.", "test"))
            }
        )

        assertThat(topics.first.isEmpty)
            .withFailMessage("Record Topic is empty")
            .isFalse()
        val record = topics.first.readRecord()
        val recordValue = record.value
        val recordKey = record.key

        assertAll(
            {
                assertThat(sort(recordValue))
                    .isEqualTo(sort(readFile("output.nt", 3)))
            },
            {
                assertThat(recordKey)
                    .isEqualTo("https://memobase.ch/record/rst-001-22861")
            }

        )
    }

    @Test
    fun `test 4 if proxy field in mapping works`() {
        val topics = setupTest(4, "test-4")

189
190
191
192
193
194
195
        while (topics.first.isEmpty) {

        }
        assertThat(topics.second.isEmpty)
            .withFailMessage("Reports topic is empty.")
            .isFalse()

196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
        val report = topics.second.readRecord()
        val reportKey = report.key
        val reportValue = report.value

        val record = topics.first.readRecord()
        val recordValue = record.value
        val recordKey = record.key

        assertAll(
            {
                assertThat(sort(recordValue))
                    .isEqualTo(sort(readFile("output.nt", 4)))
            },
            {
                assertThat(recordKey)
                    .isEqualTo("https://memobase.ch/record/rst-001-1")
            }

        )
Jonas Waeber's avatar
Jonas Waeber committed
215
216
    }
}