Skip to content

Commit

Permalink
refactor(test): speed up management test (#4223)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed May 30, 2024
1 parent cd3d82c commit d22b18e
Show file tree
Hide file tree
Showing 13 changed files with 358 additions and 539 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ dependencies {
testImplementation(testFixtures(project(":extensions:common:sql:sql-core")))
testImplementation(libs.testcontainers.junit)
testImplementation(libs.testcontainers.postgres)

testCompileOnly(project(":system-tests:management-api:management-api-test-runtime"))
}

edcBuild {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
import org.eclipse.edc.connector.controlplane.asset.spi.index.AssetIndex;
import org.eclipse.edc.junit.annotations.EndToEndTest;
import org.eclipse.edc.junit.annotations.PostgresqlIntegrationTest;
import org.eclipse.edc.junit.extensions.EdcRuntimeExtension;
import org.eclipse.edc.spi.types.domain.DataAddress;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.UUID;

Expand All @@ -38,9 +36,6 @@
import static org.eclipse.edc.spi.constants.CoreConstants.EDC_NAMESPACE;
import static org.eclipse.edc.spi.constants.CoreConstants.EDC_PREFIX;
import static org.eclipse.edc.spi.query.Criterion.criterion;
import static org.eclipse.edc.sql.testfixtures.PostgresqlEndToEndInstance.createDatabase;
import static org.eclipse.edc.test.e2e.managementapi.Runtimes.inMemoryRuntime;
import static org.eclipse.edc.test.e2e.managementapi.Runtimes.postgresRuntime;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

Expand All @@ -51,47 +46,25 @@ public class AssetApiEndToEndTest {

@Nested
@EndToEndTest
class InMemory extends Tests {

@RegisterExtension
public static final EdcRuntimeExtension RUNTIME = inMemoryRuntime();

InMemory() {
super(RUNTIME);
}

}
@ExtendWith(ManagementEndToEndExtension.InMemory.class)
class InMemory extends Tests { }

@Nested
@PostgresqlIntegrationTest
class Postgres extends Tests {

@RegisterExtension
static final BeforeAllCallback CREATE_DATABASE = context -> createDatabase("runtime");

@RegisterExtension
public static final EdcRuntimeExtension RUNTIME = postgresRuntime();
@ExtendWith(ManagementEndToEndExtension.Postgres.class)
class Postgres extends Tests { }

Postgres() {
super(RUNTIME);
}
}

abstract static class Tests extends ManagementApiEndToEndTestBase {

Tests(EdcRuntimeExtension runtime) {
super(runtime);
}
abstract static class Tests {

@Test
void getAssetById() {
void getAssetById(ManagementEndToEndTestContext context, AssetIndex assetIndex) {
var id = UUID.randomUUID().toString();
var asset = createAsset().id(id)
.dataAddress(createDataAddress().type("addressType").build())
.build();
getAssetIndex().create(asset);
assetIndex.create(asset);

var body = baseRequest()
var body = context.baseRequest()
.get("/v3/assets/" + id)
.then()
.statusCode(200)
Expand All @@ -110,7 +83,7 @@ void getAssetById() {
}

@Test
void createAsset_shouldBeStored() {
void createAsset_shouldBeStored(ManagementEndToEndTestContext context, AssetIndex assetIndex) {
var id = UUID.randomUUID().toString();
var assetJson = createObjectBuilder()
.add(CONTEXT, createObjectBuilder().add(EDC_PREFIX, EDC_NAMESPACE))
Expand All @@ -123,7 +96,7 @@ void createAsset_shouldBeStored() {
.build())
.build();

baseRequest()
context.baseRequest()
.contentType(ContentType.JSON)
.body(assetJson)
.post("/v3/assets")
Expand All @@ -132,19 +105,19 @@ void createAsset_shouldBeStored() {
.statusCode(200)
.body(ID, is(id));

assertThat(getAssetIndex().findById(id)).isNotNull();
assertThat(assetIndex.findById(id)).isNotNull();
}

@Test
void createAsset_shouldFail_whenBodyIsNotValid() {
void createAsset_shouldFail_whenBodyIsNotValid(ManagementEndToEndTestContext context) {
var assetJson = createObjectBuilder()
.add(CONTEXT, createObjectBuilder().add(EDC_PREFIX, EDC_NAMESPACE))
.add(TYPE, "Asset")
.add(ID, " ")
.add("properties", createPropertiesBuilder().build())
.build();

baseRequest()
context.baseRequest()
.contentType(ContentType.JSON)
.body(assetJson)
.post("/v3/assets")
Expand All @@ -154,7 +127,7 @@ void createAsset_shouldFail_whenBodyIsNotValid() {
}

@Test
void createAsset_withoutPrefix_shouldAddEdcNamespace() {
void createAsset_withoutPrefix_shouldAddEdcNamespace(ManagementEndToEndTestContext context, AssetIndex assetIndex) {
var id = UUID.randomUUID().toString();
var assetJson = createObjectBuilder()
.add(CONTEXT, createObjectBuilder().add(EDC_PREFIX, EDC_NAMESPACE))
Expand All @@ -169,7 +142,7 @@ void createAsset_withoutPrefix_shouldAddEdcNamespace() {
.build())
.build();

baseRequest()
context.baseRequest()
.contentType(ContentType.JSON)
.body(assetJson)
.post("/v3/assets")
Expand All @@ -178,26 +151,26 @@ void createAsset_withoutPrefix_shouldAddEdcNamespace() {
.statusCode(200)
.body(ID, is(id));

var asset = getAssetIndex().findById(id);
var asset = assetIndex.findById(id);
assertThat(asset).isNotNull();
//make sure unprefixed keys are caught and prefixed with the EDC_NAMESPACE ns.
assertThat(asset.getProperties().keySet())
.hasSize(6)
.allMatch(key -> key.startsWith(EDC_NAMESPACE));

var dataAddress = getAssetIndex().resolveForAsset(asset.getId());
var dataAddress = assetIndex.resolveForAsset(asset.getId());
assertThat(dataAddress).isNotNull();
assertThat(dataAddress.getProperties().keySet())
.hasSize(2)
.allMatch(key -> key.startsWith(EDC_NAMESPACE));
}

@Test
void queryAsset_byContentType() {
void queryAsset_byContentType(ManagementEndToEndTestContext context, AssetIndex assetIndex) {
//insert one asset into the index
var id = UUID.randomUUID().toString();
var asset = Asset.Builder.newInstance().id(id).contentType("application/octet-stream").dataAddress(createDataAddress().build()).build();
getAssetIndex().create(asset);
assetIndex.create(asset);

var query = createObjectBuilder()
.add(CONTEXT, createObjectBuilder().add(EDC_PREFIX, EDC_NAMESPACE))
Expand All @@ -212,7 +185,7 @@ void queryAsset_byContentType() {
.add("operandRight", "application/octet-stream"))
).build();

baseRequest()
context.baseRequest()
.contentType(ContentType.JSON)
.body(query)
.post("/v3/assets/request")
Expand All @@ -223,17 +196,17 @@ void queryAsset_byContentType() {
}

@Test
void queryAsset_byCustomStringProperty() {
getAssetIndex().create(Asset.Builder.newInstance()
void queryAsset_byCustomStringProperty(ManagementEndToEndTestContext context, AssetIndex assetIndex) {
assetIndex.create(Asset.Builder.newInstance()
.id("test-asset")
.contentType("application/octet-stream")
.property("myProp", "myVal")
.dataAddress(createDataAddress().build())
.build());

baseRequest()
context.baseRequest()
.contentType(ContentType.JSON)
.body(query(criterion("myProp", "=", "myVal")))
.body(context.query(criterion("myProp", "=", "myVal")))
.post("/v3/assets/request")
.then()
.log().ifError()
Expand All @@ -242,7 +215,7 @@ void queryAsset_byCustomStringProperty() {
}

@Test
void queryAsset_byCustomComplexProperty() {
void queryAsset_byCustomComplexProperty(ManagementEndToEndTestContext context) {
var id = UUID.randomUUID().toString();
var assetJson = createObjectBuilder()
.add(CONTEXT, createObjectBuilder().add(EDC_PREFIX, EDC_NAMESPACE))
Expand All @@ -258,7 +231,7 @@ void queryAsset_byCustomComplexProperty() {
.build())
.build();

baseRequest()
context.baseRequest()
.contentType(ContentType.JSON)
.body(assetJson)
.post("/v3/assets")
Expand All @@ -267,12 +240,12 @@ void queryAsset_byCustomComplexProperty() {
.statusCode(200)
.body(ID, is(id));

var query = query(
var query = context.query(
criterion("'%sid".formatted(EDC_NAMESPACE), "=", id),
criterion("'%snested'.@id".formatted(EDC_NAMESPACE), "=", "test-nested-id")
);

baseRequest()
context.baseRequest()
.contentType(ContentType.JSON)
.body(query)
.post("/v3/assets/request")
Expand All @@ -283,9 +256,9 @@ void queryAsset_byCustomComplexProperty() {
}

@Test
void updateAsset() {
void updateAsset(ManagementEndToEndTestContext context, AssetIndex assetIndex) {
var asset = createAsset().build();
getAssetIndex().create(asset);
assetIndex.create(asset);

var assetJson = createObjectBuilder()
.add(CONTEXT, createObjectBuilder().add(EDC_PREFIX, EDC_NAMESPACE))
Expand All @@ -297,7 +270,7 @@ void updateAsset() {
.add("type", "addressType"))
.build();

baseRequest()
context.baseRequest()
.contentType(ContentType.JSON)
.body(assetJson)
.put("/v3/assets")
Expand All @@ -306,16 +279,12 @@ void updateAsset() {
.statusCode(204)
.body(notNullValue());

var dbAsset = getAssetIndex().findById(asset.getId());
var dbAsset = assetIndex.findById(asset.getId());
assertThat(dbAsset).isNotNull();
assertThat(dbAsset.getProperties()).containsEntry(EDC_NAMESPACE + "some-new-property", "some-new-value");
assertThat(dbAsset.getDataAddress().getType()).isEqualTo("addressType");
}

private AssetIndex getAssetIndex() {
return runtime.getContext().getService(AssetIndex.class);
}

private DataAddress.Builder createDataAddress() {
return DataAddress.Builder.newInstance().type("test-type");
}
Expand Down
Loading

0 comments on commit d22b18e

Please sign in to comment.