|
| 1 | +/* |
| 2 | + * |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | + * contributor license agreements. See the NOTICE file distributed with |
| 5 | + * this work for additional information regarding copyright ownership. |
| 6 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | + * (the "License"); you may not use this file except in compliance with |
| 8 | + * the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +package org.dinky.trans.ddl; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
| 23 | +import static org.mockito.Mockito.*; |
| 24 | + |
| 25 | +import org.dinky.cdc.SinkBuilder; |
| 26 | +import org.dinky.data.model.Table; |
| 27 | + |
| 28 | +import java.lang.reflect.Method; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +import org.junit.jupiter.api.BeforeEach; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +/** |
| 37 | + * Unit tests for the private static method |
| 38 | + * {@code CreateCDCSourceOperation#setSinkTable} introduced in this commit. |
| 39 | + * |
| 40 | + * <p>The method is exercised via reflection to avoid exposing it as package-visible. |
| 41 | + */ |
| 42 | +class CreateCDCSourceOperationTest { |
| 43 | + |
| 44 | + private Method setSinkTableMethod; |
| 45 | + private SinkBuilder sinkBuilder; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setUp() throws Exception { |
| 49 | + setSinkTableMethod = CreateCDCSourceOperation.class.getDeclaredMethod( |
| 50 | + "setSinkTable", Table.class, List.class, SinkBuilder.class, |
| 51 | + org.dinky.metadata.driver.Driver.class); |
| 52 | + setSinkTableMethod.setAccessible(true); |
| 53 | + |
| 54 | + sinkBuilder = mock(SinkBuilder.class); |
| 55 | + } |
| 56 | + |
| 57 | + private void invoke(Table table, List<Table> sinkTables, SinkBuilder sb, |
| 58 | + org.dinky.metadata.driver.Driver driver) throws Exception { |
| 59 | + setSinkTableMethod.invoke(null, table, sinkTables, sb, driver); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * When the sinkBuilder returns a table name that matches one of the sink tables |
| 64 | + * (plain name, no schema prefix), the source table's sinkTable is set. |
| 65 | + */ |
| 66 | + @Test |
| 67 | + void testMatchBySinkTableName_plain() throws Exception { |
| 68 | + Table sourceTable = new Table("orders", "public", null); |
| 69 | + Table sinkTable = new Table("orders", "public", null); |
| 70 | + |
| 71 | + when(sinkBuilder.getSinkTableName(sourceTable)).thenReturn("orders"); |
| 72 | + |
| 73 | + invoke(sourceTable, Collections.singletonList(sinkTable), sinkBuilder, null); |
| 74 | + |
| 75 | + assertNotNull(sourceTable.getSinkTable()); |
| 76 | + assertEquals("orders", sourceTable.getSinkTable().getName()); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * When the sink table carries a schema prefix (e.g. "public.orders"), the method |
| 81 | + * must split on '.' and match using only the table-name part. |
| 82 | + */ |
| 83 | + @Test |
| 84 | + void testMatchBySinkTableName_withSchemaPrefix() throws Exception { |
| 85 | + Table sourceTable = new Table("orders", "public", null); |
| 86 | + Table sinkTable = new Table("orders", "public", null); |
| 87 | + // getSchemaTableName() returns "public.orders" |
| 88 | + |
| 89 | + when(sinkBuilder.getSinkTableName(sourceTable)).thenReturn("orders"); |
| 90 | + |
| 91 | + invoke(sourceTable, Collections.singletonList(sinkTable), sinkBuilder, null); |
| 92 | + |
| 93 | + assertNotNull(sourceTable.getSinkTable()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * No matching sink table -> sinkTable stays null. |
| 98 | + */ |
| 99 | + @Test |
| 100 | + void testNoMatch_sinkTableRemainsNull() throws Exception { |
| 101 | + Table sourceTable = new Table("orders", "public", null); |
| 102 | + Table sinkTable = new Table("customers", "public", null); |
| 103 | + |
| 104 | + when(sinkBuilder.getSinkTableName(sourceTable)).thenReturn("orders"); |
| 105 | + |
| 106 | + invoke(sourceTable, Collections.singletonList(sinkTable), sinkBuilder, null); |
| 107 | + |
| 108 | + assertNull(sourceTable.getSinkTable()); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * When sinkRealDriver is provided, columns should be loaded from it. |
| 113 | + */ |
| 114 | + @Test |
| 115 | + void testMatchWithDriver_columnsAreSet() throws Exception { |
| 116 | + Table sourceTable = new Table("orders", "public", null); |
| 117 | + Table sinkTable = new Table("orders", "public", null); |
| 118 | + |
| 119 | + when(sinkBuilder.getSinkTableName(sourceTable)).thenReturn("orders"); |
| 120 | + |
| 121 | + org.dinky.metadata.driver.Driver driver = mock(org.dinky.metadata.driver.Driver.class); |
| 122 | + when(driver.listColumnsSortByPK("public", "orders")) |
| 123 | + .thenReturn(Collections.emptyList()); |
| 124 | + |
| 125 | + invoke(sourceTable, Collections.singletonList(sinkTable), sinkBuilder, driver); |
| 126 | + |
| 127 | + assertNotNull(sourceTable.getSinkTable()); |
| 128 | + verify(driver).listColumnsSortByPK("public", "orders"); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Empty sink table list -> sinkTable stays null, no exception. |
| 133 | + */ |
| 134 | + @Test |
| 135 | + void testEmptySinkTableList() throws Exception { |
| 136 | + Table sourceTable = new Table("orders", "public", null); |
| 137 | + |
| 138 | + when(sinkBuilder.getSinkTableName(sourceTable)).thenReturn("orders"); |
| 139 | + |
| 140 | + invoke(sourceTable, Collections.emptyList(), sinkBuilder, null); |
| 141 | + |
| 142 | + assertNull(sourceTable.getSinkTable()); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Multiple candidates; only the first match should be used (break after first match). |
| 147 | + */ |
| 148 | + @Test |
| 149 | + void testFirstMatchWins() throws Exception { |
| 150 | + Table sourceTable = new Table("orders", "public", null); |
| 151 | + Table sinkTable1 = new Table("orders", "schema1", null); |
| 152 | + Table sinkTable2 = new Table("orders", "schema2", null); |
| 153 | + |
| 154 | + when(sinkBuilder.getSinkTableName(sourceTable)).thenReturn("orders"); |
| 155 | + |
| 156 | + invoke(sourceTable, Arrays.asList(sinkTable1, sinkTable2), sinkBuilder, null); |
| 157 | + |
| 158 | + assertNotNull(sourceTable.getSinkTable()); |
| 159 | + assertEquals("schema1", sourceTable.getSinkTable().getSchema()); |
| 160 | + } |
| 161 | +} |
0 commit comments