Refactor probes, extracted out to a generic class
This commit is contained in:
parent
40b34cce13
commit
a46fcbff9a
@ -7,6 +7,7 @@ import static org.oneedtech.inspect.util.code.Defensives.checkNotNull;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
import org.oneedtech.inspect.core.probe.Probe;
|
import org.oneedtech.inspect.core.probe.Probe;
|
||||||
import org.oneedtech.inspect.core.probe.RunContext;
|
import org.oneedtech.inspect.core.probe.RunContext;
|
||||||
@ -24,32 +25,29 @@ import com.google.common.collect.ImmutableMap;
|
|||||||
*
|
*
|
||||||
* @author mgylling
|
* @author mgylling
|
||||||
*/
|
*/
|
||||||
public class ContextPropertyProbe extends Probe<JsonNode> {
|
public class ContextPropertyProbe extends PropertyProbe {
|
||||||
private final VerifiableCredential.Type type;
|
private final VerifiableCredential.Type type;
|
||||||
|
|
||||||
public ContextPropertyProbe(VerifiableCredential.Type type) {
|
public ContextPropertyProbe(VerifiableCredential.Type type) {
|
||||||
super(ID);
|
super(ID, "@context");
|
||||||
this.type = checkNotNull(type);
|
this.type = checkNotNull(type);
|
||||||
|
setValidations(this::validate);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ReportItems run(JsonNode root, RunContext ctx) throws Exception {
|
protected ReportItems reportForNonExistentProperty(RunContext ctx) {
|
||||||
|
|
||||||
ArrayNode contextNode = (ArrayNode) root.get("@context");
|
|
||||||
if (contextNode == null) {
|
|
||||||
return notRun("No @context property", ctx);
|
return notRun("No @context property", ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ReportItems validate(List<String> nodeValues, RunContext ctx) {
|
||||||
List<String> expected = values.get(values.keySet()
|
List<String> expected = values.get(values.keySet()
|
||||||
.stream()
|
.stream()
|
||||||
.filter(s->s.contains(type))
|
.filter(s->s.contains(type))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.orElseThrow(()-> new IllegalArgumentException(type.name() + " not recognized")));
|
.orElseThrow(()-> new IllegalArgumentException(type.name() + " not recognized")));
|
||||||
|
|
||||||
List<String> given = JsonNodeUtil.asStringList(contextNode);
|
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
for (String uri : expected) {
|
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);
|
return error("missing required @context uri " + uri + " at position " + (pos + 1), ctx);
|
||||||
}
|
}
|
||||||
pos++;
|
pos++;
|
||||||
|
@ -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<JsonNode> {
|
||||||
|
private final String propertyName;
|
||||||
|
private BiFunction<List<String>, RunContext, ReportItems> validations;
|
||||||
|
|
||||||
|
public PropertyProbe(String id, String propertyName) {
|
||||||
|
super(id);
|
||||||
|
this.propertyName = propertyName;
|
||||||
|
this.validations = this::defaultValidation;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValidations(BiFunction<List<String>, 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<String> values = JsonNodeUtil.asStringList(propertyNode);
|
||||||
|
return validations.apply(values, ctx);
|
||||||
|
}
|
||||||
|
protected ReportItems reportForNonExistentProperty(RunContext ctx) {
|
||||||
|
return fatal("No " + propertyName + " property", ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ReportItems defaultValidation(List<String> nodeValues, RunContext ctx) {
|
||||||
|
return notRun("Not additional validations run", ctx);
|
||||||
|
}
|
||||||
|
}
|
@ -4,38 +4,25 @@ import static org.oneedtech.inspect.util.code.Defensives.checkNotNull;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.oneedtech.inspect.core.probe.Probe;
|
|
||||||
import org.oneedtech.inspect.core.probe.RunContext;
|
import org.oneedtech.inspect.core.probe.RunContext;
|
||||||
import org.oneedtech.inspect.core.report.ReportItems;
|
import org.oneedtech.inspect.core.report.ReportItems;
|
||||||
import org.oneedtech.inspect.vc.VerifiableCredential;
|
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.
|
* A Probe that verifies a credential's type property.
|
||||||
*
|
*
|
||||||
* @author mgylling
|
* @author mgylling
|
||||||
*/
|
*/
|
||||||
public class TypePropertyProbe extends Probe<JsonNode> {
|
public class TypePropertyProbe extends PropertyProbe {
|
||||||
private final VerifiableCredential.Type expected;
|
private final VerifiableCredential.Type expected;
|
||||||
|
|
||||||
public TypePropertyProbe(VerifiableCredential.Type expected) {
|
public TypePropertyProbe(VerifiableCredential.Type expected) {
|
||||||
super(ID);
|
super(ID, "type");
|
||||||
this.expected = checkNotNull(expected);
|
this.expected = checkNotNull(expected);
|
||||||
|
this.setValidations(this::validate);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public ReportItems validate(List<String> values, RunContext ctx) {
|
||||||
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<String> values = JsonNodeUtil.asStringList(typeNode);
|
|
||||||
|
|
||||||
if (!values.contains("VerifiableCredential")) {
|
if (!values.contains("VerifiableCredential")) {
|
||||||
return fatal("The type property does not contain the entry 'VerifiableCredential'", ctx);
|
return fatal("The type property does not contain the entry 'VerifiableCredential'", ctx);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user