Search in sources :

Example 1 with JdbcWriter

use of org.apache.gobblin.writer.JdbcWriter in project incubator-gobblin by apache.

the class JdbcWriterTest method writeFailRollbackTest.

@Test
public void writeFailRollbackTest() throws SQLException, IOException {
    final String database = "db";
    final String table = "users";
    JdbcWriterCommands writerCommands = mock(JdbcWriterCommands.class);
    Connection conn = mock(Connection.class);
    doThrow(RuntimeException.class).when(writerCommands).insert(anyString(), anyString(), any(JdbcEntryData.class));
    JdbcWriter writer = new JdbcWriter(writerCommands, new State(), database, table, conn);
    try {
        writer.write(null);
        Assert.fail("Test case didn't throw Exception.");
    } catch (RuntimeException e) {
        Assert.assertTrue(e instanceof RuntimeException);
    }
    writer.close();
    verify(writerCommands, times(1)).insert(anyString(), anyString(), any(JdbcEntryData.class));
    verify(conn, times(1)).rollback();
    verify(conn, never()).commit();
    verify(conn, times(1)).close();
    Assert.assertEquals(writer.recordsWritten(), 0L);
}
Also used : JdbcWriter(org.apache.gobblin.writer.JdbcWriter) JdbcWriterCommands(org.apache.gobblin.writer.commands.JdbcWriterCommands) State(org.apache.gobblin.configuration.State) Connection(java.sql.Connection) JdbcEntryData(org.apache.gobblin.converter.jdbc.JdbcEntryData) Test(org.testng.annotations.Test)

Example 2 with JdbcWriter

use of org.apache.gobblin.writer.JdbcWriter in project incubator-gobblin by apache.

the class JdbcWriterTest method writeAndCommitTest.

@Test
public void writeAndCommitTest() throws SQLException, IOException {
    final String database = "db";
    final String table = "users";
    final int writeCount = 25;
    JdbcWriterCommands writerCommands = mock(JdbcWriterCommands.class);
    Connection conn = mock(Connection.class);
    try (JdbcWriter writer = new JdbcWriter(writerCommands, new State(), database, table, conn)) {
        for (int i = 0; i < writeCount; i++) {
            writer.write(null);
        }
        writer.commit();
        Assert.assertEquals(writer.recordsWritten(), writeCount);
    }
    verify(writerCommands, times(writeCount)).insert(anyString(), anyString(), any(JdbcEntryData.class));
    verify(conn, times(1)).commit();
    verify(conn, never()).rollback();
    verify(writerCommands, times(1)).flush();
    verify(conn, times(1)).close();
}
Also used : JdbcWriter(org.apache.gobblin.writer.JdbcWriter) JdbcWriterCommands(org.apache.gobblin.writer.commands.JdbcWriterCommands) State(org.apache.gobblin.configuration.State) Connection(java.sql.Connection) JdbcEntryData(org.apache.gobblin.converter.jdbc.JdbcEntryData) Test(org.testng.annotations.Test)

Aggregations

Connection (java.sql.Connection)2 State (org.apache.gobblin.configuration.State)2 JdbcEntryData (org.apache.gobblin.converter.jdbc.JdbcEntryData)2 JdbcWriter (org.apache.gobblin.writer.JdbcWriter)2 JdbcWriterCommands (org.apache.gobblin.writer.commands.JdbcWriterCommands)2 Test (org.testng.annotations.Test)2