Elffuss BraidHub

kikocis/EDC public

feat: prevent using SQL modules with the NoopTransactionContext (#4326)

* feat: prevent using SQL modules with the NoopTransactionContext

* DEPENDENCIES

* add txlocal to test runner
Changeset3cd7a22d5fb761b761e4edd724dfaa02689013d74ae9b926fafae42bdedbe2fd
AuthorPaul Latzelsperger <43503240+paullatzelsperger@users.noreply.github.com> · 2024-07-05 07:54 +02:00
CommitterGitHub <noreply@github.com> · 2024-07-05 07:54 +02:00
Parents119681864e62

Browse the files at this changeset

5 files changed

modified extensions/common/sql/sql-core/src/main/java/org/eclipse/edc/sql/SqlCoreExtension.java +14 −0
@@ -15,10 +15,14 @@
1515 package org.eclipse.edc.sql;
1616
1717 import org.eclipse.edc.runtime.metamodel.annotation.Extension;
18+import org.eclipse.edc.runtime.metamodel.annotation.Inject;
1819 import org.eclipse.edc.runtime.metamodel.annotation.Provider;
1920 import org.eclipse.edc.runtime.metamodel.annotation.Setting;
21+import org.eclipse.edc.spi.EdcException;
2022 import org.eclipse.edc.spi.system.ServiceExtension;
2123 import org.eclipse.edc.spi.system.ServiceExtensionContext;
24+import org.eclipse.edc.transaction.spi.NoopTransactionContext;
25+import org.eclipse.edc.transaction.spi.TransactionContext;
2226
2327 import static java.lang.Integer.parseInt;
2428
@@ -31,11 +35,21 @@
3135 @Setting(value = "Fetch size value used in SQL queries", defaultValue = DEFAULT_EDC_SQL_FETCH_SIZE)
3236 public static final String EDC_SQL_FETCH_SIZE = "edc.sql.fetch.size";
3337
38+ @Inject
39+ private TransactionContext transactionContext;
40+
3441 @Override
3542 public String name() {
3643 return NAME;
3744 }
3845
46+ @Override
47+ public void initialize(ServiceExtensionContext context) {
48+ if (transactionContext instanceof NoopTransactionContext) {
49+ throw new EdcException("The EDC SQL implementations cannot be used with a '%s'. Please provide a TransactionContext implementation.".formatted(NoopTransactionContext.class.getName()));
50+ }
51+ }
52+
3953 @Provider
4054 public QueryExecutor sqlQueryExecutor(ServiceExtensionContext context) {
4155 var fetchSize = context.getSetting(EDC_SQL_FETCH_SIZE, parseInt(DEFAULT_EDC_SQL_FETCH_SIZE));
added extensions/common/sql/sql-core/src/test/java/org/eclipse/edc/sql/SqlCoreExtensionTest.java +45 −0
@@ -0,0 +1,45 @@
1+/*
2+ * Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
3+ *
4+ * This program and the accompanying materials are made available under the
5+ * terms of the Apache License, Version 2.0 which is available at
6+ * https://www.apache.org/licenses/LICENSE-2.0
7+ *
8+ * SPDX-License-Identifier: Apache-2.0
9+ *
10+ * Contributors:
11+ * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
12+ *
13+ */
14+
15+package org.eclipse.edc.sql;
16+
17+import org.eclipse.edc.junit.annotations.ComponentTest;
18+import org.eclipse.edc.junit.extensions.DependencyInjectionExtension;
19+import org.eclipse.edc.spi.EdcException;
20+import org.eclipse.edc.spi.system.ServiceExtensionContext;
21+import org.eclipse.edc.transaction.spi.NoopTransactionContext;
22+import org.eclipse.edc.transaction.spi.TransactionContext;
23+import org.junit.jupiter.api.BeforeEach;
24+import org.junit.jupiter.api.Test;
25+import org.junit.jupiter.api.extension.ExtendWith;
26+
27+import static org.assertj.core.api.Assertions.assertThatThrownBy;
28+
29+@ComponentTest
30+@ExtendWith(DependencyInjectionExtension.class)
31+class SqlCoreExtensionTest {
32+ private ServiceExtensionContext context;
33+
34+ @BeforeEach
35+ void setup(ServiceExtensionContext context) {
36+ this.context = context;
37+ context.registerService(TransactionContext.class, new NoopTransactionContext());
38+ }
39+
40+ @Test
41+ void initialize(SqlCoreExtension extension) {
42+ assertThatThrownBy(() -> extension.initialize(context)).isInstanceOf(EdcException.class)
43+ .hasMessage("The EDC SQL implementations cannot be used with a '%s'. Please provide a TransactionContext implementation.".formatted(NoopTransactionContext.class.getName()));
44+ }
45+}
No newline at the end of the file
modified system-tests/e2e-transfer-test/runner/build.gradle.kts +1 −0
@@ -20,6 +20,7 @@
2020 testImplementation(project(":spi:control-plane:transfer-spi"))
2121 testImplementation(project(":spi:data-plane:data-plane-spi"))
2222 testImplementation(project(":extensions:common:sql:sql-core"))
23+ testImplementation(project(":extensions:common:transaction:transaction-local"))
2324
2425 testImplementation(project(":spi:common:web-spi"))
2526 testImplementation(project(":core:common:connector-core"))
modified system-tests/management-api/management-api-test-runner/build.gradle.kts +1 −0
@@ -39,6 +39,7 @@
3939 testImplementation(libs.awaitility)
4040 testImplementation(libs.junit.jupiter.api)
4141 testImplementation(testFixtures(project(":extensions:common:sql:sql-core")))
42+ testImplementation(project(":extensions:common:transaction:transaction-local"))
4243 testImplementation(libs.testcontainers.junit)
4344 testImplementation(libs.testcontainers.postgres)
4445 }
modified system-tests/version-api/version-api-test-runner/build.gradle.kts +1 −0
@@ -42,6 +42,7 @@
4242 testImplementation(libs.awaitility)
4343 testImplementation(libs.junit.jupiter.api)
4444 testImplementation(testFixtures(project(":extensions:common:sql:sql-core")))
45+ testImplementation(project(":extensions:common:transaction:transaction-local"))
4546 testImplementation(libs.testcontainers.junit)
4647 testImplementation(libs.testcontainers.postgres)
4748 }