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

View File

@ -13,6 +13,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; 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.Assertion.Type;
import org.oneedtech.inspect.vc.Credential.CredentialEnum; import org.oneedtech.inspect.vc.Credential.CredentialEnum;
import org.oneedtech.inspect.vc.jsonld.JsonLdGeneratedObject; 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.GraphFetcherProbe;
import org.oneedtech.inspect.vc.jsonld.probe.JsonLDCompactionProve; import org.oneedtech.inspect.vc.jsonld.probe.JsonLDCompactionProve;
import org.oneedtech.inspect.vc.jsonld.probe.JsonLDValidationProbe; import org.oneedtech.inspect.vc.jsonld.probe.JsonLDValidationProbe;
@ -164,21 +166,31 @@ public class OB20Inspector extends VCInspector {
if(broken(accumulator)) return abort(ctx, accumulator, probeCount); if(broken(accumulator)) return abort(ctx, accumulator, probeCount);
} }
// extension validations
List<JsonNode> jsonLdGeneratedObjects = ctx.getGeneratedObjects().values().stream()
.filter(generatedObject -> generatedObject instanceof JsonLdGeneratedObject)
.map(obj -> {
try {
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 // 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(); OB20EndorsementInspector endorsementInspector = new OB20EndorsementInspector.Builder().documentLoader(documentLoader).build();
// get endorsements for all JSON_LD objects in the graph // get endorsements for all JSON_LD objects in the graph
List<JsonNode> endorsements = ctx.getGeneratedObjects().values().stream() List<JsonNode> endorsements = jsonLdGeneratedObjects.stream().flatMap(node -> {
.filter(generatedObject -> generatedObject instanceof JsonLdGeneratedObject)
.flatMap(obj -> {
JsonNode node;
try {
node = mapper.readTree(((JsonLdGeneratedObject) obj).getJson());
// return endorsement node, filtering out the on inside @context // return endorsement node, filtering out the on inside @context
return asNodeList(node, "$..endorsement", jsonPath).stream().filter(endorsementNode -> !endorsementNode.isObject()); return asNodeList(node, "$..endorsement", jsonPath).stream().filter(endorsementNode -> !endorsementNode.isObject());
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Couldn't not parse " + obj.getId() + ": contains invalid JSON");
}
}) })
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@ -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) {
}
}

View File

@ -268,6 +268,24 @@ public class OB20Tests {
}); });
} }
@Test
void testExtensionNode() {
assertDoesNotThrow(()->{
Report report = validator.run(Samples.OB20.JSON.ASSERTION_WITH_EXTENSION_NODE_BASIC_JSON.asFileResource());
if(verbose) PrintHelper.print(report, true);
assertValid(report);
});
}
@Test
void testInvalidExtensionNode() {
assertDoesNotThrow(()->{
Report report = validator.run(Samples.OB20.JSON.ASSERTION_WITH_EXTENSION_NODE_INVALID_JSON.asFileResource());
if(verbose) PrintHelper.print(report, true);
assertInvalid(report);
});
}
@Nested @Nested
static class WarningTests { static class WarningTests {
@BeforeAll @BeforeAll

View File

@ -91,6 +91,10 @@ public class Samples {
public final static Sample BADGE_WITH_COMPLEX_IMAGE_JSON = new Sample("ob20/assets/badgeclass-with-complex-image.json", true); public final static Sample BADGE_WITH_COMPLEX_IMAGE_JSON = new Sample("ob20/assets/badgeclass-with-complex-image.json", true);
// original: test_validate_endorsements // original: test_validate_endorsements
public final static Sample ASSERTION_WITH_ENDORSEMENTS = new Sample("ob20/assertion-with-endorsements.json", true); public final static Sample ASSERTION_WITH_ENDORSEMENTS = new Sample("ob20/assertion-with-endorsements.json", true);
// original: test_validate_extensions: test_validate_extension_node_basic
public final static Sample ASSERTION_WITH_EXTENSION_NODE_BASIC_JSON = new Sample("ob20/assertion-with-extension-node-basic.json", true);
// original: test_validate_extensions: test_validate_extension_node_invalid
public final static Sample ASSERTION_WITH_EXTENSION_NODE_INVALID_JSON = new Sample("ob20/assertion-with-extension-node-invalid.json", true);
} }
public static final class PNG { public static final class PNG {

View File

@ -0,0 +1,29 @@
{
"@context": ["https://w3id.org/openbadges/v2","https://w3id.org/openbadges/extensions/exampleExtension/context.json"],
"id": "http://example.org/assertion",
"type": "Assertion",
"recipient": {
"type": "email",
"hashed": true,
"salt": "deadsea",
"identity": "sha256$ecf5409f3f4b91ab60cc5ef4c02aef7032354375e70cf4d8e43f6a1d29891942"
},
"image": "https://example.org/beths-robot-badge.png",
"issuedOn": "2016-12-31T23:59:59Z",
"badge": "https://example.org/robotics-badge.json",
"verification": {
"type": "hosted"
},
"evidence": {
"id": "_:b1",
"narrative": "Rocked the free world"
},
"extensions:exampleExtension": {
"id": "_:b0",
"type": [
"Extension",
"extensions:ExampleExtension"
],
"schema:text": "I'm a property, short and sweet"
}
}

View File

@ -0,0 +1,29 @@
{
"@context": ["https://w3id.org/openbadges/v2","https://w3id.org/openbadges/extensions/exampleExtension/context.json"],
"id": "http://example.org/assertion",
"type": "Assertion",
"recipient": {
"type": "email",
"hashed": true,
"salt": "deadsea",
"identity": "sha256$ecf5409f3f4b91ab60cc5ef4c02aef7032354375e70cf4d8e43f6a1d29891942"
},
"image": "https://example.org/beths-robot-badge.png",
"issuedOn": "2016-12-31T23:59:59Z",
"badge": "https://example.org/robotics-badge.json",
"verification": {
"type": "hosted"
},
"evidence": {
"id": "_:b1",
"narrative": "Rocked the free world"
},
"extensions:exampleExtension": {
"id": "_:b0",
"type": [
"Extension",
"extensions:ExampleExtension"
],
"schema:text": 1337
}
}