Commit cf3f731d authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Improves tests for service

parent 3c3e754d
/*
* mapper-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/>.
*/
package org.memobase
data class KafkaTestParams(
val count: Int,
val key: String,
val expectedKeys: List<String>
)
\ No newline at end of file
......@@ -17,28 +17,21 @@
*/
package org.memobase
import com.beust.klaxon.Klaxon
import org.apache.kafka.common.serialization.StringDeserializer
import org.apache.kafka.common.serialization.StringSerializer
import org.apache.kafka.streams.KafkaStreams
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.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.assertAll
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import org.memobase.builder.ResourceBuilder
import org.memobase.helpers.StringHelpers
import org.memobase.mapping.MappingConfig
import org.memobase.settings.SettingsLoader
import java.io.File
import java.io.FileOutputStream
import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.Paths
import java.util.stream.Stream
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
......@@ -91,15 +84,17 @@ class Tests {
}*/
private val regex = Regex("(_:B[A-Za-z0-9]+)")
@Test
fun `test kafka topology`() {
@ParameterizedTest
@MethodSource("kafkaTests")
fun `test kafka topology`(params: KafkaTestParams) {
val settings = SettingsLoader(
listOf(
"institutionId",
"recordSetId",
"configs"
),
fileName = "kafkaTest1.yml",
fileName = "kafkaTest${params.count}.yml",
useStreamsConfig = true
)
val testDriver = TopologyTestDriver(KafkaTopology(settings).build(), settings.kafkaStreamsSettings)
......@@ -108,36 +103,63 @@ class Tests {
)
testDriver.pipeInput(
factory.create(
settings.inputTopic, "", readFile("kafkaTests/1/input.json")
settings.inputTopic, params.key, readFile("kafkaTests/${params.count}/input.json")
)
)
val record = testDriver.readOutput(
var record = testDriver.readOutput(
settings.outputTopic,
StringDeserializer(),
StringDeserializer()
)
val sortedResult = record.value().lines().map {
var replacedString = it
for (matchResult in regex.findAll(it)) {
replacedString = replacedString.replace(matchResult.groups[0]?.value.orEmpty(), "_:B")
}
replacedString
}.sorted().reduce { acc, s -> acc + "\n" + s }
var recordCount = 1
while (record != null) {
val sortedResult = record.value().lines().map {
var replacedString = it
for (matchResult in regex.findAll(it)) {
replacedString = replacedString.replace(matchResult.groups[0]?.value.orEmpty(), "_:B")
}
replacedString
}.sorted().reduce { acc, s -> acc + "\n" + s }
assertThat(record)
.isNotNull
.hasFieldOrPropertyWithValue("key", "https://memobase.ch/record/BAZ-MEI_49884")
assertThat(record)
.isNotNull
.hasFieldOrPropertyWithValue("key", params.expectedKeys[recordCount - 1])
assertThat(sortedResult)
.isEqualTo(readFile("kafkaTests/${params.count}/output$recordCount.nt"))
record = testDriver.readOutput(
settings.outputTopic,
StringDeserializer(),
StringDeserializer()
)
recordCount += 1
}
assertThat(sortedResult)
.isEqualTo(readFile("kafkaTests/1/output.nt"))
}
private fun kafkaTests() = Stream.of(
KafkaTestParams(
1,
"MEI_49884",
listOf("https://memobase.ch/record/BAZ-MEI_49884",
"https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0")
),
KafkaTestParams(
2,
"Sig Han 1293",
listOf(
"https://memobase.ch/record/TEST-Sig_Han_1293",
"https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0")
)
)
@ParameterizedTest
@MethodSource("idExamples")
......
id: jobXYZ
app:
institutionId: "TEST"
recordSetId: "TEST_RECORD_SET"
configs: src/test/resources/kafkaTests/2/config
kafka:
streams:
bootstrap.servers: localhost:12345
application.id: test-clinet-1234
topic:
in: test-topic-in
out: test-topic-out
process: test-topic-process
\ No newline at end of file
<https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0> <http://rdaregistry.info/Elements/u/P60558> "sw" .
<https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0> <http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#hasMedium> "Negativ Nitrat (NN)"@de .
<https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.ica.org/standards/RiC/ontology#Instantiation> .
<https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0> <https://www.ica.org/standards/RiC/ontology#heldBy> <https://memobase.ch/institution/BAZ> .
<https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0> <https://www.ica.org/standards/RiC/ontology#identifiedBy> _:B .
<https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0> <https://www.ica.org/standards/RiC/ontology#identifiedBy> _:B .
<https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0> <https://www.ica.org/standards/RiC/ontology#instantiates> <https://memobase.ch/record/BAZ-MEI_49884> .
<https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0> <https://www.ica.org/standards/RiC/ontology#physicalCharacteristics> "18x24"@de .
<https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0> <https://www.ica.org/standards/RiC/ontology#physicalCharacteristics> "Nitratschaden"@de .
<https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0> <https://www.ica.org/standards/RiC/ontology#physicalCharacteristics> "Querformat"@de .
<https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0> <https://www.ica.org/standards/RiC/ontology#type> "physicalObject" .
_:B <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.ica.org/standards/RiC/ontology#Identifier> .
_:B <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.ica.org/standards/RiC/ontology#Identifier> .
_:B <https://www.ica.org/standards/RiC/ontology#identifier> "MEI_49884" .
_:B <https://www.ica.org/standards/RiC/ontology#identifier> "https://memobase.ch/instatiation/physical/BAZ-MEI_49884-0" .
_:B <https://www.ica.org/standards/RiC/ontology#type> "main" .
_:B <https://www.ica.org/standards/RiC/ontology#type> "original" .
\ No newline at end of file
record:
uri: signatur
type: Video # is always a constant!
digital:
locator: # ebucore:locator
- link
descriptiveNote:
de:
- beschreibung
duration: duration
rights: # blank nodes rico:Rule with type "x"
holder: rechte
usage:
const: cc-by-nc-nd
access:
const: public
\ No newline at end of file
{
"signatur": "Sig Han 1293",
"link": "http://exampl.org/video/play",
"beschreibung": "Eine Bespielbeschreibung",
"rechte": "Familie XYZ",
"duration": "10:10:10"
}
\ No newline at end of file
<https://memobase.ch/record/TEST-Sig_Han_1293> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.ica.org/standards/RiC/ontology#Record> .
<https://memobase.ch/record/TEST-Sig_Han_1293> <https://www.ica.org/standards/RiC/ontology#hasInstantiation> <https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> .
<https://memobase.ch/record/TEST-Sig_Han_1293> <https://www.ica.org/standards/RiC/ontology#heldBy> <https://memobase.ch/institution/TEST> .
<https://memobase.ch/record/TEST-Sig_Han_1293> <https://www.ica.org/standards/RiC/ontology#identifiedBy> _:B .
<https://memobase.ch/record/TEST-Sig_Han_1293> <https://www.ica.org/standards/RiC/ontology#isPartOf> <https://memobase.ch/recordSet/TEST_RECORD_SET> .
<https://memobase.ch/record/TEST-Sig_Han_1293> <https://www.ica.org/standards/RiC/ontology#type> "Video" .
_:B <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.ica.org/standards/RiC/ontology#Identifier> .
_:B <https://www.ica.org/standards/RiC/ontology#identifier> "https://memobase.ch/record/TEST-Sig_Han_1293" .
_:B <https://www.ica.org/standards/RiC/ontology#type> "main" .
\ No newline at end of file
<https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> <http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#duration> "10:10:10" .
<https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> <http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#locator> "http://exampl.org/video/play" .
<https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.ica.org/standards/RiC/ontology#Instantiation> .
<https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> <https://www.ica.org/standards/RiC/ontology#descriptiveNote> "Eine Bespielbeschreibung"@de .
<https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> <https://www.ica.org/standards/RiC/ontology#heldBy> <https://memobase.ch/institution/TEST> .
<https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> <https://www.ica.org/standards/RiC/ontology#identifiedBy> _:B .
<https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> <https://www.ica.org/standards/RiC/ontology#instantiates> <https://memobase.ch/record/TEST-Sig_Han_1293> .
<https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> <https://www.ica.org/standards/RiC/ontology#regulatedBy> _:B .
<https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> <https://www.ica.org/standards/RiC/ontology#regulatedBy> _:B .
<https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> <https://www.ica.org/standards/RiC/ontology#regulatedBy> _:B .
<https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> <https://www.ica.org/standards/RiC/ontology#type> "digitalObject" .
_:B <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.ica.org/standards/RiC/ontology#Identifier> .
_:B <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.ica.org/standards/RiC/ontology#Rule> .
_:B <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.ica.org/standards/RiC/ontology#Rule> .
_:B <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.ica.org/standards/RiC/ontology#Rule> .
_:B <https://www.ica.org/standards/RiC/ontology#identifier> "https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0" .
_:B <https://www.ica.org/standards/RiC/ontology#regulates> <https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> .
_:B <https://www.ica.org/standards/RiC/ontology#regulates> <https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> .
_:B <https://www.ica.org/standards/RiC/ontology#regulates> <https://memobase.ch/instantiation/digital/TEST-Sig_Han_1293-0> .
_:B <https://www.ica.org/standards/RiC/ontology#title> "Familie XYZ" .
_:B <https://www.ica.org/standards/RiC/ontology#title> "cc-by-nc-nd" .
_:B <https://www.ica.org/standards/RiC/ontology#title> "public" .
_:B <https://www.ica.org/standards/RiC/ontology#type> "access" .
_:B <https://www.ica.org/standards/RiC/ontology#type> "holder" .
_:B <https://www.ica.org/standards/RiC/ontology#type> "main" .
_:B <https://www.ica.org/standards/RiC/ontology#type> "usage" .
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment