Skip to content

Commit

Permalink
Added unit tests for TableMetadataReviseEngine (#28535)
Browse files Browse the repository at this point in the history
  • Loading branch information
gagan-gv committed Oct 17, 2023
1 parent 29defea commit 3b1e1b7
Showing 1 changed file with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

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;

class TableMetadataReviseEngineTest<T extends ShardingSphereRule> {

@Mock
private T mockRule;

@Mock
private DatabaseType mockDatabaseType;

@Mock
private DataSource mockDataSource;

@Mock
private MetaDataReviseEntry<T> mockMetaDataReviseEntry;

@InjectMocks
private TableMetaDataReviseEngine<T> mockTableMetaDataReviseEngine;

@BeforeEach
void setup() {
MockitoAnnotations.openMocks(this);
}

@Test
void assertGetRevisedTableName() {
TableNameReviser<T> tableNameReviser = mock(TableNameReviser.class);

TableMetaData originalMetaData = new TableMetaData(
"originalTableName",
new ArrayList<>(),
null,
null);

doReturn(Optional.of(tableNameReviser)).when(mockMetaDataReviseEntry).getTableNameReviser();
when(tableNameReviser.revise(anyString(), eq(mockRule))).thenReturn("revisedTableName");

TableMetaData revisedMetaData = mockTableMetaDataReviseEngine.revise(originalMetaData);

assertThat(revisedMetaData.getName(), is("revisedTableName"));
}

@Test
void assertGetOriginalTableName() {
Collection<ColumnMetaData> columns = new ArrayList<>();
columns.add(new ColumnMetaData(
"column1",
2,
true,
true,
true,
false,
false,
false));

Collection<IndexMetaData> indexes = new ArrayList<>();
indexes.add(new IndexMetaData("index1"));

TableMetaData tableMetaData = new TableMetaData(
"originalTableName",
columns,
indexes,
null);

when(mockMetaDataReviseEntry.getTableNameReviser()).thenReturn(Optional.empty());

TableMetaData revisedMetaData = mockTableMetaDataReviseEngine.revise(tableMetaData);

assertThat(revisedMetaData.getName(), is("originalTableName"));
}
}

0 comments on commit 3b1e1b7

Please sign in to comment.