Skip to content

Commit

Permalink
fix: asset json-ld handling
Browse files Browse the repository at this point in the history
  • Loading branch information
richardtreier committed Sep 19, 2023
1 parent fb7c92e commit 5e51ebc
Show file tree
Hide file tree
Showing 20 changed files with 366 additions and 360 deletions.
2 changes: 1 addition & 1 deletion extensions/wrapper/client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ dependencies {
testImplementation("${jettyGroup}:jetty-util:${jettyVersion}")
testImplementation("${jettyGroup}:jetty-webapp:${jettyVersion}")

testImplementation("${edcGroup}:json-ld-spi:${edcVersion}")
testImplementation("${edcGroup}:json-ld:${edcVersion}")
testImplementation("${edcGroup}:dsp-http-spi:${edcVersion}")
testImplementation("${edcGroup}:dsp-api-configuration:${edcVersion}")
testImplementation(project(":extensions:wrapper:wrapper"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import de.sovity.edc.client.gen.model.UiAsset;
import de.sovity.edc.client.gen.model.UiAssetCreateRequest;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyMapperUtils;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.FailedMappingException;
import de.sovity.edc.utils.jsonld.vocab.Prop;
import lombok.SneakyThrows;
import org.eclipse.edc.connector.spi.asset.AssetService;
Expand Down Expand Up @@ -92,7 +93,11 @@ void testAssetCreation(AssetService assetService) {
var client = TestUtils.edcClient();
var dataAddressProperties = Map.of(
Prop.Edc.TYPE, "HttpData",
Prop.Edc.BASE_URL, DATA_SINK
Prop.Edc.BASE_URL, DATA_SINK,
Prop.Edc.PROXY_METHOD, "true",
Prop.Edc.PROXY_PATH, "true",
Prop.Edc.PROXY_QUERY_PARAMS, "true",
Prop.Edc.PROXY_BODY, "true"
);
var uiAssetRequest = UiAssetCreateRequest.builder()
.id("asset-1")
Expand All @@ -118,9 +123,11 @@ void testAssetCreation(AssetService assetService) {

// assert
assertThat(response.getId()).isEqualTo("asset-1");

var assets = client.uiApi().assetPage().getAssets();
assertThat(assets).hasSize(1);
var asset = assets.get(0);
assertThat(asset.getAssetId()).isEqualTo("asset-1");
assertThat(asset.getName()).isEqualTo("AssetName");
assertThat(asset.getDescription()).isEqualTo("AssetDescription");
assertThat(asset.getVersion()).isEqualTo("1.0.0");
Expand All @@ -135,11 +142,68 @@ void testAssetCreation(AssetService assetService) {
assertThat(asset.getKeywords()).isEqualTo(List.of("keyword1", "keyword2"));
assertThat(asset.getCreatorOrganizationName()).isEqualTo("creatorOrganizationName");
assertThat(asset.getPublisherHomepage()).isEqualTo("publisherHomepage");
assertThat(asset.getHttpDatasourceHintsProxyMethod()).isTrue();
assertThat(asset.getHttpDatasourceHintsProxyPath()).isTrue();
assertThat(asset.getHttpDatasourceHintsProxyQueryParams()).isTrue();
assertThat(asset.getHttpDatasourceHintsProxyBody()).isTrue();

var assetWithDataAddress = assetService.query(QuerySpec.max()).getContent().toList().get(0);
var assetWithDataAddress = assetService.query(QuerySpec.max()).orElseThrow(FailedMappingException::ofFailure).toList().get(0);
assertThat(assetWithDataAddress.getDataAddress().getProperties()).isEqualTo(dataAddressProperties);
}

@Test
void testAssetCreation_noProxying() {
// arrange
var client = TestUtils.edcClient();
var dataAddressProperties = Map.of(
Prop.Edc.TYPE, "HttpData",
Prop.Edc.BASE_URL, DATA_SINK
);
var uiAssetRequest = UiAssetCreateRequest.builder()
.id("asset-1")
.dataAddressProperties(dataAddressProperties)
.build();

// act
var response = client.uiApi().createAsset(uiAssetRequest);

// assert
assertThat(response.getId()).isEqualTo("asset-1");
var assets = client.uiApi().assetPage().getAssets();
assertThat(assets).hasSize(1);
var asset = assets.get(0);
assertThat(asset.getHttpDatasourceHintsProxyMethod()).isFalse();
assertThat(asset.getHttpDatasourceHintsProxyPath()).isFalse();
assertThat(asset.getHttpDatasourceHintsProxyQueryParams()).isFalse();
assertThat(asset.getHttpDatasourceHintsProxyBody()).isFalse();
}

@Test
void testAssetCreation_differentDataAddressType() {
// arrange
var client = TestUtils.edcClient();
var dataAddressProperties = Map.of(
Prop.Edc.TYPE, "Unknown"
);
var uiAssetRequest = UiAssetCreateRequest.builder()
.id("asset-1")
.dataAddressProperties(dataAddressProperties)
.build();

// act
var response = client.uiApi().createAsset(uiAssetRequest);

// assert
assertThat(response.getId()).isEqualTo("asset-1");
var assets = client.uiApi().assetPage().getAssets();
assertThat(assets).hasSize(1);
var asset = assets.get(0);
assertThat(asset.getHttpDatasourceHintsProxyMethod()).isNull();
assertThat(asset.getHttpDatasourceHintsProxyPath()).isNull();
assertThat(asset.getHttpDatasourceHintsProxyQueryParams()).isNull();
assertThat(asset.getHttpDatasourceHintsProxyBody()).isNull();
}

@Test
void testDeleteAsset(AssetService assetService) {
// arrange
Expand All @@ -160,10 +224,9 @@ private void createAsset(
String date,
Map<String, String> properties
) {

DataAddress dataAddress = DataAddress.Builder.newInstance()
.type("HttpData")
.property("baseUrl", DATA_SINK)
.property(Prop.Edc.BASE_URL, DATA_SINK)
.build();

var asset = Asset.Builder.newInstance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public static Map<String, String> createConfiguration(

public static void setupExtension(EdcExtension extension) {
extension.registerServiceMock(ProtocolWebhook.class, mock(ProtocolWebhook.class));
extension.registerServiceMock(JsonLd.class, mock(JsonLd.class));
extension.setConfiguration(createConfiguration(Map.of()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,18 @@ public class UiAsset {
@Schema(description = "Transport Mode", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private String transportMode;

@Schema(description = "Asset additional Properties", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@Schema(description = "Unhandled Asset Properties (that were strings)", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private Map<String, String> additionalProperties;

@Schema(description = "Asset Private Properties", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private Map<String, Object> privateProperties;

@Schema(description = "Asset Json Properties", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
@Schema(description = "Unhandled Asset Properties (that were not strings but other JSON values)", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private Map<String, String> additionalJsonProperties;

@Schema(description = "Private Asset Properties (that were strings)", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private Map<String, String> privateProperties;

@Schema(description = "Private Asset Properties (that were not strings but other JSON values)", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private Map<String, String> privateJsonProperties;

@Schema(description = "Contains the entire asset in its original expanded JSON-LD format", requiredMode = Schema.RequiredMode.NOT_REQUIRED)
private String assetJsonLd;
}
1 change: 1 addition & 0 deletions extensions/wrapper/wrapper-common-mappers/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {

testAnnotationProcessor("org.projectlombok:lombok:${lombokVersion}")
testCompileOnly("org.projectlombok:lombok:${lombokVersion}")
testImplementation("${edcGroup}:json-ld:${edcVersion}")
testImplementation("org.mockito:mockito-core:${mockitoVersion}")
testImplementation("org.mockito:mockito-inline:${mockitoVersion}")
testImplementation("org.mockito:mockito-junit-jupiter:${mockitoVersion}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,60 @@
package de.sovity.edc.ext.wrapper.api.common.mappers;

import de.sovity.edc.ext.wrapper.api.common.mappers.utils.UiAssetBuilder;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.FailedMappingException;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.UiAssetMapper;
import de.sovity.edc.ext.wrapper.api.common.model.UiAsset;
import de.sovity.edc.ext.wrapper.api.common.model.UiAssetCreateRequest;
import de.sovity.edc.utils.jsonld.JsonLdUtils;
import de.sovity.edc.utils.jsonld.vocab.Prop;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import lombok.RequiredArgsConstructor;
import org.eclipse.edc.jsonld.spi.JsonLd;
import org.eclipse.edc.spi.types.domain.asset.Asset;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;

import java.util.Map;
import java.util.Optional;


@RequiredArgsConstructor
public class AssetMapper {
private final TypeTransformerRegistry typeTransformerRegistry;
private final UiAssetBuilder uiAssetBuilder;
private final UiAssetMapper uiAssetMapper;
private final JsonLd jsonLd;

public UiAsset buildUiAsset(Asset asset) {
var jsonLd = buildAssetJsonLd(asset);
return buildUiAsset(jsonLd);
var assetJsonLd = buildAssetJsonLd(asset);
return buildUiAsset(assetJsonLd);
}

public UiAsset buildUiAsset(JsonObject assetJsonLd) {
return uiAssetBuilder.buildUiAsset(assetJsonLd);
return uiAssetMapper.buildUiAsset(assetJsonLd);
}

public Asset buildAsset(UiAssetCreateRequest createRequest) {
var assetJsonLd = uiAssetBuilder.buildAssetJsonLd(createRequest);
return buildAsset(assetJsonLd, createRequest.getCreatorOrganizationName(), createRequest.getPublisherHomepage());
var assetJsonLd = uiAssetMapper.buildAssetJsonLd(createRequest);
return buildAsset(assetJsonLd);
}

public Asset buildAssetFromDatasetProperties(JsonObject json) {
return typeTransformerRegistry.transform(buildJsonLdFromDatasetProperties(json), Asset.class).getContent();
return buildAsset(buildAssetJsonLdFromDatasetProperties(json));
}

private JsonObject buildJsonLdFromDatasetProperties(JsonObject json) {
public Asset buildAsset(JsonObject assetJsonLd) {
var expanded = jsonLd.expand(assetJsonLd)
.orElseThrow(FailedMappingException::ofFailure);
return typeTransformerRegistry.transform(expanded, Asset.class)
.orElseThrow(FailedMappingException::ofFailure);
}

private JsonObject buildAssetJsonLd(Asset asset) {
var assetJsonLd = typeTransformerRegistry.transform(asset, JsonObject.class)
.orElseThrow(FailedMappingException::ofFailure);
return jsonLd.expand(assetJsonLd)
.orElseThrow(FailedMappingException::ofFailure);
}

private JsonObject buildAssetJsonLdFromDatasetProperties(JsonObject json) {
// Try to use the EDC Prop ID, but if it's not available, fall back to the "@id" property
var assetId = Optional.ofNullable(JsonLdUtils.string(json, Prop.Edc.ID))
.orElseGet(() -> JsonLdUtils.string(json, Prop.ID));
Expand All @@ -48,29 +64,4 @@ private JsonObject buildJsonLdFromDatasetProperties(JsonObject json) {
.add(Prop.Edc.PROPERTIES, json)
.build();
}

private JsonObject buildAssetJsonLd(Asset asset) {
return typeTransformerRegistry.transform(asset, JsonObject.class).getContent();
}

/**
* Builds an Asset from the provided assetJsonLd with additional properties for creator and publisher.
*
* @param assetJsonLd the base JsonObject to build the Asset from
* @param creator the creator to add as a property to the Asset (can be null)
* @param publisher the publisher to add as a property to the Asset (can be null)
* @return the built Asset with the added properties
*/
private Asset buildAsset(JsonObject assetJsonLd, String creator, String publisher) {
var asset = typeTransformerRegistry.transform(assetJsonLd, Asset.class).getContent();
var assetBuilder = asset.toBuilder();

if (creator != null) {
assetBuilder.property(Prop.Dcterms.CREATOR, Map.of(Prop.TYPE, Prop.Foaf.ORGANIZATION, Prop.Foaf.NAME, creator));
}
if (publisher != null) {
assetBuilder.property(Prop.Dcterms.PUBLISHER, Map.of(Prop.TYPE, Prop.Foaf.ORGANIZATION, Prop.Foaf.HOMEPAGE, publisher));
}
return assetBuilder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.sovity.edc.ext.wrapper.api.common.mappers.utils.AtomicConstraintMapper;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.ConstraintExtractor;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.FailedMappingException;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.MappingErrors;
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.PolicyValidator;
import de.sovity.edc.ext.wrapper.api.common.model.UiPolicyCreateRequest;
Expand Down Expand Up @@ -80,7 +81,8 @@ public Policy buildPolicy(UiPolicyCreateRequest policyCreateDto) {
* @return {@link Policy}
*/
public Policy buildPolicy(JsonObject policyJsonLd) {
return typeTransformerRegistry.transform(policyJsonLd, Policy.class).getContent();
return typeTransformerRegistry.transform(policyJsonLd, Policy.class)
.orElseThrow(FailedMappingException::ofFailure);
}

/**
Expand All @@ -104,6 +106,7 @@ public Policy buildPolicy(String policyJsonLd) {
* @return policy JSON-LD
*/
public JsonObject buildPolicyJsonLd(Policy policy) {
return typeTransformerRegistry.transform(policy, JsonObject.class).getContent();
return typeTransformerRegistry.transform(policy, JsonObject.class)
.orElseThrow(FailedMappingException::ofFailure);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 5e51ebc

Please sign in to comment.