From ccbacc5016d467d9e87f07ba3a273856d35ba403 Mon Sep 17 00:00:00 2001 From: Jens Pots Date: Thu, 23 May 2024 18:09:28 +0200 Subject: [PATCH] test: check the optional type --- src/main/kotlin/runner/Arguments.kt | 1 + src/main/kotlin/runner/Processor.kt | 9 ++++-- src/test/kotlin/sources/OptionalsTest.kt | 31 +++++++++++++++++++ src/test/resources/pipelines/optionals.ttl | 14 +++++++++ src/test/resources/processors/optionals.ttl | 33 +++++++++++++++++++++ src/test/resources/sources/Optionals.java | 23 ++++++++++++++ 6 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/test/kotlin/sources/OptionalsTest.kt create mode 100644 src/test/resources/pipelines/optionals.ttl create mode 100644 src/test/resources/processors/optionals.ttl create mode 100644 src/test/resources/sources/Optionals.java diff --git a/src/main/kotlin/runner/Arguments.kt b/src/main/kotlin/runner/Arguments.kt index ef3badf..7e5d35c 100644 --- a/src/main/kotlin/runner/Arguments.kt +++ b/src/main/kotlin/runner/Arguments.kt @@ -40,6 +40,7 @@ class Arguments(private val shape: Shape) { // Mark the not set optionals explicitly. this.shape.getProperties().forEach { + // Check if is optional. if (it.value.count != Property.Count.OPTIONAL) { return@forEach } diff --git a/src/main/kotlin/runner/Processor.kt b/src/main/kotlin/runner/Processor.kt index ba686c5..953ad57 100644 --- a/src/main/kotlin/runner/Processor.kt +++ b/src/main/kotlin/runner/Processor.kt @@ -22,8 +22,13 @@ abstract class Processor( } fun getOptionalArgument(name: String): Optional { - val result = arguments[name] as T? - return Optional.ofNullable(result) as Optional + val result = arguments[name] ?: log.fatal("Argument $name is missing") + + if (result is Optional<*>) { + return result as Optional + } + + log.fatal("Argument $name is not optional") } abstract fun exec() diff --git a/src/test/kotlin/sources/OptionalsTest.kt b/src/test/kotlin/sources/OptionalsTest.kt new file mode 100644 index 0000000..143d9e9 --- /dev/null +++ b/src/test/kotlin/sources/OptionalsTest.kt @@ -0,0 +1,31 @@ +package sources + +import java.io.File +import java.util.Optional +import kotlin.test.Test +import kotlin.test.assertEquals +import technology.idlab.runner.Pipeline + +class OptionalsTest { + @Test + fun parameters() { + val configPath = this.javaClass.getResource("/pipelines/optionals.ttl") + val config = File(configPath!!.path) + val pipeline = Pipeline(config) + val processors = pipeline.processors + + // Get the sole processor in the pipeline. + assertEquals(processors.size, 1) + val processor = processors[0] + + // Retrieve arguments. + val required: String = processor.getArgument("required") + val present: Optional = processor.getOptionalArgument("present") + val missing: Optional = processor.getOptionalArgument("missing") + + // Check if results are correct. + assertEquals("A Required String", required) + assertEquals("An Optional String", present.get()) + assertEquals(Optional.empty(), missing) + } +} diff --git a/src/test/resources/pipelines/optionals.ttl b/src/test/resources/pipelines/optionals.ttl new file mode 100644 index 0000000..b810fef --- /dev/null +++ b/src/test/resources/pipelines/optionals.ttl @@ -0,0 +1,14 @@ +@prefix jvm: . +@prefix xsd: . +@prefix owl: . + +# Include processor definitions. +<> owl:imports + <../pipeline.ttl>, + <../processors/optionals.ttl>. + +# Define a filter processor. +[] + a jvm:Optionals; + jvm:required "A Required String"^^xsd:string; + jvm:present "An Optional String"^^xsd:string. diff --git a/src/test/resources/processors/optionals.ttl b/src/test/resources/processors/optionals.ttl new file mode 100644 index 0000000..bf3132e --- /dev/null +++ b/src/test/resources/processors/optionals.ttl @@ -0,0 +1,33 @@ +@prefix jvm: . +@prefix fno: . +@prefix fnom: . +@prefix xsd: . +@prefix owl: . +@prefix : . +@prefix sh: . +@prefix rdf: . + +<> owl:imports <../pipeline.ttl>. + +jvm:Optionals a jvm:Processor; + jvm:file <../sources/Optionals.java>; + jvm:language "Java". + +[] a sh:NodeShape; + sh:targetClass jvm:Optionals; + sh:property [ + sh:path jvm:required; + sh:datatype xsd:string; + sh:minCount 1; + sh:maxCount 1; + ], [ + sh:path jvm:present; + sh:datatype xsd:string; + sh:maxCount 1; + ], [ + sh:path jvm:missing; + sh:datatype xsd:string; + sh:maxCount 1; + ]; + sh:closed true; + sh:ignoredProperties (rdf:type). diff --git a/src/test/resources/sources/Optionals.java b/src/test/resources/sources/Optionals.java new file mode 100644 index 0000000..afd4900 --- /dev/null +++ b/src/test/resources/sources/Optionals.java @@ -0,0 +1,23 @@ +import java.util.Optional; +import java.util.Map; + +import technology.idlab.runner.Processor; + +public class Optionals extends Processor { + // Parameters + public final String required; + public final Optional present; + public final Optional missing; + + public Optionals(Map args) { + // Call super constructor. + super(args); + + // Parameters + this.required = this.getArgument("required"); + this.present = this.getOptionalArgument("present"); + this.missing = this.getOptionalArgument("missing"); + } + + public void exec() {} +}