From c547e752326b3ab95bba8da8a67fcc8c2ca6518f Mon Sep 17 00:00:00 2001 From: andrea bertagnolli Date: Wed, 15 Jan 2025 15:20:38 +0100 Subject: [PATCH] refactor(test): make `Participant` lazy (#4723) * refactor(test): make Participant lazy * improve error output --- .../edc/junit/extensions/EmbeddedRuntime.java | 4 +- .../extensions/RuntimePerClassExtension.java | 4 +- .../build.gradle.kts | 1 + .../test/system/utils/Participant.java | 66 +++++++++++-------- .../e2e/participant/DataPlaneParticipant.java | 7 -- .../test/e2e/TransferEndToEndParticipant.java | 19 ++---- 6 files changed, 49 insertions(+), 52 deletions(-) diff --git a/core/common/junit/src/main/java/org/eclipse/edc/junit/extensions/EmbeddedRuntime.java b/core/common/junit/src/main/java/org/eclipse/edc/junit/extensions/EmbeddedRuntime.java index 5fa49968688..fd85638996b 100644 --- a/core/common/junit/src/main/java/org/eclipse/edc/junit/extensions/EmbeddedRuntime.java +++ b/core/common/junit/src/main/java/org/eclipse/edc/junit/extensions/EmbeddedRuntime.java @@ -125,11 +125,11 @@ public void boot(boolean addShutdownHook) { }); try { - if (!latch.await(20, SECONDS)) { + if (!latch.await(30, SECONDS)) { throw new EdcException("Failed to start EDC runtime", runtimeThrowable.get()); } } catch (InterruptedException e) { - throw new EdcException("Failed to start EDC runtime", runtimeThrowable.get()); + throw new EdcException("Failed to start EDC runtime: interrupted", runtimeThrowable.get()); } monitor.info("Runtime %s started".formatted(name)); diff --git a/core/common/junit/src/main/java/org/eclipse/edc/junit/extensions/RuntimePerClassExtension.java b/core/common/junit/src/main/java/org/eclipse/edc/junit/extensions/RuntimePerClassExtension.java index a42f317c8eb..ea7098be79a 100644 --- a/core/common/junit/src/main/java/org/eclipse/edc/junit/extensions/RuntimePerClassExtension.java +++ b/core/common/junit/src/main/java/org/eclipse/edc/junit/extensions/RuntimePerClassExtension.java @@ -18,15 +18,13 @@ import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.ExtensionContext; -import static java.util.Collections.emptyMap; - /** * Spin up a static runtime to be used for multiple tests */ public class RuntimePerClassExtension extends RuntimeExtension implements BeforeAllCallback, AfterAllCallback { public RuntimePerClassExtension() { - this(new EmbeddedRuntime("runtime", emptyMap())); + this(new EmbeddedRuntime("runtime")); } public RuntimePerClassExtension(EmbeddedRuntime runtime) { diff --git a/extensions/control-plane/api/management-api/management-api-test-fixtures/build.gradle.kts b/extensions/control-plane/api/management-api/management-api-test-fixtures/build.gradle.kts index efb6140c34e..c79a6d25672 100644 --- a/extensions/control-plane/api/management-api/management-api-test-fixtures/build.gradle.kts +++ b/extensions/control-plane/api/management-api/management-api-test-fixtures/build.gradle.kts @@ -30,4 +30,5 @@ dependencies { testFixturesImplementation(libs.assertj) testFixturesImplementation(libs.restAssured) testFixturesImplementation(libs.awaitility) + testFixturesImplementation(libs.mockito.core) } diff --git a/extensions/control-plane/api/management-api/management-api-test-fixtures/src/testFixtures/java/org/eclipse/edc/connector/controlplane/test/system/utils/Participant.java b/extensions/control-plane/api/management-api/management-api-test-fixtures/src/testFixtures/java/org/eclipse/edc/connector/controlplane/test/system/utils/Participant.java index 480f0de4945..7334def51c1 100644 --- a/extensions/control-plane/api/management-api/management-api-test-fixtures/src/testFixtures/java/org/eclipse/edc/connector/controlplane/test/system/utils/Participant.java +++ b/extensions/control-plane/api/management-api/management-api-test-fixtures/src/testFixtures/java/org/eclipse/edc/connector/controlplane/test/system/utils/Participant.java @@ -36,6 +36,7 @@ import java.util.Map; import java.util.Objects; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.UnaryOperator; import static io.restassured.RestAssured.given; import static io.restassured.http.ContentType.JSON; @@ -52,6 +53,7 @@ import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_POLICY_ATTRIBUTE; import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.ODRL_TARGET_ATTRIBUTE; import static org.eclipse.edc.spi.constants.CoreConstants.EDC_NAMESPACE; +import static org.eclipse.edc.util.io.Ports.getFreePort; /** * Essentially a wrapper around the management API enabling to test interactions with other participants, eg. catalog, transfer... @@ -60,13 +62,16 @@ public class Participant { protected String id; protected String name; + protected LazySupplier controlPlaneManagement = new LazySupplier<>(() -> URI.create("http://localhost:" + getFreePort() + "/management")); + protected LazySupplier controlPlaneProtocol = new LazySupplier<>(() -> URI.create("http://localhost:" + getFreePort() + "/protocol")); + protected UnaryOperator enrichManagementRequest = r -> r; + @Deprecated(since = "0.11.0") protected Endpoint managementEndpoint; + @Deprecated(since = "0.11.0") protected Endpoint protocolEndpoint; protected JsonLd jsonLd; protected ObjectMapper objectMapper; - protected Duration timeout = Duration.ofSeconds(30); - protected String protocol = "dataspace-protocol-http"; protected Participant() { @@ -88,10 +93,7 @@ public void setProtocol(String protocol) { this.protocol = protocol; } - public Endpoint getProtocolEndpoint() { - return protocolEndpoint; - } - + @Deprecated(since = "0.11.0") public Endpoint getManagementEndpoint() { return managementEndpoint; } @@ -116,7 +118,7 @@ public String createAsset(String assetId, Map properties, Map { - var response = managementEndpoint.baseRequest() + var response = baseManagementRequest() .contentType(JSON) .when() .body(requestBodyBuilder.build()) @@ -254,11 +256,11 @@ public JsonObject getDatasetForAsset(Participant provider, String assetId) { .add(TYPE, "DatasetRequest") .add(ID, assetId) .add("counterPartyId", provider.id) - .add("counterPartyAddress", provider.protocolEndpoint.url.toString()) + .add("counterPartyAddress", provider.controlPlaneProtocol.get().toString()) .add("protocol", protocol) .build(); - var response = managementEndpoint.baseRequest() + var response = baseManagementRequest() .contentType(JSON) .when() .body(requestBody) @@ -303,12 +305,12 @@ public String initContractNegotiation(Participant provider, JsonObject policy) { var requestBody = createObjectBuilder() .add(CONTEXT, createObjectBuilder().add(VOCAB, EDC_NAMESPACE)) .add(TYPE, "ContractRequest") - .add("counterPartyAddress", provider.protocolEndpoint.getUrl().toString()) + .add("counterPartyAddress", provider.controlPlaneProtocol.get().toString()) .add("protocol", protocol) .add("policy", jsonLd.compact(policy).getContent()) .build(); - return managementEndpoint.baseRequest() + return baseManagementRequest() .contentType(JSON) .body(requestBody) .when() @@ -370,7 +372,7 @@ public String initiateTransfer(Participant provider, String contractAgreementId, .add("protocol", protocol) .add("contractId", contractAgreementId) .add("connectorId", provider.id) - .add("counterPartyAddress", provider.protocolEndpoint.url.toString()); + .add("counterPartyAddress", provider.controlPlaneProtocol.get().toString()); if (privateProperties != null) { requestBodyBuilder.add("privateProperties", privateProperties); @@ -390,7 +392,7 @@ public String initiateTransfer(Participant provider, String contractAgreementId, var requestBody = requestBodyBuilder.build(); - return managementEndpoint.baseRequest() + return baseManagementRequest() .contentType(JSON) .body(requestBody) .when() @@ -421,7 +423,7 @@ public JsonArray getTransferProcesses() { * @return The transfer processes */ public JsonArray getTransferProcesses(JsonObject query) { - return managementEndpoint.baseRequest() + return baseManagementRequest() .contentType(JSON) .body(query) .when() @@ -444,7 +446,7 @@ public RequestAsset requestAssetFrom(String assetId, Participant provider) { * @return state of the transfer process. */ public String getTransferProcessState(String id) { - return managementEndpoint.baseRequest() + return baseManagementRequest() .contentType(JSON) .when() .get("/v3/transferprocesses/{id}/state", id) @@ -464,7 +466,7 @@ public void suspendTransfer(String id, String reason) { .add(TYPE, "SuspendTransfer") .add("reason", reason); - managementEndpoint.baseRequest() + baseManagementRequest() .contentType(JSON) .body(requestBodyBuilder.build()) .when() @@ -480,7 +482,7 @@ public void suspendTransfer(String id, String reason) { * @param id transfer process id. */ public void resumeTransfer(String id) { - managementEndpoint.baseRequest() + baseManagementRequest() .contentType(JSON) .when() .post("/v3/transferprocesses/{id}/resume", id) @@ -495,7 +497,7 @@ public void terminateTransfer(String id) { .add(TYPE, "TerminateTransfer") .add("reason", "any reason"); - managementEndpoint.baseRequest() + baseManagementRequest() .contentType(JSON) .body(requestBodyBuilder.build()) .when() @@ -520,7 +522,7 @@ public void awaitTransferToBeInState(String transferProcessId, TransferProcessSt } protected String getContractNegotiationField(String negotiationId, String fieldName) { - return managementEndpoint.baseRequest() + return baseManagementRequest() .contentType(JSON) .when() .get("/v3/contractnegotiations/{id}", negotiationId) @@ -554,9 +556,17 @@ private String getContractAgreementId(String negotiationId) { return contractAgreementId; } + public RequestSpecification baseManagementRequest() { + var request = given().baseUri(controlPlaneManagement.get().toString()); + return enrichManagementRequest.apply(request); + } + /** * Represent an endpoint exposed by a {@link Participant}. + * + * @deprecated it will be removed in the upcoming versions. */ + @Deprecated(since = "0.11.0") public static class Endpoint { private final URI url; private final Map headers; @@ -578,6 +588,7 @@ public RequestSpecification baseRequest() { public URI getUrl() { return url; } + } public static class Builder

> { @@ -611,11 +622,13 @@ public B timeout(Duration timeout) { return self(); } + @Deprecated(since = "0.11.0") public B managementEndpoint(Endpoint managementEndpoint) { participant.managementEndpoint = managementEndpoint; return self(); } + @Deprecated(since = "0.11.0") public B protocolEndpoint(Endpoint protocolEndpoint) { participant.protocolEndpoint = protocolEndpoint; return self(); @@ -631,11 +644,10 @@ public B objectMapper(ObjectMapper objectMapper) { return self(); } - public Participant build() { + public P build() { Objects.requireNonNull(participant.id, "id"); Objects.requireNonNull(participant.name, "name"); - Objects.requireNonNull(participant.managementEndpoint, "managementEndpoint"); - Objects.requireNonNull(participant.protocolEndpoint, "protocolEndpoint"); + if (participant.jsonLd == null) { participant.jsonLd = new TitaniumJsonLd(new ConsoleMonitor()); } diff --git a/system-tests/e2e-dataplane-tests/tests/src/test/java/org/eclipse/edc/test/e2e/participant/DataPlaneParticipant.java b/system-tests/e2e-dataplane-tests/tests/src/test/java/org/eclipse/edc/test/e2e/participant/DataPlaneParticipant.java index 91b6918e6f4..fdc26615504 100644 --- a/system-tests/e2e-dataplane-tests/tests/src/test/java/org/eclipse/edc/test/e2e/participant/DataPlaneParticipant.java +++ b/system-tests/e2e-dataplane-tests/tests/src/test/java/org/eclipse/edc/test/e2e/participant/DataPlaneParticipant.java @@ -87,13 +87,6 @@ public static Builder newInstance() { return new Builder(); } - @Override - public DataPlaneParticipant build() { - super.managementEndpoint(new Endpoint(URI.create("http://localhost:" + getFreePort() + "/api/management"))); - super.protocolEndpoint(new Endpoint(URI.create("http://localhost:" + getFreePort() + "/protocol"))); - super.build(); - return participant; - } } } diff --git a/system-tests/e2e-transfer-test/runner/src/test/java/org/eclipse/edc/test/e2e/TransferEndToEndParticipant.java b/system-tests/e2e-transfer-test/runner/src/test/java/org/eclipse/edc/test/e2e/TransferEndToEndParticipant.java index b64085e48e8..3243363e629 100644 --- a/system-tests/e2e-transfer-test/runner/src/test/java/org/eclipse/edc/test/e2e/TransferEndToEndParticipant.java +++ b/system-tests/e2e-transfer-test/runner/src/test/java/org/eclipse/edc/test/e2e/TransferEndToEndParticipant.java @@ -53,13 +53,13 @@ public Config controlPlaneConfig() { put(PARTICIPANT_ID, id); put("web.http.port", String.valueOf(getFreePort())); put("web.http.path", "/api"); - put("web.http.protocol.port", String.valueOf(protocolEndpoint.getUrl().getPort())); - put("web.http.protocol.path", protocolEndpoint.getUrl().getPath()); - put("web.http.management.port", String.valueOf(managementEndpoint.getUrl().getPort())); - put("web.http.management.path", managementEndpoint.getUrl().getPath()); + put("web.http.protocol.port", String.valueOf(controlPlaneProtocol.get().getPort())); + put("web.http.protocol.path", controlPlaneProtocol.get().getPath()); + put("web.http.management.port", String.valueOf(controlPlaneManagement.get().getPort())); + put("web.http.management.path", controlPlaneManagement.get().getPath()); put("web.http.control.port", String.valueOf(controlPlaneControl.get().getPort())); put("web.http.control.path", controlPlaneControl.get().getPath()); - put("edc.dsp.callback.address", protocolEndpoint.getUrl().toString()); + put("edc.dsp.callback.address", controlPlaneProtocol.get().toString()); put("edc.keystore", resourceAbsolutePath("certs/cert.pfx")); put("edc.keystore.password", "123456"); put("edc.transfer.proxy.endpoint", dataPlanePublic.get().toString()); @@ -128,7 +128,7 @@ public int getHttpProvisionerPort() { * @return The cached {@link DataAddress} */ public DataAddress getEdr(String transferProcessId) { - var dataAddressRaw = managementEndpoint.baseRequest() + var dataAddressRaw = baseManagementRequest() .contentType(JSON) .when() .get("/v3/edrs/{id}/dataaddress", transferProcessId) @@ -189,13 +189,6 @@ public static Builder newInstance() { return new Builder(); } - @Override - public TransferEndToEndParticipant build() { - super.managementEndpoint(new Endpoint(URI.create("http://localhost:" + getFreePort() + "/api/management"))); - super.protocolEndpoint(new Endpoint(URI.create("http://localhost:" + getFreePort() + "/protocol"))); - super.build(); - return participant; - } } }