package org.embulk.output.oracle; import static org.embulk.output.oracle.OracleTests.execute; import static org.embulk.output.oracle.OracleTests.selectRecords; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; import java.nio.file.FileSystems; import java.nio.file.Path; import org.embulk.config.ConfigDiff; import org.embulk.config.ConfigSource; import org.embulk.exec.PartialExecutionException; import org.embulk.output.OracleOutputPlugin; import org.embulk.spi.OutputPlugin; import org.embulk.test.EmbulkTests; import org.embulk.test.TestingEmbulk; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import com.google.common.io.Resources; public class BasicTest { private static final String BASIC_RESOURCE_PATH = "org/embulk/output/oracle/test/expect/basic/"; private static ConfigSource loadYamlResource(TestingEmbulk embulk, String fileName) { return embulk.loadYamlResource(BASIC_RESOURCE_PATH + fileName); } private static String readResource(String fileName) { return EmbulkTests.readResource(BASIC_RESOURCE_PATH + fileName); } @Rule public TestingEmbulk embulk = TestingEmbulk.builder() .registerPlugin(OutputPlugin.class, "oracle", OracleOutputPlugin.class) .build(); private ConfigSource baseConfig; @Before public void setup() throws IOException { baseConfig = OracleTests.baseConfig(); execute(embulk, readResource("setup.sql")); // setup rows if (System.getProperty("path.separator").equals(";")) { // for Windows (because encoding will be set to UTF8 when executed by Eclipse) System.setProperty("file.encoding", "MS932"); } } @Test public void testInsert() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testInsertCreate() throws Exception { execute(embulk, "DROP TABLE TEST1;" + System.lineSeparator() + "EXIT;"); Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_create_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testInsertMultibyteTable() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_multibyte_table.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testInsertEmpty() throws Exception { Path in1 = getEmptyDir(); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_empty_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testInsertDirect() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_direct.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } // insert_method: direct @Test public void testInsertDirectDirect() throws Exception { Path in1 = toPath("test1.csv"); try { TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_direct_direct.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } catch (PartialExecutionException e) { if (e.getCause() != null && e.getCause().getClass().equals(RuntimeException.class) && e.getCause().getCause() != null && e.getCause().getCause().getClass().equals(AssertionError.class)) { // ignore error e.printStackTrace(); System.err.println("For some reason, the 'direct' mode doesn't work in gradle test task..."); return; } throw e; } } @Test public void testInsertDirectOCI() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_direct_oci.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testInsertDirectCreate() throws Exception { execute(embulk, "DROP TABLE TEST1;" + System.lineSeparator() + "EXIT;"); Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_direct.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_create_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testInsertDirectMultibyteTable() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_direct_multibyte_table.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testInsertDirectOCIMultibyteTable() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_direct_oci_multibyte_table.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testInsertDirectDuplicate() throws Exception { execute(embulk, "INSERT INTO TEST1(ID) VALUES('A002');" + System.lineSeparator() + "EXIT;"); Path in1 = toPath("test1.csv"); try { embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_direct.yml")), in1); fail("Exception expected."); } catch (Exception e) { System.out.println(e); } } @Test public void testInsertDirectOCIDuplicate() throws Exception { execute(embulk, "INSERT INTO TEST1(ID) VALUES('A002');" + System.lineSeparator() + "EXIT;"); Path in1 = toPath("test1.csv"); try { embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_direct_oci.yml")), in1); fail("Exception expected."); } catch (Exception e) { System.out.println(e); } } @Test public void testInsertDirectOCIMultibyteTableDuplicate() throws Exception { execute(embulk, "INSERT INTO TEST1(ID) VALUES('A002');" + System.lineSeparator() + "EXIT;"); Path in1 = toPath("test1.csv"); try { embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_direct_oci_multibyte_table.yml")), in1); fail("Exception expected."); } catch (Exception e) { System.out.println(e); } } // Multiple output tasks will be executed. @Test public void testInsertDirectLarge() throws Exception { Path in1 = toPath("test_large.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_direct_large.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_large_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } // Multiple output tasks will be executed. @Test public void testInsertDirectOCILarge() throws Exception { Path in1 = toPath("test_large.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_direct_oci_large.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_large_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testInsertDirectEmpty() throws Exception { Path in1 = getEmptyDir(); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_insert_direct.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_empty_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testTruncateInsert() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_truncate_insert.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_truncate_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testTruncateInsertOCI() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_truncate_insert_oci.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_truncate_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testTruncateInsertCreate() throws Exception { execute(embulk, "DROP TABLE TEST1;" + System.lineSeparator() + "EXIT;"); Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_truncate_insert.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_create_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testReplace() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_replace.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_create_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testReplaceCreate() throws Exception { execute(embulk, "DROP TABLE TEST1;" + System.lineSeparator() + "EXIT;"); Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_replace.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_create_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testReplaceWithColumnOptions() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_replace_column_options.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_truncate_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testReplaceOCI() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_replace_oci.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_create_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testReplaceLongNameTable() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_replace_longname.yml")), in1); assertThat(selectRecords(embulk, "TEST12345678901234567890123456"), is(readResource("test_insert_create_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testReplaceLongNameMultibyteTable() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_replace_longname_multibyte.yml")), in1); assertThat(selectRecords(embulk, "TEST123456789012345678"), is(readResource("test_insert_create_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testReplaceEmpty() throws Exception { Path in1 = getEmptyDir(); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_replace.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is("\n")); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testMerge() throws Exception { execute(embulk, "INSERT INTO TEST_MERGE1 VALUES('A001', 'AAA', 12.34);" + System.lineSeparator() + "EXIT;"); execute(embulk, "INSERT INTO TEST_MERGE1 VALUES('A003', NULL, NULL);" + System.lineSeparator() + "EXIT;"); execute(embulk, "INSERT INTO TEST_MERGE1 VALUES('A005', 'EEE', 56.78);" + System.lineSeparator() + "EXIT;"); execute(embulk, "INSERT INTO TEST_MERGE1 VALUES('A006', 'FFF', 0);" + System.lineSeparator() + "EXIT;"); Path in1 = toPath("test_merge.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_merge.yml")), in1); assertThat(selectRecords(embulk, "TEST_MERGE1"), is(readResource("test_merge_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testMergeWithKeys() throws Exception { execute(embulk, "INSERT INTO TEST_MERGE1 VALUES('A001', 'AAA', 11.11);" + System.lineSeparator() + "EXIT;"); execute(embulk, "INSERT INTO TEST_MERGE1 VALUES('A002', 'BBB', 22.22);" + System.lineSeparator() + "EXIT;"); execute(embulk, "INSERT INTO TEST_MERGE1 VALUES('A003', 'CCC', 33.33);" + System.lineSeparator() + "EXIT;"); Path in1 = toPath("test_merge_keys.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_merge_keys.yml")), in1); assertThat(selectRecords(embulk, "TEST_MERGE1"), is(readResource("test_merge_keys_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testMergeRule() throws Exception { execute(embulk, "INSERT INTO TEST_MERGE1 VALUES('A002', 'BBB', 22.22);" + System.lineSeparator() + "EXIT;"); execute(embulk, "INSERT INTO TEST_MERGE1 VALUES('A004', 'DDD', 44.44);" + System.lineSeparator() + "EXIT;"); execute(embulk, "INSERT INTO TEST_MERGE1 VALUES('A006', 'FFF', 66.66);" + System.lineSeparator() + "EXIT;"); Path in1 = toPath("test_merge_rule.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_merge_rule.yml")), in1); assertThat(selectRecords(embulk, "TEST_MERGE1"), is(readResource("test_merge_rule_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testLowerTable() throws Exception { Path in1 = toPath("test1.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_lower_table.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testLowerColumn() throws Exception { Path in1 = toPath("test_lower_column.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_lower_column.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testLowerColumnOptions() throws Exception { Path in1 = toPath("test_lower_column_options.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_lower_column_options.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testStringToTimestamp() throws Exception { Path in1 = toPath("test_string_timestamp.csv"); TestingEmbulk.RunResult result1 = embulk.runOutput(baseConfig.merge(loadYamlResource(embulk, "test_string_timestamp.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } @Test public void testJdbcUrl() throws Exception { Path in1 = toPath("test1.csv"); String host = baseConfig.get(String.class, "host"); String port = baseConfig.get(String.class, "port", "1521"); String user = baseConfig.get(String.class, "user"); String password = baseConfig.get(String.class, "password"); String database = baseConfig.get(String.class, "database"); ConfigSource config = embulk.newConfig(); config.set("type", "oracle"); config.set("url", "jdbc:oracle:thin:@" + host + ":" + port + ":" + database); config.set("user", user); config.set("password", password); TestingEmbulk.RunResult result1 = embulk.runOutput(config.merge(loadYamlResource(embulk, "test_insert.yml")), in1); assertThat(selectRecords(embulk, "TEST1"), is(readResource("test_insert_expected.csv"))); //assertThat(result1.getConfigDiff(), is((ConfigDiff) loadYamlResource(embulk, "test_expected.diff"))); } private Path toPath(String fileName) throws URISyntaxException { URL url = Resources.getResource(BASIC_RESOURCE_PATH + fileName); return FileSystems.getDefault().getPath(new File(url.toURI()).getAbsolutePath()); } private Path getEmptyDir() throws URISyntaxException { URL url = Resources.getResource(BASIC_RESOURCE_PATH + "test1.csv"); File dir = new File(new File(url.toURI()).getParentFile(), "empty.csv"); dir.mkdir(); Path path = FileSystems.getDefault().getPath(dir.getAbsolutePath()); // TestingEmbulk will throw exception when it can't open input file. // DummyFileSystemProvider will open InputStream no matter whether file exists or not. return new DummyPath(path, "id:string"); } }