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

fix: document cache usage + test #4236

Merged
merged 3 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@ -28,6 +28,7 @@
import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.verify.VerificationTimes;

import java.net.URI;

Expand Down Expand Up @@ -285,6 +286,26 @@ void documentResolution_shouldCallHttpEndpoint_whenContextIsNotRegistered_andHtt
.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()));

AbstractResultAssert.assertThat(service.expand(jsonObject)).isSucceeded();
wolf4ood marked this conversation as resolved.
Show resolved Hide resolved
AbstractResultAssert.assertThat(service.expand(jsonObject)).isSucceeded();

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

}

private JsonLd httpEnabledService() {
Expand Down
Loading