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 b6eab4f
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
package org.eclipse.tractusx.edc.tests;


import org.eclipse.edc.api.query.QuerySpecDto;
import org.eclipse.edc.connector.policy.spi.PolicyDefinition;
import org.eclipse.edc.junit.annotations.EndToEndTest;
import org.eclipse.edc.policy.model.Action;
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.iam.IdentityService;
import org.eclipse.tractusx.edc.token.MockDapsService;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Map;

import static java.util.stream.IntStream.range;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.tractusx.edc.policy.PolicyHelperFunctions.businessPartnerNumberPolicy;

@EndToEndTest
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 @@ -61,24 +56,70 @@ void requestCatalog_fulfillsPolicy_shouldReturnOffer() {
@Test
@DisplayName("Verify that Plato receives only the offers he is permitted to")
void requestCatalog_filteredByBpn_shouldReject() {
var bpnAccessPolicy = businessPartnerNumberPolicy("ap", "BPN1", "BPN2", "PLATOBPN");
var onlyPlatoPolicy = businessPartnerNumberPolicy("ap", "BPN1", "BPN2", "PLATOBPN");
var onlyDiogenesPolicy = businessPartnerNumberPolicy("dp", "DIOGENESBPN");
var noConstraintPolicyId = "no-constraint";

sokrates.createPolicy(bpnAccessPolicy);
sokrates.createPolicy(onlyPlatoPolicy);
sokrates.createPolicy(noConstraintPolicy(noConstraintPolicyId));

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

sokrates.createContractDefinition("test-asset1", "def1", noConstraintPolicyId, noConstraintPolicyId, 60);
sokrates.createContractDefinition("test-asset2", "def2", "ap", noConstraintPolicyId, 60);
sokrates.createContractDefinition("test-asset2", "def2", onlyPlatoPolicy.getId(), noConstraintPolicyId, 60);
sokrates.createContractDefinition("test-asset3", "def3", onlyDiogenesPolicy.getId(), noConstraintPolicyId, 60);


// act
var catalog = plato.requestCatalog(sokrates);
assertThat(catalog.getContractOffers()).hasSize(2);
}

@Test
@DisplayName("Multiple ContractDefinitions exist for one Asset")
void requestCatalog_multipleOffersForAsset() {
sokrates.createAsset("asset-1", Map.of("test-key", "test-val"));
sokrates.createPolicy(noConstraintPolicy("policy-1"));
sokrates.createPolicy(businessPartnerNumberPolicy("policy-2", "PLATOBPN"));

sokrates.createContractDefinition("asset-1", "def1", "policy-1", "policy-1", 60);
sokrates.createContractDefinition("asset-1", "def2", "policy-2", "policy-1", 60);

var catalog = plato.requestCatalog(sokrates);
assertThat(catalog.getContractOffers()).hasSize(2)
.allSatisfy(cd -> assertThat(cd.getAsset().getId()).isEqualTo("asset-1"))
// .hasToString is advisable as it handles NPEs better:
.allSatisfy(cd -> assertThat(cd.getConsumer()).hasToString(plato.idsId()))
.allSatisfy(cd -> assertThat(cd.getProvider()).hasToString(sokrates.idsId()));
}

@Test
@DisplayName("Catalog with 1000 offers")
void requestCatalog_of1000Assets_shouldContainAll() {
var policy = businessPartnerNumberPolicy("policy-1", "PLATOBPN");
sokrates.createPolicy(policy);
sokrates.createPolicy(noConstraintPolicy("noconstraint"));

range(0, 1000)
.forEach(i -> {
var assetId = "asset-" + i;
sokrates.createAsset(assetId, Map.of());
sokrates.createContractDefinition(assetId, "def-" + i, policy.getId(), "noconstraint", 60);
});

// request all at once
var o = plato.requestCatalog(sokrates, QuerySpecDto.Builder.newInstance().limit(1000).offset(0).build()).getContractOffers();
assertThat(o).hasSize(1000);

// request in chunks
var o2 = plato.requestCatalog(sokrates, QuerySpecDto.Builder.newInstance().limit(500).offset(0).build()).getContractOffers();
var o3 = plato.requestCatalog(sokrates, QuerySpecDto.Builder.newInstance().limit(500).offset(500).build()).getContractOffers();
assertThat(o2).doesNotContainAnyElementsOf(o3);

}

private PolicyDefinition noConstraintPolicy(String id) {
return PolicyDefinition.Builder.newInstance()
.id(id)
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
@@ -1,29 +1,38 @@
package org.eclipse.tractusx.edc.tests;

import io.restassured.specification.RequestSpecification;
import org.eclipse.edc.api.query.QuerySpecDto;
import org.eclipse.edc.catalog.spi.Catalog;
import org.eclipse.edc.connector.api.management.catalog.model.CatalogRequestDto;
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 +42,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 +102,6 @@ public void createContractDefinition(String assetId, String definitionId, String
.contentType(JSON).contentType(JSON);
}


public void createPolicy(PolicyDefinition policyDefinition) {
baseRequest()
.body(policyDefinition)
Expand All @@ -89,10 +113,16 @@ public void createPolicy(PolicyDefinition policyDefinition) {
}

public Catalog requestCatalog(Participant other) {
return requestCatalog(other, QuerySpecDto.Builder.newInstance().build());
}

public Catalog requestCatalog(Participant other, QuerySpecDto query) {
var response = baseRequest()
.when()
.body(CatalogRequestDto.Builder.newInstance().providerUrl(other.idsEndpoint()+"/data").build())
.body(CatalogRequestDto.Builder.newInstance()
.providerUrl(other.idsEndpoint() + "/data")
.querySpec(query)
.build())
.post("/catalog/request")
.then();

Expand All @@ -108,6 +138,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 b6eab4f

Please sign in to comment.