From 62600ad9f08d79597c710370d1a8e590e9ea3a43 Mon Sep 17 00:00:00 2001 From: Enrico Risa Date: Sat, 30 Mar 2024 18:51:57 +0100 Subject: [PATCH] chore: use of ContractRequest in edrs API + old edrs schema and migration removal (#1172) --- .../tractusx/edc/api/edr/v2/EdrCacheApi.java | 4 +- .../edc/api/edr/v2/EdrCacheApiController.java | 18 +- .../edc/api/edr/v2/EdrCacheApiExtension.java | 7 - .../edr/v2/dto/NegotiateEdrRequestDto.java | 120 ----------- .../edc/api/edr/v2/schema/EdrSchema.java | 109 ---------- ...ctToNegotiateEdrRequestDtoTransformer.java | 87 -------- ...tDtoToNegotiatedEdrRequestTransformer.java | 63 ------ .../NegotiateEdrRequestDtoValidator.java | 51 ----- .../api/edr/v2/EdrCacheApiControllerTest.java | 27 ++- .../edc/api/edr/v2/TestFunctions.java | 46 +--- ...NegotiateEdrRequestDtoTransformerTest.java | 155 ------------- ...oToNegotiateEdrRequestTransformerTest.java | 99 --------- .../NegotiateEdrRequestDtoValidatorTest.java | 100 --------- .../callback/ContractNegotiationCallback.java | 4 +- .../EdrPostgresqlMigrationExtension.java | 28 --- ...rg.eclipse.edc.spi.system.ServiceExtension | 1 - .../edr/V0_0_1__Init_Edr_Database_Schema.sql | 34 --- .../edr/V0_0_2__Add_ProviderId_Column.sql | 19 -- .../V0_0_3__Add_StatefulEntity_Columns.sql | 24 --- ..._0_4__Add_ContractNegotiationId_Column.sql | 19 -- .../tests/edrv2/AbstractNegotiateEdrTest.java | 166 -------------- .../tests/edrv2/NegotiateEdrInMemoryTest.java | 46 ---- .../edc/tests/edrv2/NegotiateEdrTest.java | 204 ++++++++++++++++++ .../EdrNegotiationHelperFunctions.java | 4 +- .../tests/runtimes/PgParticipantRuntime.java | 2 +- .../edr/spi/types/NegotiateEdrRequest.java | 107 --------- 26 files changed, 241 insertions(+), 1303 deletions(-) delete mode 100644 edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/dto/NegotiateEdrRequestDto.java delete mode 100644 edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/schema/EdrSchema.java delete mode 100644 edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/transform/JsonObjectToNegotiateEdrRequestDtoTransformer.java delete mode 100644 edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/transform/NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer.java delete mode 100644 edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/validation/NegotiateEdrRequestDtoValidator.java delete mode 100644 edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/transform/JsonObjectToNegotiateEdrRequestDtoTransformerTest.java delete mode 100644 edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/transform/NegotiateEdrRequestDtoToNegotiateEdrRequestTransformerTest.java delete mode 100644 edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/validation/NegotiateEdrRequestDtoValidatorTest.java delete mode 100644 edc-extensions/postgresql-migration/src/main/java/org/eclipse/tractusx/edc/postgresql/migration/EdrPostgresqlMigrationExtension.java delete mode 100644 edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_1__Init_Edr_Database_Schema.sql delete mode 100644 edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_2__Add_ProviderId_Column.sql delete mode 100644 edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_3__Add_StatefulEntity_Columns.sql delete mode 100644 edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_4__Add_ContractNegotiationId_Column.sql delete mode 100644 edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/AbstractNegotiateEdrTest.java delete mode 100644 edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/NegotiateEdrInMemoryTest.java create mode 100644 edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/NegotiateEdrTest.java delete mode 100644 spi/edr-spi/src/main/java/org/eclipse/tractusx/edc/edr/spi/types/NegotiateEdrRequest.java diff --git a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApi.java b/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApi.java index 19ee52186..29dd28843 100644 --- a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApi.java +++ b/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApi.java @@ -32,9 +32,9 @@ import jakarta.json.JsonObject; import org.eclipse.edc.api.model.ApiCoreSchema; import org.eclipse.edc.connector.api.management.configuration.ManagementApiSchema; +import org.eclipse.edc.connector.controlplane.api.management.contractnegotiation.ContractNegotiationApi; import org.eclipse.edc.edr.spi.types.EndpointDataReferenceEntry; import org.eclipse.edc.web.spi.ApiErrorDetail; -import org.eclipse.tractusx.edc.api.edr.v2.schema.EdrSchema; import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.ID; import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; @@ -51,7 +51,7 @@ public interface EdrCacheApi { @ApiResponse(responseCode = "400", description = "Request body was malformed", content = @Content(array = @ArraySchema(schema = @Schema(implementation = ApiErrorDetail.class)))), }) - JsonObject initiateEdrNegotiation(@Schema(implementation = EdrSchema.NegotiateEdrRequestSchema.class) JsonObject dto); + JsonObject initiateEdrNegotiation(@Schema(implementation = ContractNegotiationApi.ContractRequestSchema.class) JsonObject dto); @Operation(description = "Request all Edr entries according to a particular query", requestBody = @RequestBody( diff --git a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApiController.java b/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApiController.java index 415b936da..ea201d0af 100644 --- a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApiController.java +++ b/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApiController.java @@ -45,15 +45,14 @@ import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry; import org.eclipse.edc.web.spi.exception.InvalidRequestException; import org.eclipse.edc.web.spi.exception.ValidationFailureException; -import org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto; import org.eclipse.tractusx.edc.edr.spi.service.EdrService; -import org.eclipse.tractusx.edc.edr.spi.types.NegotiateEdrRequest; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; import static jakarta.json.stream.JsonCollectors.toJsonArray; +import static org.eclipse.edc.connector.controlplane.contract.spi.types.negotiation.ContractRequest.CONTRACT_REQUEST_TYPE; import static org.eclipse.edc.spi.query.QuerySpec.EDC_QUERY_SPEC_TYPE; import static org.eclipse.edc.web.spi.exception.ServiceResultHandler.exceptionMapper; import static org.eclipse.tractusx.edc.edr.spi.types.RefreshMode.AUTO_REFRESH; @@ -95,13 +94,14 @@ public EdrCacheApiController(EndpointDataReferenceStore edrStore, @POST @Override public JsonObject initiateEdrNegotiation(JsonObject requestObject) { - validator.validate(NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE, requestObject).orElseThrow(ValidationFailureException::new); - var edrNegotiationRequest = transformerRegistry.transform(requestObject, NegotiateEdrRequestDto.class) - .compose(dto -> transformerRegistry.transform(dto, NegotiateEdrRequest.class)) + validator.validate(CONTRACT_REQUEST_TYPE, requestObject) + .orElseThrow(ValidationFailureException::new); + + var contractRequest = transformerRegistry.transform(requestObject, ContractRequest.class) .orElseThrow(InvalidRequestException::new); - var contractNegotiation = contractNegotiationService.initiateNegotiation(createContractRequest(edrNegotiationRequest)); + var contractNegotiation = contractNegotiationService.initiateNegotiation(enrichContractRequest(contractRequest)); var idResponse = IdResponse.Builder.newInstance() .id(contractNegotiation.getId()) @@ -170,12 +170,12 @@ public JsonObject refreshEdr(@PathParam("transferProcessId") String transferProc .orElseThrow(f -> new EdcException(f.getFailureDetail())); } - private ContractRequest createContractRequest(NegotiateEdrRequest request) { + private ContractRequest enrichContractRequest(ContractRequest request) { var callbacks = Stream.concat(request.getCallbackAddresses().stream(), Stream.of(LOCAL_CALLBACK)).collect(Collectors.toList()); return ContractRequest.Builder.newInstance() - .counterPartyAddress(request.getConnectorAddress()) - .contractOffer(request.getOffer()) + .counterPartyAddress(request.getCounterPartyAddress()) + .contractOffer(request.getContractOffer()) .protocol(request.getProtocol()) .callbackAddresses(callbacks).build(); } diff --git a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApiExtension.java b/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApiExtension.java index b90d2f045..585c0fcc7 100644 --- a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApiExtension.java +++ b/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApiExtension.java @@ -31,11 +31,7 @@ import org.eclipse.edc.transform.spi.TypeTransformerRegistry; import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry; import org.eclipse.edc.web.spi.WebService; -import org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto; import org.eclipse.tractusx.edc.api.edr.v2.transform.JsonObjectFromEndpointDataReferenceEntryTransformer; -import org.eclipse.tractusx.edc.api.edr.v2.transform.JsonObjectToNegotiateEdrRequestDtoTransformer; -import org.eclipse.tractusx.edc.api.edr.v2.transform.NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer; -import org.eclipse.tractusx.edc.api.edr.v2.validation.NegotiateEdrRequestDtoValidator; import org.eclipse.tractusx.edc.edr.spi.service.EdrService; import java.util.Map; @@ -74,10 +70,7 @@ public class EdrCacheApiExtension implements ServiceExtension { public void initialize(ServiceExtensionContext context) { jsonLdService.registerNamespace(TX_PREFIX, TX_NAMESPACE); var mgmtApiTransformerRegistry = transformerRegistry.forContext("management-api"); - mgmtApiTransformerRegistry.register(new NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer()); - mgmtApiTransformerRegistry.register(new JsonObjectToNegotiateEdrRequestDtoTransformer()); mgmtApiTransformerRegistry.register(new JsonObjectFromEndpointDataReferenceEntryTransformer(Json.createBuilderFactory(Map.of()))); - validatorRegistry.register(NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE, NegotiateEdrRequestDtoValidator.instance()); webService.registerResource(apiConfig.getContextAlias(), new EdrCacheApiController(edrStore, mgmtApiTransformerRegistry, validatorRegistry, monitor, edrService, contractNegotiationService)); } } diff --git a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/dto/NegotiateEdrRequestDto.java b/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/dto/NegotiateEdrRequestDto.java deleted file mode 100644 index 46760d228..000000000 --- a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/dto/NegotiateEdrRequestDto.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.eclipse.tractusx.edc.api.edr.v2.dto; - -import org.eclipse.edc.connector.controlplane.api.management.contractnegotiation.model.ContractOfferDescription; -import org.eclipse.edc.spi.types.domain.callback.CallbackAddress; - -import java.util.ArrayList; -import java.util.List; - -import static org.eclipse.edc.spi.constants.CoreConstants.EDC_NAMESPACE; -import static org.eclipse.tractusx.edc.edr.spi.CoreConstants.TX_NAMESPACE; - -public class NegotiateEdrRequestDto { - - public static final String EDR_REQUEST_SIMPLE_DTO_TYPE = "NegotiateEdrRequestDto"; - public static final String EDR_REQUEST_DTO_TYPE = TX_NAMESPACE + EDR_REQUEST_SIMPLE_DTO_TYPE; - public static final String EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS = EDC_NAMESPACE + "counterPartyAddress"; - public static final String EDR_REQUEST_DTO_PROTOCOL = EDC_NAMESPACE + "protocol"; - 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_CALLBACK_ADDRESSES = EDC_NAMESPACE + "callbackAddresses"; - - private String counterPartyAddress; - private String protocol = "ids-multipart"; - private String counterPartyId; - - private String providerId; - - private ContractOfferDescription offer; - private List callbackAddresses = new ArrayList<>(); - - private NegotiateEdrRequestDto() { - - } - - public String getCounterPartyAddress() { - return counterPartyAddress; - } - - public String getProtocol() { - return protocol; - } - - public String getCounterPartyId() { - return counterPartyId; - } - - public List getCallbackAddresses() { - return callbackAddresses; - } - - public ContractOfferDescription getOffer() { - return offer; - } - - public static final class Builder { - private final NegotiateEdrRequestDto dto; - - private Builder() { - dto = new NegotiateEdrRequestDto(); - } - - public static Builder newInstance() { - return new Builder(); - } - - public Builder connectorAddress(String connectorAddress) { - dto.counterPartyAddress = connectorAddress; - return this; - } - - public Builder protocol(String protocol) { - dto.protocol = protocol; - return this; - } - - public Builder counterPartyId(String connectorId) { - dto.counterPartyId = connectorId; - return this; - } - - public Builder offer(ContractOfferDescription offer) { - dto.offer = offer; - return this; - } - - public Builder providerId(String providerId) { - dto.providerId = providerId; - return this; - } - - public Builder callbackAddresses(List callbackAddresses) { - dto.callbackAddresses = callbackAddresses; - return this; - } - - public NegotiateEdrRequestDto build() { - return dto; - } - } -} diff --git a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/schema/EdrSchema.java b/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/schema/EdrSchema.java deleted file mode 100644 index e62ac0d03..000000000 --- a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/schema/EdrSchema.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.eclipse.tractusx.edc.api.edr.v2.schema; - -import io.swagger.v3.oas.annotations.media.Schema; -import org.eclipse.edc.connector.api.management.configuration.ManagementApiSchema; -import org.eclipse.edc.connector.controlplane.api.management.contractnegotiation.ContractNegotiationApi; -import org.eclipse.edc.edr.spi.types.EndpointDataReferenceEntry; -import org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto; - -import java.util.List; - -import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; -import static org.eclipse.tractusx.edc.api.edr.v2.schema.EdrSchema.EndpointDataReferenceEntrySchema.ENDPOINT_DATA_REFERENCE_ENTRY_EXAMPLE; -import static org.eclipse.tractusx.edc.api.edr.v2.schema.EdrSchema.NegotiateEdrRequestSchema.NEGOTIATE_EDR_REQUEST_EXAMPLE; - -public class EdrSchema { - - @Schema(name = "NegotiateEdrRequest", example = NEGOTIATE_EDR_REQUEST_EXAMPLE) - public record NegotiateEdrRequestSchema( - @Schema(name = TYPE, example = NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE) - String type, - String protocol, - String connectorAddress, - @Deprecated(since = "0.1.3") - @Schema(deprecated = true, description = "please use providerId instead") - String connectorId, - String providerId, - ContractNegotiationApi.ContractOfferDescriptionSchema offer, - 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", - "offer": { - "offerId": "offer-id", - "assetId": "asset-id", - "policy": { - "@context": "http://www.w3.org/ns/odrl.jsonld", - "@type": "Set", - "@id": "offer-id", - "permission": [{ - "target": "asset-id", - "action": "display" - }] - } - }, - "callbackAddresses": [{ - "transactional": false, - "uri": "http://callback/url", - "events": ["contract.negotiation", "transfer.process"] - }] - } - """; - } - - @Schema(name = "EndpointDataReferenceEntry", example = ENDPOINT_DATA_REFERENCE_ENTRY_EXAMPLE) - public record EndpointDataReferenceEntrySchema( - @Schema(name = TYPE, example = EndpointDataReferenceEntry.SIMPLE_TYPE) - String type, - String agreementId, - String assetId, - String providerId, - String edrState, - Long expirationDate - ) { - public static final String ENDPOINT_DATA_REFERENCE_ENTRY_EXAMPLE = """ - { - "@type": "tx:EndpointDataReferenceEntry", - "agreementId": "MQ==:MQ==:ZTY3MzQ4YWEtNTdmZC00YzA0LTg2ZmQtMGMxNzk0MWM3OTkw", - "transferProcessId": "78a66945-d638-4c0a-be71-b35a0318a410", - "assetId": "1", - "providerId": "BPNL00DATAP00001", - "tx:edrState": "NEGOTIATED", - "tx:expirationDate": 1690811364000, - "@context": { - "dct": "https://purl.org/dc/terms/", - "tx": "https://w3id.org/tractusx/v0.0.1/ns/", - "edc": "https://w3id.org/edc/v0.0.1/ns/", - "dcat": "https://www.w3.org/ns/dcat/", - "odrl": "http://www.w3.org/ns/odrl/2/", - "dspace": "https://w3id.org/dspace/v0.8/" - } - } - """; - } - -} diff --git a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/transform/JsonObjectToNegotiateEdrRequestDtoTransformer.java b/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/transform/JsonObjectToNegotiateEdrRequestDtoTransformer.java deleted file mode 100644 index ea630cdc5..000000000 --- a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/transform/JsonObjectToNegotiateEdrRequestDtoTransformer.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.eclipse.tractusx.edc.api.edr.v2.transform; - -import jakarta.json.JsonObject; -import jakarta.json.JsonValue; -import org.eclipse.edc.connector.controlplane.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.transform.spi.TransformerContext; -import org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.ArrayList; - - -public class JsonObjectToNegotiateEdrRequestDtoTransformer extends AbstractJsonLdTransformer { - - public JsonObjectToNegotiateEdrRequestDtoTransformer() { - super(JsonObject.class, NegotiateEdrRequestDto.class); - } - - @Override - public @Nullable NegotiateEdrRequestDto transform(@NotNull JsonObject jsonObject, @NotNull TransformerContext context) { - var builder = NegotiateEdrRequestDto.Builder.newInstance(); - - visitProperties(jsonObject, (k, v) -> setProperties(k, v, builder, context)); - return builder.build(); - } - - private void setProperties(String key, JsonValue value, NegotiateEdrRequestDto.Builder builder, TransformerContext context) { - switch (key) { - case NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS: - transformString(value, builder::connectorAddress, context); - break; - case NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROTOCOL: - transformString(value, builder::protocol, context); - break; - case NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ID: - transformString(value, builder::counterPartyId, context); - break; - case NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROVIDER_ID: - transformString(value, builder::providerId, context); - break; - case NegotiateEdrRequestDto.EDR_REQUEST_DTO_CALLBACK_ADDRESSES: - var addresses = new ArrayList(); - transformArrayOrObject(value, CallbackAddress.class, addresses::add, context); - builder.callbackAddresses(addresses); - break; - case NegotiateEdrRequestDto.EDR_REQUEST_DTO_OFFER: - transformArrayOrObject(value, ContractOfferDescription.class, builder::offer, context); - break; - default: - context.problem() - .unexpectedType() - .type(NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE) - .property(key) - .actual(key) - .expected(NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS) - .expected(NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROTOCOL) - .expected(NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ID) - .expected(NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROVIDER_ID) - .expected(NegotiateEdrRequestDto.EDR_REQUEST_DTO_CALLBACK_ADDRESSES) - .expected(NegotiateEdrRequestDto.EDR_REQUEST_DTO_OFFER) - .report(); - break; - } - } -} diff --git a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/transform/NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer.java b/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/transform/NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer.java deleted file mode 100644 index cb1f62d63..000000000 --- a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/transform/NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.eclipse.tractusx.edc.api.edr.v2.transform; - -import org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractOffer; -import org.eclipse.edc.transform.spi.TransformerContext; -import org.eclipse.edc.transform.spi.TypeTransformer; -import org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto; -import org.eclipse.tractusx.edc.edr.spi.types.NegotiateEdrRequest; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -public class NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer implements TypeTransformer { - - @Override - public Class getInputType() { - return NegotiateEdrRequestDto.class; - } - - @Override - public Class getOutputType() { - return NegotiateEdrRequest.class; - } - - @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(); - - return NegotiateEdrRequest.Builder.newInstance() - .connectorId(object.getCounterPartyId()) - .connectorAddress(object.getCounterPartyAddress()) - .protocol(object.getProtocol()) - .offer(contractOffer) - .callbackAddresses(object.getCallbackAddresses()) - .build(); - } - - private String getId(String value, String defaultValue) { - return value != null ? value : defaultValue; - } - -} diff --git a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/validation/NegotiateEdrRequestDtoValidator.java b/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/validation/NegotiateEdrRequestDtoValidator.java deleted file mode 100644 index a7086bd39..000000000 --- a/edc-extensions/edr/edr-api-v2/src/main/java/org/eclipse/tractusx/edc/api/edr/v2/validation/NegotiateEdrRequestDtoValidator.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.eclipse.tractusx.edc.api.edr.v2.validation; - -import jakarta.json.JsonObject; -import org.eclipse.edc.validator.jsonobject.JsonObjectValidator; -import org.eclipse.edc.validator.jsonobject.validators.MandatoryObject; -import org.eclipse.edc.validator.jsonobject.validators.MandatoryValue; -import org.eclipse.edc.validator.spi.Validator; -import org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto; - -import static org.eclipse.edc.connector.controlplane.api.management.contractnegotiation.model.ContractOfferDescription.ASSET_ID; -import static org.eclipse.edc.connector.controlplane.api.management.contractnegotiation.model.ContractOfferDescription.OFFER_ID; -import static org.eclipse.edc.connector.controlplane.api.management.contractnegotiation.model.ContractOfferDescription.POLICY; - - -public class NegotiateEdrRequestDtoValidator { - - private NegotiateEdrRequestDtoValidator() { - } - - public static Validator instance() { - return JsonObjectValidator.newValidator() - .verify(NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS, MandatoryValue::new) - .verify(NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROTOCOL, MandatoryValue::new) - .verify(NegotiateEdrRequestDto.EDR_REQUEST_DTO_OFFER, MandatoryObject::new) - .verifyObject(NegotiateEdrRequestDto.EDR_REQUEST_DTO_OFFER, v -> v - .verify(OFFER_ID, MandatoryValue::new) - .verify(ASSET_ID, MandatoryValue::new) - .verify(POLICY, MandatoryObject::new) - ) - .build(); - } -} diff --git a/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApiControllerTest.java b/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApiControllerTest.java index 6d8fce426..ae7017dfa 100644 --- a/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApiControllerTest.java +++ b/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/EdrCacheApiControllerTest.java @@ -24,10 +24,13 @@ import jakarta.json.JsonObject; import jakarta.json.JsonObjectBuilder; import org.eclipse.edc.api.model.IdResponse; +import org.eclipse.edc.connector.controlplane.contract.spi.types.negotiation.ContractRequest; +import org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractOffer; import org.eclipse.edc.connector.controlplane.services.spi.contractnegotiation.ContractNegotiationService; import org.eclipse.edc.edr.spi.store.EndpointDataReferenceStore; import org.eclipse.edc.edr.spi.types.EndpointDataReferenceEntry; import org.eclipse.edc.junit.annotations.ApiTest; +import org.eclipse.edc.policy.model.Policy; import org.eclipse.edc.spi.query.QuerySpec; import org.eclipse.edc.spi.result.Result; import org.eclipse.edc.spi.result.ServiceResult; @@ -37,9 +40,7 @@ import org.eclipse.edc.validator.spi.JsonObjectValidatorRegistry; import org.eclipse.edc.validator.spi.ValidationResult; import org.eclipse.edc.web.jersey.testfixtures.RestControllerTestBase; -import org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto; import org.eclipse.tractusx.edc.edr.spi.service.EdrService; -import org.eclipse.tractusx.edc.edr.spi.types.NegotiateEdrRequest; import org.junit.jupiter.api.Test; import java.util.List; @@ -47,6 +48,7 @@ import static io.restassured.RestAssured.given; import static io.restassured.http.ContentType.JSON; import static jakarta.json.Json.createObjectBuilder; +import static java.util.UUID.randomUUID; import static org.eclipse.edc.api.model.IdResponse.ID_RESPONSE_TYPE; import static org.eclipse.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_AGREEMENT_ID; import static org.eclipse.edc.edr.spi.types.EndpointDataReferenceEntry.EDR_ENTRY_ASSET_ID; @@ -62,7 +64,6 @@ import static org.eclipse.edc.spi.constants.CoreConstants.EDC_PREFIX; import static org.eclipse.tractusx.edc.api.edr.v2.TestFunctions.createContractNegotiation; import static org.eclipse.tractusx.edc.api.edr.v2.TestFunctions.negotiationRequest; -import static org.eclipse.tractusx.edc.api.edr.v2.TestFunctions.openRequest; import static org.eclipse.tractusx.edc.edr.spi.types.RefreshMode.AUTO_REFRESH; import static org.eclipse.tractusx.edc.edr.spi.types.RefreshMode.NO_REFRESH; import static org.hamcrest.Matchers.equalTo; @@ -96,12 +97,10 @@ public class EdrCacheApiControllerTest extends RestControllerTestBase { void initEdrNegotiation_shouldWork_whenValidRequest() { when(validator.validate(any(), any())).thenReturn(ValidationResult.success()); - var openRequest = openRequest(); var contractNegotiation = createContractNegotiation(); var responseBody = Json.createObjectBuilder().add(TYPE, ID_RESPONSE_TYPE).add(ID, contractNegotiation.getId()).build(); - when(transformerRegistry.transform(any(JsonObject.class), eq(NegotiateEdrRequestDto.class))).thenReturn(Result.success(NegotiateEdrRequestDto.Builder.newInstance().build())); - when(transformerRegistry.transform(any(), eq(NegotiateEdrRequest.class))).thenReturn(Result.success(openRequest)); + when(transformerRegistry.transform(any(JsonObject.class), eq(ContractRequest.class))).thenReturn(Result.success(createContractRequest())); when(contractNegotiationService.initiateNegotiation(any())).thenReturn(contractNegotiation); when(transformerRegistry.transform(any(IdResponse.class), eq(JsonObject.class))).thenReturn(Result.success(responseBody)); @@ -121,12 +120,11 @@ void initEdrNegotiation_shouldWork_whenValidRequest() { void initEdrNegotiation_shouldReturnBadRequest_whenValidInvalidRequest() { when(validator.validate(any(), any())).thenReturn(ValidationResult.success()); - var request = NegotiateEdrRequestDto.Builder.newInstance().build(); - when(transformerRegistry.transform(any(JsonObject.class), eq(NegotiateEdrRequestDto.class))).thenReturn(Result.failure("fail")); + when(transformerRegistry.transform(any(JsonObject.class), eq(ContractRequest.class))).thenReturn(Result.failure("fail")); baseRequest() .contentType(JSON) - .body(request) + .body(Json.createObjectBuilder().build()) .post("/v2/edrs") .then() .statusCode(400); @@ -296,6 +294,17 @@ private JsonObjectBuilder createContextBuilder() { .add(EDC_PREFIX, EDC_NAMESPACE); } + private ContractRequest createContractRequest() { + return ContractRequest.Builder.newInstance() + .protocol("test-protocol") + .counterPartyAddress("test-cb") + .contractOffer(ContractOffer.Builder.newInstance() + .id("test-offer-id") + .assetId(randomUUID().toString()) + .policy(Policy.Builder.newInstance().build()) + .build()) + .build(); + } private RequestSpecification baseRequest() { return given() diff --git a/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/TestFunctions.java b/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/TestFunctions.java index 44837c176..45ac84014 100644 --- a/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/TestFunctions.java +++ b/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/TestFunctions.java @@ -21,47 +21,17 @@ import jakarta.json.Json; import jakarta.json.JsonObject; -import org.eclipse.edc.connector.controlplane.api.management.contractnegotiation.model.ContractOfferDescription; import org.eclipse.edc.connector.controlplane.contract.spi.types.negotiation.ContractNegotiation; -import org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractOffer; -import org.eclipse.edc.policy.model.Policy; -import org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto; -import org.eclipse.tractusx.edc.edr.spi.types.NegotiateEdrRequest; - -import java.util.UUID; +import org.eclipse.edc.connector.controlplane.contract.spi.types.negotiation.ContractRequest; import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.TYPE; import static org.eclipse.edc.spi.constants.CoreConstants.EDC_NAMESPACE; public class TestFunctions { - public static ContractOfferDescription createOffer(String offerId, String assetId) { - return ContractOfferDescription.Builder.newInstance() - .offerId(offerId) - .assetId(assetId) - .policy(Policy.Builder.newInstance().build()) - .build(); - } - - public static ContractOfferDescription createOffer(Policy policy) { - return ContractOfferDescription.Builder.newInstance() - .offerId(UUID.randomUUID().toString()) - .assetId(UUID.randomUUID().toString()) - .policy(policy) - .build(); - } - - public static ContractOfferDescription createOffer(String offerId) { - return createOffer(offerId, UUID.randomUUID().toString()); - } - - public static ContractOfferDescription createOffer() { - return createOffer(UUID.randomUUID().toString(), UUID.randomUUID().toString()); - } - public static JsonObject negotiationRequest() { return Json.createObjectBuilder() - .add(TYPE, NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE) + .add(TYPE, ContractRequest.CONTRACT_REQUEST_TYPE) .add(EDC_NAMESPACE + "connectorId", "test") .add(EDC_NAMESPACE + "providerId", "test") .add(EDC_NAMESPACE + "connectorAddress", "test") @@ -74,18 +44,6 @@ public static JsonObject negotiationRequest() { .build(); } - public static NegotiateEdrRequest openRequest() { - return NegotiateEdrRequest.Builder.newInstance() - .connectorAddress("test") - .connectorId("id") - .protocol("test-protocol") - .offer(ContractOffer.Builder.newInstance() - .id("offerId") - .assetId("assetId") - .policy(Policy.Builder.newInstance().build()).build()) - .build(); - } - public static ContractNegotiation createContractNegotiation() { return ContractNegotiation.Builder.newInstance() .id("id") diff --git a/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/transform/JsonObjectToNegotiateEdrRequestDtoTransformerTest.java b/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/transform/JsonObjectToNegotiateEdrRequestDtoTransformerTest.java deleted file mode 100644 index d48b3943e..000000000 --- a/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/transform/JsonObjectToNegotiateEdrRequestDtoTransformerTest.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.eclipse.tractusx.edc.api.edr.v2.transform; - -import jakarta.json.Json; -import jakarta.json.JsonArrayBuilder; -import jakarta.json.JsonObject; -import jakarta.json.JsonValue; -import org.eclipse.edc.connector.controlplane.api.management.contractnegotiation.model.ContractOfferDescription; -import org.eclipse.edc.jsonld.TitaniumJsonLd; -import org.eclipse.edc.jsonld.spi.JsonLd; -import org.eclipse.edc.spi.monitor.Monitor; -import org.eclipse.edc.spi.types.domain.callback.CallbackAddress; -import org.eclipse.edc.transform.spi.ProblemBuilder; -import org.eclipse.edc.transform.spi.TransformerContext; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import java.util.Set; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.edc.connector.controlplane.api.management.contractnegotiation.model.ContractOfferDescription.OFFER_ID; -import static org.eclipse.edc.connector.controlplane.contract.spi.types.negotiation.ContractRequest.CALLBACK_ADDRESSES; -import static org.eclipse.edc.connector.controlplane.contract.spi.types.negotiation.ContractRequest.CONTRACT_REQUEST_TYPE; -import static org.eclipse.edc.connector.controlplane.contract.spi.types.negotiation.ContractRequest.OFFER; -import static org.eclipse.edc.connector.controlplane.contract.spi.types.negotiation.ContractRequest.POLICY; -import static org.eclipse.edc.connector.controlplane.contract.spi.types.negotiation.ContractRequest.PROTOCOL; -import static org.eclipse.edc.edr.spi.types.EndpointDataReferenceEntry.ASSET_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.spi.constants.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; -import static org.eclipse.edc.spi.types.domain.callback.CallbackAddress.URI; -import static org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS; -import static org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ID; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -class JsonObjectToNegotiateEdrRequestDtoTransformerTest { - - private final JsonLd jsonLd = new TitaniumJsonLd(mock(Monitor.class)); - private final TransformerContext context = mock(TransformerContext.class); - private JsonObjectToNegotiateEdrRequestDtoTransformer transformer; - - @BeforeEach - void setUp() { - transformer = new JsonObjectToNegotiateEdrRequestDtoTransformer(); - } - - @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(CALLBACK_ADDRESSES, createCallbackAddress()) - .add(OFFER, Json.createObjectBuilder() - .add(OFFER_ID, "test-offer-id") - .add(ASSET_ID, "test-asset") - .add(POLICY, createPolicy()) - .build()) - .build(); - - when(context.transform(any(JsonValue.class), eq(ContractOfferDescription.class))).thenReturn(ContractOfferDescription.Builder.newInstance().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.getOffer()).isNotNull(); - - } - - @Test - void transform_reportErrors() { - - when(context.problem()).thenReturn(new ProblemBuilder(context)); - - var jsonObject = Json.createObjectBuilder() - .add(TYPE, CONTRACT_REQUEST_TYPE) - .add(EDC_NAMESPACE + "notFound", "test-address") - .build(); - - var dto = transformer.transform(jsonLd.expand(jsonObject).getContent(), context); - - assertThat(dto).isNotNull(); - verify(context, times(1)).reportProblem(anyString()); - } - - private JsonArrayBuilder createCallbackAddress() { - var builder = Json.createArrayBuilder(); - return builder.add(Json.createObjectBuilder() - .add(IS_TRANSACTIONAL, true) - .add(URI, "http://test.local/") - .add(EVENTS, Json.createArrayBuilder().build())); - } - - private JsonObject createPolicy() { - var permissionJson = getJsonObject("permission"); - var prohibitionJson = getJsonObject("prohibition"); - var dutyJson = getJsonObject("duty"); - return Json.createObjectBuilder() - .add(TYPE, ODRL_POLICY_TYPE_SET) - .add(ODRL_PERMISSION_ATTRIBUTE, permissionJson) - .add(ODRL_PROHIBITION_ATTRIBUTE, prohibitionJson) - .add(ODRL_OBLIGATION_ATTRIBUTE, dutyJson) - .build(); - } - - private JsonObject getJsonObject(String type) { - return Json.createObjectBuilder() - .add(TYPE, type) - .build(); - } -} diff --git a/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/transform/NegotiateEdrRequestDtoToNegotiateEdrRequestTransformerTest.java b/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/transform/NegotiateEdrRequestDtoToNegotiateEdrRequestTransformerTest.java deleted file mode 100644 index 24a31450c..000000000 --- a/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/transform/NegotiateEdrRequestDtoToNegotiateEdrRequestTransformerTest.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.eclipse.tractusx.edc.api.edr.v2.transform; - -import org.eclipse.edc.spi.types.domain.callback.CallbackAddress; -import org.eclipse.edc.transform.spi.TransformerContext; -import org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.eclipse.tractusx.edc.api.edr.v2.TestFunctions.createOffer; -import static org.mockito.Mockito.mock; - -public class NegotiateEdrRequestDtoToNegotiateEdrRequestTransformerTest { - - private final NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer transformer = new NegotiateEdrRequestDtoToNegotiatedEdrRequestTransformer(); - - private final TransformerContext context = mock(TransformerContext.class); - - @Test - void inputOutputType() { - assertThat(transformer.getInputType()).isNotNull(); - assertThat(transformer.getOutputType()).isNotNull(); - } - - @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") - .offer(createOffer("offerId", "assetId")) - .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_transfor_withNoProviderId() { - var dto = NegotiateEdrRequestDto.Builder.newInstance() - .counterPartyId("connectorId") - .connectorAddress("address") - .protocol("protocol") - // do not set provider ID - .offer(createOffer("offerId", "assetId")) - .build(); - - var request = transformer.transform(dto, context); - - assertThat(request).isNotNull(); - } - - @Test - void verify_transform_withNoConsumerId() { - var dto = NegotiateEdrRequestDto.Builder.newInstance() - .counterPartyId("connectorId") - .connectorAddress("address") - .protocol("protocol") - // do not set consumer ID - .providerId("urn:connector:test-provider") - .offer(createOffer("offerId", "assetId")) - .build(); - - var request = transformer.transform(dto, context); - assertThat(request).isNotNull(); - } -} diff --git a/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/validation/NegotiateEdrRequestDtoValidatorTest.java b/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/validation/NegotiateEdrRequestDtoValidatorTest.java deleted file mode 100644 index 8f75402e0..000000000 --- a/edc-extensions/edr/edr-api-v2/src/test/java/org/eclipse/tractusx/edc/api/edr/v2/validation/NegotiateEdrRequestDtoValidatorTest.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.eclipse.tractusx.edc.api.edr.v2.validation; - -import jakarta.json.Json; -import jakarta.json.JsonArrayBuilder; -import jakarta.json.JsonObject; -import org.eclipse.edc.validator.spi.ValidationFailure; -import org.eclipse.edc.validator.spi.Validator; -import org.eclipse.edc.validator.spi.Violation; -import org.junit.jupiter.api.Test; - -import static jakarta.json.Json.createArrayBuilder; -import static jakarta.json.Json.createObjectBuilder; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.InstanceOfAssertFactories.list; -import static org.eclipse.edc.connector.controlplane.api.management.contractnegotiation.model.ContractOfferDescription.ASSET_ID; -import static org.eclipse.edc.connector.controlplane.api.management.contractnegotiation.model.ContractOfferDescription.OFFER_ID; -import static org.eclipse.edc.connector.controlplane.api.management.contractnegotiation.model.ContractOfferDescription.POLICY; -import static org.eclipse.edc.jsonld.spi.JsonLdKeywords.VALUE; -import static org.eclipse.edc.junit.assertions.AbstractResultAssert.assertThat; -import static org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_COUNTERPARTY_ADDRESS; -import static org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_OFFER; -import static org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROTOCOL; -import static org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto.EDR_REQUEST_DTO_PROVIDER_ID; - -public class NegotiateEdrRequestDtoValidatorTest { - - private final Validator validator = NegotiateEdrRequestDtoValidator.instance(); - - @Test - void shouldSuccess_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_OFFER, createArrayBuilder().add(createObjectBuilder() - .add(OFFER_ID, value("offerId")) - .add(ASSET_ID, value("offerId")) - .add(POLICY, createArrayBuilder().add(createObjectBuilder())) - )) - .build(); - - var result = validator.validate(input); - - assertThat(result).isSucceeded(); - } - - @Test - void shouldFail_whenMandatoryPropertiesAreMissing() { - var input = Json.createObjectBuilder().build(); - - var result = validator.validate(input); - - assertThat(result).isFailed().extracting(ValidationFailure::getViolations).asInstanceOf(list(Violation.class)) - .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)); - } - - @Test - void shouldFail_whenOfferMandatoryPropertiesAreMissing() { - 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_OFFER, createArrayBuilder().add(createObjectBuilder())) - .build(); - - var result = validator.validate(input); - - assertThat(result).isFailed().extracting(ValidationFailure::getViolations).asInstanceOf(list(Violation.class)) - .isNotEmpty() - .anySatisfy(violation -> assertThat(violation.path()).isEqualTo(EDR_REQUEST_DTO_OFFER + "/" + OFFER_ID)) - .anySatisfy(violation -> assertThat(violation.path()).isEqualTo(EDR_REQUEST_DTO_OFFER + "/" + ASSET_ID)) - .anySatisfy(violation -> assertThat(violation.path()).isEqualTo(EDR_REQUEST_DTO_OFFER + "/" + POLICY)); - } - - private JsonArrayBuilder value(String value) { - return createArrayBuilder().add(createObjectBuilder().add(VALUE, value)); - } -} diff --git a/edc-extensions/edr/edr-callback/src/main/java/org/eclipse/tractusx/edc/callback/ContractNegotiationCallback.java b/edc-extensions/edr/edr-callback/src/main/java/org/eclipse/tractusx/edc/callback/ContractNegotiationCallback.java index 015bf53ab..b9e2afda0 100644 --- a/edc-extensions/edr/edr-callback/src/main/java/org/eclipse/tractusx/edc/callback/ContractNegotiationCallback.java +++ b/edc-extensions/edr/edr-callback/src/main/java/org/eclipse/tractusx/edc/callback/ContractNegotiationCallback.java @@ -35,7 +35,8 @@ public class ContractNegotiationCallback implements InProcessCallback { - public static final DataAddress DATA_DESTINATION = DataAddress.Builder.newInstance().type("HttpProxy-PULL").build(); + public static final DataAddress DATA_DESTINATION = DataAddress.Builder.newInstance().type("HttpProxy").build(); + private static final String TRANSFER_TYPE = "HttpData-PULL"; private final TransferProcessService transferProcessService; private final Monitor monitor; @@ -62,6 +63,7 @@ private Result initiateTransfer(ContractNegotiationFinalized negotiationFi .counterPartyAddress(negotiationFinalized.getCounterPartyAddress()) .protocol(negotiationFinalized.getProtocol()) .dataDestination(DATA_DESTINATION) + .transferType(TRANSFER_TYPE) .callbackAddresses(negotiationFinalized.getCallbackAddresses()) .build(); diff --git a/edc-extensions/postgresql-migration/src/main/java/org/eclipse/tractusx/edc/postgresql/migration/EdrPostgresqlMigrationExtension.java b/edc-extensions/postgresql-migration/src/main/java/org/eclipse/tractusx/edc/postgresql/migration/EdrPostgresqlMigrationExtension.java deleted file mode 100644 index cbf82303d..000000000 --- a/edc-extensions/postgresql-migration/src/main/java/org/eclipse/tractusx/edc/postgresql/migration/EdrPostgresqlMigrationExtension.java +++ /dev/null @@ -1,28 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ - -package org.eclipse.tractusx.edc.postgresql.migration; - -public class EdrPostgresqlMigrationExtension extends AbstractPostgresqlMigrationExtension { - private static final String NAME_SUBSYSTEM = "edr"; - - protected String getSubsystemName() { - return NAME_SUBSYSTEM; - } -} diff --git a/edc-extensions/postgresql-migration/src/main/resources/META-INF/services/org.eclipse.edc.spi.system.ServiceExtension b/edc-extensions/postgresql-migration/src/main/resources/META-INF/services/org.eclipse.edc.spi.system.ServiceExtension index d89bb3f09..1d524a071 100644 --- a/edc-extensions/postgresql-migration/src/main/resources/META-INF/services/org.eclipse.edc.spi.system.ServiceExtension +++ b/edc-extensions/postgresql-migration/src/main/resources/META-INF/services/org.eclipse.edc.spi.system.ServiceExtension @@ -25,5 +25,4 @@ org.eclipse.tractusx.edc.postgresql.migration.ContractNegotiationPostgresqlMigra org.eclipse.tractusx.edc.postgresql.migration.PolicyPostgresqlMigrationExtension org.eclipse.tractusx.edc.postgresql.migration.PolicyMonitorPostgresqlMigrationExtension org.eclipse.tractusx.edc.postgresql.migration.TransferProcessPostgresqlMigrationExtension -org.eclipse.tractusx.edc.postgresql.migration.EdrPostgresqlMigrationExtension org.eclipse.tractusx.edc.postgresql.migration.BusinessGroupPostgresMigrationExtension diff --git a/edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_1__Init_Edr_Database_Schema.sql b/edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_1__Init_Edr_Database_Schema.sql deleted file mode 100644 index 0e166045b..000000000 --- a/edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_1__Init_Edr_Database_Schema.sql +++ /dev/null @@ -1,34 +0,0 @@ --- --- Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) --- --- This program and the accompanying materials are made available under the --- terms of the Apache License, Version 2.0 which is available at --- https://www.apache.org/licenses/LICENSE-2.0 --- --- SPDX-License-Identifier: Apache-2.0 --- --- Contributors: --- Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation --- - --- Statements are designed for and tested with Postgres only! - - -CREATE TABLE IF NOT EXISTS edc_edr_cache -( - transfer_process_id VARCHAR NOT NULL PRIMARY KEY, - agreement_id VARCHAR NOT NULL, - asset_id VARCHAR NOT NULL, - edr_id VARCHAR NOT NULL, - created_at BIGINT NOT NULL, - updated_at BIGINT NOT NULL -); - - -CREATE INDEX IF NOT EXISTS edc_edr_asset_id_index - ON edc_edr_cache (asset_id); - - -CREATE INDEX IF NOT EXISTS edc_edr_agreement_id_index - ON edc_edr_cache (agreement_id); - diff --git a/edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_2__Add_ProviderId_Column.sql b/edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_2__Add_ProviderId_Column.sql deleted file mode 100644 index ee20b3990..000000000 --- a/edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_2__Add_ProviderId_Column.sql +++ /dev/null @@ -1,19 +0,0 @@ --- --- Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) --- --- This program and the accompanying materials are made available under the --- terms of the Apache License, Version 2.0 which is available at --- https://www.apache.org/licenses/LICENSE-2.0 --- --- SPDX-License-Identifier: Apache-2.0 --- --- Contributors: --- Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation --- - --- Statements are designed for and tested with Postgres only! - - --- add column -ALTER TABLE edc_edr_cache ADD COLUMN provider_id VARCHAR; - diff --git a/edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_3__Add_StatefulEntity_Columns.sql b/edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_3__Add_StatefulEntity_Columns.sql deleted file mode 100644 index a3442e00b..000000000 --- a/edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_3__Add_StatefulEntity_Columns.sql +++ /dev/null @@ -1,24 +0,0 @@ --- --- Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) --- --- This program and the accompanying materials are made available under the --- terms of the Apache License, Version 2.0 which is available at --- https://www.apache.org/licenses/LICENSE-2.0 --- --- SPDX-License-Identifier: Apache-2.0 --- --- Contributors: --- Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation --- - --- Statements are designed for and tested with Postgres only! - - --- add column -ALTER TABLE edc_edr_cache ADD COLUMN expiration_timestamp BIGINT; -ALTER TABLE edc_edr_cache ADD COLUMN state INTEGER DEFAULT 50 NOT NULL; -ALTER TABLE edc_edr_cache ADD COLUMN state_count INTEGER DEFAULT 0; -ALTER TABLE edc_edr_cache ADD COLUMN state_timestamp BIGINT; -ALTER TABLE edc_edr_cache ADD COLUMN error_detail VARCHAR; -ALTER TABLE edc_edr_cache ADD COLUMN lease_id VARCHAR CONSTRAINT edc_edr_cache_lease_lease_id_fk REFERENCES edc_lease ON DELETE SET NULL; - diff --git a/edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_4__Add_ContractNegotiationId_Column.sql b/edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_4__Add_ContractNegotiationId_Column.sql deleted file mode 100644 index 291f325c3..000000000 --- a/edc-extensions/postgresql-migration/src/main/resources/org/eclipse/tractusx/edc/postgresql/migration/edr/V0_0_4__Add_ContractNegotiationId_Column.sql +++ /dev/null @@ -1,19 +0,0 @@ --- --- Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) --- --- This program and the accompanying materials are made available under the --- terms of the Apache License, Version 2.0 which is available at --- https://www.apache.org/licenses/LICENSE-2.0 --- --- SPDX-License-Identifier: Apache-2.0 --- --- Contributors: --- Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation --- - --- Statements are designed for and tested with Postgres only! - - --- add column -ALTER TABLE edc_edr_cache ADD COLUMN contract_negotiation_id VARCHAR; - diff --git a/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/AbstractNegotiateEdrTest.java b/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/AbstractNegotiateEdrTest.java deleted file mode 100644 index 4443d84f5..000000000 --- a/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/AbstractNegotiateEdrTest.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.eclipse.tractusx.edc.tests.edrv2; - -import jakarta.json.Json; -import okhttp3.mockwebserver.MockResponse; -import okhttp3.mockwebserver.MockWebServer; -import org.eclipse.edc.connector.controlplane.contract.spi.event.contractnegotiation.ContractNegotiationAgreed; -import org.eclipse.edc.connector.controlplane.contract.spi.event.contractnegotiation.ContractNegotiationFinalized; -import org.eclipse.edc.connector.controlplane.contract.spi.event.contractnegotiation.ContractNegotiationInitiated; -import org.eclipse.edc.connector.controlplane.contract.spi.event.contractnegotiation.ContractNegotiationRequested; -import org.eclipse.edc.connector.controlplane.contract.spi.event.contractnegotiation.ContractNegotiationVerified; -import org.eclipse.edc.connector.controlplane.transfer.spi.event.TransferProcessInitiated; -import org.eclipse.edc.connector.controlplane.transfer.spi.event.TransferProcessProvisioned; -import org.eclipse.edc.connector.controlplane.transfer.spi.event.TransferProcessRequested; -import org.eclipse.edc.connector.controlplane.transfer.spi.event.TransferProcessStarted; -import org.eclipse.edc.policy.model.Operator; -import org.eclipse.tractusx.edc.tests.TestRuntimeConfiguration; -import org.eclipse.tractusx.edc.tests.helpers.EdrNegotiationHelperFunctions; -import org.eclipse.tractusx.edc.tests.helpers.PolicyHelperFunctions; -import org.eclipse.tractusx.edc.tests.participant.TransferParticipant; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.awaitility.Awaitility.await; -import static org.eclipse.tractusx.edc.tests.helpers.EdrNegotiationHelperFunctions.createEvent; -import static org.eclipse.tractusx.edc.tests.helpers.Functions.waitForEvent; -import static org.eclipse.tractusx.edc.tests.participant.TractusxParticipantBase.ASYNC_POLL_INTERVAL; -import static org.eclipse.tractusx.edc.tests.participant.TractusxParticipantBase.ASYNC_TIMEOUT; - -public abstract class AbstractNegotiateEdrTest { - - protected static final TransferParticipant SOKRATES = TransferParticipant.Builder.newInstance() - .name(TestRuntimeConfiguration.SOKRATES_NAME) - .id(TestRuntimeConfiguration.SOKRATES_BPN) - .build(); - - protected static final TransferParticipant PLATO = TransferParticipant.Builder.newInstance() - .name(TestRuntimeConfiguration.PLATO_NAME) - .id(TestRuntimeConfiguration.PLATO_BPN) - .build(); - - MockWebServer server; - - @BeforeEach - void setup() { - server = new MockWebServer(); - } - - @Test - @DisplayName("Verify that the callbacks are invoked when negotiating an EDR") - void negotiateEdr_shouldInvokeCallbacks() throws IOException { - - var expectedEvents = List.of( - createEvent(ContractNegotiationInitiated.class), - createEvent(ContractNegotiationRequested.class), - createEvent(ContractNegotiationAgreed.class), - createEvent(ContractNegotiationFinalized.class), - createEvent(ContractNegotiationVerified.class), - createEvent(TransferProcessInitiated.class), - createEvent(TransferProcessProvisioned.class), - createEvent(TransferProcessRequested.class), - createEvent(TransferProcessStarted.class)); - - var assetId = "api-asset-1"; - var url = server.url("/mock/api"); - server.start(); - - var authCodeHeaderName = "test-authkey"; - var authCode = "test-authcode"; - Map dataAddress = Map.of( - "name", "transfer-test", - "baseUrl", url.toString(), - "type", "HttpData", - "contentType", "application/json", - "authKey", authCodeHeaderName, - "authCode", authCode - ); - - PLATO.createAsset(assetId, Map.of(), dataAddress); - - PLATO.storeBusinessPartner(SOKRATES.getBpn(), "test-group1", "test-group2"); - var accessPolicy = PLATO.createPolicyDefinition(PolicyHelperFunctions.bpnGroupPolicy(Operator.IS_NONE_OF, "forbidden-policy")); - var contractPolicy = PLATO.createPolicyDefinition(PolicyHelperFunctions.bpnGroupPolicy(Operator.IS_ALL_OF, "test-group1", "test-group2")); - PLATO.createContractDefinition(assetId, "def-1", accessPolicy, contractPolicy); - - - expectedEvents.forEach(event -> server.enqueue(new MockResponse())); - - var callbacks = Json.createArrayBuilder() - .add(EdrNegotiationHelperFunctions.createCallback(url.toString(), true, Set.of("contract.negotiation", "transfer.process"))) - .build(); - - var contractNegotiationId = SOKRATES.edrs().negotiateEdr(PLATO, assetId, callbacks); - - var events = expectedEvents.stream() - .map(receivedEvent -> waitForEvent(server)) - .collect(Collectors.toList()); - - - await().pollInterval(ASYNC_POLL_INTERVAL) - .atMost(ASYNC_TIMEOUT) - .untilAsserted(() -> { - var edrCaches = SOKRATES.edrs().getEdrEntriesByAssetId(assetId); - assertThat(edrCaches).hasSize(1); - }); - - assertThat(expectedEvents).usingRecursiveFieldByFieldElementComparator().containsAll(events); - - var edrCaches = SOKRATES.edrs().getEdrEntriesByAssetId(assetId); - - assertThat(edrCaches).hasSize(1); - - assertThat(SOKRATES.edrs().getEdrEntriesByContractNegotiationId(contractNegotiationId)).hasSize(1); - - assertThat(edrCaches).hasSize(1); - - var transferProcessId = edrCaches.get(0).asJsonObject().getString("transferProcessId"); - var cnId = edrCaches.get(0).asJsonObject().getString("contractNegotiationId"); - var agreementId = edrCaches.get(0).asJsonObject().getString("agreementId"); - - assertThat(cnId).isEqualTo(contractNegotiationId); - assertThat(SOKRATES.edrs().getEdrEntriesByAgreementId(agreementId)).hasSize(1); - - - var edr = SOKRATES.edrs().getEdr(transferProcessId); - - assertThat(edr.getJsonString("type").getString()).isEqualTo("https://w3id.org/idsa/v4.1/HTTP"); - assertThat(edr.getJsonString("endpoint").getString()).isNotNull(); - assertThat(edr.getJsonString("endpointType").getString()).isEqualTo(edr.getJsonString("type").getString()); - assertThat(edr.getJsonString("authorization").getString()).isNotNull(); - - } - - @AfterEach - void teardown() throws IOException { - server.shutdown(); - } - -} diff --git a/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/NegotiateEdrInMemoryTest.java b/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/NegotiateEdrInMemoryTest.java deleted file mode 100644 index dd25e45e1..000000000 --- a/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/NegotiateEdrInMemoryTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.eclipse.tractusx.edc.tests.edrv2; - - -import org.eclipse.edc.junit.annotations.EndToEndTest; -import org.eclipse.tractusx.edc.tests.runtimes.ParticipantRuntime; -import org.junit.jupiter.api.extension.RegisterExtension; - -@EndToEndTest -public class NegotiateEdrInMemoryTest extends AbstractNegotiateEdrTest { - - @RegisterExtension - protected static final ParticipantRuntime SOKRATES_RUNTIME = new ParticipantRuntime( - ":edc-tests:runtime:runtime-memory", - SOKRATES.getName(), - SOKRATES.getBpn(), - SOKRATES.getConfiguration() - ); - - @RegisterExtension - protected static final ParticipantRuntime PLATO_RUNTIME = new ParticipantRuntime( - ":edc-tests:runtime:runtime-memory", - PLATO.getName(), - PLATO.getBpn(), - PLATO.getConfiguration() - ); - -} diff --git a/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/NegotiateEdrTest.java b/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/NegotiateEdrTest.java new file mode 100644 index 000000000..fa91ed005 --- /dev/null +++ b/edc-tests/edc-controlplane/edr-api-tests/src/test/java/org/eclipse/tractusx/edc/tests/edrv2/NegotiateEdrTest.java @@ -0,0 +1,204 @@ +/******************************************************************************** + * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) + * + * See the NOTICE file(s) distributed with this work for additional + * information regarding copyright ownership. + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + ********************************************************************************/ + +package org.eclipse.tractusx.edc.tests.edrv2; + +import jakarta.json.Json; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import org.eclipse.edc.connector.controlplane.contract.spi.event.contractnegotiation.ContractNegotiationAgreed; +import org.eclipse.edc.connector.controlplane.contract.spi.event.contractnegotiation.ContractNegotiationFinalized; +import org.eclipse.edc.connector.controlplane.contract.spi.event.contractnegotiation.ContractNegotiationInitiated; +import org.eclipse.edc.connector.controlplane.contract.spi.event.contractnegotiation.ContractNegotiationRequested; +import org.eclipse.edc.connector.controlplane.contract.spi.event.contractnegotiation.ContractNegotiationVerified; +import org.eclipse.edc.connector.controlplane.transfer.spi.event.TransferProcessInitiated; +import org.eclipse.edc.connector.controlplane.transfer.spi.event.TransferProcessProvisioned; +import org.eclipse.edc.connector.controlplane.transfer.spi.event.TransferProcessRequested; +import org.eclipse.edc.connector.controlplane.transfer.spi.event.TransferProcessStarted; +import org.eclipse.edc.junit.annotations.EndToEndTest; +import org.eclipse.edc.junit.annotations.PostgresqlIntegrationTest; +import org.eclipse.edc.policy.model.Operator; +import org.eclipse.tractusx.edc.tests.helpers.EdrNegotiationHelperFunctions; +import org.eclipse.tractusx.edc.tests.helpers.PolicyHelperFunctions; +import org.eclipse.tractusx.edc.tests.participant.TransferParticipant; +import org.eclipse.tractusx.edc.tests.runtimes.ParticipantRuntime; +import org.eclipse.tractusx.edc.tests.runtimes.PgParticipantRuntime; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; +import static org.eclipse.tractusx.edc.tests.TestRuntimeConfiguration.PLATO_BPN; +import static org.eclipse.tractusx.edc.tests.TestRuntimeConfiguration.PLATO_NAME; +import static org.eclipse.tractusx.edc.tests.TestRuntimeConfiguration.SOKRATES_BPN; +import static org.eclipse.tractusx.edc.tests.TestRuntimeConfiguration.SOKRATES_NAME; +import static org.eclipse.tractusx.edc.tests.helpers.EdrNegotiationHelperFunctions.createEvent; +import static org.eclipse.tractusx.edc.tests.helpers.Functions.waitForEvent; +import static org.eclipse.tractusx.edc.tests.participant.TractusxParticipantBase.ASYNC_POLL_INTERVAL; +import static org.eclipse.tractusx.edc.tests.participant.TractusxParticipantBase.ASYNC_TIMEOUT; +import static org.eclipse.tractusx.edc.tests.runtimes.Runtimes.memoryRuntime; +import static org.eclipse.tractusx.edc.tests.runtimes.Runtimes.pgRuntime; + +public class NegotiateEdrTest { + + protected static final TransferParticipant SOKRATES = TransferParticipant.Builder.newInstance() + .name(SOKRATES_NAME) + .id(SOKRATES_BPN) + .build(); + + protected static final TransferParticipant PLATO = TransferParticipant.Builder.newInstance() + .name(PLATO_NAME) + .id(PLATO_BPN) + .build(); + + + abstract static class Tests { + + MockWebServer server; + + @BeforeEach + void setup() { + server = new MockWebServer(); + } + + @Test + @DisplayName("Verify that the callbacks are invoked when negotiating an EDR") + void negotiateEdr_shouldInvokeCallbacks() throws IOException { + + var expectedEvents = List.of( + createEvent(ContractNegotiationInitiated.class), + createEvent(ContractNegotiationRequested.class), + createEvent(ContractNegotiationAgreed.class), + createEvent(ContractNegotiationFinalized.class), + createEvent(ContractNegotiationVerified.class), + createEvent(TransferProcessInitiated.class), + createEvent(TransferProcessProvisioned.class), + createEvent(TransferProcessRequested.class), + createEvent(TransferProcessStarted.class)); + + var assetId = "api-asset-1"; + var url = server.url("/mock/api"); + server.start(); + + var authCodeHeaderName = "test-authkey"; + var authCode = "test-authcode"; + Map dataAddress = Map.of( + "name", "transfer-test", + "baseUrl", url.toString(), + "type", "HttpData", + "contentType", "application/json", + "authKey", authCodeHeaderName, + "authCode", authCode + ); + + PLATO.createAsset(assetId, Map.of(), dataAddress); + + PLATO.storeBusinessPartner(SOKRATES.getBpn(), "test-group1", "test-group2"); + var accessPolicy = PLATO.createPolicyDefinition(PolicyHelperFunctions.bpnGroupPolicy(Operator.IS_NONE_OF, "forbidden-policy")); + var contractPolicy = PLATO.createPolicyDefinition(PolicyHelperFunctions.bpnGroupPolicy(Operator.IS_ALL_OF, "test-group1", "test-group2")); + PLATO.createContractDefinition(assetId, "def-1", accessPolicy, contractPolicy); + + + expectedEvents.forEach(event -> server.enqueue(new MockResponse())); + + var callbacks = Json.createArrayBuilder() + .add(EdrNegotiationHelperFunctions.createCallback(url.toString(), true, Set.of("contract.negotiation", "transfer.process"))) + .build(); + + var contractNegotiationId = SOKRATES.edrs().negotiateEdr(PLATO, assetId, callbacks); + + var events = expectedEvents.stream() + .map(receivedEvent -> waitForEvent(server)) + .collect(Collectors.toList()); + + + await().pollInterval(ASYNC_POLL_INTERVAL) + .atMost(ASYNC_TIMEOUT) + .untilAsserted(() -> { + var edrCaches = SOKRATES.edrs().getEdrEntriesByAssetId(assetId); + assertThat(edrCaches).hasSize(1); + }); + + assertThat(expectedEvents).usingRecursiveFieldByFieldElementComparator().containsAll(events); + + var edrCaches = SOKRATES.edrs().getEdrEntriesByAssetId(assetId); + + assertThat(edrCaches).hasSize(1); + + assertThat(SOKRATES.edrs().getEdrEntriesByContractNegotiationId(contractNegotiationId)).hasSize(1); + + assertThat(edrCaches).hasSize(1); + + var transferProcessId = edrCaches.get(0).asJsonObject().getString("transferProcessId"); + var cnId = edrCaches.get(0).asJsonObject().getString("contractNegotiationId"); + var agreementId = edrCaches.get(0).asJsonObject().getString("agreementId"); + + assertThat(cnId).isEqualTo(contractNegotiationId); + assertThat(SOKRATES.edrs().getEdrEntriesByAgreementId(agreementId)).hasSize(1); + + + var edr = SOKRATES.edrs().getEdr(transferProcessId); + + assertThat(edr.getJsonString("type").getString()).isEqualTo("https://w3id.org/idsa/v4.1/HTTP"); + assertThat(edr.getJsonString("endpoint").getString()).isNotNull(); + assertThat(edr.getJsonString("endpointType").getString()).isEqualTo(edr.getJsonString("type").getString()); + assertThat(edr.getJsonString("authorization").getString()).isNotNull(); + + } + + @AfterEach + void teardown() throws IOException { + server.shutdown(); + } + } + + @Nested + @EndToEndTest + class InMemory extends Tests { + + @RegisterExtension + protected static final ParticipantRuntime SOKRATES_RUNTIME = memoryRuntime(SOKRATES.getName(), SOKRATES.getBpn(), SOKRATES.getConfiguration()); + + @RegisterExtension + protected static final ParticipantRuntime PLATO_RUNTIME = memoryRuntime(PLATO.getName(), PLATO.getBpn(), PLATO.getConfiguration()); + + } + + @Nested + @PostgresqlIntegrationTest + class Postgres extends Tests { + + @RegisterExtension + protected static final PgParticipantRuntime SOKRATES_RUNTIME = pgRuntime(SOKRATES.getName(), SOKRATES.getBpn(), SOKRATES.getConfiguration()); + + @RegisterExtension + protected static final PgParticipantRuntime PLATO_RUNTIME = pgRuntime(PLATO.getName(), PLATO.getBpn(), PLATO.getConfiguration()); + + } +} diff --git a/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/helpers/EdrNegotiationHelperFunctions.java b/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/helpers/EdrNegotiationHelperFunctions.java index 76a927708..425331fd9 100644 --- a/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/helpers/EdrNegotiationHelperFunctions.java +++ b/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/helpers/EdrNegotiationHelperFunctions.java @@ -23,11 +23,11 @@ import jakarta.json.JsonArray; import jakarta.json.JsonArrayBuilder; import jakarta.json.JsonObject; +import org.eclipse.edc.connector.controlplane.contract.spi.types.negotiation.ContractRequest; import org.eclipse.edc.jsonld.TitaniumJsonLd; import org.eclipse.edc.jsonld.spi.JsonLd; import org.eclipse.edc.spi.event.Event; import org.eclipse.edc.spi.monitor.ConsoleMonitor; -import org.eclipse.tractusx.edc.api.edr.v2.dto.NegotiateEdrRequestDto; import java.util.Set; @@ -40,7 +40,7 @@ public class EdrNegotiationHelperFunctions { public static JsonObject createEdrNegotiationRequest(String connectorAddress, String providerId, String offerId, String assetId, JsonObject policy, JsonArray callbacks) { return Json.createObjectBuilder() - .add(TYPE, NegotiateEdrRequestDto.EDR_REQUEST_DTO_TYPE) + .add(TYPE, ContractRequest.CONTRACT_REQUEST_TYPE) .add(EDC_NAMESPACE + "counterPartyId", providerId) .add(EDC_NAMESPACE + "counterPartyAddress", connectorAddress) .add(EDC_NAMESPACE + "protocol", "dataspace-protocol-http") diff --git a/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PgParticipantRuntime.java b/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PgParticipantRuntime.java index 9d720b30f..b3bfe98f4 100644 --- a/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PgParticipantRuntime.java +++ b/edc-tests/edc-controlplane/fixtures/src/testFixtures/java/org/eclipse/tractusx/edc/tests/runtimes/PgParticipantRuntime.java @@ -81,7 +81,7 @@ public Map postgresqlConfiguration(String name) { var jdbcUrl = jdbcUrl(name); return new HashMap<>() { { - Stream.of("asset", "contractdefinition", "contractnegotiation", "policy", "transferprocess", "edr", "bpn", "policy-monitor") + Stream.of("asset", "contractdefinition", "contractnegotiation", "policy", "transferprocess", "bpn", "policy-monitor") .forEach(context -> { var group = "edc.datasource." + context; put(group + ".name", context); diff --git a/spi/edr-spi/src/main/java/org/eclipse/tractusx/edc/edr/spi/types/NegotiateEdrRequest.java b/spi/edr-spi/src/main/java/org/eclipse/tractusx/edc/edr/spi/types/NegotiateEdrRequest.java deleted file mode 100644 index efd5299a5..000000000 --- a/spi/edr-spi/src/main/java/org/eclipse/tractusx/edc/edr/spi/types/NegotiateEdrRequest.java +++ /dev/null @@ -1,107 +0,0 @@ -/******************************************************************************** - * Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - * - * See the NOTICE file(s) distributed with this work for additional - * information regarding copyright ownership. - * - * This program and the accompanying materials are made available under the - * terms of the Apache License, Version 2.0 which is available at - * https://www.apache.org/licenses/LICENSE-2.0. - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - * SPDX-License-Identifier: Apache-2.0 - ********************************************************************************/ - -package org.eclipse.tractusx.edc.edr.spi.types; - -import org.eclipse.edc.connector.controlplane.contract.spi.types.offer.ContractOffer; -import org.eclipse.edc.spi.types.domain.callback.CallbackAddress; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -public class NegotiateEdrRequest { - - private String connectorAddress; - private String protocol = "ids-multipart"; - private String connectorId; - private ContractOffer offer; - - private List callbackAddresses = new ArrayList<>(); - - private NegotiateEdrRequest() { - - } - - public String getConnectorAddress() { - return connectorAddress; - } - - public String getProtocol() { - return protocol; - } - - public String getConnectorId() { - return connectorId; - } - - - public List getCallbackAddresses() { - return callbackAddresses; - } - - public ContractOffer getOffer() { - return offer; - } - - - public static final class Builder { - private final NegotiateEdrRequest entity; - - private Builder() { - entity = new NegotiateEdrRequest(); - } - - public static Builder newInstance() { - return new Builder(); - } - - public Builder connectorAddress(String connectorAddress) { - entity.connectorAddress = connectorAddress; - return this; - } - - public Builder protocol(String protocol) { - entity.protocol = protocol; - return this; - } - - public Builder connectorId(String connectorId) { - entity.connectorId = connectorId; - return this; - } - - public Builder offer(ContractOffer offer) { - entity.offer = offer; - return this; - } - - public Builder callbackAddresses(List callbackAddresses) { - entity.callbackAddresses = callbackAddresses; - return this; - } - - public NegotiateEdrRequest build() { - Objects.requireNonNull(entity.protocol, "protocol should not be null"); - Objects.requireNonNull(entity.connectorAddress, "connector address should not be null"); - Objects.requireNonNull(entity.offer, "offer should not be null"); - return entity; - } - } -}