Skip to content

Commit

Permalink
fix: document cache usage + test (#4236)
Browse files Browse the repository at this point in the history
* chore: fixes document cache usage + test

* chore: pr suggestions

* chore: deps file
  • Loading branch information
wolf4ood authored Jun 5, 2024
1 parent 564d627 commit f3e0712
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ maven/mavencentral/com.lmax/disruptor/3.4.4, Apache-2.0, approved, clearlydefine
maven/mavencentral/com.networknt/json-schema-validator/1.0.76, Apache-2.0, approved, CQ22638
maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.28, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.39.3, Apache-2.0, approved, #14830
maven/mavencentral/com.puppycrawl.tools/checkstyle/10.17.0, , restricted, clearlydefined
maven/mavencentral/com.puppycrawl.tools/checkstyle/10.17.0, LGPL-2.1-or-later AND (Apache-2.0 AND LGPL-2.1-or-later) AND Apache-2.0, approved, #15077
maven/mavencentral/com.samskivert/jmustache/1.15, BSD-2-Clause, approved, clearlydefined
maven/mavencentral/com.squareup.okhttp3/okhttp-dnsoverhttps/4.12.0, Apache-2.0, approved, #11159
maven/mavencentral/com.squareup.okhttp3/okhttp/4.12.0, Apache-2.0, approved, #11156
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,12 @@ public Document loadDocument(URI url, DocumentLoaderOptions options) throws Json
.map(uriCache::get)
.orElse(url);

return Optional.ofNullable(documentCache.get(uri))
.orElse(loader.loadDocument(uri, options));
var document = documentCache.get(uri);
if (document != null) {
return document;
} else {
return loader.loadDocument(uri, options);
}
}

public void register(String contextUrl, URI uri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.assertj.core.api.Assertions;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.jsonld.spi.JsonLdKeywords;
import org.eclipse.edc.junit.assertions.AbstractResultAssert;
import org.eclipse.edc.junit.testfixtures.TestUtils;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.util.io.Ports;
Expand All @@ -28,11 +27,13 @@
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.verify.VerificationTimes;

import java.net.URI;

import static jakarta.json.Json.createArrayBuilder;
import static jakarta.json.Json.createObjectBuilder;
import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat;
import static org.mockito.Mockito.mock;

class TitaniumJsonLdTest {
Expand All @@ -58,7 +59,7 @@ void expand() {

var expanded = defaultService().expand(jsonObject);

AbstractResultAssert.assertThat(expanded).isSucceeded().extracting(Object::toString).asString()
assertThat(expanded).isSucceeded().extracting(Object::toString).asString()
.contains("test:item")
.contains("test:key1")
.contains("@value\":\"value1\"")
Expand All @@ -72,7 +73,7 @@ void expand_shouldFail_whenPropertiesWithoutNamespaceAndContextIsMissing() {

var expanded = defaultService().expand(emptyJson);

AbstractResultAssert.assertThat(expanded).isFailed();
assertThat(expanded).isFailed();
}

@Test
Expand All @@ -88,7 +89,7 @@ void expand_withCustomContext() {

var expanded = defaultService().expand(jsonObject);

AbstractResultAssert.assertThat(expanded).isSucceeded().extracting(Object::toString).asString()
assertThat(expanded).isSucceeded().extracting(Object::toString).asString()
.contains("test:item")
.contains("test:key1")
.contains("@value\":\"value1\"")
Expand All @@ -108,7 +109,7 @@ void compact() {

var compacted = defaultService().compact(expanded);

AbstractResultAssert.assertThat(compacted).isSucceeded().satisfies(c -> {
assertThat(compacted).isSucceeded().satisfies(c -> {
Assertions.assertThat(c.getJsonObject(ns + "item")).isNotNull();
Assertions.assertThat(c.getJsonObject(ns + "item").getJsonString(ns + "key1").getString()).isEqualTo("value1");
Assertions.assertThat(c.getJsonObject(ns + "item").getJsonString(ns + "key2").getString()).isEqualTo("value2");
Expand All @@ -130,7 +131,7 @@ void compact_withCustomPrefix() {
service.registerNamespace(prefix, ns);
var compacted = service.compact(expanded);

AbstractResultAssert.assertThat(compacted).isSucceeded().satisfies(c -> {
assertThat(compacted).isSucceeded().satisfies(c -> {
Assertions.assertThat(c.getJsonObject(prefix + ":item")).isNotNull();
Assertions.assertThat(c.getJsonObject(prefix + ":item").getJsonString(prefix + ":key1").getString()).isEqualTo("value1");
Assertions.assertThat(c.getJsonObject(prefix + ":item").getJsonString(prefix + ":key2").getString()).isEqualTo("value2");
Expand All @@ -153,14 +154,14 @@ void expandAndCompact_withCustomContext() {

var expanded = service.expand(input);

AbstractResultAssert.assertThat(expanded).isSucceeded().satisfies(c -> {
assertThat(expanded).isSucceeded().satisfies(c -> {
Assertions.assertThat(c.getJsonArray(context + "name").get(0).asJsonObject().getJsonString(JsonLdKeywords.VALUE).getString()).isEqualTo("Jane Doe");
Assertions.assertThat(c.getJsonArray(context + "jobTitle").get(0).asJsonObject().getJsonString(JsonLdKeywords.VALUE).getString()).isEqualTo("Professor");
});

var compacted = service.compact(expanded.getContent());

AbstractResultAssert.assertThat(compacted).isSucceeded().satisfies(c -> {
assertThat(compacted).isSucceeded().satisfies(c -> {
Assertions.assertThat(c).isEqualTo(input);
});
}
Expand All @@ -185,14 +186,14 @@ void expandAndCompact_withCustomContextAndNameClash() {

var expanded = service.expand(input);

AbstractResultAssert.assertThat(expanded).isSucceeded().satisfies(c -> {
assertThat(expanded).isSucceeded().satisfies(c -> {
Assertions.assertThat(c.getJsonArray(testSchemaContext + "name").get(0).asJsonObject().getJsonString(JsonLdKeywords.VALUE).getString()).isEqualTo("Jane Doe");
Assertions.assertThat(c.getJsonArray(schemaContext + "jobTitle").get(0).asJsonObject().getJsonString(JsonLdKeywords.VALUE).getString()).isEqualTo("Professor");
});

var compacted = service.compact(expanded.getContent());

AbstractResultAssert.assertThat(compacted).isSucceeded().satisfies(c -> {
assertThat(compacted).isSucceeded().satisfies(c -> {
Assertions.assertThat(c).isEqualTo(input);
});
}
Expand All @@ -218,14 +219,14 @@ void expandAndCompact_withCustomContextAndCustomScope() {

var expanded = service.expand(input);

AbstractResultAssert.assertThat(expanded).isSucceeded().satisfies(c -> {
assertThat(expanded).isSucceeded().satisfies(c -> {
Assertions.assertThat(c.getJsonArray(testSchemaContext + "name").get(0).asJsonObject().getJsonString(JsonLdKeywords.VALUE).getString()).isEqualTo("Jane Doe");
Assertions.assertThat(c.getJsonArray(schemaContext + "jobTitle").get(0).asJsonObject().getJsonString(JsonLdKeywords.VALUE).getString()).isEqualTo("Professor");
});

var compacted = service.compact(expanded.getContent(), customScope);

AbstractResultAssert.assertThat(compacted).isSucceeded().satisfies(c -> {
assertThat(compacted).isSucceeded().satisfies(c -> {
Assertions.assertThat(c).isEqualTo(input);
});
}
Expand All @@ -243,7 +244,7 @@ void documentResolution_shouldNotCallHttpEndpoint_whenFileContextIsRegistered()
var expanded = service.expand(jsonObject);

server.verifyZeroInteractions();
AbstractResultAssert.assertThat(expanded).isSucceeded().satisfies(json -> {
assertThat(expanded).isSucceeded().satisfies(json -> {
Assertions.assertThat(json.getJsonArray("http://test.org/context/key")).hasSize(1).first()
.extracting(JsonValue::asJsonObject)
.extracting(it -> it.getString(JsonLdKeywords.VALUE))
Expand All @@ -263,7 +264,7 @@ void documentResolution_shouldFailByDefault_whenContextIsNotRegisteredAndHttpIsN

var expanded = service.expand(jsonObject);

AbstractResultAssert.assertThat(expanded).isFailed();
assertThat(expanded).isFailed();
}

@Test
Expand All @@ -279,12 +280,32 @@ void documentResolution_shouldCallHttpEndpoint_whenContextIsNotRegistered_andHtt

var expanded = service.expand(jsonObject);

AbstractResultAssert.assertThat(expanded).isSucceeded().satisfies(json -> {
assertThat(expanded).isSucceeded().satisfies(json -> {
Assertions.assertThat(json.getJsonArray("http://test.org/context/key")).hasSize(1).first()
.extracting(JsonValue::asJsonObject)
.extracting(it -> it.getString(JsonLdKeywords.VALUE))
.isEqualTo("value");
});

}


@Test
void documentResolution_shouldCallHttpEndpointOnlyOnce_whenContextIsNotRegistered_andHttpIsEnabled() {
server.when(HttpRequest.request()).respond(HttpResponse.response(TestUtils.getResourceFileContentAsString("test-context.jsonld")));
var contextUrl = "http://localhost:" + port;
var jsonObject = createObjectBuilder()
.add(JsonLdKeywords.CONTEXT, contextUrl)
.add("test:key", "value")
.build();
var service = httpEnabledService();
service.registerCachedDocument("http//any.other/url", URI.create("http://localhost:" + server.getLocalPort()));

assertThat(service.expand(jsonObject)).isSucceeded();
assertThat(service.expand(jsonObject)).isSucceeded();

server.verify(HttpRequest.request().withMethod("GET"), VerificationTimes.exactly(1));

}

private JsonLd httpEnabledService() {
Expand Down

0 comments on commit f3e0712

Please sign in to comment.