TestIntegration.kt 5.15 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
/*
 * xml-data-transform
 * 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/>.
 */

package org.memobase

21
import com.beust.klaxon.Klaxon
Jonas Waeber's avatar
Jonas Waeber committed
22
23
24
import org.apache.kafka.common.header.internals.RecordHeader
import org.apache.kafka.common.header.internals.RecordHeaders
import org.apache.kafka.common.serialization.ByteArraySerializer
25
26
27
28
29
30
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
Jonas Waeber's avatar
Jonas Waeber committed
31
import org.junit.jupiter.api.TestInstance
32
33
34
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
35
36
import org.memobase.models.Report
import org.memobase.models.ReportStatus
37
38
39
40
41
import org.memobase.testing.EmbeddedSftpServer
import java.io.File
import java.io.FileInputStream
import java.nio.charset.Charset
import java.util.stream.Stream
Jonas Waeber's avatar
Jonas Waeber committed
42
43
44

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestIntegration {
45
    private val log = LogManager.getLogger("TestLogger")
Jonas Waeber's avatar
Jonas Waeber committed
46

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

52
53
54
55
56
57
58
59
60
61
    private val sftpServer = EmbeddedSftpServer(22000, "user", "password")

    @ParameterizedTest
    @MethodSource("integrationTestParams")
    fun `test kafka integration single record`(params: TestParams) {
        sftpServer.putFile(
            "/memobase/test${params.count}/data.xml",
            FileInputStream("$resourcePath/${params.count}/data.xml")
        )

Jonas Waeber's avatar
Jonas Waeber committed
62
63
64
65
66
67
        val headers = RecordHeaders()
        headers.add(RecordHeader("sessionId", "test-session-id".toByteArray()))
        headers.add(RecordHeader("recordSetId", "test-record-set-id".toByteArray()))
        headers.add(RecordHeader("institutionId", "test-institution-id".toByteArray()))
        headers.add(RecordHeader("isPublished", "false".toByteArray()))
        headers.add(RecordHeader("xmlRecordTag", "record".toByteArray()))
Jonas Waeber's avatar
Jonas Waeber committed
68
        headers.add(RecordHeader("xmlIdentifierFieldName", "identifierMain".toByteArray()))
Jonas Waeber's avatar
Jonas Waeber committed
69
70
71
72
        headers.add(RecordHeader("tableSheetIndex", "1".toByteArray()))
        headers.add(RecordHeader("tableHeaderCount", "1".toByteArray()))
        headers.add(RecordHeader("tableHeaderIndex", "1".toByteArray()))
        headers.add(RecordHeader("tableIdentifierIndex", "1".toByteArray()))
73
74
75

        val service = Service("test${params.count}.yml")
        val testDriver = TopologyTestDriver(service.topology, service.settings.kafkaStreamsSettings)
Jonas Waeber's avatar
Jonas Waeber committed
76
77

        val factoryConfig = ConsumerRecordFactory(
78
79
            StringSerializer(), StringSerializer()
        )
Jonas Waeber's avatar
Jonas Waeber committed
80

81
        testDriver.pipeInput(
Jonas Waeber's avatar
Jonas Waeber committed
82
83
84
85
            factoryConfig.create(
                "import-process-config",
                "test-record-set-id#transform",
                readFile("${params.count}/config/transform.xslt")
Jonas Waeber's avatar
Jonas Waeber committed
86
87
88
89
            )
        )


Jonas Waeber's avatar
Jonas Waeber committed
90
91
92
        val factory = ConsumerRecordFactory(
            StringSerializer(), StringSerializer()
        )
Jonas Waeber's avatar
Jonas Waeber committed
93
        testDriver.pipeInput(
Jonas Waeber's avatar
Jonas Waeber committed
94
95
96
            factory.create(
                service.settings.inputTopic, params.inputKey, readFile("${params.count}/input.json"),
                headers
97
98
99
            )
        )

Jonas Waeber's avatar
Jonas Waeber committed
100

101
102
103
104
105
106
107
108
109
110
111
        val record = testDriver.readOutput(
            service.settings.outputTopic,
            StringDeserializer(),
            StringDeserializer()
        )

        val reportedRecord = testDriver.readOutput(
            service.settings.processReportTopic,
            StringDeserializer(),
            StringDeserializer()
        )
Jonas Waeber's avatar
Jonas Waeber committed
112
113
114
115

        val key = record.key()
        val data = record.value()
        val report = Klaxon().parse<Report>(reportedRecord.value())
116
117
118

        assertAll("check single record transform",
            {
Jonas Waeber's avatar
Jonas Waeber committed
119
120
                assertThat(key)
                    .isEqualTo(params.outputKey)
121
122
            },
            {
Jonas Waeber's avatar
Jonas Waeber committed
123
124
                assertThat(data)
                    .isEqualTo(readFile("${params.count}/output.json"))
125
126
            },
            {
Jonas Waeber's avatar
Jonas Waeber committed
127
128
                assertThat(report)
                    .isEqualTo(params.expectedOutputReport)
129
130
131
132
133
134
135
136
137
138
139
140
141
            }

        )

    }


    fun integrationTestParams() = Stream.of(
        TestParams(
            1,
            "simple test",
            "data.xml",
            "ADG-102683",
Jonas Waeber's avatar
Jonas Waeber committed
142
            Report("ADG-102683", ReportStatus.success, "Successfully transformed xml to json!")
143
144
        )
    )
Jonas Waeber's avatar
Jonas Waeber committed
145
146

}