Skip to content

Commit

Permalink
chore: use map instead of DataAddress for input
Browse files Browse the repository at this point in the history
  • Loading branch information
ronjaquensel committed Jul 21, 2023
1 parent da66276 commit ced411b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 27 deletions.
2 changes: 0 additions & 2 deletions extensions/wrapper/wrapper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,5 +18,5 @@ public class ConsumptionInputDto {
private String offerId;
private String assetId;
private PolicyDto policy;
private DataAddress dataDestination;
private Map<String, String> dataDestination;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -166,6 +177,30 @@ private ConsumptionDto findByNegotiation(ContractNegotiation contractNegotiation
.orElse(null);
}

private DataAddress createDataAddress(Map<String, String> 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 <T> Result<T> logIfFailedResult(Result<T> result) {
if (result.failed()) {
log.error(format("Failed to transform contract negotiation: %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -85,7 +85,7 @@ void startConsumptionProcess_shouldInitiateNegotiation_whenValidInput() {
.assetId(assetId)
.offerId(offerId)
.policy(policyDto())
.dataDestination(dataAddress())
.dataDestination(dataAddressProperties())
.build();

// ACT
Expand Down Expand Up @@ -114,7 +114,7 @@ void startConsumptionProcess_shouldThrowException_whenInvalidInput(String connec
String requestedAssetId,
String contractOfferId,
PolicyDto policy,
DataAddress destination) {
Map<String, String> destination) {
// ARRANGE
var input = ConsumptionInputDto.builder()
.connectorId(connectorId)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -374,10 +374,8 @@ private static Policy policy() {
.build();
}

private static DataAddress dataAddress() {
return DataAddress.Builder.newInstance()
.type("test")
.build();
private static Map<String, String> dataAddressProperties() {
return Map.of(dataAddressTypeProperty, "test");
}

private static TransferProcess transferProcess() {
Expand All @@ -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)
Expand Down Expand Up @@ -423,15 +421,15 @@ private static class InvalidInputArgumentsProvider implements ArgumentsProvider
public Stream<? extends Arguments> 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)
);
Expand Down

0 comments on commit ced411b

Please sign in to comment.