From af48747b571ac7caa2df4f2555a0824453c4f39a Mon Sep 17 00:00:00 2001 From: Xavi Aracil Date: Thu, 24 Nov 2022 12:01:05 +0100 Subject: [PATCH] Added a test OB20 inspector, with local redirections --- .../inspect/vc/util/TestOB20Inspector.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/TestOB20Inspector.java diff --git a/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/TestOB20Inspector.java b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/TestOB20Inspector.java new file mode 100644 index 0000000..e3c51cf --- /dev/null +++ b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/TestOB20Inspector.java @@ -0,0 +1,57 @@ +package org.oneedtech.inspect.vc.util; + +import java.net.URI; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.oneedtech.inspect.util.resource.ResourceType; +import org.oneedtech.inspect.util.spec.Specification; +import org.oneedtech.inspect.vc.Assertion; +import org.oneedtech.inspect.vc.OB20Inspector; +import org.oneedtech.inspect.vc.probe.JsonLDCompactionProve; + +/** + * OpenBadges 2.0 Test inspector. + * It's a subclass of main OB2.0 inspector, setting redirection of urls to local resources for testing + */ +public class TestOB20Inspector extends OB20Inspector { + protected final Map localDomains; + + protected TestOB20Inspector(TestBuilder builder) { + super(builder); + if (getBehavior(OB20Inspector.Behavior.ALLOW_LOCAL_REDIRECTION) == Boolean.TRUE) { + this.localDomains = builder.localDomains; + } else { + this.localDomains = Collections.emptyMap(); + } + } + + @Override + protected JsonLDCompactionProve getCompactionProbe(Assertion assertion) { + return new JsonLDCompactionProve(assertion.getContext().get(0), localDomains); + } + + public static class TestBuilder extends OB20Inspector.Builder { + final Map localDomains; + + public TestBuilder() { + super(); + // don't allow local redirections by default + super.behaviors.put(OB20Inspector.Behavior.ALLOW_LOCAL_REDIRECTION, true); + this.localDomains = new HashMap<>(); + } + + public TestBuilder add(URI localDomain, String resourcePath) { + localDomains.put(localDomain, resourcePath); + return this; + } + + @Override + public TestOB20Inspector build() { + set(Specification.OB20); + set(ResourceType.OPENBADGE); + return new TestOB20Inspector(this); + } + } +}