Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(test): make Participant lazy #4723

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ dependencies {
testFixturesImplementation(libs.assertj)
testFixturesImplementation(libs.restAssured)
testFixturesImplementation(libs.awaitility)
testFixturesImplementation(libs.mockito.core)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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...
Expand All @@ -60,13 +62,16 @@ public class Participant {

protected String id;
protected String name;
protected LazySupplier<URI> controlPlaneManagement = new LazySupplier<>(() -> URI.create("http://localhost:" + getFreePort() + "/management"));
protected LazySupplier<URI> controlPlaneProtocol = new LazySupplier<>(() -> URI.create("http://localhost:" + getFreePort() + "/protocol"));
protected UnaryOperator<RequestSpecification> 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() {
Expand All @@ -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;
}
Expand All @@ -116,7 +118,7 @@ public String createAsset(String assetId, Map<String, Object> properties, Map<St
.add("dataAddress", createObjectBuilder(dataAddressProperties))
.build();

return managementEndpoint.baseRequest()
return baseManagementRequest()
.contentType(JSON)
.body(requestBody)
.when()
Expand All @@ -141,13 +143,13 @@ public String createPolicyDefinition(JsonObject policy) {
.add("policy", policy)
.build();

return managementEndpoint.baseRequest()
return baseManagementRequest()
.contentType(JSON)
.body(requestBody)
.when()
.post("/v3/policydefinitions")
.then()
.log().ifError()
.log().ifValidationFails()
.statusCode(200)
.contentType(JSON)
.extract().jsonPath().getString(ID);
Expand Down Expand Up @@ -178,7 +180,7 @@ public String createContractDefinition(String assetId, String definitionId, Stri
.build())
.build();

return managementEndpoint.baseRequest()
return baseManagementRequest()
.contentType(JSON)
.body(requestBody)
.when()
Expand Down Expand Up @@ -210,15 +212,15 @@ public JsonArray getCatalogDatasets(Participant provider, JsonObject querySpec)
.add(CONTEXT, createObjectBuilder().add(VOCAB, EDC_NAMESPACE))
.add(TYPE, "CatalogRequest")
.add("counterPartyId", provider.id)
.add("counterPartyAddress", provider.protocolEndpoint.url.toString())
.add("counterPartyAddress", provider.controlPlaneProtocol.get().toString())
.add("protocol", protocol);

if (querySpec != null) {
requestBodyBuilder.add("querySpec", querySpec);
}

await().atMost(timeout).untilAsserted(() -> {
var response = managementEndpoint.baseRequest()
var response = baseManagementRequest()
.contentType(JSON)
.when()
.body(requestBodyBuilder.build())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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<String, String> headers;
Expand All @@ -578,6 +588,7 @@ public RequestSpecification baseRequest() {
public URI getUrl() {
return url;
}

}

public static class Builder<P extends Participant, B extends Participant.Builder<P, B>> {
Expand Down Expand Up @@ -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();
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
}

}
Loading