Skip to content

Commit

Permalink
fix(dsp catalog dispatcher): compact catalog request (eclipse-edc#2756)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronjaquensel authored Apr 19, 2023
1 parent e7a092b commit 65479dc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ plugins {

dependencies {
api(project(":data-protocols:dsp:dsp-catalog:dsp-catalog-spi"))
api(project(":data-protocols:dsp:dsp-catalog:dsp-catalog-transform"))
api(project(":data-protocols:dsp:dsp-http-core"))
api(project(":data-protocols:dsp:dsp-http-spi"))
api(project(":extensions:common:json-ld"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import okhttp3.MediaType;
import okhttp3.Request;
Expand All @@ -30,9 +31,14 @@
import java.io.IOException;
import java.util.function.Function;

import static java.lang.String.format;
import static java.lang.String.join;
import static org.eclipse.edc.jsonld.util.JsonLdUtil.compact;
import static org.eclipse.edc.jsonld.util.JsonLdUtil.expand;
import static org.eclipse.edc.protocol.dsp.catalog.spi.CatalogApiPaths.BASE_PATH;
import static org.eclipse.edc.protocol.dsp.catalog.spi.CatalogApiPaths.CATALOG_REQUEST;
import static org.eclipse.edc.protocol.dsp.catalog.transform.DspCatalogPropertyAndTypeNames.DSPACE_PREFIX;
import static org.eclipse.edc.protocol.dsp.catalog.transform.DspCatalogPropertyAndTypeNames.DSPACE_SCHEMA;

/**
* Delegate for dispatching catalog requests as defined in the dataspace protocol specification.
Expand Down Expand Up @@ -90,7 +96,7 @@ public Function<Response, Catalog> parseResponse() {
if (result.succeeded()) {
return result.getContent();
} else {
throw new EdcException("Failed to read response body.");
throw new EdcException(format("Failed to read response body: %s", join(", ", result.getFailureMessages())));
}
} catch (NullPointerException e) {
throw new EdcException("Failed to read response body, as body was null.");
Expand All @@ -106,11 +112,18 @@ private String toJson(org.eclipse.edc.catalog.spi.protocol.CatalogRequestMessage
try {
var transformResult = transformerRegistry.transform(message, JsonObject.class);
if (transformResult.succeeded()) {
return mapper.writeValueAsString(transformResult.getContent());
var compacted = compact(transformResult.getContent(), jsonLdContext());
return mapper.writeValueAsString(compacted);
}
throw new EdcException("Failed to write request.");
throw new EdcException(format("Failed to write request: %s", join(", ", transformResult.getFailureMessages())));
} catch (JsonProcessingException e) {
throw new EdcException("Failed to serialize catalog request", e);
}
}

private JsonObject jsonLdContext() {
return Json.createObjectBuilder()
.add(DSPACE_PREFIX, DSPACE_SCHEMA)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import static org.eclipse.edc.jsonld.JsonLdKeywords.CONTEXT;
import static org.eclipse.edc.protocol.dsp.catalog.spi.CatalogApiPaths.BASE_PATH;
import static org.eclipse.edc.protocol.dsp.catalog.spi.CatalogApiPaths.CATALOG_REQUEST;
import static org.eclipse.edc.protocol.dsp.catalog.transform.DspCatalogPropertyAndTypeNames.DSPACE_PREFIX;
import static org.eclipse.edc.protocol.dsp.catalog.transform.DspCatalogPropertyAndTypeNames.DSPACE_SCHEMA;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -65,12 +67,14 @@ void getMessageType_returnCatalogRequest() {

@Test
void buildRequest_returnRequest() throws IOException {
var jsonObject = getJsonObject();
var jsonObject = Json.createObjectBuilder()
.add(DSPACE_SCHEMA + "key", "value")
.build();
var serializedBody = "catalog request";

when(registry.transform(isA(org.eclipse.edc.catalog.spi.protocol.CatalogRequestMessage.class), eq(JsonObject.class)))
.thenReturn(Result.success(jsonObject));
when(mapper.writeValueAsString(jsonObject)).thenReturn(serializedBody);
when(mapper.writeValueAsString(any(JsonObject.class))).thenReturn(serializedBody);

var message = getCatalogRequest();
var httpRequest = delegate.buildRequest(message);
Expand All @@ -80,7 +84,8 @@ void buildRequest_returnRequest() throws IOException {

verify(registry, times(1))
.transform(argThat(requestMessage -> ((org.eclipse.edc.catalog.spi.protocol.CatalogRequestMessage) requestMessage).getFilter().equals(message.getQuerySpec())), eq(JsonObject.class));
verify(mapper, times(1)).writeValueAsString(jsonObject);
verify(mapper, times(1))
.writeValueAsString(argThat(json -> ((JsonObject) json).get(CONTEXT) != null && ((JsonObject) json).get(DSPACE_PREFIX + ":key") != null));
}

@Test
Expand Down

0 comments on commit 65479dc

Please sign in to comment.