From ced411bdfa2298f9b85efda4e0038c6436ede94a Mon Sep 17 00:00:00 2001 From: Ronja Quensel Date: Fri, 21 Jul 2023 10:39:00 +0200 Subject: [PATCH] chore: use map instead of DataAddress for input --- extensions/wrapper/wrapper/build.gradle.kts | 2 - .../usecase/model/ConsumptionInputDto.java | 5 ++- .../usecase/services/ConsumptionService.java | 41 +++++++++++++++++-- .../UseCaseResourceIntegrationTest.java | 7 +--- .../services/ConsumptionServiceTest.java | 28 ++++++------- 5 files changed, 56 insertions(+), 27 deletions(-) diff --git a/extensions/wrapper/wrapper/build.gradle.kts b/extensions/wrapper/wrapper/build.gradle.kts index 646f288a9..41994ea74 100644 --- a/extensions/wrapper/wrapper/build.gradle.kts +++ b/extensions/wrapper/wrapper/build.gradle.kts @@ -50,7 +50,6 @@ dependencies { testImplementation("org.junit.jupiter:junit-jupiter-params:5.9.3") testImplementation("org.awaitility:awaitility:4.2.0") - //TODO move to own module? testImplementation("${edcGroup}:iam-mock:${edcVersion}") testImplementation("${edcGroup}:dsp:${edcVersion}") testImplementation("${edcGroup}:management-api:${edcVersion}") @@ -60,7 +59,6 @@ val openapiFileDir = "${project.buildDir}/swagger" val openapiFileFilename = "edc-api-wrapper.yaml" val openapiFile = "$openapiFileDir/$openapiFileFilename" -//TODO move to own module? tasks.register("printClasspath") { doLast { println(sourceSets["main"].runtimeClasspath.asPath) diff --git a/extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/usecase/model/ConsumptionInputDto.java b/extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/usecase/model/ConsumptionInputDto.java index dc7120f81..21dfd5bc5 100644 --- a/extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/usecase/model/ConsumptionInputDto.java +++ b/extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/usecase/model/ConsumptionInputDto.java @@ -5,7 +5,8 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.eclipse.edc.spi.types.domain.DataAddress; + +import java.util.Map; @AllArgsConstructor @NoArgsConstructor @@ -17,5 +18,5 @@ public class ConsumptionInputDto { private String offerId; private String assetId; private PolicyDto policy; - private DataAddress dataDestination; + private Map dataDestination; } diff --git a/extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/usecase/services/ConsumptionService.java b/extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/usecase/services/ConsumptionService.java index e6d341629..8048d4556 100644 --- a/extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/usecase/services/ConsumptionService.java +++ b/extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/usecase/services/ConsumptionService.java @@ -18,15 +18,21 @@ import org.eclipse.edc.connector.transfer.spi.types.DataRequest; import org.eclipse.edc.connector.transfer.spi.types.TransferRequest; import org.eclipse.edc.spi.result.Result; +import org.eclipse.edc.spi.types.domain.DataAddress; import org.eclipse.edc.transform.spi.TypeTransformerRegistry; import org.eclipse.edc.web.spi.exception.InvalidRequestException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.AbstractMap; import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import static java.lang.String.format; import static java.util.UUID.randomUUID; +import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE; @RequiredArgsConstructor @Slf4j @@ -85,12 +91,13 @@ public void negotiationConfirmed(ContractNegotiation contractNegotiation) { if (process != null) { var agreementId = contractNegotiation.getContractAgreement().getId(); + var destination = createDataAddress(process.getInput().getDataDestination()); var dataRequest = DataRequest.Builder.newInstance() .id(randomUUID().toString()) .connectorId(process.getInput().getConnectorId()) .connectorAddress(process.getInput().getConnectorAddress()) .protocol("dataspace-protocol-http") - .dataDestination(process.getInput().getDataDestination()) + .dataDestination(destination) .assetId(process.getInput().getAssetId()) .contractId(agreementId) .build(); @@ -125,7 +132,7 @@ public ConsumptionOutputDto getConsumptionProcess(String id) { var transferProcessDto = Optional.ofNullable(process.getTransferProcessId()) .map(transferProcessStore::findById) .map(tp -> transformerRegistry.transform(tp, TransferProcessOutputDto.class)) - .map(this::logIfFailedResult) + .map(this::logIfFailedResult) //TODO throw exception on error .filter(Result::succeeded) .map(Result::getContent) .orElse(null); @@ -153,8 +160,12 @@ private void validateInput(ConsumptionInputDto input) { if (input.getPolicy() == null) throw new InvalidRequestException(format(message, "policy")); - if (input.getDataDestination() == null) + var destination = input.getDataDestination(); + if (destination == null) throw new InvalidRequestException(format(message, "dataDestination")); + + if (!destination.containsKey("type") && !destination.containsKey(EDC_NAMESPACE + "type")) + throw new InvalidRequestException("dataDestination must have type property."); } private ConsumptionDto findByNegotiation(ContractNegotiation contractNegotiation) { @@ -166,6 +177,30 @@ private ConsumptionDto findByNegotiation(ContractNegotiation contractNegotiation .orElse(null); } + private DataAddress createDataAddress(Map properties) { + var nameSpacedProperties = properties.entrySet().stream() + .map(entry -> { + if (isValidUri(entry.getKey())) { + return new AbstractMap.SimpleEntry<>(entry.getKey(), entry.getValue()); + } + var key = EDC_NAMESPACE + entry.getKey(); + return new AbstractMap.SimpleEntry<>(key, entry.getValue()); + }) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + return DataAddress.Builder.newInstance() + .properties(nameSpacedProperties) + .build(); + } + + private boolean isValidUri(String string) { + try { + new URI(string); + return true; + } catch (URISyntaxException e) { + return false; + } + } + private Result logIfFailedResult(Result result) { if (result.failed()) { log.error(format("Failed to transform contract negotiation: %s", diff --git a/extensions/wrapper/wrapper/src/test/java/de/sovity/edc/ext/wrapper/api/usecase/UseCaseResourceIntegrationTest.java b/extensions/wrapper/wrapper/src/test/java/de/sovity/edc/ext/wrapper/api/usecase/UseCaseResourceIntegrationTest.java index 239964c29..d4b919515 100644 --- a/extensions/wrapper/wrapper/src/test/java/de/sovity/edc/ext/wrapper/api/usecase/UseCaseResourceIntegrationTest.java +++ b/extensions/wrapper/wrapper/src/test/java/de/sovity/edc/ext/wrapper/api/usecase/UseCaseResourceIntegrationTest.java @@ -10,7 +10,6 @@ import de.sovity.edc.ext.wrapper.api.usecase.model.PolicyDefinitionRequestDto; import jakarta.json.JsonArray; import org.eclipse.edc.junit.extensions.EdcRuntimeExtension; -import org.eclipse.edc.spi.types.domain.DataAddress; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -109,7 +108,7 @@ void consumeOffering() { .extract() .path("id"); - // wait until transfer has been terminated (due to unsupported data address type) + // wait until transfer has been terminated (will be due to unsupported data address type) await().atMost(45, TimeUnit.SECONDS).untilAsserted(() -> given() .baseUri(consumerManagementUrl.toString()) .contentType(JSON) @@ -170,9 +169,7 @@ private ConsumptionInputDto consumptionInputDto() { .policy(PolicyDto.builder() .permission(PermissionDto.builder().build()) .build()) - .dataDestination(DataAddress.Builder.newInstance() - .type("test") - .build()) + .dataDestination(Map.of("type", "test")) .build(); } } diff --git a/extensions/wrapper/wrapper/src/test/java/de/sovity/edc/ext/wrapper/api/usecase/services/ConsumptionServiceTest.java b/extensions/wrapper/wrapper/src/test/java/de/sovity/edc/ext/wrapper/api/usecase/services/ConsumptionServiceTest.java index f5f93063f..2b239dae6 100644 --- a/extensions/wrapper/wrapper/src/test/java/de/sovity/edc/ext/wrapper/api/usecase/services/ConsumptionServiceTest.java +++ b/extensions/wrapper/wrapper/src/test/java/de/sovity/edc/ext/wrapper/api/usecase/services/ConsumptionServiceTest.java @@ -19,7 +19,6 @@ import org.eclipse.edc.policy.model.Policy; import org.eclipse.edc.service.spi.result.ServiceResult; import org.eclipse.edc.spi.result.Result; -import org.eclipse.edc.spi.types.domain.DataAddress; import org.eclipse.edc.transform.spi.TypeTransformerRegistry; import org.eclipse.edc.web.spi.exception.InvalidRequestException; import org.junit.jupiter.api.BeforeEach; @@ -53,6 +52,7 @@ class ConsumptionServiceTest { private static final String negotiationId = "negotiationId"; private static final String transferProcessId = "transferProcessId"; private static final String agreementId = "agreementId"; + private static final String dataAddressTypeProperty = "type"; private final ContractNegotiationService negotiationService = mock(ContractNegotiationService.class); private final TransferProcessService transferProcessService = mock(TransferProcessService.class); @@ -85,7 +85,7 @@ void startConsumptionProcess_shouldInitiateNegotiation_whenValidInput() { .assetId(assetId) .offerId(offerId) .policy(policyDto()) - .dataDestination(dataAddress()) + .dataDestination(dataAddressProperties()) .build(); // ACT @@ -114,7 +114,7 @@ void startConsumptionProcess_shouldThrowException_whenInvalidInput(String connec String requestedAssetId, String contractOfferId, PolicyDto policy, - DataAddress destination) { + Map destination) { // ARRANGE var input = ConsumptionInputDto.builder() .connectorId(connectorId) @@ -151,8 +151,8 @@ void negotiationConfirmed_shouldInitiateTransfer_whenProcessExists() throws Exce assertThat(dataRequest.getContractId()).isEqualTo(agreementId); assertThat(dataRequest.getConnectorId()).isEqualTo(counterPartyId); assertThat(dataRequest.getConnectorAddress()).isEqualTo(counterPartyAddress); - assertThat(dataRequest.getDataDestination()) - .isEqualTo(process.getInput().getDataDestination()); + assertThat(dataRequest.getDataDestination().getType()) + .isEqualTo(process.getInput().getDataDestination().get(dataAddressTypeProperty)); assertThat(process.getTransferProcessId()) .isNotNull() @@ -374,10 +374,8 @@ private static Policy policy() { .build(); } - private static DataAddress dataAddress() { - return DataAddress.Builder.newInstance() - .type("test") - .build(); + private static Map dataAddressProperties() { + return Map.of(dataAddressTypeProperty, "test"); } private static TransferProcess transferProcess() { @@ -395,7 +393,7 @@ private static TransferProcessOutputDto transferProcessOutputDto() { } private static ConsumptionDto consumptionDto(String negotiationId, String transferProcessId) { - var destination = dataAddress(); + var destination = dataAddressProperties(); var input = ConsumptionInputDto.builder() .connectorId(counterPartyId) .connectorAddress(counterPartyAddress) @@ -423,15 +421,15 @@ private static class InvalidInputArgumentsProvider implements ArgumentsProvider public Stream provideArguments(ExtensionContext context) { return Stream.of( Arguments.of(null, counterPartyAddress, assetId, offerId, policyDto(), - dataAddress()), + dataAddressProperties()), Arguments.of(counterPartyId, null, assetId, offerId, policyDto(), - dataAddress()), + dataAddressProperties()), Arguments.of(counterPartyId, counterPartyAddress, null, offerId, policyDto(), - dataAddress()), + dataAddressProperties()), Arguments.of(counterPartyId, counterPartyAddress, assetId, null, policyDto(), - dataAddress()), + dataAddressProperties()), Arguments.of(counterPartyId, counterPartyAddress, assetId, offerId, null, - dataAddress()), + dataAddressProperties()), Arguments.of(counterPartyId, counterPartyAddress, assetId, offerId, policyDto(), null) );