|
| 1 | +/* |
| 2 | + * Copyright © 2026 Cask Data, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.cdap.plugin.oracle; |
| 18 | + |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +public class OracleETLDBOutputFormatTest { |
| 23 | + |
| 24 | + private final OracleETLDBOutputFormat outputFormat = new OracleETLDBOutputFormat(); |
| 25 | + |
| 26 | + @Test |
| 27 | + public void testConstructUpsertQueryBasic() { |
| 28 | + String[] fieldNames = {"id", "name", "age"}; |
| 29 | + String[] listKeys = {"id"}; |
| 30 | + String table = "my_table"; |
| 31 | + |
| 32 | + String result = outputFormat.constructUpsertQuery(table, fieldNames, listKeys); |
| 33 | + |
| 34 | + String expected = "MERGE INTO my_table target " + |
| 35 | + "USING (SELECT ? AS id, ? AS name, ? AS age FROM dual) source " + |
| 36 | + "ON (target.id = source.id) " + |
| 37 | + "WHEN MATCHED THEN UPDATE SET target.name = source.name, target.age = source.age " + |
| 38 | + "WHEN NOT MATCHED THEN INSERT (id, name, age) VALUES (source.id, source.name, source.age)"; |
| 39 | + |
| 40 | + Assert.assertEquals(expected, result); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testConstructUpsertQueryMultipleKeys() { |
| 45 | + String[] fieldNames = {"id", "code", "name", "value"}; |
| 46 | + String[] listKeys = {"id", "code"}; |
| 47 | + String table = "composite_key_table"; |
| 48 | + |
| 49 | + String result = outputFormat.constructUpsertQuery(table, fieldNames, listKeys); |
| 50 | + |
| 51 | + String expected = "MERGE INTO composite_key_table target " |
| 52 | + + "USING (SELECT ? AS id, ? AS code, ? AS name, ? AS value FROM dual) source " |
| 53 | + + "ON (target.id = source.id AND target.code = source.code) " |
| 54 | + + "WHEN MATCHED THEN UPDATE SET target.name = source.name, target.value = source.value " |
| 55 | + + "WHEN NOT MATCHED THEN INSERT (id, code, name, value) VALUES (source.id, source.code, source.name, source" |
| 56 | + + ".value)"; |
| 57 | + |
| 58 | + Assert.assertEquals(expected, result); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testConstructUpsertQuerySingleField() { |
| 63 | + String[] fieldNames = {"id", "name"}; |
| 64 | + String[] listKeys = {"id"}; |
| 65 | + String table = "single_field_update_table"; |
| 66 | + |
| 67 | + String result = outputFormat.constructUpsertQuery(table, fieldNames, listKeys); |
| 68 | + |
| 69 | + String expected = "MERGE INTO single_field_update_table target " + |
| 70 | + "USING (SELECT ? AS id, ? AS name FROM dual) source " + |
| 71 | + "ON (target.id = source.id) " + |
| 72 | + "WHEN MATCHED THEN UPDATE SET target.name = source.name " + |
| 73 | + "WHEN NOT MATCHED THEN INSERT (id, name) VALUES (source.id, source.name)"; |
| 74 | + |
| 75 | + Assert.assertEquals(expected, result); |
| 76 | + } |
| 77 | + |
| 78 | + @Test(expected = IllegalArgumentException.class) |
| 79 | + public void testConstructUpsertQueryNullListKeys() { |
| 80 | + String[] fieldNames = {"id", "name", "age"}; |
| 81 | + String table = "my_table"; |
| 82 | + |
| 83 | + outputFormat.constructUpsertQuery(table, fieldNames, null); |
| 84 | + } |
| 85 | + |
| 86 | + @Test(expected = IllegalArgumentException.class) |
| 87 | + public void testConstructUpsertQueryNullFieldNames() { |
| 88 | + String[] listKeys = {"id"}; |
| 89 | + String table = "my_table"; |
| 90 | + |
| 91 | + outputFormat.constructUpsertQuery(table, null, listKeys); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testConstructUpsertQueryAllFieldsAreKeys() { |
| 96 | + String[] fieldNames = {"id", "code"}; |
| 97 | + String[] listKeys = {"id", "code"}; |
| 98 | + String table = "all_keys_table"; |
| 99 | + |
| 100 | + String result = outputFormat.constructUpsertQuery(table, fieldNames, listKeys); |
| 101 | + |
| 102 | + // When all fields are keys, the UPDATE SET clause will be empty after "SET " |
| 103 | + // Note: There's an extra space before "WHEN NOT MATCHED" due to implementation |
| 104 | + String expected = "MERGE INTO all_keys_table target " + |
| 105 | + "USING (SELECT ? AS id, ? AS code FROM dual) source " + |
| 106 | + "ON (target.id = source.id AND target.code = source.code) " + |
| 107 | + "WHEN MATCHED THEN UPDATE SET " + |
| 108 | + "WHEN NOT MATCHED THEN INSERT (id, code) VALUES (source.id, source.code)"; |
| 109 | + |
| 110 | + Assert.assertEquals(expected, result); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testConstructUpsertQueryWithSpecialTableName() { |
| 115 | + String[] fieldNames = {"id", "name"}; |
| 116 | + String[] listKeys = {"id"}; |
| 117 | + String table = "SCHEMA.MY_TABLE"; |
| 118 | + |
| 119 | + String result = outputFormat.constructUpsertQuery(table, fieldNames, listKeys); |
| 120 | + |
| 121 | + String expected = "MERGE INTO SCHEMA.MY_TABLE target " + |
| 122 | + "USING (SELECT ? AS id, ? AS name FROM dual) source " + |
| 123 | + "ON (target.id = source.id) " + |
| 124 | + "WHEN MATCHED THEN UPDATE SET target.name = source.name " + |
| 125 | + "WHEN NOT MATCHED THEN INSERT (id, name) VALUES (source.id, source.name)"; |
| 126 | + |
| 127 | + Assert.assertEquals(expected, result); |
| 128 | + } |
| 129 | +} |
0 commit comments