From 9645842633c99d2b9665e639488db991434ddb9a Mon Sep 17 00:00:00 2001 From: Christophe Loiseau <116@lab0.net> Date: Fri, 16 Feb 2024 15:23:32 +0100 Subject: [PATCH] feat: stable contract offer id in DspCatalogService (#795) --- .../catalog/mapper/DspDataOfferBuilder.java | 6 ++++- .../utils/catalog/DspCatalogServiceTest.java | 2 +- .../mapper/DspContractOfferUtilsTest.java | 24 +++++++++++++++++++ .../edc/utils/catalog/catalogResponse.json | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 utils/catalog-parser/src/test/java/de/sovity/edc/utils/catalog/mapper/DspContractOfferUtilsTest.java diff --git a/utils/catalog-parser/src/main/java/de/sovity/edc/utils/catalog/mapper/DspDataOfferBuilder.java b/utils/catalog-parser/src/main/java/de/sovity/edc/utils/catalog/mapper/DspDataOfferBuilder.java index 119d9d102..f5ef31736 100644 --- a/utils/catalog-parser/src/main/java/de/sovity/edc/utils/catalog/mapper/DspDataOfferBuilder.java +++ b/utils/catalog-parser/src/main/java/de/sovity/edc/utils/catalog/mapper/DspDataOfferBuilder.java @@ -22,6 +22,7 @@ import jakarta.json.Json; import jakarta.json.JsonObject; import lombok.RequiredArgsConstructor; +import lombok.val; import org.eclipse.edc.jsonld.spi.JsonLd; import org.jetbrains.annotations.NotNull; @@ -66,6 +67,9 @@ private DspDataOffer buildDataOffer(JsonObject dataset) { @NotNull private DspContractOffer buildContractOffer(JsonObject json) { - return new DspContractOffer(JsonLdUtils.id(json), json); + val stableId = DspContractOfferUtils.buildStableId(json); + val withStableId = Json.createObjectBuilder(json).remove(Prop.ID).add(Prop.ID, stableId).build(); + + return new DspContractOffer(stableId, withStableId); } } diff --git a/utils/catalog-parser/src/test/java/de/sovity/edc/utils/catalog/DspCatalogServiceTest.java b/utils/catalog-parser/src/test/java/de/sovity/edc/utils/catalog/DspCatalogServiceTest.java index 88fa35cb2..4a34cb5a1 100644 --- a/utils/catalog-parser/src/test/java/de/sovity/edc/utils/catalog/DspCatalogServiceTest.java +++ b/utils/catalog-parser/src/test/java/de/sovity/edc/utils/catalog/DspCatalogServiceTest.java @@ -72,7 +72,7 @@ void testCatalogMapping() { assertThat(offer.getContractOffers()).hasSize(1); var co = offer.getContractOffers().get(0); - assertThat(co.getContractOfferId()).isEqualTo("policy-1"); + assertThat(co.getContractOfferId()).isEqualTo("contract-id:asset-id:ODJjMDNjMmM4YzQ5NmE0OTg4ZjVjOTY4OGU2MmMyN2UxYjIxZDAwZQ=="); assertThat(toJson(co.getPolicyJsonLd())).contains("ALWAYS_TRUE"); assertThat(offer.getDistributions()).hasSize(1); diff --git a/utils/catalog-parser/src/test/java/de/sovity/edc/utils/catalog/mapper/DspContractOfferUtilsTest.java b/utils/catalog-parser/src/test/java/de/sovity/edc/utils/catalog/mapper/DspContractOfferUtilsTest.java new file mode 100644 index 000000000..21ceeed8d --- /dev/null +++ b/utils/catalog-parser/src/test/java/de/sovity/edc/utils/catalog/mapper/DspContractOfferUtilsTest.java @@ -0,0 +1,24 @@ +package de.sovity.edc.utils.catalog.mapper; + +import jakarta.json.Json; +import lombok.val; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +class DspContractOfferUtilsTest { + @Test + void testCanConvertTheRandomIdToStableId() { + // arrange + val contractOffer = Json.createObjectBuilder() + .add("@id", "part1:part2:part3") + .add("somefield", "somevalue") + .build(); + + // act + val result = DspContractOfferUtils.buildStableId(contractOffer); + + // assert + assertThat(result).isEqualTo("part1:part2:MjliNzcwMjdjMzA2YzE3ZGYyZWNhZjY2OGI3OTYxY2U5OTY1YmExNw=="); + } +} diff --git a/utils/catalog-parser/src/test/resources/de/sovity/edc/utils/catalog/catalogResponse.json b/utils/catalog-parser/src/test/resources/de/sovity/edc/utils/catalog/catalogResponse.json index d069f7302..613b7ac49 100644 --- a/utils/catalog-parser/src/test/resources/de/sovity/edc/utils/catalog/catalogResponse.json +++ b/utils/catalog-parser/src/test/resources/de/sovity/edc/utils/catalog/catalogResponse.json @@ -5,7 +5,7 @@ "@id": "test-1.0", "@type": "dcat:Dataset", "odrl:hasPolicy": { - "@id": "policy-1", + "@id": "contract-id:asset-id:policy-id", "@type": "odrl:Set", "odrl:permission": { "odrl:target": "test-1.0",