Skip to content

Commit

Permalink
test: check the optional type
Browse files Browse the repository at this point in the history
  • Loading branch information
jenspots committed May 23, 2024
1 parent fe39f13 commit ccbacc5
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/runner/Arguments.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/kotlin/runner/Processor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ abstract class Processor(
}

fun <T> getOptionalArgument(name: String): Optional<T> {
val result = arguments[name] as T?
return Optional.ofNullable(result) as Optional<T>
val result = arguments[name] ?: log.fatal("Argument $name is missing")

if (result is Optional<*>) {
return result as Optional<T>
}

log.fatal("Argument $name is not optional")
}

abstract fun exec()
Expand Down
31 changes: 31 additions & 0 deletions src/test/kotlin/sources/OptionalsTest.kt
Original file line number Diff line number Diff line change
@@ -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<String> = processor.getOptionalArgument("present")
val missing: Optional<String> = processor.getOptionalArgument("missing")

// Check if results are correct.
assertEquals("A Required String", required)
assertEquals("An Optional String", present.get())
assertEquals(Optional.empty<String>(), missing)
}
}
14 changes: 14 additions & 0 deletions src/test/resources/pipelines/optionals.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@prefix jvm: <https://w3id.org/conn/jvm#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix owl: <http://www.w3.org/2002/07/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.
33 changes: 33 additions & 0 deletions src/test/resources/processors/optionals.ttl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@prefix jvm: <https://w3id.org/conn/jvm#>.
@prefix fno: <https://w3id.org/function/ontology#>.
@prefix fnom: <https://w3id.org/function/vocabulary/mapping#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix owl: <http://www.w3.org/2002/07/owl#>.
@prefix : <https://w3id.org/conn#>.
@prefix sh: <http://www.w3.org/ns/shacl#>.
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.

<> 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).
23 changes: 23 additions & 0 deletions src/test/resources/sources/Optionals.java
Original file line number Diff line number Diff line change
@@ -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<String> present;
public final Optional<String> missing;

public Optionals(Map<String, Object> 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() {}
}

0 comments on commit ccbacc5

Please sign in to comment.