TestRecordSetSearchDoc.kt 5.61 KB
Newer Older
1 2
package org.memobase

3
import ch.memobase.reporting.Report
Jonas Waeber's avatar
Jonas Waeber committed
4
import ch.memobase.reporting.ReportStatus
5 6 7 8 9 10 11 12 13 14
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.mockk.every
import io.mockk.mockk
import java.io.File
import java.nio.charset.Charset
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
15
import org.assertj.core.api.Assertions.assertThat
16
import org.junit.jupiter.api.Disabled
17 18
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
19 20 21 22 23
import org.junit.jupiter.api.assertAll
import org.memobase.helpers.ElasticSearchWrapper
import org.memobase.helpers.JSON
import org.memobase.model.FacetContainer
import org.memobase.model.LanguageContainer
24 25 26 27 28
import org.memobase.model.RecordSetSearchDoc

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TestRecordSetSearchDoc {

29 30
    private val reader = ObjectMapper().registerKotlinModule().reader()

31 32 33 34
    private val dataPath = "src/test/resources/data/recordSets"
    private fun readFile(fileName: String): String {
        return File("$dataPath/$fileName").readText(Charset.defaultCharset())
    }
35

36 37 38 39
    @Test
    fun `test create default record set `() {
        val searchDoc = RecordSetSearchDoc.DEFAULT
        assertThat(searchDoc.toJson())
40
            .isEqualTo(readFile("default_record_set.json"))
41 42 43 44 45

    }

    @Test
    fun `transform record set search doc`() {
46
        val settings = App.createSettings("kafkaTest1.yml")
Jonas Waeber's avatar
Jonas Waeber committed
47
        val data = readFile("completeExample.json")
48 49
        val wrapper = mockk<ElasticSearchWrapper>()

Jonas Waeber's avatar
Jonas Waeber committed
50
        every { wrapper.countNumberOfDocuments("testComplete") } returns 102
51 52 53 54 55 56 57
        every { wrapper.getDocumentTypesFromRecords("testComplete", "recordSet.facet") } returns listOf(
            FacetContainer(
                LanguageContainer(listOf("Fotographie"), listOf("Photographie"), listOf("Fotografia"), emptyList()),
                null,
                emptyList()
            )
        )
Jonas Waeber's avatar
Jonas Waeber committed
58
        every { wrapper.getInstitutionName("completeInstitution") } returns FacetContainer(
Jonas Waeber's avatar
Jonas Waeber committed
59
            LanguageContainer(
Jonas Waeber's avatar
Jonas Waeber committed
60 61 62
                listOf("Test Complete"),
                listOf("Test Complete"),
                listOf("Test Complete"),
Jonas Waeber's avatar
Jonas Waeber committed
63 64
                emptyList()
            ),
Jonas Waeber's avatar
Jonas Waeber committed
65
            "completeExampleTest",
Jonas Waeber's avatar
Jonas Waeber committed
66 67
            emptyList()
        )
68 69 70 71 72 73 74
        every {
            wrapper.getRecordSetName("testComplete")
        } returns LanguageContainer(
            listOf("Complete Record Set (DE)"),
            listOf("Complete Record Set (FR)"),
            listOf("Complete Record Set (IT)")
        )
75 76 77
        val input = JSON.unpack(JSON.parse(data))
        val searchDocBuilder = RecordSetSearchDocBuilder(wrapper)

Jonas Waeber's avatar
Jonas Waeber committed
78
        val result = searchDocBuilder.transform("https://memobase.ch/recordSet/completeExample", input)
79 80

        result as RecordSetSearchDoc
Jonas Waeber's avatar
Jonas Waeber committed
81
        val resultString = result.toJson().replace(TestUtilities.dateRegex, "2020")
Jonas Waeber's avatar
Jonas Waeber committed
82
        val targetString = readFile("completeExampleOutput.json").replace(TestUtilities.dateRegex, "2020")
83 84

        assertAll("",
85 86 87 88 89 90 91 92
            {
                assertThat(result.id).isEqualTo("testComplete")
            },
            {
                assertThat(resultString).isEqualTo(
                    targetString
                )
            }
93 94 95 96 97 98 99
        )

    }

    @Test
    @Disabled
    fun `integration test record sets`() {
100 101 102 103 104 105 106 107 108 109 110
        val settings = App.createSettings("kafkaTest1.yml")

        every { TestUtilities.elasticSearchWrapperMocked.countNumberOfDocuments("") } returns 123
        every { TestUtilities.elasticSearchWrapperMocked.getDocumentTypesFromRecords("", "") } returns listOf(
            FacetContainer(
                LanguageContainer(listOf("Foto"), listOf("Foto"), listOf("Foto"), emptyList()), "Foto", emptyList()
            )
        )
        val topology =
            KafkaTopology(settings, TestUtilities.translationMappers, TestUtilities.elasticSearchWrapperMocked)
        val testDriver = TopologyTestDriver(topology.build(), settings.kafkaStreamsSettings)
111
        val factory = ConsumerRecordFactory(
112
            StringSerializer(), StringSerializer()
113 114
        )
        testDriver.pipeInput(
115 116 117 118 119
            factory.create(
                settings.inputTopic,
                "testComplete",
                readFile("completeExample.json")
            )
120 121 122
        )

        val record = testDriver.readOutput(
123 124 125
            settings.outputTopic,
            StringDeserializer(),
            StringDeserializer()
126 127 128
        )

        val report = testDriver.readOutput(
129 130 131
            settings.processReportTopic,
            StringDeserializer(),
            StringDeserializer()
132 133 134 135 136 137
        )

        val reportKey = report.key()
        val reportValue = reader.readValue(report.value(), Report::class.java)

        val key = record.key()
Jonas Waeber's avatar
Jonas Waeber committed
138
        val value = record.value().replace(TestUtilities.dateRegex, "2020")
Jonas Waeber's avatar
Jonas Waeber committed
139
        val resultValue = readFile("completeExampleOutput.json").replace(TestUtilities.dateRegex, "2020")
140

141
        assertAll("",
142 143 144 145 146 147 148 149 150 151 152 153 154
            {
                assertThat(value)
                    .isEqualTo(resultValue)
            },
            { assertThat(key).isEqualTo("testComplete") },
            { assertThat(reportKey).isEqualTo("testComplete") },
            {
                assertThat(reportValue).isEqualTo(
                    Report(
                        "testComplete",
                        ReportStatus.success,
                        "",
                        Service.name
155
                    )
156 157
                )
            }
158
        )
159 160
    }
}