From a46fcbff9a70578454a817e84873f98b044c9bde Mon Sep 17 00:00:00 2001 From: Xavi Aracil Date: Fri, 25 Nov 2022 08:45:38 +0100 Subject: [PATCH] Refactor probes, extracted out to a generic class --- .../vc/probe/ContextPropertyProbe.java | 20 ++++----- .../inspect/vc/probe/PropertyProbe.java | 45 +++++++++++++++++++ .../inspect/vc/probe/TypePropertyProbe.java | 21 ++------- 3 files changed, 58 insertions(+), 28 deletions(-) create mode 100644 inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/PropertyProbe.java diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/ContextPropertyProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/ContextPropertyProbe.java index d3fd35d..67774f2 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/ContextPropertyProbe.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/ContextPropertyProbe.java @@ -7,6 +7,7 @@ import static org.oneedtech.inspect.util.code.Defensives.checkNotNull; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.BiFunction; import org.oneedtech.inspect.core.probe.Probe; import org.oneedtech.inspect.core.probe.RunContext; @@ -24,32 +25,29 @@ import com.google.common.collect.ImmutableMap; * * @author mgylling */ -public class ContextPropertyProbe extends Probe { +public class ContextPropertyProbe extends PropertyProbe { private final VerifiableCredential.Type type; public ContextPropertyProbe(VerifiableCredential.Type type) { - super(ID); + super(ID, "@context"); this.type = checkNotNull(type); + setValidations(this::validate); } @Override - public ReportItems run(JsonNode root, RunContext ctx) throws Exception { - - ArrayNode contextNode = (ArrayNode) root.get("@context"); - if (contextNode == null) { - return notRun("No @context property", ctx); - } + protected ReportItems reportForNonExistentProperty(RunContext ctx) { + return notRun("No @context property", ctx); + } + public ReportItems validate(List nodeValues, RunContext ctx) { List expected = values.get(values.keySet() .stream() .filter(s->s.contains(type)) .findFirst() .orElseThrow(()-> new IllegalArgumentException(type.name() + " not recognized"))); - - List given = JsonNodeUtil.asStringList(contextNode); int pos = 0; for (String uri : expected) { - if ((given.size() < pos + 1) || !given.get(pos).equals(uri)) { + if ((nodeValues.size() < pos + 1) || !nodeValues.get(pos).equals(uri)) { return error("missing required @context uri " + uri + " at position " + (pos + 1), ctx); } pos++; diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/PropertyProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/PropertyProbe.java new file mode 100644 index 0000000..fb4a108 --- /dev/null +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/PropertyProbe.java @@ -0,0 +1,45 @@ +package org.oneedtech.inspect.vc.probe; + +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.Function; +import java.util.function.Predicate; + +import org.oneedtech.inspect.core.probe.Probe; +import org.oneedtech.inspect.core.probe.RunContext; +import org.oneedtech.inspect.core.report.ReportItems; +import org.oneedtech.inspect.vc.util.JsonNodeUtil; + +import com.fasterxml.jackson.databind.JsonNode; + +public class PropertyProbe extends Probe { + private final String propertyName; + private BiFunction, RunContext, ReportItems> validations; + + public PropertyProbe(String id, String propertyName) { + super(id); + this.propertyName = propertyName; + this.validations = this::defaultValidation; + } + + public void setValidations(BiFunction, RunContext, ReportItems> validations) { + this.validations = validations; + } + + @Override + public ReportItems run(JsonNode root, RunContext ctx) throws Exception { + JsonNode propertyNode = root.get(propertyName); + if (propertyNode == null) { + return reportForNonExistentProperty(ctx); + } + List values = JsonNodeUtil.asStringList(propertyNode); + return validations.apply(values, ctx); + } + protected ReportItems reportForNonExistentProperty(RunContext ctx) { + return fatal("No " + propertyName + " property", ctx); + } + + private ReportItems defaultValidation(List nodeValues, RunContext ctx) { + return notRun("Not additional validations run", ctx); + } +} diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/TypePropertyProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/TypePropertyProbe.java index ed67b8b..c8e03f0 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/TypePropertyProbe.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/TypePropertyProbe.java @@ -4,38 +4,25 @@ import static org.oneedtech.inspect.util.code.Defensives.checkNotNull; import java.util.List; -import org.oneedtech.inspect.core.probe.Probe; import org.oneedtech.inspect.core.probe.RunContext; import org.oneedtech.inspect.core.report.ReportItems; import org.oneedtech.inspect.vc.VerifiableCredential; -import org.oneedtech.inspect.vc.VerifiableCredential.Type; -import org.oneedtech.inspect.vc.util.JsonNodeUtil; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.node.ArrayNode; /** * A Probe that verifies a credential's type property. * * @author mgylling */ -public class TypePropertyProbe extends Probe { +public class TypePropertyProbe extends PropertyProbe { private final VerifiableCredential.Type expected; public TypePropertyProbe(VerifiableCredential.Type expected) { - super(ID); + super(ID, "type"); this.expected = checkNotNull(expected); + this.setValidations(this::validate); } - @Override - public ReportItems run(JsonNode root, RunContext ctx) throws Exception { - - ArrayNode typeNode = (ArrayNode) root.get("type"); - if (typeNode == null) - return fatal("No type property", ctx); - - List values = JsonNodeUtil.asStringList(typeNode); - + public ReportItems validate(List values, RunContext ctx) { if (!values.contains("VerifiableCredential")) { return fatal("The type property does not contain the entry 'VerifiableCredential'", ctx); }