From 6bcba90240f9ac5fbb8fb40a6848441c4df02efa Mon Sep 17 00:00:00 2001 From: gagan-gv Date: Tue, 3 Oct 2023 15:20:14 +0530 Subject: [PATCH] Added unit tests for TableMetadataReviseEngine (#28535) --- .../table/TableMetadataReviseEngineTest.java | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/table/TableMetadataReviseEngineTest.java diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/table/TableMetadataReviseEngineTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/table/TableMetadataReviseEngineTest.java new file mode 100644 index 00000000000000..1ca1dd8c0d1cf0 --- /dev/null +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/table/TableMetadataReviseEngineTest.java @@ -0,0 +1,135 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.infra.metadata.database.schema.reviser.table; + +import org.apache.shardingsphere.infra.database.core.metadata.data.model.ColumnMetaData; +import org.apache.shardingsphere.infra.database.core.metadata.data.model.IndexMetaData; +import org.apache.shardingsphere.infra.database.core.metadata.data.model.TableMetaData; +import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.metadata.database.schema.reviser.MetaDataReviseEntry; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; + +import org.junit.jupiter.api.BeforeEach; + +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import javax.sql.DataSource; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class TableMetadataReviseEngineTest { + + @Mock + private T mockRule; + + @Mock + private DatabaseType mockDatabaseType; + + @Mock + private DataSource mockDataSource; + + @Mock + private MetaDataReviseEntry mockMetaDataReviseEntry; + + @InjectMocks + private TableMetaDataReviseEngine mockTableMetaDataReviseEngine; + + @BeforeEach + void setup() { + MockitoAnnotations.openMocks(this); + } + + /** + * Testing revise method with TableNameReviser + */ + @Test + public void testReviseWithTableNameReviser() { + // Arrange + TableNameReviser tableNameReviser = mock(TableNameReviser.class); + + // Create a sample original TableMetaData + TableMetaData originalMetaData = new TableMetaData( + "originalTableName", + new ArrayList<>(), + null, + null); + + // Act + doReturn(Optional.of(tableNameReviser)).when(mockMetaDataReviseEntry).getTableNameReviser(); + when(tableNameReviser.revise(anyString(), eq(mockRule))).thenReturn("revisedTableName"); + + // Call the revise method + TableMetaData revisedMetaData = mockTableMetaDataReviseEngine.revise(originalMetaData); + + // Assert + // Verify that the revised table name is returned + assertEquals("revisedTableName", revisedMetaData.getName()); + } + + /** + * Testing revise method without TableNameReviser + */ + @Test + public void testReviseWithoutTableNameReviser() { + // Arrange + Collection columns = new ArrayList<>(); + columns.add(new ColumnMetaData( + "column1", + 2, + true, + true, + true, + false, + false, + false)); + + Collection indexes = new ArrayList<>(); + indexes.add(new IndexMetaData("index1")); + + // Create a sample TableMetaData + TableMetaData tableMetaData = new TableMetaData( + "originalTableName", + columns, + indexes, + null); + + // Act + // Mock TableNameReviser to return an empty Optional + when(mockMetaDataReviseEntry.getTableNameReviser()).thenReturn(Optional.empty()); + + // Call the revise method + TableMetaData revisedMetaData = mockTableMetaDataReviseEngine.revise(tableMetaData); + + // Assert + // Verify that the original table name is returned since there's no reviser + assertEquals("originalTableName", revisedMetaData.getName()); + } +}