Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/sovity/edc-extensions into …
Browse files Browse the repository at this point in the history
…461-add-contract-definition-page-to-the-api-wrapper

# Conflicts:
#	extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/WrapperExtensionContextBuilder.java
#	extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/ui/UiResource.java
  • Loading branch information
SaadEGI committed Jul 26, 2023
2 parents f65a7dd + 8438245 commit 94395a6
Show file tree
Hide file tree
Showing 9 changed files with 481 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2023 sovity GmbH
*
* 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:
* sovity GmbH - initial API and implementation
*
*/

package de.sovity.edc.client;

import org.eclipse.edc.connector.contract.spi.negotiation.store.ContractNegotiationStore;
import org.eclipse.edc.connector.spi.asset.AssetService;
import org.eclipse.edc.connector.transfer.spi.store.TransferProcessStore;
import org.eclipse.edc.junit.annotations.ApiTest;
import org.eclipse.edc.junit.extensions.EdcExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.text.ParseException;
import java.util.Map;

import static de.sovity.edc.client.TransferProcessTestUtils.createConsumingTransferProcesses;
import static de.sovity.edc.client.TransferProcessTestUtils.createProvidingTransferProcesses;
import static org.assertj.core.api.Assertions.assertThat;

@ApiTest
@ExtendWith(EdcExtension.class)
class GetTransferProcessAssetApiServiceTest {

@BeforeEach
void setUp(EdcExtension extension) {
extension.setConfiguration(TestUtils.createConfiguration(Map.of()));
}

@Test
void testProviderTransferProcess(ContractNegotiationStore negotiationStore,
TransferProcessStore transferProcessStore,
AssetService assetStore) throws ParseException {

var client = TestUtils.edcClient();

//arrange data
createProvidingTransferProcesses(negotiationStore, transferProcessStore, assetStore);

//act
var providerAssetResult = client.uiApi().getTransferProcessAsset(TransferProcessTestUtils.PROVIDING_TRANSFER_PROCESS_ID);

//assert
assertThat(providerAssetResult.getAssetId()).isEqualTo(TransferProcessTestUtils.VALID_ASSET_ID);
assertThat(providerAssetResult.getProperties().get("asset:prop:name")).isEqualTo(TransferProcessTestUtils.ASSET_NAME);
}

@Test
void testConsumerTransferProcess(ContractNegotiationStore negotiationStore,
TransferProcessStore transferProcessStore) throws ParseException {

var client = TestUtils.edcClient();

//arrange data
createConsumingTransferProcesses(negotiationStore, transferProcessStore);

//act
var consumerAssetResult = client.uiApi().getTransferProcessAsset(TransferProcessTestUtils.CONSUMING_TRANSFER_PROCESS_ID);

//assert
assertThat(consumerAssetResult.getAssetId()).isEqualTo(TransferProcessTestUtils.UNKNOWN_ASSET_ID);
assertThat(consumerAssetResult.getProperties().get("asset:prop:name")).isNull();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2023 sovity GmbH
*
* 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:
* sovity GmbH - initial API and implementation
*
*/

package de.sovity.edc.client;

import org.eclipse.edc.connector.contract.spi.negotiation.store.ContractNegotiationStore;
import org.eclipse.edc.connector.spi.asset.AssetService;
import org.eclipse.edc.connector.transfer.spi.store.TransferProcessStore;
import org.eclipse.edc.junit.annotations.ApiTest;
import org.eclipse.edc.junit.extensions.EdcExtension;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.text.ParseException;
import java.util.Map;

import static de.sovity.edc.client.TransferProcessTestUtils.createConsumingTransferProcesses;
import static de.sovity.edc.client.TransferProcessTestUtils.createProvidingTransferProcesses;
import static de.sovity.edc.client.gen.model.TransferHistoryEntry.DirectionEnum.CONSUMING;
import static de.sovity.edc.client.gen.model.TransferHistoryEntry.DirectionEnum.PROVIDING;
import static org.assertj.core.api.Assertions.assertThat;

@ApiTest
@ExtendWith(EdcExtension.class)
class TransferHistoryPageApiServiceTest {

@BeforeEach
void setUp(EdcExtension extension) {
extension.setConfiguration(TestUtils.createConfiguration(Map.of()));
}

@Test
void startTransferProcessForAgreement(
ContractNegotiationStore negotiationStore,
TransferProcessStore transferProcessStore,
AssetService assetStore
) throws ParseException {
var client = TestUtils.edcClient();

// arrange
createProvidingTransferProcesses(negotiationStore, transferProcessStore, assetStore);
createConsumingTransferProcesses(negotiationStore, transferProcessStore);

// act
var result = client.uiApi().transferHistoryPageEndpoint();

// get
var transferProcess = result.getTransferEntries();

// assert for the order of entries
assertThat(transferProcess.get(1).getTransferProcessId()).isEqualTo(TransferProcessTestUtils.PROVIDING_TRANSFER_PROCESS_ID);

// assert for consuming request entry
var consumingProcess = transferProcess.get(0);
assertThat(consumingProcess.getTransferProcessId()).isEqualTo(TransferProcessTestUtils.CONSUMING_TRANSFER_PROCESS_ID);
assertThat(consumingProcess.getAssetId()).isEqualTo(TransferProcessTestUtils.UNKNOWN_ASSET_ID);
assertThat(consumingProcess.getCounterPartyConnectorEndpoint()).isEqualTo(TransferProcessTestUtils.COUNTER_PARTY_ADDRESS);
assertThat(consumingProcess.getContractAgreementId()).isEqualTo(TransferProcessTestUtils.CONSUMING_CONTRACT_ID);
assertThat(consumingProcess.getDirection()).isEqualTo(CONSUMING);
assertThat(consumingProcess.getState().getCode()).isEqualTo(800);
assertThat(consumingProcess.getAssetName()).isEqualTo(TransferProcessTestUtils.UNKNOWN_ASSET_ID);
assertThat(consumingProcess.getErrorMessage()).isEqualTo("");

// assert for providing request entry
var providingProcess = transferProcess.get(1);
assertThat(providingProcess.getTransferProcessId()).isEqualTo(TransferProcessTestUtils.PROVIDING_TRANSFER_PROCESS_ID);
assertThat(providingProcess.getAssetId()).isEqualTo(TransferProcessTestUtils.VALID_ASSET_ID);
assertThat(providingProcess.getCounterPartyConnectorEndpoint()).isEqualTo(TransferProcessTestUtils.COUNTER_PARTY_ADDRESS);
assertThat(providingProcess.getContractAgreementId()).isEqualTo(TransferProcessTestUtils.PROVIDING_CONTRACT_ID);
assertThat(providingProcess.getDirection()).isEqualTo(PROVIDING);
assertThat(providingProcess.getState().getCode()).isEqualTo(800);
assertThat(providingProcess.getAssetName()).isEqualTo(TransferProcessTestUtils.ASSET_NAME);
assertThat(providingProcess.getErrorMessage()).isEqualTo("TransferProcessManager: attempt #8 failed to send transfer");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package de.sovity.edc.client;

import org.eclipse.edc.connector.contract.spi.negotiation.store.ContractNegotiationStore;
import org.eclipse.edc.connector.contract.spi.types.agreement.ContractAgreement;
import org.eclipse.edc.connector.contract.spi.types.negotiation.ContractNegotiation;
import org.eclipse.edc.connector.spi.asset.AssetService;
import org.eclipse.edc.connector.transfer.spi.store.TransferProcessStore;
import org.eclipse.edc.connector.transfer.spi.types.DataRequest;
import org.eclipse.edc.connector.transfer.spi.types.TransferProcess;
import org.eclipse.edc.policy.model.Policy;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.eclipse.edc.spi.types.domain.asset.Asset;
import org.jetbrains.annotations.NotNull;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.UUID;

public class TransferProcessTestUtils {
public static final String DATA_SINK = "http://my-data-sink/api/stuff";
public static final String COUNTER_PARTY_ADDRESS = "http://some-other-connector/api/v1/ids/data";
public static final String PROVIDING_CONTRACT_ID = "provider-contract:eb934d1f-6582-4bab-85e6-af19a76f7e2b";
public static final String CONSUMING_CONTRACT_ID = "consumer-contract:f52a5d30-6356-4a55-a75a-3c45d7a88c3e";
public static final String VALID_ASSET_ID = "urn:artifact:asset";
public static final String UNKNOWN_ASSET_ID = "urn:junk";
public static final String ASSET_NAME = "Test asset";
public static final String PROVIDING_TRANSFER_PROCESS_ID = "81cdf4cf-8427-480f-9662-8a29d66ddd3b";
public static final String CONSUMING_TRANSFER_PROCESS_ID = "be0cac12-bb43-420e-aa29-d66bb3d0e0ac";

@NotNull
public static void createProvidingTransferProcesses(ContractNegotiationStore store, TransferProcessStore transferProcessStore, AssetService assetStore) throws ParseException {
DataAddress dataAddress = getDataAddress();
createAsset(assetStore, dataAddress, VALID_ASSET_ID, ASSET_NAME);

// preparing providing transfer process
var providerAgreement = createContractAgreement(PROVIDING_CONTRACT_ID, VALID_ASSET_ID);
createContractNegotiation(store, COUNTER_PARTY_ADDRESS, providerAgreement, ContractNegotiation.Type.PROVIDER);
createTransferProcess(VALID_ASSET_ID,
PROVIDING_CONTRACT_ID,
dataAddress,
TransferProcess.Type.PROVIDER,
PROVIDING_TRANSFER_PROCESS_ID,
"2023-07-08",
"TransferProcessManager: attempt #8 failed to send transfer",
transferProcessStore);
}

@NotNull
public static void createConsumingTransferProcesses(ContractNegotiationStore store, TransferProcessStore transferProcessStore) throws ParseException {
DataAddress dataAddress = getDataAddress();

// preparing consuming transfer process
var consumerAgreement = createContractAgreement(CONSUMING_CONTRACT_ID, UNKNOWN_ASSET_ID);
createContractNegotiation(store, COUNTER_PARTY_ADDRESS, consumerAgreement, ContractNegotiation.Type.CONSUMER);
createTransferProcess(UNKNOWN_ASSET_ID,
CONSUMING_CONTRACT_ID,
dataAddress,
TransferProcess.Type.CONSUMER,
CONSUMING_TRANSFER_PROCESS_ID,
"2023-07-10",
"",
transferProcessStore);
}

private static DataAddress getDataAddress() {
var dataAddress = DataAddress.Builder.newInstance()
.type("HttpData")
.property("baseUrl", DATA_SINK)
.build();
return dataAddress;
}

private static void createAsset(AssetService assetStore, DataAddress dataAddress, String assetId, String assetName) throws ParseException {
var asset = Asset.Builder.newInstance()
.id(assetId)
.property("asset:prop:name", assetName)
.createdAt(dateFormatterToLong("2023-06-01"))
.build();

assetStore.create(asset, dataAddress);
}

private static ContractAgreement createContractAgreement(
String agreementId,
String assetId
) {
var agreement = ContractAgreement.Builder.newInstance()
.id(agreementId)
.providerAgentId(UUID.randomUUID().toString())
.consumerAgentId(UUID.randomUUID().toString())
.assetId(assetId)
.policy(Policy.Builder.newInstance().build())
.build();

return agreement;
}

private static void createContractNegotiation(
ContractNegotiationStore store,
String counterPartyAddress,
ContractAgreement agreement,
ContractNegotiation.Type type
) {
var negotiation = ContractNegotiation.Builder.newInstance()
.id(UUID.randomUUID().toString())
.counterPartyId(UUID.randomUUID().toString())
.counterPartyAddress(counterPartyAddress)
.protocol("protocol")
.contractAgreement(agreement)
.type(type)
.correlationId(UUID.randomUUID().toString())
.build();

store.save(negotiation);
}

private static void createTransferProcess(String assetId, String contractId, DataAddress dataAddress, TransferProcess.Type type, String transferProcessId, String lastUpdateDateForTransferProcess, String errorMessage, TransferProcessStore transferProcessStore) throws ParseException {

var dataRequestForTransfer = DataRequest.Builder.newInstance()
.assetId(assetId)
.contractId(contractId)
.dataDestination(dataAddress)
.build();

var transferProcess = TransferProcess.Builder.newInstance()
.id(transferProcessId)
.type(type)
.dataRequest(dataRequestForTransfer)
.createdAt(dateFormatterToLong("2023-07-08"))
.updatedAt(dateFormatterToLong(lastUpdateDateForTransferProcess))
.state(800)
.errorDetail(errorMessage)
.build();

transferProcessStore.save(transferProcess);
}

private static long dateFormatterToLong(String date) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
return formatter.parse(date).getTime();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.edc.connector.contract.spi.negotiation.store.ContractNegotiationStore;
import org.eclipse.edc.connector.contract.spi.offer.store.ContractDefinitionStore;
import org.eclipse.edc.connector.policy.spi.store.PolicyDefinitionStore;
import org.eclipse.edc.connector.spi.asset.AssetService;
import org.eclipse.edc.connector.spi.contractagreement.ContractAgreementService;
import org.eclipse.edc.connector.spi.contractdefinition.ContractDefinitionService;
import org.eclipse.edc.connector.spi.contractnegotiation.ContractNegotiationService;
Expand All @@ -38,6 +39,8 @@ public class WrapperExtension implements ServiceExtension {
@Inject
private AssetIndex assetIndex;
@Inject
private AssetService assetService;
@Inject
private ContractAgreementService contractAgreementService;
@Inject
private ContractDefinitionStore contractDefinitionStore;
Expand Down Expand Up @@ -75,6 +78,7 @@ public void initialize(ServiceExtensionContext context) {

var wrapperExtensionContext = WrapperExtensionContextBuilder.buildContext(
assetIndex,
assetService,
contractAgreementService,
contractDefinitionStore,
contractNegotiationService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import de.sovity.edc.ext.wrapper.api.ui.pages.contracts.services.utils.ContractNegotiationUtils;
import de.sovity.edc.ext.wrapper.api.ui.pages.contracts.services.utils.TransformerRegistryUtils;
import de.sovity.edc.ext.wrapper.api.ui.pages.transferhistory.TransferHistoryPageApiService;
import de.sovity.edc.ext.wrapper.api.ui.pages.transferhistory.TransferHistoryPageAssetFetcherService;
import de.sovity.edc.ext.wrapper.api.usecase.UseCaseResource;
import de.sovity.edc.ext.wrapper.api.usecase.services.KpiApiService;
import de.sovity.edc.ext.wrapper.api.usecase.services.OfferingService;
Expand All @@ -38,6 +39,7 @@
import org.eclipse.edc.connector.contract.spi.negotiation.store.ContractNegotiationStore;
import org.eclipse.edc.connector.contract.spi.offer.store.ContractDefinitionStore;
import org.eclipse.edc.connector.policy.spi.store.PolicyDefinitionStore;
import org.eclipse.edc.connector.spi.asset.AssetService;
import org.eclipse.edc.connector.spi.contractagreement.ContractAgreementService;
import org.eclipse.edc.connector.spi.contractnegotiation.ContractNegotiationService;
import org.eclipse.edc.connector.spi.transferprocess.TransferProcessService;
Expand All @@ -62,6 +64,7 @@ public class WrapperExtensionContextBuilder {

public static WrapperExtensionContext buildContext(
AssetIndex assetIndex,
AssetService assetService,
ContractAgreementService contractAgreementService,
ContractDefinitionStore contractDefinitionStore,
ContractNegotiationService contractNegotiationService,
Expand All @@ -86,6 +89,9 @@ public static WrapperExtensionContext buildContext(
contractAgreementDataFetcher,
contractAgreementPageCardBuilder
);
var transferHistoryPageApiService = new TransferHistoryPageApiService(assetService, contractAgreementService, contractNegotiationStore,
transferProcessService, transferProcessStateService);
var transferHistoryPageAssetFetcherService = new TransferHistoryPageAssetFetcherService(assetService, transferProcessService);
var transferHistoryPageApiService = new TransferHistoryPageApiService();
var contractDefinitionApiService = new ContractDefinitionApiService();
var transformerRegistryUtils = new TransformerRegistryUtils(dtoTransformerRegistry);
Expand All @@ -104,8 +110,9 @@ public static WrapperExtensionContext buildContext(
);
var uiResource = new UiResource(
contractAgreementApiService,
transferHistoryPageApiService,
contractAgreementTransferApiService,
transferHistoryPageApiService,
transferHistoryPageAssetFetcherService
contractDefinitionApiService
);

Expand Down
Loading

0 comments on commit 94395a6

Please sign in to comment.