Skip to content

Commit

Permalink
only boot the runtime once, wipe data after each test
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Mar 23, 2023
1 parent 304384c commit cce9ace
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import org.eclipse.edc.policy.model.Permission;
import org.eclipse.edc.policy.model.Policy;
import org.eclipse.edc.policy.model.PolicyType;
import org.eclipse.edc.spi.asset.AssetIndex;
import org.eclipse.edc.spi.iam.IdentityService;
import org.eclipse.tractusx.edc.token.MockDapsService;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand All @@ -22,16 +24,10 @@
public class CatalogTest extends MultiRuntimeTest {


private static MockDapsService sokratesIsMock;
private static MockDapsService platoIsMock;

@BeforeAll
static void setup() {
platoIsMock = new MockDapsService("PLATOBPN");
sokratesIsMock = new MockDapsService("SOKRATESBPN");

sokrates.registerServiceMock(IdentityService.class, sokratesIsMock);
plato.registerServiceMock(IdentityService.class, platoIsMock);
}

@Test
Expand Down Expand Up @@ -68,6 +64,7 @@ void requestCatalog_filteredByBpn_shouldReject() {
sokrates.createPolicy(noConstraintPolicy(noConstraintPolicyId));

sokrates.createAsset("test-asset1", Map.of("canSee", "true"));
sokrates.createAsset("test-asset3", Map.of("canSee", "true"));
sokrates.createAsset("test-asset2", Map.of("canSee", "false"));

sokrates.createContractDefinition("test-asset1", "def1", noConstraintPolicyId, noConstraintPolicyId, 60);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.eclipse.tractusx.edc.tests;

import org.eclipse.edc.connector.contract.spi.offer.store.ContractDefinitionStore;
import org.eclipse.edc.connector.policy.spi.store.PolicyDefinitionStore;
import org.eclipse.edc.spi.asset.AssetIndex;
import org.eclipse.edc.spi.query.QuerySpec;
import org.eclipse.edc.spi.system.ServiceExtensionContext;

import java.util.stream.Collectors;

public class DataWiper {

private final ServiceExtensionContext context;

public DataWiper(ServiceExtensionContext context) {
this.context = context;
}

public void clearPersistence() {
clearAssetIndex();
clearPolicies();
clearContractDefinitions();
}

private void clearContractDefinitions() {
var cds = context.getService(ContractDefinitionStore.class);
cds.findAll(QuerySpec.none())
.forEach(cd -> cds.deleteById(cd.getId()));
}

private void clearPolicies() {
var ps = context.getService(PolicyDefinitionStore.class);
ps.findAll(QuerySpec.none()).collect(Collectors.toList())
.forEach(p -> ps.deleteById(p.getId()));
}

private void clearAssetIndex() {
var index = context.getService(AssetIndex.class);
index.queryAssets(QuerySpec.none())
.forEach(asset -> index.deleteById(asset.getId()));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.eclipse.tractusx.edc.tests;


import org.eclipse.edc.spi.iam.IdentityService;
import org.eclipse.tractusx.edc.token.MockDapsService;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.util.Map;
Expand All @@ -27,7 +29,8 @@ public class MultiRuntimeTest {

public static final String SOKRATES_ASSET_PATH = format("%s/%s.txt", tempDirectory(), SOKRATES_ASSET_FILE);


private static MockDapsService sokratesIsMock;
private static MockDapsService platoIsMock;
@RegisterExtension
protected static Participant sokrates = new Participant(
":edc-tests:runtime",
Expand All @@ -49,14 +52,23 @@ public class MultiRuntimeTest {
"PLATO",
Map.of(
"edc.ids.id", "urn:connector:plato",
"web.http.port", String.valueOf(PLATO_CONNECTOR_PORT),
"web.http.path", PLATO_CONNECTOR_PATH,
"web.http.default.port", String.valueOf(PLATO_CONNECTOR_PORT),
"web.http.default.path", PLATO_CONNECTOR_PATH,
"web.http.management.port", String.valueOf(PLATO_MANAGEMENT_PORT),
"web.http.management.path", PLATO_MANAGEMENT_PATH,
"web.http.ids.port", String.valueOf(PLATO_IDS_API_PORT),
"web.http.ids.path", IDS_PATH,
"edc.api.auth.key", "testkey",
"ids.webhook.address", PLATO_IDS_API));

// this needs to be in a static initializer block, because it must be invoked before
// the BeforeEach and BeforeAll callbacks of the participants
static {
platoIsMock = new MockDapsService("PLATOBPN");
sokratesIsMock = new MockDapsService("SOKRATESBPN");

sokrates.registerServiceMock(IdentityService.class, sokratesIsMock);
plato.registerServiceMock(IdentityService.class, platoIsMock);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,32 @@
import org.eclipse.edc.connector.policy.spi.PolicyDefinition;
import org.eclipse.edc.junit.extensions.EdcRuntimeExtension;
import org.eclipse.edc.spi.asset.AssetSelectorExpression;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.injection.InjectionContainer;
import org.eclipse.edc.spi.types.TypeManager;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static io.restassured.RestAssured.given;
import static io.restassured.http.ContentType.JSON;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;

public class Participant extends EdcRuntimeExtension {
public class Participant extends EdcRuntimeExtension implements BeforeAllCallback, AfterAllCallback {

private final String managementUrl;
private final String apiKey;
private final String idsEndpoint;
private final TypeManager typeManager = new TypeManager();
private final String idsId;
private DataWiper wiper;

public Participant(String moduleName, String runtimeName, Map<String, String> properties) {
super(moduleName, runtimeName, properties);
Expand All @@ -33,6 +41,22 @@ public Participant(String moduleName, String runtimeName, Map<String, String> pr
this.idsId = properties.get("edc.ids.id");
}

@Override
public void beforeTestExecution(ExtensionContext extensionContext) throws Exception {
//do nothing - we only want to start the runtime once
wiper.clearPersistence();
}

@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
}

@Override
protected void bootExtensions(ServiceExtensionContext context, List<InjectionContainer<ServiceExtension>> serviceExtensions) {
super.bootExtensions(context, serviceExtensions);
wiper = new DataWiper(context);
}

public void createAsset(String id, Map<String, String> properties) {
properties = new HashMap<>(properties);
properties.put("asset:prop:id", id);
Expand Down Expand Up @@ -77,7 +101,6 @@ public void createContractDefinition(String assetId, String definitionId, String
.contentType(JSON).contentType(JSON);
}


public void createPolicy(PolicyDefinition policyDefinition) {
baseRequest()
.body(policyDefinition)
Expand All @@ -92,7 +115,7 @@ public Catalog requestCatalog(Participant other) {

var response = baseRequest()
.when()
.body(CatalogRequestDto.Builder.newInstance().providerUrl(other.idsEndpoint()+"/data").build())
.body(CatalogRequestDto.Builder.newInstance().providerUrl(other.idsEndpoint() + "/data").build())
.post("/catalog/request")
.then();

Expand All @@ -108,6 +131,17 @@ public String idsId() {
return idsId;
}

@Override
public void beforeAll(ExtensionContext context) throws Exception {
//only run this once
super.beforeTestExecution(context);
}

@Override
public void afterAll(ExtensionContext context) throws Exception {
super.afterTestExecution(context);
}

private String idsEndpoint() {
return idsEndpoint;
}
Expand Down

0 comments on commit cce9ace

Please sign in to comment.