From dd89fe1b1bc049c8795510ec8adc7b27a067baac Mon Sep 17 00:00:00 2001 From: Enrico Risa Date: Tue, 31 Oct 2023 12:07:45 +0100 Subject: [PATCH] feat: mark the transfer as completed on EDR expiration --- core/edr-core/build.gradle.kts | 1 - .../TransferProcessLocalCallback.java | 10 +++++++++ .../iam/ssi/miw/api/MiwClientException.java | 3 +++ .../edc/tests/edr/AbstractRenewalEdrTest.java | 22 ++++++++++++++++--- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/core/edr-core/build.gradle.kts b/core/edr-core/build.gradle.kts index c8fe53456..2b136f325 100644 --- a/core/edr-core/build.gradle.kts +++ b/core/edr-core/build.gradle.kts @@ -28,7 +28,6 @@ dependencies { implementation(project(":spi:edr-spi")) implementation(project(":spi:core-spi")) - testImplementation(libs.edc.junit) testImplementation(libs.awaitility) testImplementation(testFixtures(project(":spi:edr-spi"))) diff --git a/edc-extensions/edr/edr-callback/src/main/java/org/eclipse/tractusx/edc/callback/TransferProcessLocalCallback.java b/edc-extensions/edr/edr-callback/src/main/java/org/eclipse/tractusx/edc/callback/TransferProcessLocalCallback.java index b04033ae7..6d7774bf8 100644 --- a/edc-extensions/edr/edr-callback/src/main/java/org/eclipse/tractusx/edc/callback/TransferProcessLocalCallback.java +++ b/edc-extensions/edr/edr-callback/src/main/java/org/eclipse/tractusx/edc/callback/TransferProcessLocalCallback.java @@ -148,6 +148,16 @@ private void cleanOldEdr(String assetId, String agreementId) { monitor.debug(format("Expiring EDR for transfer process %s", entry.getTransferProcessId())); entry.transitionToExpired(); edrCache.update(entry); + + var transferProcess = transferProcessStore.findById(entry.getTransferProcessId()); + + if (transferProcess != null && transferProcess.canBeCompleted()) { + transferProcess.transitionCompleting(); + transferProcessStore.save(transferProcess); + } else { + monitor.info(format("Cannot terminate transfer process with id: %s", entry.getTransferProcessId())); + } + })); } diff --git a/edc-extensions/ssi/ssi-miw-credential-client/src/main/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwClientException.java b/edc-extensions/ssi/ssi-miw-credential-client/src/main/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwClientException.java index 17ec5a369..e241c6981 100644 --- a/edc-extensions/ssi/ssi-miw-credential-client/src/main/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwClientException.java +++ b/edc-extensions/ssi/ssi-miw-credential-client/src/main/java/org/eclipse/tractusx/edc/iam/ssi/miw/api/MiwClientException.java @@ -17,6 +17,9 @@ import okhttp3.Response; import org.eclipse.edc.spi.http.EdcHttpClientException; +/** + * Custom client exception for handling failure and retries when fetching data from MIW. + */ public class MiwClientException extends EdcHttpClientException { private final Response response; diff --git a/edc-tests/e2e-tests/src/test/java/org/eclipse/tractusx/edc/tests/edr/AbstractRenewalEdrTest.java b/edc-tests/e2e-tests/src/test/java/org/eclipse/tractusx/edc/tests/edr/AbstractRenewalEdrTest.java index 3fceac948..036d6f84c 100644 --- a/edc-tests/e2e-tests/src/test/java/org/eclipse/tractusx/edc/tests/edr/AbstractRenewalEdrTest.java +++ b/edc-tests/e2e-tests/src/test/java/org/eclipse/tractusx/edc/tests/edr/AbstractRenewalEdrTest.java @@ -20,6 +20,7 @@ import okhttp3.mockwebserver.MockWebServer; import org.assertj.core.api.Condition; import org.eclipse.edc.connector.transfer.spi.event.TransferProcessStarted; +import org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates; import org.eclipse.edc.policy.model.Operator; import org.eclipse.tractusx.edc.lifecycle.Participant; import org.junit.jupiter.api.AfterEach; @@ -38,6 +39,7 @@ import static org.assertj.core.api.Assertions.anyOf; import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; +import static org.awaitility.pollinterval.FibonacciPollInterval.fibonacci; import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE; import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntryStates.EXPIRED; import static org.eclipse.tractusx.edc.edr.spi.types.EndpointDataReferenceEntryStates.NEGOTIATED; @@ -108,21 +110,35 @@ void negotiateEdr_shouldRenewTheEdr() throws IOException { assertThat(expectedEvents).usingRecursiveFieldByFieldElementComparator().containsAll(events); - JsonArrayBuilder edrCaches = Json.createArrayBuilder(); + JsonArrayBuilder edrCachesBuilder = Json.createArrayBuilder(); await().atMost(ASYNC_TIMEOUT) .pollInterval(ASYNC_POLL_INTERVAL) .untilAsserted(() -> { var localEdrCaches = SOKRATES.getEdrEntriesByAssetId(assetId); assertThat(localEdrCaches).hasSizeGreaterThan(1); - localEdrCaches.forEach(edrCaches::add); + localEdrCaches.forEach(edrCachesBuilder::add); }); + var edrCaches = edrCachesBuilder.build(); - assertThat(edrCaches.build()) + assertThat(edrCaches) .extracting(json -> json.asJsonObject().getJsonString("tx:edrState").getString()) .areAtMost(1, anyOf(stateCondition(NEGOTIATED.name(), "Negotiated"), stateCondition(REFRESHING.name(), "Refreshing"))) .areAtLeast(1, stateCondition(EXPIRED.name(), "Expired")); + + var transferProcessId = edrCaches.stream() + .filter(json -> json.asJsonObject().getJsonString("tx:edrState").getString().equals(EXPIRED.name())) + .map(json -> json.asJsonObject().getJsonString("edc:transferProcessId").getString()) + .findFirst() + .orElseThrow(); + + await().pollInterval(fibonacci()) + .atMost(ASYNC_TIMEOUT) + .untilAsserted(() -> { + var tpState = SOKRATES.getTransferProcessState(transferProcessId); + assertThat(tpState).isNotNull().isEqualTo(TransferProcessStates.COMPLETED.toString()); + }); }