From 7ddba8e7733d84a4e76a50c4597375f677bd8c50 Mon Sep 17 00:00:00 2001 From: "Sascha Isele (ZF Friedrichshafen AG)" <127207440+saschaisele-zf@users.noreply.github.com> Date: Mon, 18 Mar 2024 15:20:47 +0100 Subject: [PATCH] feature(api): Update NegotiateEdrRequestDto (#1135) * feature(api): Update NegotiateEdrRequestDto Signed-off-by: Sascha Isele * feature(api): adds `LogDeprecatedValue` to `NegotiateEdrRequestDtoValidator` Signed-off-by: Sascha Isele --------- Signed-off-by: Sascha Isele --- .../tractusx/edc/api/edr/EdrApiExtension.java | 4 +- .../api/edr/dto/NegotiateEdrRequestDto.java | 15 ++++++ .../edc/api/edr/schema/EdrSchema.java | 28 ++++++++++ ...ctToNegotiateEdrRequestDtoTransformer.java | 6 +++ ...tDtoToNegotiatedEdrRequestTransformer.java | 20 +++---- .../NegotiateEdrRequestDtoValidator.java | 47 +++++++++++++--- .../tractusx/edc/api/edr/EdrApiTest.java | 24 ++++++++- .../tractusx/edc/api/edr/TestFunctions.java | 7 +++ ...NegotiateEdrRequestDtoTransformerTest.java | 54 ++++++++++++++++++- ...oToNegotiateEdrRequestTransformerTest.java | 32 +++++++++-- .../NegotiateEdrRequestDtoValidatorTest.java | 25 +++++++-- 11 files changed, 234 insertions(+), 28 deletions(-) diff --git a/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/EdrApiExtension.java b/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/EdrApiExtension.java index 2eb9cdd9b..d4127a778 100644 --- a/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/EdrApiExtension.java +++ b/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/EdrApiExtension.java @@ -20,6 +20,7 @@ package org.eclipse.tractusx.edc.api.edr; import org.eclipse.edc.connector.api.management.configuration.ManagementApiConfiguration; +import org.eclipse.edc.connector.api.management.contractnegotiation.transform.JsonObjectToContractOfferTransformer; import org.eclipse.edc.jsonld.spi.JsonLd; import org.eclipse.edc.runtime.metamodel.annotation.Inject; import org.eclipse.edc.spi.monitor.Monitor; @@ -67,9 +68,10 @@ public void initialize(ServiceExtensionContext context) { var mgmtApiTransformerRegistry = transformerRegistry.forContext("management-api"); mgmtApiTransformerRegistry.register(new NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer()); mgmtApiTransformerRegistry.register(new JsonObjectToNegotiateEdrRequestDtoTransformer()); + mgmtApiTransformerRegistry.register(new JsonObjectToContractOfferTransformer()); mgmtApiTransformerRegistry.register(new JsonObjectFromEndpointDataReferenceEntryTransformer()); mgmtApiTransformerRegistry.register(new EndpointDataReferenceToDataAddressTransformer()); - validatorRegistry.register(EDR_REQUEST_DTO_TYPE, NegotiateEdrRequestDtoValidator.instance()); + validatorRegistry.register(EDR_REQUEST_DTO_TYPE, NegotiateEdrRequestDtoValidator.instance(context.getMonitor())); webService.registerResource(apiConfig.getContextAlias(), new EdrController(edrService, mgmtApiTransformerRegistry, validatorRegistry, monitor)); } } diff --git a/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/dto/NegotiateEdrRequestDto.java b/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/dto/NegotiateEdrRequestDto.java index cfce992ba..36032948c 100644 --- a/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/dto/NegotiateEdrRequestDto.java +++ b/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/dto/NegotiateEdrRequestDto.java @@ -21,6 +21,7 @@ import org.eclipse.edc.connector.api.management.contractnegotiation.model.ContractOfferDescription; import org.eclipse.edc.spi.types.domain.callback.CallbackAddress; +import org.eclipse.edc.spi.types.domain.offer.ContractOffer; import java.util.ArrayList; import java.util.List; @@ -37,6 +38,7 @@ public class NegotiateEdrRequestDto { public static final String EDR_REQUEST_DTO_COUNTERPARTY_ID = EDC_NAMESPACE + "counterPartyId"; public static final String EDR_REQUEST_DTO_PROVIDER_ID = EDC_NAMESPACE + "providerId"; public static final String EDR_REQUEST_DTO_OFFER = EDC_NAMESPACE + "offer"; + public static final String EDR_REQUEST_DTO_POLICY = EDC_NAMESPACE + "policy"; public static final String EDR_REQUEST_DTO_CALLBACK_ADDRESSES = EDC_NAMESPACE + "callbackAddresses"; private String counterPartyAddress; @@ -45,7 +47,9 @@ public class NegotiateEdrRequestDto { private String providerId; + @Deprecated(since = "0.6.1") private ContractOfferDescription offer; + private ContractOffer contractOffer; private List callbackAddresses = new ArrayList<>(); private NegotiateEdrRequestDto() { @@ -72,10 +76,15 @@ public List getCallbackAddresses() { return callbackAddresses; } + @Deprecated(since = "0.6.1") public ContractOfferDescription getOffer() { return offer; } + public ContractOffer getContractOffer() { + return contractOffer; + } + public static final class Builder { private final NegotiateEdrRequestDto dto; @@ -98,11 +107,17 @@ public Builder counterPartyId(String connectorId) { return this; } + @Deprecated(since = "0.6.1") public Builder offer(ContractOfferDescription offer) { dto.offer = offer; return this; } + public Builder contractOffer(ContractOffer contractOffer) { + dto.contractOffer = contractOffer; + return this; + } + public Builder providerId(String providerId) { dto.providerId = providerId; return this; diff --git a/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/schema/EdrSchema.java b/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/schema/EdrSchema.java index c7dd3fdcb..aaf158007 100644 --- a/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/schema/EdrSchema.java +++ b/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/schema/EdrSchema.java @@ -43,10 +43,38 @@ public record NegotiateEdrRequestSchema( @Schema(deprecated = true, description = "please use providerId instead") String connectorId, String providerId, + @Deprecated(since = "0.6.1") + @Schema(deprecated = true, description = "please use policy instead") ContractNegotiationApi.ContractOfferDescriptionSchema offer, + ManagementApiSchema.PolicySchema policy, List callbackAddresses) { public static final String NEGOTIATE_EDR_REQUEST_EXAMPLE = """ + { + "@context": { "edc": "https://w3id.org/edc/v0.0.1/ns/" }, + "@type": "NegotiateEdrRequestDto", + "counterPartyAddress": "http://provider-address", + "protocol": "dataspace-protocol-http", + "providerId": "provider-id", + "policy": { + "@context": "http://www.w3.org/ns/odrl.jsonld", + "@type": "Set", + "@id": "policy-id", + "target": "asset-id", + "permission": [{ + "action": "display" + }] + }, + "callbackAddresses": [{ + "transactional": false, + "uri": "http://callback/url", + "events": ["contract.negotiation", "transfer.process"] + }] + } + """; + + @Deprecated(since = "0.6.1") + public static final String NEGOTIATE_EDR_REQUEST_DEPRECATED_EXAMPLE = """ { "@context": { "edc": "https://w3id.org/edc/v0.0.1/ns/" }, "@type": "NegotiateEdrRequestDto", diff --git a/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/transform/JsonObjectToNegotiateEdrRequestDtoTransformer.java b/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/transform/JsonObjectToNegotiateEdrRequestDtoTransformer.java index 78ee54cbf..2ab415eeb 100644 --- a/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/transform/JsonObjectToNegotiateEdrRequestDtoTransformer.java +++ b/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/transform/JsonObjectToNegotiateEdrRequestDtoTransformer.java @@ -24,6 +24,7 @@ import org.eclipse.edc.connector.api.management.contractnegotiation.model.ContractOfferDescription; import org.eclipse.edc.jsonld.spi.transformer.AbstractJsonLdTransformer; import org.eclipse.edc.spi.types.domain.callback.CallbackAddress; +import org.eclipse.edc.spi.types.domain.offer.ContractOffer; import org.eclipse.edc.transform.spi.TransformerContext; import org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto; import org.jetbrains.annotations.NotNull; @@ -35,6 +36,7 @@ import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS; import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ID; import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_OFFER; +import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_POLICY; import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROTOCOL; import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROVIDER_ID; import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE; @@ -76,6 +78,9 @@ private void setProperties(String key, JsonValue value, NegotiateEdrRequestDto.B case EDR_REQUEST_DTO_OFFER: transformArrayOrObject(value, ContractOfferDescription.class, builder::offer, context); break; + case EDR_REQUEST_DTO_POLICY: + transformArrayOrObject(value, ContractOffer.class, builder::contractOffer, context); + break; default: context.problem() .unexpectedType() @@ -88,6 +93,7 @@ private void setProperties(String key, JsonValue value, NegotiateEdrRequestDto.B .expected(EDR_REQUEST_DTO_PROVIDER_ID) .expected(EDR_REQUEST_DTO_CALLBACK_ADDRESSES) .expected(EDR_REQUEST_DTO_OFFER) + .expected(EDR_REQUEST_DTO_POLICY) .report(); break; } diff --git a/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/transform/NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer.java b/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/transform/NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer.java index 46bf3e022..fbed87095 100644 --- a/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/transform/NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer.java +++ b/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/transform/NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer.java @@ -41,11 +41,17 @@ public Class getOutputType() { @Override public @Nullable NegotiateEdrRequest transform(@NotNull NegotiateEdrRequestDto object, @NotNull TransformerContext context) { - var contractOffer = ContractOffer.Builder.newInstance() - .id(object.getOffer().getOfferId()) - .assetId(object.getOffer().getAssetId()) - .policy(object.getOffer().getPolicy()) - .build(); + ContractOffer contractOffer = null; + + if (object.getContractOffer() != null) { + contractOffer = object.getContractOffer(); + } else if (object.getOffer() != null) { + contractOffer = ContractOffer.Builder.newInstance() + .id(object.getOffer().getOfferId()) + .assetId(object.getOffer().getAssetId()) + .policy(object.getOffer().getPolicy()) + .build(); + } return NegotiateEdrRequest.Builder.newInstance() .connectorId(object.getCounterPartyId()) @@ -56,8 +62,4 @@ public Class getOutputType() { .build(); } - private String getId(String value, String defaultValue) { - return value != null ? value : defaultValue; - } - } diff --git a/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/validation/NegotiateEdrRequestDtoValidator.java b/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/validation/NegotiateEdrRequestDtoValidator.java index 2650e2ba3..75fa1960d 100644 --- a/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/validation/NegotiateEdrRequestDtoValidator.java +++ b/edc-extensions/edr/edr-api/src/main/java/org/eclipse/tractusx/edc/api/edr/validation/NegotiateEdrRequestDtoValidator.java @@ -20,17 +20,22 @@ package org.eclipse.tractusx.edc.api.edr.validation; import jakarta.json.JsonObject; +import org.eclipse.edc.spi.monitor.Monitor; +import org.eclipse.edc.validator.jsonobject.JsonLdPath; import org.eclipse.edc.validator.jsonobject.JsonObjectValidator; +import org.eclipse.edc.validator.jsonobject.validators.LogDeprecatedValue; import org.eclipse.edc.validator.jsonobject.validators.MandatoryObject; import org.eclipse.edc.validator.jsonobject.validators.MandatoryValue; +import org.eclipse.edc.validator.spi.ValidationResult; import org.eclipse.edc.validator.spi.Validator; import static org.eclipse.edc.connector.api.management.contractnegotiation.model.ContractOfferDescription.ASSET_ID; import static org.eclipse.edc.connector.api.management.contractnegotiation.model.ContractOfferDescription.OFFER_ID; -import static org.eclipse.edc.connector.api.management.contractnegotiation.model.ContractOfferDescription.POLICY; import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS; import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_OFFER; +import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_POLICY; import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROTOCOL; +import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE; public class NegotiateEdrRequestDtoValidator { @@ -38,16 +43,42 @@ public class NegotiateEdrRequestDtoValidator { private NegotiateEdrRequestDtoValidator() { } - public static Validator instance() { + public static Validator instance(Monitor monitor) { return JsonObjectValidator.newValidator() .verify(EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS, MandatoryValue::new) .verify(EDR_REQUEST_DTO_PROTOCOL, MandatoryValue::new) - .verify(EDR_REQUEST_DTO_OFFER, MandatoryObject::new) - .verifyObject(EDR_REQUEST_DTO_OFFER, v -> v - .verify(OFFER_ID, MandatoryValue::new) - .verify(ASSET_ID, MandatoryValue::new) - .verify(POLICY, MandatoryObject::new) - ) + .verify(EDR_REQUEST_DTO_OFFER, path -> new LogDeprecatedValue(path, EDR_REQUEST_DTO_TYPE, EDR_REQUEST_DTO_POLICY, monitor)) + .verify(MandatoryOfferOrPolicy::new) .build(); } + + private record MandatoryOfferOrPolicy(JsonLdPath path) implements Validator { + @Override + public ValidationResult validate(JsonObject input) { + if (input.containsKey(EDR_REQUEST_DTO_OFFER)) { + return new OfferValidator(path.append(EDR_REQUEST_DTO_OFFER)).validate(input); + } + return new EdrPolicyValidator(path).validate(input); + } + } + + private record OfferValidator(JsonLdPath path) implements Validator { + @Override + public ValidationResult validate(JsonObject input) { + return JsonObjectValidator.newValidator() + .verifyObject(EDR_REQUEST_DTO_OFFER, v -> v + .verify(OFFER_ID, MandatoryValue::new) + .verify(ASSET_ID, MandatoryValue::new) + .verify(EDR_REQUEST_DTO_POLICY, MandatoryObject::new) + ).build().validate(input); + } + } + + private record EdrPolicyValidator(JsonLdPath path) implements Validator { + @Override + public ValidationResult validate(JsonObject input) { + return JsonObjectValidator.newValidator() + .verify(EDR_REQUEST_DTO_POLICY, MandatoryObject::new).build().validate(input); + } + } } diff --git a/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/EdrApiTest.java b/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/EdrApiTest.java index 6649d669a..ebbd44f5a 100644 --- a/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/EdrApiTest.java +++ b/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/EdrApiTest.java @@ -24,6 +24,7 @@ import jakarta.json.JsonObject; import org.eclipse.edc.api.transformer.JsonObjectToCallbackAddressTransformer; import org.eclipse.edc.connector.api.management.contractnegotiation.transform.JsonObjectToContractOfferDescriptionTransformer; +import org.eclipse.edc.connector.api.management.contractnegotiation.transform.JsonObjectToContractOfferTransformer; import org.eclipse.edc.connector.api.management.contractnegotiation.transform.JsonObjectToContractRequestTransformer; import org.eclipse.edc.core.transform.TypeTransformerRegistryImpl; import org.eclipse.edc.core.transform.transformer.odrl.OdrlTransformersFactory; @@ -43,6 +44,7 @@ import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VALUE; import static org.eclipse.edc.junit.extensions.TestServiceExtensionContext.testServiceExtensionContext; import static org.eclipse.tractusx.edc.api.edr.schema.EdrSchema.EndpointDataReferenceEntrySchema.ENDPOINT_DATA_REFERENCE_ENTRY_EXAMPLE; +import static org.eclipse.tractusx.edc.api.edr.schema.EdrSchema.NegotiateEdrRequestSchema.NEGOTIATE_EDR_REQUEST_DEPRECATED_EXAMPLE; import static org.eclipse.tractusx.edc.api.edr.schema.EdrSchema.NegotiateEdrRequestSchema.NEGOTIATE_EDR_REQUEST_EXAMPLE; import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_AGREEMENT_ID; import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_ASSET_ID; @@ -66,6 +68,7 @@ public class EdrApiTest { void setUp() { transformer.register(new JsonObjectToContractRequestTransformer()); transformer.register(new JsonObjectToContractOfferDescriptionTransformer()); + transformer.register(new JsonObjectToContractOfferTransformer()); transformer.register(new JsonObjectToCallbackAddressTransformer()); transformer.register(new JsonObjectToNegotiateEdrRequestDtoTransformer()); var mapper = mock(ParticipantIdMapper.class); @@ -77,11 +80,30 @@ void setUp() { @Test void edrRequestExample() throws JsonProcessingException { - var validator = NegotiateEdrRequestDtoValidator.instance(); + var validator = NegotiateEdrRequestDtoValidator.instance(testServiceExtensionContext().getMonitor()); var jsonObject = objectMapper.readValue(NEGOTIATE_EDR_REQUEST_EXAMPLE, JsonObject.class); assertThat(jsonObject).isNotNull(); + var expanded = jsonLd.expand(jsonObject); + AbstractResultAssert.assertThat(expanded).isSucceeded() + .satisfies(exp -> AbstractResultAssert.assertThat(validator.validate(exp)).isSucceeded()) + .extracting(e -> transformer.transform(e, NegotiateEdrRequestDto.class)) + .satisfies(transformResult -> AbstractResultAssert.assertThat(transformResult).isSucceeded() + .satisfies(transformed -> { + assertThat(transformed.getContractOffer()).isNotNull(); + assertThat(transformed.getCallbackAddresses()).asList().hasSize(1); + assertThat(transformed.getProviderId()).isNotBlank(); + })); + } + + @Test + void edrDeprecatedRequestExample() throws JsonProcessingException { + var validator = NegotiateEdrRequestDtoValidator.instance(testServiceExtensionContext().getMonitor()); + + var jsonObject = objectMapper.readValue(NEGOTIATE_EDR_REQUEST_DEPRECATED_EXAMPLE, JsonObject.class); + assertThat(jsonObject).isNotNull(); + var expanded = jsonLd.expand(jsonObject); AbstractResultAssert.assertThat(expanded).isSucceeded() .satisfies(exp -> AbstractResultAssert.assertThat(validator.validate(exp)).isSucceeded()) diff --git a/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/TestFunctions.java b/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/TestFunctions.java index fc622b523..8b05acae7 100644 --- a/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/TestFunctions.java +++ b/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/TestFunctions.java @@ -58,6 +58,13 @@ public static ContractOfferDescription createOffer() { return createOffer(UUID.randomUUID().toString(), UUID.randomUUID().toString()); } + public static ContractOffer createContractOffer() { + return ContractOffer.Builder.newInstance() + .id("offerId") + .assetId("assetId") + .policy(Policy.Builder.newInstance().build()).build(); + } + public static JsonObject negotiationRequest() { return Json.createObjectBuilder() .add(TYPE, NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE) diff --git a/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/transform/JsonObjectToNegotiateEdrRequestDtoTransformerTest.java b/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/transform/JsonObjectToNegotiateEdrRequestDtoTransformerTest.java index 5d493cd38..459423c9d 100644 --- a/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/transform/JsonObjectToNegotiateEdrRequestDtoTransformerTest.java +++ b/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/transform/JsonObjectToNegotiateEdrRequestDtoTransformerTest.java @@ -26,8 +26,10 @@ import org.eclipse.edc.connector.api.management.contractnegotiation.model.ContractOfferDescription; import org.eclipse.edc.jsonld.TitaniumJsonLd; import org.eclipse.edc.jsonld.spi.JsonLd; +import org.eclipse.edc.policy.model.Policy; import org.eclipse.edc.spi.monitor.Monitor; import org.eclipse.edc.spi.types.domain.callback.CallbackAddress; +import org.eclipse.edc.spi.types.domain.offer.ContractOffer; import org.eclipse.edc.transform.spi.ProblemBuilder; import org.eclipse.edc.transform.spi.TransformerContext; import org.junit.jupiter.api.BeforeEach; @@ -44,11 +46,13 @@ import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.OFFER; import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.PROTOCOL; import static org.eclipse.edc.connector.contract.spi.types.negotiation.ContractRequest.PROVIDER_ID; +import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID; import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_OBLIGATION_ATTRIBUTE; import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PERMISSION_ATTRIBUTE; import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_TYPE_SET; import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_PROHIBITION_ATTRIBUTE; +import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_TARGET_ATTRIBUTE; import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE; import static org.eclipse.edc.spi.types.domain.callback.CallbackAddress.EVENTS; import static org.eclipse.edc.spi.types.domain.callback.CallbackAddress.IS_TRANSACTIONAL; @@ -76,6 +80,46 @@ void setUp() { @Test void transform() { + var jsonObject = Json.createObjectBuilder() + .add(TYPE, CONTRACT_REQUEST_TYPE) + .add(EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS, "test-address") + .add(EDR_REQUEST_DTO_COUNTERPARTY_ID, "test-conn-id") + .add(PROTOCOL, "test-protocol") + .add(PROVIDER_ID, "test-provider-id") + .add(CALLBACK_ADDRESSES, createCallbackAddress()) + .add(POLICY, createPolicyJson()) + .build(); + + when(context.transform(any(JsonValue.class), eq(ContractOffer.class))).thenReturn(ContractOffer.Builder.newInstance() + .id("test-offer-id") + .assetId("test-asset-id") + .policy(createPolicyObject()) + .build()); + + when(context.transform(any(JsonObject.class), eq(CallbackAddress.class))).thenReturn(CallbackAddress.Builder.newInstance() + .uri("http://test.local") + .events(Set.of("foo", "bar")) + .transactional(true) + .build()); + when(context.transform(any(CallbackAddress.class), eq(CallbackAddress.class))).thenReturn(CallbackAddress.Builder.newInstance() + .uri("http://test.local") + .events(Set.of("foo", "bar")) + .transactional(true) + .build()); + var dto = transformer.transform(jsonLd.expand(jsonObject).getContent(), context); + + assertThat(dto).isNotNull(); + assertThat(dto.getCallbackAddresses()).isNotEmpty(); + assertThat(dto.getProtocol()).isEqualTo("test-protocol"); + assertThat(dto.getCounterPartyAddress()).isEqualTo("test-address"); + assertThat(dto.getCounterPartyId()).isEqualTo("test-conn-id"); + assertThat(dto.getProviderId()).isEqualTo("test-provider-id"); + assertThat(dto.getContractOffer()).isNotNull(); + + } + + @Test + void transformDeprecatedOffer() { var jsonObject = Json.createObjectBuilder() .add(TYPE, CONTRACT_REQUEST_TYPE) .add(EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS, "test-address") @@ -86,7 +130,7 @@ void transform() { .add(OFFER, Json.createObjectBuilder() .add(OFFER_ID, "test-offer-id") .add(ASSET_ID, "test-asset") - .add(POLICY, createPolicy()) + .add(POLICY, createPolicyJson()) .build()) .build(); @@ -138,18 +182,24 @@ private JsonArrayBuilder createCallbackAddress() { .add(EVENTS, Json.createArrayBuilder().build())); } - private JsonObject createPolicy() { + private JsonObject createPolicyJson() { var permissionJson = getJsonObject("permission"); var prohibitionJson = getJsonObject("prohibition"); var dutyJson = getJsonObject("duty"); return Json.createObjectBuilder() .add(TYPE, ODRL_POLICY_TYPE_SET) + .add(ID, "test-offer-id") .add(ODRL_PERMISSION_ATTRIBUTE, permissionJson) .add(ODRL_PROHIBITION_ATTRIBUTE, prohibitionJson) .add(ODRL_OBLIGATION_ATTRIBUTE, dutyJson) + .add(ODRL_TARGET_ATTRIBUTE, "test-asset-id") .build(); } + private Policy createPolicyObject() { + return Policy.Builder.newInstance().build(); + } + private JsonObject getJsonObject(String type) { return Json.createObjectBuilder() .add(TYPE, type) diff --git a/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/transform/NegotiateEdrRequestDtoToNegotiateEdrRequestTransformerTest.java b/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/transform/NegotiateEdrRequestDtoToNegotiateEdrRequestTransformerTest.java index ed7d31abb..e271eac72 100644 --- a/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/transform/NegotiateEdrRequestDtoToNegotiateEdrRequestTransformerTest.java +++ b/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/transform/NegotiateEdrRequestDtoToNegotiateEdrRequestTransformerTest.java @@ -27,6 +27,7 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.tractusx.edc.api.edr.TestFunctions.createContractOffer; import static org.eclipse.tractusx.edc.api.edr.TestFunctions.createOffer; import static org.mockito.Mockito.mock; @@ -44,6 +45,31 @@ void inputOutputType() { @Test void verify_transform() { + var callback = CallbackAddress.Builder.newInstance() + .uri("local://test") + .build(); + var dto = NegotiateEdrRequestDto.Builder.newInstance() + .counterPartyId("connectorId") + .connectorAddress("address") + .protocol("protocol") + .providerId("test-provider") + .contractOffer(createContractOffer()) + .callbackAddresses(List.of(callback)) + .build(); + + var request = transformer.transform(dto, context); + + assertThat(request).isNotNull(); + assertThat(request.getConnectorId()).isEqualTo("connectorId"); + assertThat(request.getConnectorAddress()).isEqualTo("address"); + assertThat(request.getProtocol()).isEqualTo("protocol"); + assertThat(request.getOffer().getId()).isEqualTo("offerId"); + assertThat(request.getOffer().getPolicy()).isNotNull(); + assertThat(request.getCallbackAddresses()).hasSize(1); + } + + @Test + void verify_transformDeprecatedOffer() { var callback = CallbackAddress.Builder.newInstance() .uri("local://test") .build(); @@ -68,13 +94,13 @@ void verify_transform() { } @Test - void verify_transfor_withNoProviderId() { + void verify_transform_withNoProviderId() { var dto = NegotiateEdrRequestDto.Builder.newInstance() .counterPartyId("connectorId") .connectorAddress("address") .protocol("protocol") // do not set provider ID - .offer(createOffer("offerId", "assetId")) + .contractOffer(createContractOffer()) .build(); var request = transformer.transform(dto, context); @@ -90,7 +116,7 @@ void verify_transform_withNoConsumerId() { .protocol("protocol") // do not set consumer ID .providerId("urn:connector:test-provider") - .offer(createOffer("offerId", "assetId")) + .contractOffer(createContractOffer()) .build(); var request = transformer.transform(dto, context); diff --git a/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/validation/NegotiateEdrRequestDtoValidatorTest.java b/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/validation/NegotiateEdrRequestDtoValidatorTest.java index 55f28eb91..c6f82df80 100644 --- a/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/validation/NegotiateEdrRequestDtoValidatorTest.java +++ b/edc-extensions/edr/edr-api/src/test/java/org/eclipse/tractusx/edc/api/edr/validation/NegotiateEdrRequestDtoValidatorTest.java @@ -22,6 +22,7 @@ import jakarta.json.Json; import jakarta.json.JsonArrayBuilder; import jakarta.json.JsonObject; +import org.eclipse.edc.spi.monitor.Monitor; import org.eclipse.edc.validator.spi.ValidationFailure; import org.eclipse.edc.validator.spi.Validator; import org.eclipse.edc.validator.spi.Violation; @@ -38,15 +39,31 @@ import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS; import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_OFFER; +import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_POLICY; import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROTOCOL; import static org.eclipse.tractusx.edc.api.edr.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROVIDER_ID; +import static org.mockito.Mockito.mock; public class NegotiateEdrRequestDtoValidatorTest { - private final Validator validator = NegotiateEdrRequestDtoValidator.instance(); + private final Validator validator = NegotiateEdrRequestDtoValidator.instance(mock(Monitor.class)); @Test - void shouldSuccess_whenObjectIsValid() { + void shouldSucceed_whenObjectIsValid() { + var input = Json.createObjectBuilder() + .add(EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS, value("http://connector-address")) + .add(EDR_REQUEST_DTO_PROTOCOL, value("protocol")) + .add(EDR_REQUEST_DTO_PROVIDER_ID, value("connector-id")) + .add(EDR_REQUEST_DTO_POLICY, createArrayBuilder().add(createObjectBuilder())) + .build(); + + var result = validator.validate(input); + + assertThat(result).isSucceeded(); + } + + @Test + void shouldSucceed_whenDeprecatedOfferIsUsed() { var input = Json.createObjectBuilder() .add(EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS, value("http://connector-address")) .add(EDR_REQUEST_DTO_PROTOCOL, value("protocol")) @@ -54,7 +71,7 @@ void shouldSuccess_whenObjectIsValid() { .add(EDR_REQUEST_DTO_OFFER, createArrayBuilder().add(createObjectBuilder() .add(OFFER_ID, value("offerId")) .add(ASSET_ID, value("offerId")) - .add(POLICY, createArrayBuilder().add(createObjectBuilder())) + .add(EDR_REQUEST_DTO_POLICY, createArrayBuilder().add(createObjectBuilder())) )) .build(); @@ -73,7 +90,7 @@ void shouldFail_whenMandatoryPropertiesAreMissing() { .isNotEmpty() .anySatisfy(violation -> assertThat(violation.path()).isEqualTo(EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS)) .anySatisfy(violation -> assertThat(violation.path()).isEqualTo(EDR_REQUEST_DTO_PROTOCOL)) - .anySatisfy(violation -> assertThat(violation.path()).isEqualTo(EDR_REQUEST_DTO_OFFER)); + .anySatisfy(violation -> assertThat(violation.path()).isEqualTo(EDR_REQUEST_DTO_POLICY)); } @Test