Two type of generic property probes, depending of the type of validations

This commit is contained in:
Xavi Aracil 2022-11-28 14:17:10 +01:00
parent 521cce5dd4
commit 7cd9103001
4 changed files with 45 additions and 8 deletions

View File

@ -13,13 +13,13 @@ import org.oneedtech.inspect.vc.Credential.CredentialEnum;
* *
* @author mgylling * @author mgylling
*/ */
public class ContextPropertyProbe extends PropertyProbe { public class ContextPropertyProbe extends StringValuePropertyProbe {
private final CredentialEnum type; private final CredentialEnum type;
public ContextPropertyProbe(CredentialEnum type) { public ContextPropertyProbe(CredentialEnum type) {
super(ID, "@context"); super(ID, "@context");
this.type = checkNotNull(type); this.type = checkNotNull(type);
setValidations(this::validate); setValueValidations(this::validate);
} }
@Override @Override

View File

@ -12,7 +12,7 @@ import com.fasterxml.jackson.databind.JsonNode;
public class PropertyProbe extends Probe<JsonNode> { public class PropertyProbe extends Probe<JsonNode> {
private final String propertyName; private final String propertyName;
private BiFunction<List<String>, RunContext, ReportItems> validations; private BiFunction<JsonNode, RunContext, ReportItems> validations;
public PropertyProbe(String id, String propertyName) { public PropertyProbe(String id, String propertyName) {
super(id); super(id);
@ -20,7 +20,7 @@ public class PropertyProbe extends Probe<JsonNode> {
this.validations = this::defaultValidation; this.validations = this::defaultValidation;
} }
public void setValidations(BiFunction<List<String>, RunContext, ReportItems> validations) { public void setValidations(BiFunction<JsonNode, RunContext, ReportItems> validations) {
this.validations = validations; this.validations = validations;
} }
@ -30,14 +30,16 @@ public class PropertyProbe extends Probe<JsonNode> {
if (propertyNode == null) { if (propertyNode == null) {
return reportForNonExistentProperty(ctx); return reportForNonExistentProperty(ctx);
} }
List<String> values = JsonNodeUtil.asStringList(propertyNode); List<String> values = JsonNodeUtil.asStringList(propertyNode);
return validations.apply(values, ctx); return validations.apply(propertyNode, ctx);
} }
protected ReportItems reportForNonExistentProperty(RunContext ctx) { protected ReportItems reportForNonExistentProperty(RunContext ctx) {
return fatal("No " + propertyName + " property", ctx); return fatal("No " + propertyName + " property", ctx);
} }
private ReportItems defaultValidation(List<String> nodeValues, RunContext ctx) { private ReportItems defaultValidation(JsonNode node, RunContext ctx) {
return notRun("Not additional validations run", ctx); return notRun("Not additional validations run", ctx);
} }
} }

View File

@ -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<List<String>, RunContext, ReportItems> valueValidations;
public StringValuePropertyProbe(String id, String propertyName) {
super(id, propertyName);
this.valueValidations = this::defaultValidation;
super.setValidations(this::nodeValidation);
}
public void setValueValidations(BiFunction<List<String>, RunContext, ReportItems> validations) {
this.valueValidations = validations;
}
private ReportItems nodeValidation(JsonNode node, RunContext ctx) {
List<String> values = JsonNodeUtil.asStringList(node);
return valueValidations.apply(values, ctx);
}
private ReportItems defaultValidation(List<String> nodeValues, RunContext ctx) {
return notRun("Not additional validations run", ctx);
}
}

View File

@ -14,13 +14,13 @@ import org.oneedtech.inspect.vc.Credential.CredentialEnum;
* *
* @author mgylling * @author mgylling
*/ */
public class TypePropertyProbe extends PropertyProbe { public class TypePropertyProbe extends StringValuePropertyProbe {
private final CredentialEnum expected; private final CredentialEnum expected;
public TypePropertyProbe(CredentialEnum expected) { public TypePropertyProbe(CredentialEnum expected) {
super(ID, "type"); super(ID, "type");
this.expected = checkNotNull(expected); this.expected = checkNotNull(expected);
this.setValidations(this::validate); this.setValueValidations(this::validate);
} }
public ReportItems validate(List<String> values, RunContext ctx) { public ReportItems validate(List<String> values, RunContext ctx) {