Skip to content

Commit

Permalink
feat: register additional JSON-LD files via config (#3317)
Browse files Browse the repository at this point in the history
* feat: register additional JSON-LD files via config

* use partition

* DEPENDENCIES

* cleanup
  • Loading branch information
paullatzelsperger authored Jul 21, 2023
1 parent 42cc36d commit 5e8aaa0
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<URI> getResourceUri(String name) {
var uri = getClass().getClassLoader().getResource(name);
Expand All @@ -113,4 +131,5 @@ private Result<URI> getResourceUri(String name) {
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<String, URI> 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<String, URI> 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<String, URI> 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<String, URI> 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<String, URI> 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"));

}
}

0 comments on commit 5e8aaa0

Please sign in to comment.