Skip to content

Commit

Permalink
feat: prevent using SQL modules with the NoopTransactionContext (#4326)
Browse files Browse the repository at this point in the history
* feat: prevent using SQL modules with the NoopTransactionContext

* DEPENDENCIES

* add txlocal to test runner
  • Loading branch information
paullatzelsperger authored Jul 5, 2024
1 parent baf2e2b commit 2cf9bf2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
package org.eclipse.edc.sql;

import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.transaction.spi.NoopTransactionContext;
import org.eclipse.edc.transaction.spi.TransactionContext;

import static java.lang.Integer.parseInt;

Expand All @@ -31,11 +35,21 @@ public class SqlCoreExtension implements ServiceExtension {
@Setting(value = "Fetch size value used in SQL queries", defaultValue = DEFAULT_EDC_SQL_FETCH_SIZE)
public static final String EDC_SQL_FETCH_SIZE = "edc.sql.fetch.size";

@Inject
private TransactionContext transactionContext;

@Override
public String name() {
return NAME;
}

@Override
public void initialize(ServiceExtensionContext context) {
if (transactionContext instanceof NoopTransactionContext) {
throw new EdcException("The EDC SQL implementations cannot be used with a '%s'. Please provide a TransactionContext implementation.".formatted(NoopTransactionContext.class.getName()));
}
}

@Provider
public QueryExecutor sqlQueryExecutor(ServiceExtensionContext context) {
var fetchSize = context.getSetting(EDC_SQL_FETCH_SIZE, parseInt(DEFAULT_EDC_SQL_FETCH_SIZE));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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;

import org.eclipse.edc.junit.annotations.ComponentTest;
import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.transaction.spi.NoopTransactionContext;
import org.eclipse.edc.transaction.spi.TransactionContext;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

@ComponentTest
@ExtendWith(DependencyInjectionExtension.class)
class SqlCoreExtensionTest {
private ServiceExtensionContext context;

@BeforeEach
void setup(ServiceExtensionContext context) {
this.context = context;
context.registerService(TransactionContext.class, new NoopTransactionContext());
}

@Test
void initialize(SqlCoreExtension extension) {
assertThatThrownBy(() -> extension.initialize(context)).isInstanceOf(EdcException.class)
.hasMessage("The EDC SQL implementations cannot be used with a '%s'. Please provide a TransactionContext implementation.".formatted(NoopTransactionContext.class.getName()));
}
}
1 change: 1 addition & 0 deletions system-tests/e2e-transfer-test/runner/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
testImplementation(project(":spi:control-plane:transfer-spi"))
testImplementation(project(":spi:data-plane:data-plane-spi"))
testImplementation(project(":extensions:common:sql:sql-core"))
testImplementation(project(":extensions:common:transaction:transaction-local"))

testImplementation(project(":spi:common:web-spi"))
testImplementation(project(":core:common:connector-core"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
testImplementation(libs.awaitility)
testImplementation(libs.junit.jupiter.api)
testImplementation(testFixtures(project(":extensions:common:sql:sql-core")))
testImplementation(project(":extensions:common:transaction:transaction-local"))
testImplementation(libs.testcontainers.junit)
testImplementation(libs.testcontainers.postgres)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies {
testImplementation(libs.awaitility)
testImplementation(libs.junit.jupiter.api)
testImplementation(testFixtures(project(":extensions:common:sql:sql-core")))
testImplementation(project(":extensions:common:transaction:transaction-local"))
testImplementation(libs.testcontainers.junit)
testImplementation(libs.testcontainers.postgres)
}
Expand Down

0 comments on commit 2cf9bf2

Please sign in to comment.