From 5e8aaa01d20d27ed785afced23da9e1233e8c0cf Mon Sep 17 00:00:00 2001 From: Paul Latzelsperger <43503240+paullatzelsperger@users.noreply.github.com> Date: Fri, 21 Jul 2023 11:05:40 +0200 Subject: [PATCH] feat: register additional JSON-LD files via config (#3317) * feat: register additional JSON-LD files via config * use partition * DEPENDENCIES * cleanup --- .../eclipse/edc/jsonld/JsonLdExtension.java | 21 +++- .../edc/jsonld/JsonLdExtensionTest.java | 104 ++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) diff --git a/extensions/common/json-ld/src/main/java/org/eclipse/edc/jsonld/JsonLdExtension.java b/extensions/common/json-ld/src/main/java/org/eclipse/edc/jsonld/JsonLdExtension.java index c9d6416a11e..0b72f093d2e 100644 --- a/extensions/common/json-ld/src/main/java/org/eclipse/edc/jsonld/JsonLdExtension.java +++ b/extensions/common/json-ld/src/main/java/org/eclipse/edc/jsonld/JsonLdExtension.java @@ -57,13 +57,15 @@ public class JsonLdExtension implements ServiceExtension { public static final String NAME = "JSON-LD Extension"; + public static final String EDC_JSONLD_DOCUMENT_PREFIX = "edc.jsonld.document"; + public static final String CONFIG_VALUE_PATH = "path"; + public static final String CONFIG_VALUE_URL = "url"; private static final boolean DEFAULT_HTTP_HTTPS_RESOLUTION = false; @Setting(value = "If set enable http json-ld document resolution", type = "boolean", defaultValue = DEFAULT_HTTP_HTTPS_RESOLUTION + "") private static final String HTTP_ENABLE_SETTING = "edc.jsonld.http.enabled"; @Setting(value = "If set enable https json-ld document resolution", type = "boolean", defaultValue = DEFAULT_HTTP_HTTPS_RESOLUTION + "") private static final String HTTPS_ENABLE_SETTING = "edc.jsonld.https.enabled"; - @Inject private TypeManager typeManager; @@ -96,9 +98,25 @@ public JsonLd createJsonLdService(ServiceExtensionContext context) { .onSuccess(uri -> service.registerCachedDocument("http://www.w3.org/ns/odrl.jsonld", uri)) .onFailure(failure -> monitor.warning("Failed to register cached json-ld document: " + failure.getFailureDetail())); + registerCachedDocumentsFromConfig(context, service); + return service; } + private void registerCachedDocumentsFromConfig(ServiceExtensionContext context, TitaniumJsonLd service) { + context.getConfig() + .getConfig(EDC_JSONLD_DOCUMENT_PREFIX) + .partition() + .forEach(config -> { + var tuple = config.getRelativeEntries(); + if (tuple.containsKey(CONFIG_VALUE_PATH) && tuple.containsKey(CONFIG_VALUE_URL)) { + service.registerCachedDocument(tuple.get(CONFIG_VALUE_URL), new File(tuple.get(CONFIG_VALUE_PATH)).toURI()); + } else { + context.getMonitor().warning(format("Expected a '%s' and a '%s' entry for '%s.%s', but found only '%s'", CONFIG_VALUE_PATH, CONFIG_VALUE_URL, EDC_JSONLD_DOCUMENT_PREFIX, config.currentNode(), String.join("", tuple.keySet()))); + } + }); + } + @NotNull private Result getResourceUri(String name) { var uri = getClass().getClassLoader().getResource(name); @@ -113,4 +131,5 @@ private Result getResourceUri(String name) { } } + } diff --git a/extensions/common/json-ld/src/test/java/org/eclipse/edc/jsonld/JsonLdExtensionTest.java b/extensions/common/json-ld/src/test/java/org/eclipse/edc/jsonld/JsonLdExtensionTest.java index dbc4901aa69..6a8c790a59b 100644 --- a/extensions/common/json-ld/src/test/java/org/eclipse/edc/jsonld/JsonLdExtensionTest.java +++ b/extensions/common/json-ld/src/test/java/org/eclipse/edc/jsonld/JsonLdExtensionTest.java @@ -14,13 +14,23 @@ package org.eclipse.edc.jsonld; +import com.apicatalog.jsonld.loader.DocumentLoader; import org.eclipse.edc.junit.extensions.DependencyInjectionExtension; import org.eclipse.edc.spi.system.ServiceExtensionContext; +import org.eclipse.edc.spi.system.configuration.Config; +import org.eclipse.edc.spi.system.configuration.ConfigFactory; import org.eclipse.edc.spi.system.injection.ObjectFactory; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Map; + import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.edc.util.reflection.ReflectionUtil.getFieldValue; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; @ExtendWith(DependencyInjectionExtension.class) class JsonLdExtensionTest { @@ -34,4 +44,98 @@ void createService(ServiceExtensionContext context, ObjectFactory factory) { assertThat(service).isNotNull(); } + @Test + void verifyCachedDocsFromConfig_oneValidEntry(ServiceExtensionContext context, ObjectFactory factory) throws URISyntaxException { + Config config = ConfigFactory.fromMap(Map.of( + "edc.jsonld.document.foo.url", "http://foo.org/doc.json", + "edc.jsonld.document.foo.path", "/tmp/foo/doc.json") + ); + context = spy(context); + when(context.getConfig()).thenReturn(config); + var extension = factory.constructInstance(JsonLdExtension.class); + var service = (TitaniumJsonLd) extension.createJsonLdService(context); + + DocumentLoader documentLoader = getFieldValue("documentLoader", service); + Map cache = getFieldValue("cache", documentLoader); + + assertThat(cache).containsEntry("http://foo.org/doc.json", new URI("file:/tmp/foo/doc.json")); + } + + @Test + void verifyCachedDocsFromConfig_oneValidEntry_withSuperfluous(ServiceExtensionContext context, ObjectFactory factory) throws URISyntaxException { + Config config = ConfigFactory.fromMap(Map.of( + "edc.jsonld.document.foo.url", "http://foo.org/doc.json", + "edc.jsonld.document.foo.invalid", "should be ignored", + "edc.jsonld.document.foo.path", "/tmp/foo/doc.json") + ); + context = spy(context); + when(context.getConfig()).thenReturn(config); + var extension = factory.constructInstance(JsonLdExtension.class); + var service = (TitaniumJsonLd) extension.createJsonLdService(context); + + DocumentLoader documentLoader = getFieldValue("documentLoader", service); + Map cache = getFieldValue("cache", documentLoader); + + assertThat(cache).containsEntry("http://foo.org/doc.json", new URI("file:/tmp/foo/doc.json")); + } + + @Test + void verifyCachedDocsFromConfig_multipleValidEntries(ServiceExtensionContext context, ObjectFactory factory) throws URISyntaxException { + Config config = ConfigFactory.fromMap(Map.of( + "edc.jsonld.document.foo.url", "http://foo.org/doc.json", + "edc.jsonld.document.foo.path", "/tmp/foo/doc.json", + "edc.jsonld.document.bar.url", "http://bar.org/doc.json", + "edc.jsonld.document.bar.path", "/tmp/bar/doc.json" + )); + context = spy(context); + when(context.getConfig()).thenReturn(config); + var extension = factory.constructInstance(JsonLdExtension.class); + var service = (TitaniumJsonLd) extension.createJsonLdService(context); + + DocumentLoader documentLoader = getFieldValue("documentLoader", service); + Map cache = getFieldValue("cache", documentLoader); + + assertThat(cache).containsEntry("http://foo.org/doc.json", new URI("file:/tmp/foo/doc.json")); + assertThat(cache).containsEntry("http://bar.org/doc.json", new URI("file:/tmp/bar/doc.json")); + } + + @Test + void verifyCachedDocsFromConfig_multipleEntries_oneIncomplete(ServiceExtensionContext context, ObjectFactory factory) throws URISyntaxException { + Config config = ConfigFactory.fromMap(Map.of( + "edc.jsonld.document.foo.url", "http://foo.org/doc.json", + "edc.jsonld.document.foo.path", "/tmp/foo/doc.json", + "edc.jsonld.document.bar.url", "http://bar.org/doc.json" + )); + context = spy(context); + when(context.getConfig()).thenReturn(config); + var extension = factory.constructInstance(JsonLdExtension.class); + var service = (TitaniumJsonLd) extension.createJsonLdService(context); + + DocumentLoader documentLoader = getFieldValue("documentLoader", service); + Map cache = getFieldValue("cache", documentLoader); + + assertThat(cache).containsEntry("http://foo.org/doc.json", new URI("file:/tmp/foo/doc.json")) + .noneSatisfy((s, uri) -> assertThat(s).isEqualTo("http://bar.org/doc.json")); + + } + + @Test + void verifyCachedDocsFromConfig_multipleEntries_oneInvalid(ServiceExtensionContext context, ObjectFactory factory) throws URISyntaxException { + Config config = ConfigFactory.fromMap(Map.of( + "edc.jsonld.document.foo.url", "http://foo.org/doc.json", + "edc.jsonld.document.foo.path", "/tmp/foo/doc.json", + "edc.jsonld.document.bar.invalid", "http://bar.org/doc.json" + )); + context = spy(context); + when(context.getConfig()).thenReturn(config); + var extension = factory.constructInstance(JsonLdExtension.class); + var service = (TitaniumJsonLd) extension.createJsonLdService(context); + + DocumentLoader documentLoader = getFieldValue("documentLoader", service); + Map cache = getFieldValue("cache", documentLoader); + + assertThat(cache).containsEntry("http://foo.org/doc.json", new URI("file:/tmp/foo/doc.json")) + .noneSatisfy((s, uri) -> assertThat(s).isEqualTo("http://bar.org/doc.json")); + + } }