Added extension tests

This commit is contained in:
Xavi Aracil
2022-12-14 08:14:33 +01:00
parent 3b335867fe
commit 727bd8194b
6 changed files with 139 additions and 10 deletions
@@ -13,6 +13,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -33,6 +34,7 @@ import org.oneedtech.inspect.util.spec.Specification;
import org.oneedtech.inspect.vc.Assertion.Type;
import org.oneedtech.inspect.vc.Credential.CredentialEnum;
import org.oneedtech.inspect.vc.jsonld.JsonLdGeneratedObject;
import org.oneedtech.inspect.vc.jsonld.probe.ExtensionProbe;
import org.oneedtech.inspect.vc.jsonld.probe.GraphFetcherProbe;
import org.oneedtech.inspect.vc.jsonld.probe.JsonLDCompactionProve;
import org.oneedtech.inspect.vc.jsonld.probe.JsonLDValidationProbe;
@@ -164,23 +166,33 @@ public class OB20Inspector extends VCInspector {
if(broken(accumulator)) return abort(ctx, accumulator, probeCount);
}
// Embedded endorsements. Pass document loader because it has already cached documents, and it has localdomains for testing
OB20EndorsementInspector endorsementInspector = new OB20EndorsementInspector.Builder().documentLoader(documentLoader).build();
// get endorsements for all JSON_LD objects in the graph
List<JsonNode> endorsements = ctx.getGeneratedObjects().values().stream()
// extension validations
List<JsonNode> jsonLdGeneratedObjects = ctx.getGeneratedObjects().values().stream()
.filter(generatedObject -> generatedObject instanceof JsonLdGeneratedObject)
.flatMap(obj -> {
JsonNode node;
.map(obj -> {
try {
node = mapper.readTree(((JsonLdGeneratedObject) obj).getJson());
// return endorsement node, filtering out the on inside @context
return asNodeList(node, "$..endorsement", jsonPath).stream().filter(endorsementNode -> !endorsementNode.isObject());
return mapper.readTree(((JsonLdGeneratedObject) obj).getJson());
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Couldn't not parse " + obj.getId() + ": contains invalid JSON");
}
})
.collect(Collectors.toList());
for (JsonNode generatedObject : jsonLdGeneratedObjects) {
probeCount++;
accumulator.add(new ExtensionProbe().run(generatedObject, ctx));
if(broken(accumulator)) return abort(ctx, accumulator, probeCount);
}
// Embedded endorsements. Pass document loader because it has already cached documents, and it has localdomains for testing
OB20EndorsementInspector endorsementInspector = new OB20EndorsementInspector.Builder().documentLoader(documentLoader).build();
// get endorsements for all JSON_LD objects in the graph
List<JsonNode> endorsements = jsonLdGeneratedObjects.stream().flatMap(node -> {
// return endorsement node, filtering out the on inside @context
return asNodeList(node, "$..endorsement", jsonPath).stream().filter(endorsementNode -> !endorsementNode.isObject());
})
.collect(Collectors.toList());
for(JsonNode node : endorsements) {
probeCount++;
@@ -0,0 +1,37 @@
package org.oneedtech.inspect.vc.jsonld.probe;
import java.net.URI;
import java.util.Set;
import org.oneedtech.inspect.core.probe.Probe;
import org.oneedtech.inspect.core.probe.RunContext;
import org.oneedtech.inspect.core.probe.RunContext.Key;
import org.oneedtech.inspect.core.report.ReportItems;
import org.oneedtech.inspect.vc.util.CachingDocumentLoader;
import com.fasterxml.jackson.databind.JsonNode;
public class ExtensionProbe extends Probe<JsonNode> {
@Override
public ReportItems run(JsonNode node, RunContext ctx) throws Exception {
if (!node.isObject()) {
return success(ctx);
}
Object documentLoader = ctx.get(Key.JSON_DOCUMENT_LOADER);
Set<URI> contexts;
if (documentLoader instanceof CachingDocumentLoader) {
contexts = ((CachingDocumentLoader) documentLoader).getContexts();
} else {
contexts = Set.of();
}
// TODO Auto-generated method stub
return null;
}
private void getValidations(JsonNode node, String entryPath, Set<URI> contexts) {
}
}