|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with 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 | +package org.apache.xtable.hms.table; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.mockito.Mockito.mockStatic; |
| 23 | +import static org.mockito.Mockito.times; |
| 24 | +import static org.mockito.Mockito.verify; |
| 25 | +import static org.mockito.Mockito.when; |
| 26 | + |
| 27 | +import java.time.Instant; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import lombok.SneakyThrows; |
| 33 | + |
| 34 | +import org.apache.hadoop.hive.metastore.api.FieldSchema; |
| 35 | +import org.apache.hadoop.hive.metastore.api.SerDeInfo; |
| 36 | +import org.apache.hadoop.hive.metastore.api.StorageDescriptor; |
| 37 | +import org.apache.hadoop.hive.metastore.api.Table; |
| 38 | +import org.apache.hadoop.security.UserGroupInformation; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 41 | +import org.mockito.MockedStatic; |
| 42 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 43 | + |
| 44 | +import org.apache.xtable.hms.HMSCatalogSyncClientTestBase; |
| 45 | +import org.apache.xtable.model.storage.TableFormat; |
| 46 | + |
| 47 | +@ExtendWith(MockitoExtension.class) |
| 48 | +public class TestDeltaHMSCatalogTableBuilder extends HMSCatalogSyncClientTestBase { |
| 49 | + |
| 50 | + private DeltaHMSCatalogTableBuilder mockDeltaHmsCatalogSyncRequestProvider; |
| 51 | + |
| 52 | + private DeltaHMSCatalogTableBuilder createDeltaHMSCatalogTableBuilder() { |
| 53 | + return new DeltaHMSCatalogTableBuilder(mockHmsSchemaExtractor); |
| 54 | + } |
| 55 | + |
| 56 | + void setupCommonMocks() { |
| 57 | + mockDeltaHmsCatalogSyncRequestProvider = createDeltaHMSCatalogTableBuilder(); |
| 58 | + } |
| 59 | + |
| 60 | + @SneakyThrows |
| 61 | + @Test |
| 62 | + void testGetCreateTableRequest() { |
| 63 | + mockDeltaHmsCatalogSyncRequestProvider = createDeltaHMSCatalogTableBuilder(); |
| 64 | + when(mockHmsSchemaExtractor.toColumns( |
| 65 | + TableFormat.DELTA, TEST_DELTA_INTERNAL_TABLE.getReadSchema())) |
| 66 | + .thenReturn(Collections.emptyList()); |
| 67 | + |
| 68 | + Instant createdTime = Instant.now(); |
| 69 | + try (MockedStatic<Instant> mockZonedDateTime = mockStatic(Instant.class)) { |
| 70 | + mockZonedDateTime.when(Instant::now).thenReturn(createdTime); |
| 71 | + Table expected = new Table(); |
| 72 | + expected.setDbName(TEST_HMS_DATABASE); |
| 73 | + expected.setTableName(TEST_HMS_TABLE); |
| 74 | + expected.setOwner(UserGroupInformation.getCurrentUser().getShortUserName()); |
| 75 | + expected.setCreateTime((int) createdTime.getEpochSecond()); |
| 76 | + expected.setSd(getTestStorageDescriptor()); |
| 77 | + expected.setTableType("EXTERNAL_TABLE"); |
| 78 | + expected.setParameters(getTestParameters()); |
| 79 | + |
| 80 | + assertEquals( |
| 81 | + expected, |
| 82 | + mockDeltaHmsCatalogSyncRequestProvider.getCreateTableRequest( |
| 83 | + TEST_DELTA_INTERNAL_TABLE, TEST_CATALOG_TABLE_IDENTIFIER)); |
| 84 | + verify(mockHmsSchemaExtractor, times(1)) |
| 85 | + .toColumns(TableFormat.DELTA, TEST_DELTA_INTERNAL_TABLE.getReadSchema()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @SneakyThrows |
| 90 | + @Test |
| 91 | + void testGetUpdateTableRequest() { |
| 92 | + setupCommonMocks(); |
| 93 | + |
| 94 | + Table hmsTable = |
| 95 | + newTable( |
| 96 | + TEST_HMS_DATABASE, TEST_HMS_TABLE, getTestParameters(), getTestStorageDescriptor()); |
| 97 | + FieldSchema newColumn = new FieldSchema("new_column", "test", null); |
| 98 | + when(mockHmsSchemaExtractor.toColumns( |
| 99 | + TableFormat.DELTA, TEST_DELTA_INTERNAL_TABLE.getReadSchema())) |
| 100 | + .thenReturn(Collections.singletonList(newColumn)); |
| 101 | + |
| 102 | + Table output = |
| 103 | + mockDeltaHmsCatalogSyncRequestProvider.getUpdateTableRequest( |
| 104 | + TEST_DELTA_INTERNAL_TABLE, hmsTable, TEST_CATALOG_TABLE_IDENTIFIER); |
| 105 | + Table expected = new Table(hmsTable); |
| 106 | + expected.getSd().setCols(Collections.singletonList(newColumn)); |
| 107 | + |
| 108 | + assertEquals(expected, output); |
| 109 | + verify(mockHmsSchemaExtractor, times(1)) |
| 110 | + .toColumns(TableFormat.DELTA, TEST_DELTA_INTERNAL_TABLE.getReadSchema()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void testGetStorageDescriptor() { |
| 115 | + mockDeltaHmsCatalogSyncRequestProvider = createDeltaHMSCatalogTableBuilder(); |
| 116 | + when(mockHmsSchemaExtractor.toColumns( |
| 117 | + TableFormat.DELTA, TEST_DELTA_INTERNAL_TABLE.getReadSchema())) |
| 118 | + .thenReturn(Collections.emptyList()); |
| 119 | + StorageDescriptor expected = getTestStorageDescriptor(); |
| 120 | + assertEquals( |
| 121 | + expected, |
| 122 | + mockDeltaHmsCatalogSyncRequestProvider.getStorageDescriptor(TEST_DELTA_INTERNAL_TABLE)); |
| 123 | + verify(mockHmsSchemaExtractor, times(1)) |
| 124 | + .toColumns(TableFormat.DELTA, TEST_DELTA_INTERNAL_TABLE.getReadSchema()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void testGetTableParameters() { |
| 129 | + mockDeltaHmsCatalogSyncRequestProvider = createDeltaHMSCatalogTableBuilder(); |
| 130 | + Map<String, String> expected = getTestParameters(); |
| 131 | + assertEquals(expected, mockDeltaHmsCatalogSyncRequestProvider.getTableParameters()); |
| 132 | + } |
| 133 | + |
| 134 | + private StorageDescriptor getTestStorageDescriptor() { |
| 135 | + Map<String, String> serDeParams = new HashMap<>(); |
| 136 | + serDeParams.put("serialization.format", "1"); |
| 137 | + serDeParams.put("path", TEST_BASE_PATH); |
| 138 | + |
| 139 | + StorageDescriptor storageDescriptor = new StorageDescriptor(); |
| 140 | + storageDescriptor.setCols(Collections.emptyList()); |
| 141 | + storageDescriptor.setLocation(TEST_BASE_PATH); |
| 142 | + SerDeInfo serDeInfo = new SerDeInfo(); |
| 143 | + serDeInfo.setParameters(serDeParams); |
| 144 | + storageDescriptor.setSerdeInfo(serDeInfo); |
| 145 | + return storageDescriptor; |
| 146 | + } |
| 147 | + |
| 148 | + private Map<String, String> getTestParameters() { |
| 149 | + Map<String, String> parameters = new HashMap<>(); |
| 150 | + parameters.put("EXTERNAL", "TRUE"); |
| 151 | + parameters.put("table_type", TableFormat.DELTA); |
| 152 | + parameters.put("storage_handler", "io.delta.hive.DeltaStorageHandler"); |
| 153 | + return parameters; |
| 154 | + } |
| 155 | +} |
0 commit comments