Skip to content

Commit

Permalink
chore: fixes document cache usage + test
Browse files Browse the repository at this point in the history
  • Loading branch information
wolf4ood committed Jun 5, 2024
1 parent 564d627 commit 7e399f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
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();
AbstractResultAssert.assertThat(service.expand(jsonObject)).isSucceeded();

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

}

private JsonLd httpEnabledService() {
Expand Down

0 comments on commit 7e399f3

Please sign in to comment.