Skip to content

Commit

Permalink
feat: prevent using SQL modules with the NoopTransactionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Jul 3, 2024
1 parent 7abbdad commit 332188d
Show file tree
Hide file tree
Showing 2 changed files with 59 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()));
}
}

0 comments on commit 332188d

Please sign in to comment.