From c40e0610237985c39818eb9b5fb67da2dd30fb61 Mon Sep 17 00:00:00 2001 From: Markus Gylling Date: Wed, 6 Jul 2022 15:41:24 +0200 Subject: [PATCH] throw JsonLdError per spec --- .../inspect/vc/util/CachingDocumentLoader.java | 14 +++++++++----- .../vc/util/CachingDocumentLoaderTests.java | 18 ++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/util/CachingDocumentLoader.java b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/util/CachingDocumentLoader.java index 7032fe2..dab8845 100644 --- a/inspector-vc/src/main/java/org/oneedtech/inspect/vc/util/CachingDocumentLoader.java +++ b/inspector-vc/src/main/java/org/oneedtech/inspect/vc/util/CachingDocumentLoader.java @@ -7,10 +7,10 @@ import java.time.Duration; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.oneedtech.inspect.util.code.Closeables; import org.oneedtech.inspect.util.code.Tuple; import com.apicatalog.jsonld.JsonLdError; +import com.apicatalog.jsonld.JsonLdErrorCode; import com.apicatalog.jsonld.document.Document; import com.apicatalog.jsonld.document.JsonDocument; import com.apicatalog.jsonld.loader.DocumentLoader; @@ -34,11 +34,11 @@ public class CachingDocumentLoader implements DocumentLoader { return documentCache.get(tpl); } catch (Exception e) { logger.error("contextCache not able to load {}", url); - } - return null; + throw new JsonLdError(JsonLdErrorCode.INVALID_REMOTE_CONTEXT, e.getMessage()); + } } - private static final ImmutableMap bundled = ImmutableMap.builder() + static final ImmutableMap bundled = ImmutableMap.builder() .put("https://www.w3.org/ns/did/v1", Resources.getResource("contexts/did-v1.jsonld")) .put("https://www.w3.org/ns/odrl.jsonld", Resources.getResource("contexts/odrl.jsonld")) .put("https://w3id.org/security/suites/ed25519-2020/v1", Resources.getResource("contexts/security-suites-ed25519-2020-v1.jsonld")) @@ -46,7 +46,7 @@ public class CachingDocumentLoader implements DocumentLoader { .put("https://imsglobal.github.io/openbadges-specification/context.json", Resources.getResource("contexts/obv3.jsonld")) .build(); - private static final LoadingCache, Document> documentCache = CacheBuilder.newBuilder() + static final LoadingCache, Document> documentCache = CacheBuilder.newBuilder() .initialCapacity(32) .maximumSize(64) .expireAfterAccess(Duration.ofHours(24)) @@ -60,5 +60,9 @@ public class CachingDocumentLoader implements DocumentLoader { } }); + public static void reset() { + documentCache.invalidateAll(); + } + private static final Logger logger = LogManager.getLogger(); } diff --git a/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/CachingDocumentLoaderTests.java b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/CachingDocumentLoaderTests.java index 4710258..b56542f 100644 --- a/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/CachingDocumentLoaderTests.java +++ b/inspector-vc/src/test/java/org/oneedtech/inspect/vc/util/CachingDocumentLoaderTests.java @@ -8,17 +8,17 @@ import org.junit.jupiter.api.Test; import com.apicatalog.jsonld.document.Document; import com.apicatalog.jsonld.loader.DocumentLoader; import com.apicatalog.jsonld.loader.DocumentLoaderOptions; -import com.google.common.io.Resources; public class CachingDocumentLoaderTests { @Test - void testStaticCachedDocumentURI() { + void testStaticCachedDocumentBundled() { Assertions.assertDoesNotThrow(()->{ - DocumentLoader loader = new CachingDocumentLoader(); - URI uri = Resources.getResource("contexts/did-v1.jsonld").toURI(); - Document doc = loader.loadDocument(uri, new DocumentLoaderOptions()); - Assertions.assertNotNull(doc); + DocumentLoader loader = new CachingDocumentLoader(); + for(String id : CachingDocumentLoader.bundled.keySet()) { + Document doc = loader.loadDocument(new URI(id), new DocumentLoaderOptions()); + Assertions.assertNotNull(doc); + } }); } @@ -30,7 +30,5 @@ public class CachingDocumentLoaderTests { Document doc = loader.loadDocument(uri, new DocumentLoaderOptions()); Assertions.assertNotNull(doc); }); - } - - -} + } +} \ No newline at end of file