IntegrationTest.kt 4.56 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/>.
 */
18
package ch.memobase
Jonas Waeber's avatar
Jonas Waeber committed
19

20
21
22
23
import ch.memobase.helpers.ElasticSearchWrapper
import ch.memobase.model.FacetContainer
import ch.memobase.model.LanguageContainer
import ch.memobase.params.TestParam
24
import io.mockk.every
25
import io.mockk.mockk
26
27
28
import java.io.File
import java.nio.charset.Charset
import java.util.stream.Stream
Jonas Waeber's avatar
Jonas Waeber committed
29
30
31
32
33
34
35
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
36
import org.junit.jupiter.api.assertAll
Jonas Waeber's avatar
Jonas Waeber committed
37
38
39
40
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
41
class IntegrationTest {
Jonas Waeber's avatar
Jonas Waeber committed
42
43
44
45
46
47
48
    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())
    }

49
50
51
    private val elasticSearchWrapperMocked = run {
        val internal = mockk<ElasticSearchWrapper>()
        every { internal.getRecordSetName("soz-004") } returns LanguageContainer.placeholder("soz-004")
Jonas Waeber's avatar
Jonas Waeber committed
52
53
54
        every { internal.getExtraInstitutionsFromRecordSet("soz-004", "accessInstitution") } returns emptyList()
        every { internal.getExtraInstitutionsFromRecordSet("soz-004", "originalInstitution") } returns emptyList()
        every { internal.getExtraInstitutionsFromRecordSet("soz-004", "masterInstitution") } returns emptyList()
Jonas Waeber's avatar
Jonas Waeber committed
55
        every { internal.getInstitutionNamesFromRecordSet("soz-004") } returns listOf(FacetContainer.placeholder("soz"))
56
57
58
        internal
    }

Jonas Waeber's avatar
Jonas Waeber committed
59
60
61
    @ParameterizedTest
    @MethodSource("testParams")
    fun `integration tests`(params: TestParam) {
62
63
        val settings = App.createSettings("kafkaTest1.yml")

64
65
        every { elasticSearchWrapperMocked.countTotalNumberOfDocuments("") } returns 100
        every { elasticSearchWrapperMocked.countNumberOfDocumentsPublished("") } returns 90
66
        every { elasticSearchWrapperMocked.getDocumentTypesFromRecords("", "") } returns listOf(
67
68
69
70
            FacetContainer(
                LanguageContainer(listOf("Foto"), listOf("Foto"), listOf("Foto"), emptyList()), "Foto", emptyList()
            )
        )
71
72
73


        val topology =
74
            KafkaTopology(settings, TestUtilities.translationMappers, elasticSearchWrapperMocked)
75
        val testDriver = TopologyTestDriver(topology.build(), settings.kafkaStreamsSettings)
Jonas Waeber's avatar
Jonas Waeber committed
76
        val factory = ConsumerRecordFactory(
77
            StringSerializer(), StringSerializer()
Jonas Waeber's avatar
Jonas Waeber committed
78
79
        )
        testDriver.pipeInput(
80
81
82
            factory.create(
                settings.inputTopic, params.inputKey, readFile("${params.count}/input.json")
            )
Jonas Waeber's avatar
Jonas Waeber committed
83
84
85
        )

        val record = testDriver.readOutput(
86
87
88
            settings.outputTopic,
            StringDeserializer(),
            StringDeserializer()
Jonas Waeber's avatar
Jonas Waeber committed
89
90
        )

91
92
93
94
95
96
97
        assertAll("",
            {
                assertThat(record.value())
                    .isEqualTo(readFile("${params.count}/output.json"))
            },
            { assertThat(record.key()).isEqualTo(params.outputKey) }
        )
Jonas Waeber's avatar
Jonas Waeber committed
98
99
100
    }

    private fun testParams() = Stream.of(
101
102
103
104
        TestParam(
            "test-1",
            1,
            "kafkaTest1.yml",
105
106
            "https://memobase.ch/record/fad-001-DON3196",
            "fad-001-DON3196"
107
108
109
110
111
        ),
        TestParam(
            "test-2",
            2,
            "kafkaTest1.yml",
112
            "Burgerbib-Krebser-208576",
113
114
115
116
117
118
            "Burgerbib-Krebser-208576"
        ),
        TestParam(
            "test-3",
            3,
            "kafkaTest1.yml",
119
            "LS-film-162354",
120
121
            "LS-film-162354"
        )
Jonas Waeber's avatar
Jonas Waeber committed
122
123
    )
}