Skip to content

Commit

Permalink
feat(test): run management-api e2e tests with postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Feb 15, 2024
1 parent 1e7dabd commit f7742e6
Show file tree
Hide file tree
Showing 24 changed files with 1,800 additions and 1,502 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private static <T> T getFieldValue(List<PathItem> path, Object object) {
var closingBracketIx = first.toString().indexOf(CLOSING_BRACKET);
var propName = first.toString().substring(0, openingBracketIx);
var arrayIndex = Integer.parseInt(first.toString().substring(openingBracketIx + 1, closingBracketIx));
var iterableObject = (List) getFieldValue(propName, object);
var iterableObject = (List) getFieldValue("'%s'".formatted(propName), object);
return (T) iterableObject.get(arrayIndex);
} else {
if (object instanceof Map<?, ?> map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ void shouldGetNestedValue_whenKeyContainsDot() {
assertThat(value).isInstanceOf(String.class).isEqualTo("value");
}

@Test
void shouldMapValueFromList() {
var object = Map.of("http://namespace.domain/property", List.of(Map.of("@value", "value")));

var value = ReflectionUtil.getFieldValue("'http://namespace.domain/property'[0].@value", object);

assertThat(value).isInstanceOf(String.class).isEqualTo("value");
}
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static java.lang.String.format;

class AssetQueryValidator extends QueryValidator {
private static final Pattern VALID_QUERY_PATH_REGEX = Pattern.compile("^[A-Za-z_]+.*$");
private static final Pattern VALID_QUERY_PATH_REGEX = Pattern.compile("^[A-Za-z_']+.*$");

AssetQueryValidator() {
super(Asset.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected Result<Void> isValid(String path) {

// cannot query on extensible (=Map) types
if (type == Map.class) {
var pattern = Pattern.compile("^[0-9A-Za-z.':/]*$");
var pattern = Pattern.compile("^[0-9A-Za-z.':/@]*$");
var matcher = pattern.matcher(path);
return matcher.find() ? Result.success() :
Result.failure("Querying Map types is not yet supported");
Expand All @@ -93,7 +93,7 @@ protected Result<Void> isValid(String path) {
if (field != null) {
type = field.getType();
if (Collection.class.isAssignableFrom(type)) {
ParameterizedType genericType = (ParameterizedType) field.getGenericType();
var genericType = (ParameterizedType) field.getGenericType();
type = (Class<?>) genericType.getActualTypeArguments()[0];
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ class AssetQueryValidatorTest {
Asset.PROPERTY_CONTENT_TYPE,
"someCustomVal",
"_anotherValidVal",

"'http://some.url/property'.nestedvalue"
})
void validate_validProperty(String key) {
var query = QuerySpec.Builder.newInstance().filter(List.of(new Criterion(key, "=", "someval"))).build();

assertThat(validator.validate(query).succeeded()).isTrue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import org.eclipse.edc.spi.query.Criterion;
import org.eclipse.edc.spi.query.QuerySpec;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -34,10 +33,6 @@

class QueryValidatorTest {

@BeforeEach
void setUp() {
}

@Test
void validate_isValid() {
var queryValidator = new QueryValidator(TestObject.class);
Expand Down Expand Up @@ -89,6 +84,16 @@ void validate_isMapTypeTrue() {
assertThat(result.succeeded()).isTrue();
}

@Test
void shouldPermitQueryToJsonLdTags() {
var queryValidator = new QueryValidator(TestObject.class);
var query = with(criterion("someMap.foo.@id", "=", "bar"));

var result = queryValidator.validate(query);

assertThat(result.succeeded()).isTrue();
}

@Test
void validate_isMapTypeFalse() {
var queryValidator = new QueryValidator(TestObject.class);
Expand Down
5 changes: 3 additions & 2 deletions extensions/common/sql/sql-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ dependencies {


testImplementation(project(":core:common:junit"))
testFixturesImplementation(libs.postgres)
testFixturesImplementation(libs.junit.jupiter.api)
testFixturesImplementation(project(":spi:common:transaction-datasource-spi"))
testFixturesImplementation(project(":core:common:junit"))
testFixturesImplementation(libs.junit.jupiter.api)
testFixturesImplementation(libs.mockito.core)
testFixturesImplementation(libs.postgres)

testFixturesImplementation(libs.testcontainers.junit)
testFixturesImplementation(libs.testcontainers.postgres)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.sql.testfixtures;

import org.eclipse.edc.junit.testfixtures.TestUtils;
import org.eclipse.edc.spi.persistence.EdcPersistenceException;

import java.io.IOException;
import java.nio.file.Files;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

public interface PostgresqlEndToEndInstance {

String USER = "postgres";
String PASSWORD = "password";
String JDBC_URL_PREFIX = "jdbc:postgresql://localhost:5432/";

static void createDatabase(String participantName) {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
throw new EdcPersistenceException(e);
}

var postgres = new PostgresqlLocalInstance(USER, PASSWORD, JDBC_URL_PREFIX, participantName);
postgres.createDatabase();

var extensionsFolder = TestUtils.findBuildRoot().toPath().resolve("extensions");
var scripts = Stream.of(
"control-plane/store/sql/asset-index-sql",
"control-plane/store/sql/contract-definition-store-sql",
"control-plane/store/sql/contract-negotiation-store-sql",
"control-plane/store/sql/policy-definition-store-sql",
"control-plane/store/sql/transfer-process-store-sql",
"data-plane/store/sql/data-plane-store-sql",
"policy-monitor/store/sql/policy-monitor-store-sql"
)
.map(extensionsFolder::resolve)
.map(it -> it.resolve("docs"))
.map(it -> it.resolve("schema.sql"))
.toList();

try (var connection = postgres.getConnection(participantName)) {
for (var script : scripts) {
var sql = Files.readString(script);

try (var statement = connection.createStatement()) {
statement.execute(sql);
} catch (Exception exception) {
throw new EdcPersistenceException(exception.getMessage(), exception);
}
}
} catch (SQLException | IOException e) {
throw new EdcPersistenceException(e);
}
}

static Map<String, String> defaultDatasourceConfiguration(String name) {
return new HashMap<>() {
{
put("edc.datasource.default.url", JDBC_URL_PREFIX + name);
put("edc.datasource.default.user", USER);
put("edc.datasource.default.password", PASSWORD);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@

import java.util.HashMap;

import static org.eclipse.edc.test.e2e.PostgresUtil.createDatabase;
import static org.eclipse.edc.sql.testfixtures.PostgresqlEndToEndInstance.createDatabase;

@PostgresqlDbIntegrationTest
class EndToEndTransferPostgresqlTest extends AbstractEndToEndTransfer {

@RegisterExtension
static BeforeAllCallback createDatabase = context -> {
createDatabase(CONSUMER);
createDatabase(PROVIDER);
createDatabase(CONSUMER.getName());
createDatabase(PROVIDER.getName());
};

static String[] controlPlanePostgresqlModules = new String[] {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import jakarta.json.Json;
import jakarta.json.JsonObject;
import org.eclipse.edc.spi.types.domain.edr.EndpointDataReference;
import org.eclipse.edc.test.e2e.PostgresConstants;
import org.eclipse.edc.test.system.utils.Participant;
import org.hamcrest.Matcher;
import org.jetbrains.annotations.NotNull;
Expand All @@ -45,6 +44,7 @@
import static org.eclipse.edc.spi.CoreConstants.EDC_NAMESPACE;
import static org.eclipse.edc.spi.CoreConstants.EDC_PREFIX;
import static org.eclipse.edc.spi.system.ServiceExtensionContext.PARTICIPANT_ID;
import static org.eclipse.edc.sql.testfixtures.PostgresqlEndToEndInstance.defaultDatasourceConfiguration;

public class EndToEndTransferParticipant extends Participant {

Expand Down Expand Up @@ -208,24 +208,10 @@ public Map<String, String> controlPlaneConfiguration() {

public Map<String, String> controlPlanePostgresConfiguration() {
var baseConfiguration = controlPlaneConfiguration();

var postgresConfiguration = new HashMap<String, String>() {
{
put("edc.datasource.default.url", jdbcUrl());
put("edc.datasource.default.user", PostgresConstants.USER);
put("edc.datasource.default.password", PostgresConstants.PASSWORD);
}
};
baseConfiguration.putAll(postgresConfiguration);

baseConfiguration.putAll(defaultDatasourceConfiguration(getName()));
return baseConfiguration;
}

@NotNull
public String jdbcUrl() {
return PostgresConstants.JDBC_URL_PREFIX + getName();
}

public Map<String, String> dataPlaneConfiguration() {
return new HashMap<>() {
{
Expand All @@ -246,17 +232,7 @@ public Map<String, String> dataPlaneConfiguration() {

public Map<String, String> dataPlanePostgresConfiguration() {
var baseConfiguration = dataPlaneConfiguration();

var postgresConfiguration = new HashMap<String, String>() {
{
put("edc.datasource.default.url", jdbcUrl());
put("edc.datasource.default.user", PostgresConstants.USER);
put("edc.datasource.default.password", PostgresConstants.PASSWORD);
}
};

baseConfiguration.putAll(postgresConfiguration);

baseConfiguration.putAll(defaultDatasourceConfiguration(getName()));
return baseConfiguration;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ dependencies {
testImplementation(libs.awaitility)
testImplementation(libs.junit.jupiter.api)
testImplementation(libs.jakartaJson)
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"))
}
Expand Down
Loading

0 comments on commit f7742e6

Please sign in to comment.