Tests.kt 9.16 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
/*
Jonas Waeber's avatar
Jonas Waeber committed
2
 * sftp-reader
Jonas Waeber's avatar
Jonas Waeber committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 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/>.
 */
18
package org.memobase
Jonas Waeber's avatar
Jonas Waeber committed
19

Jonas Waeber's avatar
Jonas Waeber committed
20 21
import com.beust.klaxon.Klaxon
import java.io.FileInputStream
Jonas Waeber's avatar
Jonas Waeber committed
22 23 24
import java.time.Duration
import java.util.Properties
import java.util.stream.Stream
Jonas Waeber's avatar
Jonas Waeber committed
25 26
import org.apache.kafka.clients.admin.AdminClient
import org.apache.kafka.clients.admin.AdminClientConfig
Jonas Waeber's avatar
Jonas Waeber committed
27
import org.apache.kafka.clients.consumer.ConsumerConfig
Jonas Waeber's avatar
Jonas Waeber committed
28
import org.apache.kafka.clients.consumer.ConsumerRecord
Jonas Waeber's avatar
Jonas Waeber committed
29 30
import org.apache.kafka.clients.consumer.KafkaConsumer
import org.apache.kafka.common.serialization.StringDeserializer
Jonas Waeber's avatar
Jonas Waeber committed
31
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
32
import org.assertj.core.api.Assertions.assertThat
Jonas Waeber's avatar
Jonas Waeber committed
33
import org.junit.jupiter.api.TestInstance
34
import org.junit.jupiter.api.extension.ExtendWith
Jonas Waeber's avatar
Jonas Waeber committed
35 36 37 38
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import org.memobase.extensions.EmbeddedKafkaExtension
import org.memobase.extensions.EmbeddedSftpServer
Jonas Waeber's avatar
Jonas Waeber committed
39

Jonas Waeber's avatar
Jonas Waeber committed
40
@ExtendWith(EmbeddedKafkaExtension::class)
Jonas Waeber's avatar
Jonas Waeber committed
41
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Jonas Waeber's avatar
Jonas Waeber committed
42
class Tests {
Jonas Waeber's avatar
Jonas Waeber committed
43
    private val log = LogManager.getLogger("LocalTestsLogger")
Jonas Waeber's avatar
Jonas Waeber committed
44

Jonas Waeber's avatar
Jonas Waeber committed
45 46
    private val sftpServer = EmbeddedSftpServer(22000, "user", "password")

Jonas Waeber's avatar
Jonas Waeber committed
47
    private val adminClient =
48
        AdminClient.create(mapOf(Pair(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:12345")))
Jonas Waeber's avatar
Jonas Waeber committed
49

Jonas Waeber's avatar
Jonas Waeber committed
50 51
    init {
        sftpServer.createDirectories(
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
            "/memobase/test_institution_1/test_record_set_1/",
            "/memobase/test_institution_2/test_record_set_2/"
        )
        sftpServer.putFile(
            "/memobase/test_institution_1/test_record_set_1/brandt.csv",
            FileInputStream("src/test/resources/data/brandt.csv")
        )
        sftpServer.putFile(
            "/memobase/test_institution_2/test_record_set_2/bauGAZH_metadaten.csv",
            FileInputStream("src/test/resources/data/bauGAZH_metadaten.csv")
        )
        sftpServer.putFile(
            "/memobase/test_institution_3/test_record_set_3/invalid.csv",
            FileInputStream("src/test/resources/data/invalid.csv")
        )
        sftpServer.putFile(
            "/memobase/test_institution_4/test_record_set_4/file.txt",
            FileInputStream("src/test/resources/data/file.txt")
        )
        sftpServer.putFile(
72
            "/memobase/test_institution_5/test_record_set_5/file.xlsx",
73
            FileInputStream("src/test/resources/data/20190906_Brandt_Metadaten.xlsx")
Jonas Waeber's avatar
Jonas Waeber committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
        )
    }

    private val consumer: KafkaConsumer<String, String>

    init {
        val props = Properties()
        props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:12345")
        props.setProperty(ConsumerConfig.CLIENT_ID_CONFIG, "test-group-1")
        props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "test-group-1")
        props.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer::class.qualifiedName)
        props.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer::class.qualifiedName)
        props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest")
        consumer = KafkaConsumer(props)
Jonas Waeber's avatar
Jonas Waeber committed
88
        consumer.subscribe(listOf("sftp-reader-p1-j1", "sftp-reader-p1-j1-reporting", "p1-reporting"))
Jonas Waeber's avatar
Jonas Waeber committed
89 90 91 92 93
    }

    @ParameterizedTest
    @MethodSource("directoryReaderTests")
    fun `read valid csv from sftp`(params: TestParams) {
94 95
        val service = Service(params.configFile)
        service.run()
Jonas Waeber's avatar
Jonas Waeber committed
96

97 98 99
        val topic = service.settings.outputTopic
        val reportingTopic = service.settings.outputTopic + "-reporting"
        val processReportingTopic = service.settings.processReportTopic
Jonas Waeber's avatar
Jonas Waeber committed
100

Jonas Waeber's avatar
Jonas Waeber committed
101 102
        val totalConsumerRecords = mutableListOf<ConsumerRecord<String, String>>()

Jonas Waeber's avatar
Jonas Waeber committed
103
        var result = consumer.poll(Duration.ofMillis(10))
Jonas Waeber's avatar
Jonas Waeber committed
104 105 106 107 108
        while (totalConsumerRecords.size != 3) {
            if (result.count() > 0) {
                totalConsumerRecords.addAll(result.asIterable())
            }
            log.error(result.count())
Jonas Waeber's avatar
Jonas Waeber committed
109 110 111
            result = consumer.poll(Duration.ofMillis(10))
        }

Jonas Waeber's avatar
Jonas Waeber committed
112
        assertThat(totalConsumerRecords.find { value -> value.topic() == topic })
113 114 115
            .describedAs("Message Test")
            .hasFieldOrPropertyWithValue("key", params.expectedKey)
            .hasFieldOrPropertyWithValue("value", params.expectedValue)
Jonas Waeber's avatar
Jonas Waeber committed
116

Jonas Waeber's avatar
Jonas Waeber committed
117
        assertThat(totalConsumerRecords.find { value -> value.topic() == reportingTopic })
118 119 120
            .describedAs("Report Test")
            .hasFieldOrPropertyWithValue("key", params.expectedKey)
            .hasFieldOrPropertyWithValue("value", params.expectedReportValue)
Jonas Waeber's avatar
Jonas Waeber committed
121

Jonas Waeber's avatar
Jonas Waeber committed
122
        assertThat(totalConsumerRecords.find { value -> value.topic() == processReportingTopic })
123 124 125
            .describedAs("Process Report Test")
            .hasFieldOrPropertyWithValue("key", params.expectedProcessReport.id)
            .hasFieldOrPropertyWithValue("value", Klaxon().toJsonString(params.expectedProcessReport))
Jonas Waeber's avatar
Jonas Waeber committed
126
    }
Jonas Waeber's avatar
Jonas Waeber committed
127 128

    private fun directoryReaderTests() = Stream.of(
129 130 131 132 133 134 135 136 137 138
        TestParams(
            "test1.yml",
            expectedKey = "brandt.csv",
            expectedValue = "{\"format\" : \"CSV\", \"path\" : \"/memobase/test_institution_1/test_record_set_1/brandt.csv\"}",
            expectedReportValue = Klaxon().toJsonString(
                Report(
                    id = "brandt.csv",
                    status = "SUCCESS",
                    message = "Validated file at path /memobase/test_institution_1/test_record_set_1/brandt.csv with format CSV."
                )
Jonas Waeber's avatar
Jonas Waeber committed
139
            ),
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
            expectedProcessReport = Report(
                id = "jobXYZ",
                status = "SUCCESS",
                message = "Successfully validated 1 files."
            )
        ),
        TestParams(
            "test2.yml",
            expectedKey = "bauGAZH_metadaten.csv",
            expectedValue = "{\"format\" : \"CSV\", \"path\" : \"/memobase/test_institution_2/test_record_set_2/bauGAZH_metadaten.csv\"}",
            expectedReportValue = Klaxon().toJsonString(
                Report(
                    id = "bauGAZH_metadaten.csv",
                    status = "SUCCESS",
                    message = "Validated file at path /memobase/test_institution_2/test_record_set_2/bauGAZH_metadaten.csv with format CSV."
155 156
                )
            ),
157 158 159 160 161 162 163 164 165 166 167 168 169
            expectedProcessReport = Report(
                id = "jobXYZ",
                status = "SUCCESS",
                message = "Successfully validated 1 files."
            )
        ),
        TestParams(
            "test3.yml",
            expectedKey = "invalid.csv",
            expectedValue = "{\"format\" : \"ERROR\", \"path\" : \"/memobase/test_institution_3/test_record_set_3/invalid.csv\"}",
            expectedReportValue = Klaxon().toJsonString(
                Report(
                    id = "invalid.csv",
170
                    status = "FAILURE",
171
                    message = "CSV ERROR: Fields num seems to be 5 on each row, but on 2th csv row, fields num is 7. for file /memobase/test_institution_3/test_record_set_3/invalid.csv."
172 173
                )
            ),
174 175 176 177 178 179 180 181 182 183 184 185 186
            expectedProcessReport = Report(
                id = "jobXYZ",
                status = "FAILURE",
                message = "Failed to validate 1 of 1 files."
            )
        ),
        TestParams(
            "test4.yml",
            expectedKey = "file.txt",
            expectedValue = "{\"format\" : \"ERROR\", \"path\" : \"/memobase/test_institution_4/test_record_set_4/file.txt\"}",
            expectedReportValue = Klaxon().toJsonString(
                Report(
                    id = "file.txt",
187
                    status = "FAILURE",
188
                    message = "File Extension Error: Not a valid file extension: file.txt."
189
                )
190 191 192 193 194
            ),
            expectedProcessReport = Report(
                id = "jobXYZ",
                status = "FAILURE",
                message = "Failed to validate 1 of 1 files."
Jonas Waeber's avatar
Jonas Waeber committed
195
            )
196 197 198
        ),
        TestParams(
            "test5.yml",
199 200
            expectedKey = "file.xlsx",
            expectedValue = "{\"format\" : \"XLSX\", \"path\" : \"/memobase/test_institution_5/test_record_set_5/file.xlsx\"}",
201 202
            expectedReportValue = Klaxon().toJsonString(
                Report(
203
                    id = "file.xlsx",
204
                    status = "SUCCESS",
205
                    message = "Validated file at path /memobase/test_institution_5/test_record_set_5/file.xlsx with format XLSX."
206 207 208 209 210 211 212 213
                )
            ),
            expectedProcessReport = Report(
                id = "jobXYZ",
                status = "SUCCESS",
                message = "Successfully validated 1 files."
            )
        )
Jonas Waeber's avatar
Jonas Waeber committed
214
    )
Jonas Waeber's avatar
Jonas Waeber committed
215
}