Search in sources :

Example 1 with DataSourceRegistry

use of org.eclipse.dataspaceconnector.spi.transaction.datasource.DataSourceRegistry in project DataSpaceConnector by eclipse-dataspaceconnector.

the class SqlTransferProcessStoreTest method setUp.

@BeforeEach
void setUp() throws SQLException, IOException {
    var transactionContext = new NoopTransactionContext();
    dataSourceRegistry = mock(DataSourceRegistry.class);
    var jdbcDataSource = new JdbcDataSource();
    jdbcDataSource.setURL("jdbc:h2:mem:");
    // do not actually close
    connection = spy(jdbcDataSource.getConnection());
    doNothing().when(connection).close();
    var datasourceMock = mock(DataSource.class);
    when(datasourceMock.getConnection()).thenReturn(connection);
    when(dataSourceRegistry.resolve(DATASOURCE_NAME)).thenReturn(datasourceMock);
    var statements = new PostgresStatements();
    store = new SqlTransferProcessStore(dataSourceRegistry, DATASOURCE_NAME, transactionContext, new ObjectMapper(), statements, CONNECTOR_NAME);
    var schema = Files.readString(Paths.get("./docs/schema.sql"));
    transactionContext.execute(() -> SqlQueryExecutor.executeQuery(connection, schema));
    leaseUtil = new LeaseUtil(transactionContext, this::getConnection, statements);
}
Also used : DataSourceRegistry(org.eclipse.dataspaceconnector.spi.transaction.datasource.DataSourceRegistry) LeaseUtil(org.eclipse.dataspaceconnector.sql.lease.LeaseUtil) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) NoopTransactionContext(org.eclipse.dataspaceconnector.spi.transaction.NoopTransactionContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with DataSourceRegistry

use of org.eclipse.dataspaceconnector.spi.transaction.datasource.DataSourceRegistry in project DataSpaceConnector by eclipse-dataspaceconnector.

the class SqlAssetIndexTest method setUp.

@BeforeEach
void setUp() throws SQLException, IOException {
    var txManager = new NoopTransactionContext();
    dataSourceRegistry = mock(DataSourceRegistry.class);
    transactionContext = txManager;
    var jdbcDataSource = new JdbcDataSource();
    jdbcDataSource.setURL("jdbc:h2:mem:");
    // do not actually close
    connection = spy(jdbcDataSource.getConnection());
    doNothing().when(connection).close();
    DataSource datasourceMock = mock(DataSource.class);
    when(datasourceMock.getConnection()).thenReturn(connection);
    when(dataSourceRegistry.resolve(DATASOURCE_NAME)).thenReturn(datasourceMock);
    sqlAssetIndex = new SqlAssetIndex(dataSourceRegistry, DATASOURCE_NAME, transactionContext, new ObjectMapper(), new PostgresSqlAssetQueries());
    sqlAssetQueries = new PostgresSqlAssetQueries();
    var schema = Files.readString(Paths.get("./docs/schema.sql"));
    transactionContext.execute(() -> SqlQueryExecutor.executeQuery(connection, schema));
}
Also used : DataSourceRegistry(org.eclipse.dataspaceconnector.spi.transaction.datasource.DataSourceRegistry) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) NoopTransactionContext(org.eclipse.dataspaceconnector.spi.transaction.NoopTransactionContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) DataSource(javax.sql.DataSource) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with DataSourceRegistry

use of org.eclipse.dataspaceconnector.spi.transaction.datasource.DataSourceRegistry in project DataSpaceConnector by eclipse-dataspaceconnector.

the class AtomikosTransactionExtensionTest method verifyEndToEndTransactions.

@Test
void verifyEndToEndTransactions() {
    var extensionContext = mock(ServiceExtensionContext.class);
    when(extensionContext.getConnectorId()).thenReturn(randomUUID().toString());
    when(extensionContext.getConfig()).thenReturn(ConfigFactory.fromMap(Map.of(TransactionManagerConfigurationKeys.LOGGING, "false")));
    when(extensionContext.getConfig(isA(String.class))).thenAnswer(a -> createDataSourceConfig());
    when(extensionContext.getConfig()).thenAnswer(a -> createAtomikosConfig());
    when(extensionContext.getMonitor()).thenReturn(mock(Monitor.class));
    var dsRegistry = new DataSourceRegistry[1];
    doAnswer(invocation -> {
        dsRegistry[0] = invocation.getArgument(1);
        return null;
    }).when(extensionContext).registerService(eq(DataSourceRegistry.class), isA(DataSourceRegistry.class));
    var transactionContext = new TransactionContext[1];
    doAnswer(invocation -> {
        transactionContext[0] = invocation.getArgument(1);
        return null;
    }).when(extensionContext).registerService(eq(TransactionContext.class), isA(TransactionContext.class));
    extension.initialize(extensionContext);
    extension.start();
    transactionContext[0].execute(() -> {
        try (var conn = dsRegistry[0].resolve("default").getConnection()) {
            Statement s1 = conn.createStatement();
            s1.execute("DROP ALL OBJECTS");
            s1.execute("CREATE TABLE Foo (id number)");
            s1.execute("INSERT into Foo values (1)");
        } catch (SQLException e) {
            throw new EdcException(e);
        }
    });
    // verify committed data available in separate transaction
    int[] numberOfResults = new int[1];
    transactionContext[0].execute(() -> {
        try (var conn = dsRegistry[0].resolve("default").getConnection()) {
            Statement s1 = conn.createStatement();
            var results = s1.executeQuery("SELECT * FROM Foo where id = 1");
            numberOfResults[0] = results.last() ? results.getRow() : 0;
        } catch (SQLException e) {
            throw new EdcException(e);
        }
    });
    assertThat(numberOfResults[0]).isEqualTo(1);
    // verify rollback on exception in a nested block
    assertThatExceptionOfType(EdcException.class).isThrownBy(() -> transactionContext[0].execute(() -> {
        try (var conn = dsRegistry[0].resolve("default").getConnection()) {
            Statement s1 = conn.createStatement();
            s1.execute("INSERT into Foo values (2)");
            transactionContext[0].execute(() -> {
                throw new RuntimeException();
            });
        } catch (SQLException e) {
            throw new EdcException(e);
        }
    }));
    transactionContext[0].execute(() -> {
        try (var conn = dsRegistry[0].resolve("default").getConnection()) {
            Statement s1 = conn.createStatement();
            var results = s1.executeQuery("SELECT * FROM Foo where id <= 2");
            numberOfResults[0] = results.last() ? results.getRow() : 0;
        } catch (SQLException e) {
            throw new EdcException(e);
        }
    });
    // the second rollsback; only one row should be present
    assertThat(numberOfResults[0]).isEqualTo(1);
    extension.shutdown();
}
Also used : Monitor(org.eclipse.dataspaceconnector.spi.monitor.Monitor) DataSourceRegistry(org.eclipse.dataspaceconnector.spi.transaction.datasource.DataSourceRegistry) SQLException(java.sql.SQLException) TransactionContext(org.eclipse.dataspaceconnector.spi.transaction.TransactionContext) Statement(java.sql.Statement) EdcException(org.eclipse.dataspaceconnector.spi.EdcException) Test(org.junit.jupiter.api.Test)

Example 4 with DataSourceRegistry

use of org.eclipse.dataspaceconnector.spi.transaction.datasource.DataSourceRegistry in project DataSpaceConnector by eclipse-dataspaceconnector.

the class SqlPolicyStoreTest method setUp.

@BeforeEach
void setUp() throws SQLException, IOException {
    var transactionContext = new NoopTransactionContext();
    DataSourceRegistry dataSourceRegistry = mock(DataSourceRegistry.class);
    var jdbcDataSource = new JdbcDataSource();
    jdbcDataSource.setURL("jdbc:h2:mem:");
    // do not actually close
    connection = spy(jdbcDataSource.getConnection());
    doNothing().when(connection).close();
    var datasourceMock = mock(DataSource.class);
    when(datasourceMock.getConnection()).thenReturn(connection);
    when(dataSourceRegistry.resolve(DATASOURCE_NAME)).thenReturn(datasourceMock);
    sqlPolicyStore = new SqlPolicyStore(dataSourceRegistry, DATASOURCE_NAME, transactionContext, new TypeManager(), new PostgressStatements());
    var schema = Files.readString(Paths.get("./docs/schema.sql"));
    transactionContext.execute(() -> SqlQueryExecutor.executeQuery(connection, schema));
}
Also used : PostgressStatements(org.eclipse.dataspaceconnector.sql.policy.store.PostgressStatements) DataSourceRegistry(org.eclipse.dataspaceconnector.spi.transaction.datasource.DataSourceRegistry) SqlPolicyStore(org.eclipse.dataspaceconnector.sql.policy.store.SqlPolicyStore) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) TypeManager(org.eclipse.dataspaceconnector.spi.types.TypeManager) NoopTransactionContext(org.eclipse.dataspaceconnector.spi.transaction.NoopTransactionContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 5 with DataSourceRegistry

use of org.eclipse.dataspaceconnector.spi.transaction.datasource.DataSourceRegistry in project DataSpaceConnector by eclipse-dataspaceconnector.

the class SqlContractDefinitionStoreTest method setUp.

@BeforeEach
void setUp() throws SQLException, IOException {
    var txManager = new NoopTransactionContext();
    dataSourceRegistry = mock(DataSourceRegistry.class);
    var jdbcDataSource = new JdbcDataSource();
    jdbcDataSource.setURL("jdbc:h2:mem:");
    // do not actually close
    connection = spy(jdbcDataSource.getConnection());
    doNothing().when(connection).close();
    DataSource datasourceMock = mock(DataSource.class);
    when(datasourceMock.getConnection()).thenReturn(connection);
    when(dataSourceRegistry.resolve(DATASOURCE_NAME)).thenReturn(datasourceMock);
    statements = new PostgresStatements();
    sqlContractDefinitionStore = new SqlContractDefinitionStore(dataSourceRegistry, DATASOURCE_NAME, txManager, statements, new TypeManager());
    var schema = Files.readString(Paths.get("./docs/schema.sql"));
    txManager.execute(() -> SqlQueryExecutor.executeQuery(connection, schema));
}
Also used : DataSourceRegistry(org.eclipse.dataspaceconnector.spi.transaction.datasource.DataSourceRegistry) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) TypeManager(org.eclipse.dataspaceconnector.spi.types.TypeManager) NoopTransactionContext(org.eclipse.dataspaceconnector.spi.transaction.NoopTransactionContext) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) DataSource(javax.sql.DataSource) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

DataSourceRegistry (org.eclipse.dataspaceconnector.spi.transaction.datasource.DataSourceRegistry)6 NoopTransactionContext (org.eclipse.dataspaceconnector.spi.transaction.NoopTransactionContext)5 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 TypeManager (org.eclipse.dataspaceconnector.spi.types.TypeManager)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DataSource (javax.sql.DataSource)2 LeaseUtil (org.eclipse.dataspaceconnector.sql.lease.LeaseUtil)2 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 EdcException (org.eclipse.dataspaceconnector.spi.EdcException)1 Monitor (org.eclipse.dataspaceconnector.spi.monitor.Monitor)1 TransactionContext (org.eclipse.dataspaceconnector.spi.transaction.TransactionContext)1 PostgressStatements (org.eclipse.dataspaceconnector.sql.policy.store.PostgressStatements)1 SqlPolicyStore (org.eclipse.dataspaceconnector.sql.policy.store.SqlPolicyStore)1 Test (org.junit.jupiter.api.Test)1