From 1c4562f13e5c4ce67f6b205ff416987a0e675e9e Mon Sep 17 00:00:00 2001 From: majadlymhmd Date: Fri, 5 Jul 2024 16:03:14 +0200 Subject: [PATCH] refactor: update the E2E test of the policy provision sample to newest EDC version --- policy/policy-02-provision/filetransfer.json | 2 +- system-tests/build.gradle.kts | 4 + .../policy/PolicyProvisionSampleTest.java | 3 +- .../PolicyProvisionSampleTestCommon.java | 100 +++++++----------- 4 files changed, 43 insertions(+), 66 deletions(-) diff --git a/policy/policy-02-provision/filetransfer.json b/policy/policy-02-provision/filetransfer.json index 0a823637..42bef426 100644 --- a/policy/policy-02-provision/filetransfer.json +++ b/policy/policy-02-provision/filetransfer.json @@ -1,6 +1,6 @@ { "@context": { - "edc": "https://w3id.org/edc/v0.0.1/ns/" + "@vocab": "https://w3id.org/edc/v0.0.1/ns/" }, "@type": "DataRequest", "protocol": "dataspace-protocol-http", diff --git a/system-tests/build.gradle.kts b/system-tests/build.gradle.kts index c9076e34..66d28aba 100644 --- a/system-tests/build.gradle.kts +++ b/system-tests/build.gradle.kts @@ -47,6 +47,10 @@ dependencies { testCompileOnly(project(":policy:policy-01-policy-enforcement:policy-enforcement-provider")) testCompileOnly(project(":policy:policy-01-policy-enforcement:policy-enforcement-consumer")) testCompileOnly(project(":policy:policy-01-policy-enforcement:policy-functions")) + + testCompileOnly(project(":policy:policy-02-provision:policy-provision-consumer")) + testCompileOnly(project(":policy:policy-02-provision:policy-provision-provider")) + } tasks.compileJava { diff --git a/system-tests/src/test/java/org/eclipse/edc/samples/policy/PolicyProvisionSampleTest.java b/system-tests/src/test/java/org/eclipse/edc/samples/policy/PolicyProvisionSampleTest.java index 8790b5c7..46c5333d 100644 --- a/system-tests/src/test/java/org/eclipse/edc/samples/policy/PolicyProvisionSampleTest.java +++ b/system-tests/src/test/java/org/eclipse/edc/samples/policy/PolicyProvisionSampleTest.java @@ -14,6 +14,7 @@ package org.eclipse.edc.samples.policy; +import org.eclipse.edc.connector.controlplane.transfer.spi.types.TransferProcessStates; import org.eclipse.edc.junit.extensions.EdcRuntimeExtension; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -69,7 +70,7 @@ void runSampleSteps() throws Exception { var transferProcessId = testUtils.requestTransferFile(); testUtils.assertDestinationFileContent(); testUtils.assertFileDoesNotExist(); - testUtils.assertTransferProcessStatusConsumerSide(transferProcessId); + testUtils.checkTransferStatus(transferProcessId, TransferProcessStates.COMPLETED); } @AfterEach diff --git a/system-tests/src/test/java/org/eclipse/edc/samples/policy/PolicyProvisionSampleTestCommon.java b/system-tests/src/test/java/org/eclipse/edc/samples/policy/PolicyProvisionSampleTestCommon.java index 78129fc2..cf9f6e8d 100644 --- a/system-tests/src/test/java/org/eclipse/edc/samples/policy/PolicyProvisionSampleTestCommon.java +++ b/system-tests/src/test/java/org/eclipse/edc/samples/policy/PolicyProvisionSampleTestCommon.java @@ -18,7 +18,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.restassured.RestAssured; import io.restassured.http.ContentType; -import io.restassured.path.json.JsonPath; import org.apache.http.HttpStatus; import org.eclipse.edc.connector.controlplane.transfer.spi.types.TransferProcessStates; import org.eclipse.edc.connector.controlplane.transfer.spi.types.TransferProcess; @@ -30,11 +29,12 @@ import org.eclipse.edc.policy.model.Operator; import org.eclipse.edc.policy.model.Permission; import org.eclipse.edc.policy.model.Policy; -import org.eclipse.edc.spi.types.domain.DataAddress; import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.time.Duration; import static org.assertj.core.api.Assertions.assertThat; @@ -49,6 +49,10 @@ public class PolicyProvisionSampleTestCommon { static final String TRANSFER_PROCESS_URI = "http://localhost:9192/management/v2/transferprocesses"; static final String API_KEY_HEADER_KEY = "X-Api-Key"; static final String API_KEY_HEADER_VALUE = "password"; + public static final Duration TIMEOUT = Duration.ofSeconds(30); + public static final Duration POLL_DELAY = Duration.ofMillis(1000); + public static final Duration POLL_INTERVAL = Duration.ofMillis(500); + private static final String EDC_STATE = "state"; //endregion //region changeable test settings @@ -186,15 +190,7 @@ void initiateContractNegotiation() throws IOException { var contractOfferFile = new File(TestUtils.findBuildRoot(), PolicyProvisionSampleTest.CONTRACT_OFFER_FILE_PATH); ObjectNode contractOfferJsonRootNode = MAPPER.readValue(contractOfferFile, ObjectNode.class); - ObjectNode policyNode = (ObjectNode) contractOfferJsonRootNode.get("policy"); - if (policyNode == null) { - throw new IOException("The 'policy' node is missing in the contract offer file."); - } - - policyNode.putPOJO("odrl:permission", createContractPolicy().getPermissions()); - System.out.println("policy node " + policyNode); - System.out.println("contract offer : " + contractOfferJsonRootNode); var response = RestAssured @@ -202,11 +198,9 @@ void initiateContractNegotiation() throws IOException { .headers(API_KEY_HEADER_KEY, API_KEY_HEADER_VALUE) .contentType(ContentType.JSON) .body(contractOfferJsonRootNode) - .log().all() .when() .post(INITIATE_CONTRACT_NEGOTIATION_URI) .then() - .log().all() .statusCode(HttpStatus.SC_OK) .extract() .response(); @@ -243,7 +237,7 @@ void lookUpContractAgreementId() { .get(url) .then() .statusCode(HttpStatus.SC_OK) - .body("state", equalTo("CONFIRMED")) + .body("state", equalTo("FINALIZED")) .body("contractAgreementId", not(emptyString())) .extract().body().jsonPath().getString("contractAgreementId"); @@ -253,70 +247,48 @@ void lookUpContractAgreementId() { }); } - /** - * Assert that a POST request to initiate transfer process is successful. - * This method corresponds to the command in the sample: {@code curl -X POST -H "Content-Type: application/json" -H "X-Api-Key: password" -d @filetransfer.json "http://localhost:9192/management/v2/transferprocesses"} - * - * @throws IOException Thrown if there was an error accessing the transfer request file defined in transferFilePath. - */ String requestTransferFile() throws IOException { - var transferJsonFile = getFileFromRelativePath(PolicyProvisionSampleTest.TRANSFER_FILE_PATH); - TransferProcess sampleDataRequest = readAndUpdateDataRequestFromJsonFile(transferJsonFile, contractAgreementId); + var fileTransferFile = new File(TestUtils.findBuildRoot(), PolicyProvisionSampleTest.TRANSFER_FILE_PATH); + String fileTransferJson = new String(Files.readAllBytes(Paths.get(fileTransferFile.getPath()))); - JsonPath jsonPath = RestAssured + fileTransferJson = fileTransferJson.replace("{{contract-agreement-id}}", contractAgreementId); + + var response = RestAssured .given() .headers(API_KEY_HEADER_KEY, API_KEY_HEADER_VALUE) .contentType(ContentType.JSON) - .body(sampleDataRequest) + .body(fileTransferJson) .when() + .log().all() .post(TRANSFER_PROCESS_URI) .then() + .log().all() .statusCode(HttpStatus.SC_OK) - .body("id", not(emptyString())) .extract() - .jsonPath(); - - String transferProcessId = jsonPath.get("id"); + .response(); + return response.jsonPath().getString("@id"); - assertThat(transferProcessId).isNotEmpty(); - return transferProcessId; + } + void checkTransferStatus(String transferProcessId, TransferProcessStates status) { + await() + .atMost(TIMEOUT) + .pollDelay(POLL_DELAY) + .pollInterval(POLL_INTERVAL) + .untilAsserted(() -> { + var state = get(transferProcessId); + assertThat(state).isEqualTo(status.name()); + }); } - /** - * Reads a transfer request file with changed value for contract agreement ID and file destination path. - * - * @param transferJsonFile A {@link File} instance pointing to a JSON transfer request file. - * @param contractAgreementId This string containing a UUID will be used as value for the contract agreement ID. - * @return An instance of {@link TransferProcess} with changed values for contract agreement ID and file destination path. - * @throws IOException Thrown if there was an error accessing the file given in transferJsonFile. - */ - TransferProcess readAndUpdateDataRequestFromJsonFile(@NotNull File transferJsonFile, @NotNull String contractAgreementId) throws IOException { - TransferProcess sampleDataRequest = MAPPER.readValue(transferJsonFile, TransferProcess.class); - - var changedAddressProperties = sampleDataRequest.getDataDestination().getProperties(); - changedAddressProperties.put("path", destinationFile.getAbsolutePath()); - - DataAddress newDataDestination = DataAddress.Builder.newInstance() - .properties(changedAddressProperties) - .build(); - - return TransferProcess.Builder.newInstance() - .type(sampleDataRequest.getType()) - .protocol(sampleDataRequest.getProtocol()) - .correlationId(sampleDataRequest.getCorrelationId()) - .counterPartyAddress(sampleDataRequest.getCounterPartyAddress()) - .dataDestination(newDataDestination) - .assetId(sampleDataRequest.getAssetId()) - .contractId(contractAgreementId) - .contentDataAddress(sampleDataRequest.getContentDataAddress()) - .provisionedResourceSet(sampleDataRequest.getProvisionedResourceSet()) - .deprovisionedResources(sampleDataRequest.getDeprovisionedResources()) - .privateProperties(sampleDataRequest.getPrivateProperties()) - .callbackAddresses(sampleDataRequest.getCallbackAddresses()) - .transferType(sampleDataRequest.getTransferType()) - .protocolMessages(sampleDataRequest.getProtocolMessages()) - .dataPlaneId(sampleDataRequest.getDataPlaneId()) - .build(); + private String get(String transferProcessId) { + return RestAssured + .given() + .headers(API_KEY_HEADER_KEY, API_KEY_HEADER_VALUE) + .when() + .get(String.format("%s/%s", TRANSFER_PROCESS_URI, transferProcessId)) + .then() + .statusCode(HttpStatus.SC_OK) + .extract().jsonPath().getString(EDC_STATE); } } \ No newline at end of file