From a629fc83723515474869852cbdf2bc35c7bc68f2 Mon Sep 17 00:00:00 2001 From: Xavi Aracil Date: Thu, 24 Nov 2022 13:51:04 +0100 Subject: [PATCH] Redirect warning in compaction --- .../vc/probe/JsonLDCompactionProve.java | 37 ++++++++----------- .../org/oneedtech/inspect/vc/OB20Tests.java | 27 +++++++++++++- 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/JsonLDCompactionProve.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/JsonLDCompactionProve.java index 771fc82..3cabea1 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/JsonLDCompactionProve.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/probe/JsonLDCompactionProve.java @@ -34,29 +34,22 @@ public class JsonLDCompactionProve extends Probe { @Override public ReportItems run(Credential crd, RunContext ctx) throws Exception { - try { - // compact JSON - JsonDocument jsonDocument = JsonDocument.of(new StringReader(crd.getJson().toString())); - CompactionApi compactApi = JsonLd.compact(jsonDocument, context); - compactApi.options(new JsonLdOptions(new CachingDocumentLoader(localDomains))); - JsonObject compactedObject = compactApi.get(); + try { + // compact JSON + JsonDocument jsonDocument = JsonDocument.of(new StringReader(crd.getJson().toString())); + CompactionApi compactApi = JsonLd.compact(jsonDocument, context); + compactApi.options(new JsonLdOptions(new CachingDocumentLoader(localDomains))); + JsonObject compactedObject = compactApi.get(); - // TODO: Handle mismatch between URL node source and declared ID. - if (compactedObject.get("id") != null && crd.getResource().getID() != null - && !compactedObject.get("id").toString().equals(crd.getResource().getID())) { - - // TODO: warning!! - // report_message( - // "Node fetched from source {} declared its id as {}".format( - // task_meta['node_id'], node_id), MESSAGE_LEVEL_WARNING, success=False - // ), - - } - return success(this, ctx); - - } catch (Exception e) { - return fatal("Error while parsing credential: " + e.getMessage(), ctx); - } + // Handle mismatch between URL node source and declared ID. + if (compactedObject.get("id") != null && crd.getResource().getID() != null + && !compactedObject.get("id").toString().equals(crd.getResource().getID())) { + return warning("Node fetched from source " + crd.getResource().getID() + " declared its id as " + compactedObject.get("id").toString(), ctx); + } + return success(this, ctx); + } catch (Exception e) { + return fatal("Error while parsing credential: " + e.getMessage(), ctx); + } } public static final String ID = JsonLDCompactionProve.class.getSimpleName(); 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 8e1df53..bfbc0e9 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 @@ -2,11 +2,13 @@ package org.oneedtech.inspect.vc; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.oneedtech.inspect.test.Assertions.assertValid; +import static org.oneedtech.inspect.test.Assertions.assertWarning; import java.net.URI; import java.net.URISyntaxException; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.oneedtech.inspect.core.Inspector.Behavior; import org.oneedtech.inspect.core.report.Report; @@ -22,7 +24,7 @@ public class OB20Tests { validator = new TestBuilder() .add(new URI("https://www.example.org/"), "ob20/assets") .set(Behavior.TEST_INCLUDE_SUCCESS, true) - .set(Behavior.TEST_INCLUDE_WARNINGS, true) + .set(Behavior.TEST_INCLUDE_WARNINGS, false) .set(Behavior.VALIDATOR_FAIL_FAST, true) .set(OB20Inspector.Behavior.ALLOW_LOCAL_REDIRECTION, true) .build(); @@ -36,4 +38,27 @@ public class OB20Tests { assertValid(report); }); } + + @Nested + static class WarningTests { + @BeforeAll + static void setup() throws URISyntaxException { + validator = new TestBuilder() + .add(new URI("https://www.example.org/"), "ob20/assets") + .set(Behavior.TEST_INCLUDE_SUCCESS, true) + .set(Behavior.TEST_INCLUDE_WARNINGS, true) + .set(Behavior.VALIDATOR_FAIL_FAST, true) + .set(OB20Inspector.Behavior.ALLOW_LOCAL_REDIRECTION, true) + .build(); + } + + @Test + void testWarningRedirectionJsonValid() { + assertDoesNotThrow(()->{ + Report report = validator.run(Samples.OB20.JSON.WARNING_REDIRECTION_ASSERTION_JSON.asFileResource()); + if(verbose) PrintHelper.print(report, true); + assertWarning(report); + }); + } + } }