From 521cce5dd41f6e188e2adbc4a12e30acdca98a87 Mon Sep 17 00:00:00 2001 From: Xavi Aracil Date: Fri, 25 Nov 2022 14:27:44 +0100 Subject: [PATCH] Added invalid type test --- .../inspect/vc/probe/TypePropertyProbe.java | 3 +- .../org/oneedtech/inspect/vc/OB20Tests.java | 34 +++++++++++++------ .../org/oneedtech/inspect/vc/Samples.java | 1 + .../ob20/basic-assertion-invalid-type.json | 18 ++++++++++ 4 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 inspector-vc/src/test/resources/ob20/basic-assertion-invalid-type.json 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 2c25d7d..7896617 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 @@ -33,8 +33,7 @@ public class TypePropertyProbe extends PropertyProbe { List allowedValues = expected.getAllowedTypeValues(); if (allowedValues.isEmpty()) { - // TODO implement - throw new IllegalStateException(); + return fatal("The type property is invalid", ctx); } if (!values.stream().anyMatch(v -> allowedValues.contains(v))) { return fatal(formatMessage(values), ctx); diff --git a/inspector-vc/src/test/java/org/oneedtech/inspect/vc/OB20Tests.java b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/OB20Tests.java index 4325443..05f678f 100644 --- a/inspector-vc/src/test/java/org/oneedtech/inspect/vc/OB20Tests.java +++ b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/OB20Tests.java @@ -17,6 +17,7 @@ import org.oneedtech.inspect.core.Inspector.Behavior; import org.oneedtech.inspect.core.report.Report; import org.oneedtech.inspect.test.PrintHelper; import org.oneedtech.inspect.vc.probe.ContextPropertyProbe; +import org.oneedtech.inspect.vc.probe.TypePropertyProbe; import org.oneedtech.inspect.vc.util.TestOB20Inspector.TestBuilder; public class OB20Tests { @@ -43,17 +44,6 @@ public class OB20Tests { }); } - @Test - void testSimpleJsonInvalidContext() { - assertDoesNotThrow(()->{ - Report report = validator.run(Samples.OB20.JSON.SIMPLE_ASSERTION_INVALID_CONTEXT_JSON.asFileResource()); - if(verbose) PrintHelper.print(report, true); - assertInvalid(report); - assertFatalCount(report, 1); - assertHasProbeID(report, ContextPropertyProbe.ID, true); - }); - } - @Test void testSimplePNGPlainValid() { assertDoesNotThrow(()->{ @@ -72,6 +62,28 @@ public class OB20Tests { }); } + @Test + void testSimpleJsonInvalidContext() { + assertDoesNotThrow(()->{ + Report report = validator.run(Samples.OB20.JSON.SIMPLE_ASSERTION_INVALID_CONTEXT_JSON.asFileResource()); + if(verbose) PrintHelper.print(report, true); + assertInvalid(report); + assertFatalCount(report, 1); + assertHasProbeID(report, ContextPropertyProbe.ID, true); + }); + } + + @Test + void testSimpleJsonInvalidType() { + assertDoesNotThrow(()->{ + Report report = validator.run(Samples.OB20.JSON.SIMPLE_ASSERTION_INVALID_TYPE_JSON.asFileResource()); + if(verbose) PrintHelper.print(report, true); + assertInvalid(report); + assertFatalCount(report, 1); + assertHasProbeID(report, TypePropertyProbe.ID, true); + }); + } + @Nested diff --git a/inspector-vc/src/test/java/org/oneedtech/inspect/vc/Samples.java b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/Samples.java index c628249..6f3f861 100644 --- a/inspector-vc/src/test/java/org/oneedtech/inspect/vc/Samples.java +++ b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/Samples.java @@ -46,6 +46,7 @@ public class Samples { // original: test_verify: test_verify_function public final static Sample SIMPLE_ASSERTION_JSON = new Sample("ob20/basic-assertion.json", true); public final static Sample SIMPLE_ASSERTION_INVALID_CONTEXT_JSON = new Sample("ob20/basic-assertion-invalid-context.json", true); + public final static Sample SIMPLE_ASSERTION_INVALID_TYPE_JSON = new Sample("ob20/basic-assertion-invalid-type.json", true); // original: test_graph: test_verify_with_redirection public final static Sample WARNING_REDIRECTION_ASSERTION_JSON = new Sample("ob20/warning-with-redirection.json", true); // original: test_validation: test_can_input_badgeclass diff --git a/inspector-vc/src/test/resources/ob20/basic-assertion-invalid-type.json b/inspector-vc/src/test/resources/ob20/basic-assertion-invalid-type.json new file mode 100644 index 0000000..527cd67 --- /dev/null +++ b/inspector-vc/src/test/resources/ob20/basic-assertion-invalid-type.json @@ -0,0 +1,18 @@ +{ + "@context": "https://w3id.org/openbadges/v2", + "type": "OtherAssertion", + "id": "https://example.org/beths-robotics-badge.json", + "recipient": { + "type": "email", + "hashed": true, + "salt": "deadsea", + "identity": "sha256$ecf5409f3f4b91ab60cc5ef4c02aef7032354375e70cf4d8e43f6a1d29891942" + }, + "image": "https://example.org/beths-robot-badge.png", + "evidence": "https://example.org/beths-robot-work.html", + "issuedOn": "2016-12-31T23:59:59Z", + "badge": "https://example.org/robotics-badge.json", + "verification": { + "type": "hosted" + } +} \ No newline at end of file