From 7cd91030016a1ed61d9ac6adc36da2e45299cf2f Mon Sep 17 00:00:00 2001 From: Xavi Aracil Date: Mon, 28 Nov 2022 14:17:10 +0100 Subject: [PATCH] Two type of generic property probes, depending of the type of validations --- .../vc/probe/ContextPropertyProbe.java | 4 +-- .../inspect/vc/probe/PropertyProbe.java | 10 +++--- .../vc/probe/StringValuePropertyProbe.java | 35 +++++++++++++++++++ .../inspect/vc/probe/TypePropertyProbe.java | 4 +-- 4 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/StringValuePropertyProbe.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 1580221..8a21518 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 @@ -13,13 +13,13 @@ import org.oneedtech.inspect.vc.Credential.CredentialEnum; * * @author mgylling */ -public class ContextPropertyProbe extends PropertyProbe { +public class ContextPropertyProbe extends StringValuePropertyProbe { private final CredentialEnum type; public ContextPropertyProbe(CredentialEnum type) { super(ID, "@context"); this.type = checkNotNull(type); - setValidations(this::validate); + setValueValidations(this::validate); } @Override 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 index 9c0ba22..ca64f9e 100644 --- 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 @@ -12,7 +12,7 @@ import com.fasterxml.jackson.databind.JsonNode; public class PropertyProbe extends Probe { private final String propertyName; - private BiFunction, RunContext, ReportItems> validations; + private BiFunction validations; public PropertyProbe(String id, String propertyName) { super(id); @@ -20,7 +20,7 @@ public class PropertyProbe extends Probe { this.validations = this::defaultValidation; } - public void setValidations(BiFunction, RunContext, ReportItems> validations) { + public void setValidations(BiFunction validations) { this.validations = validations; } @@ -30,14 +30,16 @@ public class PropertyProbe extends Probe { if (propertyNode == null) { return reportForNonExistentProperty(ctx); } + List values = JsonNodeUtil.asStringList(propertyNode); - return validations.apply(values, ctx); + return validations.apply(propertyNode, ctx); } + protected ReportItems reportForNonExistentProperty(RunContext ctx) { return fatal("No " + propertyName + " property", ctx); } - private ReportItems defaultValidation(List nodeValues, RunContext ctx) { + private ReportItems defaultValidation(JsonNode node, RunContext ctx) { return notRun("Not additional validations run", ctx); } } diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/StringValuePropertyProbe.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/StringValuePropertyProbe.java new file mode 100644 index 0000000..5c792d0 --- /dev/null +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/StringValuePropertyProbe.java @@ -0,0 +1,35 @@ +package org.oneedtech.inspect.vc.probe; + +import java.util.List; +import java.util.function.BiFunction; + +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 StringValuePropertyProbe extends PropertyProbe { + private BiFunction, RunContext, ReportItems> valueValidations; + + public StringValuePropertyProbe(String id, String propertyName) { + super(id, propertyName); + this.valueValidations = this::defaultValidation; + super.setValidations(this::nodeValidation); + } + + public void setValueValidations(BiFunction, RunContext, ReportItems> validations) { + this.valueValidations = validations; + } + + private ReportItems nodeValidation(JsonNode node, RunContext ctx) { + List values = JsonNodeUtil.asStringList(node); + return valueValidations.apply(values, 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 7896617..972427c 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 @@ -14,13 +14,13 @@ import org.oneedtech.inspect.vc.Credential.CredentialEnum; * * @author mgylling */ -public class TypePropertyProbe extends PropertyProbe { +public class TypePropertyProbe extends StringValuePropertyProbe { private final CredentialEnum expected; public TypePropertyProbe(CredentialEnum expected) { super(ID, "type"); this.expected = checkNotNull(expected); - this.setValidations(this::validate); + this.setValueValidations(this::validate); } public ReportItems validate(List values, RunContext ctx) {