Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: register additional JSON-LD files via config #3317

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ maven/mavencentral/io.netty/netty-tcnative-boringssl-static/2.0.56.Final, Apache
maven/mavencentral/io.netty/netty-tcnative-classes/2.0.56.Final, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.netty/netty-transport-native-unix-common/4.1.86.Final, Apache-2.0 AND BSD-3-Clause AND MIT, approved, CQ20926
maven/mavencentral/io.netty/netty-transport/4.1.86.Final, Apache-2.0 AND BSD-3-Clause AND MIT, approved, CQ20926
maven/mavencentral/io.opentelemetry.instrumentation/opentelemetry-instrumentation-annotations/1.28.0, , restricted, clearlydefined
maven/mavencentral/io.opentelemetry.instrumentation/opentelemetry-instrumentation-annotations/1.28.0, Apache-2.0, approved, #9662
maven/mavencentral/io.opentelemetry.proto/opentelemetry-proto/0.20.0-alpha, Apache-2.0, approved, #9274
maven/mavencentral/io.opentelemetry/opentelemetry-api/1.28.0, , restricted, clearlydefined
maven/mavencentral/io.opentelemetry/opentelemetry-context/1.28.0, , restricted, clearlydefined
maven/mavencentral/io.opentelemetry/opentelemetry-api/1.28.0, Apache-2.0, approved, #9661
maven/mavencentral/io.opentelemetry/opentelemetry-context/1.28.0, Apache-2.0, approved, #9663
maven/mavencentral/io.prometheus/simpleclient/0.16.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.prometheus/simpleclient_common/0.16.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.prometheus/simpleclient_httpserver/0.16.0, Apache-2.0, approved, clearlydefined
Expand Down
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"));

}
}