From d208a8d482974b0411214a6ab4f6006a6d2531d0 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 28 Mar 2024 03:30:23 +0800 Subject: [PATCH] Revise some test cases (#30677) * Revise MaskRuleTest * Revise MaskAlgorithmChangedProcessorTest * Revise MaskTableChangedProcessorTest * Revise ShardingSphereSchemaDataTest * Revise SubqueryExpressionSegmentBinderTest * Revise ConstraintReviseEngineTest * Revise SchemaMetaDataReviseEngineTest --- .../MaskAlgorithmChangedProcessor.java | 4 +- .../mask/rule/MaskRuleTest.java | 40 ++++--------- .../MaskAlgorithmChangedProcessorTest.java | 59 +++++++++++-------- .../MaskTableChangedProcessorTest.java | 59 +++++++++++-------- .../ShardingConstraintReviserTest.java | 48 +++++++-------- .../SubqueryExpressionSegmentBinderTest.java | 4 +- .../ConstraintReviseEngineTest.java | 40 +++++-------- .../SchemaMetaDataReviseEngineTest.java | 5 +- .../ShardingSphereSchemaDataTest.java | 19 +++--- .../fixture/FixtureMetaDataRevise.java | 10 ++-- .../FixtureSchemaTableAggregationReviser.java | 2 +- 11 files changed, 132 insertions(+), 158 deletions(-) diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessor.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessor.java index ba597bb345aa2..3e8efb464b5ce 100644 --- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessor.java +++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessor.java @@ -50,8 +50,8 @@ public MaskRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase .map(optional -> getConfiguration(optional.getConfiguration())).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); } - private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration config) { - return null == config.getMaskAlgorithms() ? new MaskRuleConfiguration(config.getTables(), new LinkedHashMap<>()) : config; + private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration ruleConfig) { + return null == ruleConfig.getMaskAlgorithms() ? new MaskRuleConfiguration(ruleConfig.getTables(), new LinkedHashMap<>()) : ruleConfig; } @Override diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/MaskRuleTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/MaskRuleTest.java index 1fb44a1178292..6f85008532191 100644 --- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/MaskRuleTest.java +++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/MaskRuleTest.java @@ -17,20 +17,16 @@ package org.apache.shardingsphere.mask.rule; -import org.apache.commons.codec.digest.DigestUtils; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration; import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration; import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration; -import org.apache.shardingsphere.mask.spi.MaskAlgorithm; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.Collections; -import java.util.Optional; import java.util.Properties; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -38,37 +34,25 @@ class MaskRuleTest { private MaskRule maskRule; - private MaskRuleConfiguration mockConfiguration; - @BeforeEach - public void setUp() { - mockConfiguration = getRuleConfiguration(); - maskRule = new MaskRule(mockConfiguration); + void setUp() { + maskRule = new MaskRule(createMaskRuleConfiguration()); } - @Test - public void assertTableNameExists() { - String tableName = "t_mask"; - Optional result = maskRule.findMaskTable(tableName); - assertTrue(result.isPresent()); - MaskTable maskTable = result.get(); - Optional algorithm = maskTable.findAlgorithm("user_id"); - String value = (String) algorithm.get().mask("test"); - String matchValue = DigestUtils.md5Hex("test"); - assertEquals(value, matchValue); + private MaskRuleConfiguration createMaskRuleConfiguration() { + MaskColumnRuleConfiguration maskColumnRuleConfig = new MaskColumnRuleConfiguration("user_id", "t_mask_user_id_md5"); + MaskTableRuleConfiguration maskTableRuleConfig = new MaskTableRuleConfiguration("t_mask", Collections.singleton(maskColumnRuleConfig)); + AlgorithmConfiguration algorithmConfig = new AlgorithmConfiguration("md5", new Properties()); + return new MaskRuleConfiguration(Collections.singleton(maskTableRuleConfig), Collections.singletonMap("t_mask_user_id_md5", algorithmConfig)); } @Test - public void assertTableNameNoExists() { - String tableName = "non_existent_table"; - Optional result = maskRule.findMaskTable(tableName); - assertFalse(result.isPresent()); + void assertFindMaskTableWhenTableNameExists() { + assertTrue(maskRule.findMaskTable("t_mask").isPresent()); } - private MaskRuleConfiguration getRuleConfiguration() { - MaskColumnRuleConfiguration maskColumnRuleConfig = new MaskColumnRuleConfiguration("user_id", "t_mask_user_id_md5"); - MaskTableRuleConfiguration maskTableRuleConfig = new MaskTableRuleConfiguration("t_mask", Collections.singleton(maskColumnRuleConfig)); - AlgorithmConfiguration algorithmConfig = new AlgorithmConfiguration("md5", new Properties()); - return new MaskRuleConfiguration(Collections.singleton(maskTableRuleConfig), Collections.singletonMap("t_mask_user_id_md5", algorithmConfig)); + @Test + void assertFindMaskTableWhenTableNameDoesNotExist() { + assertFalse(maskRule.findMaskTable("non_existent_table").isPresent()); } } diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessorTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessorTest.java index a8ca86e00d25e..060fed4b1ad80 100644 --- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessorTest.java +++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskAlgorithmChangedProcessorTest.java @@ -18,58 +18,65 @@ package org.apache.shardingsphere.mask.rule.changed; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration; +import org.apache.shardingsphere.mask.rule.MaskRule; import org.junit.jupiter.api.Test; import java.util.Collections; +import java.util.HashMap; +import java.util.Optional; +import java.util.Properties; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; class MaskAlgorithmChangedProcessorTest { @Test void assertSwapRuleItemConfiguration() { - MaskAlgorithmChangedProcessor processor = new MaskAlgorithmChangedProcessor(); - AlterNamedRuleItemEvent event = mock(AlterNamedRuleItemEvent.class); - AlgorithmConfiguration algorithmConfiguration = processor.swapRuleItemConfiguration(event, "type: TEST"); - assertThat(algorithmConfiguration.getType(), is("TEST")); + assertThat(new MaskAlgorithmChangedProcessor().swapRuleItemConfiguration(mock(AlterNamedRuleItemEvent.class), "type: TEST").getType(), is("TEST")); } @Test - void assertFindRuleConfiguration() { - MaskAlgorithmChangedProcessor processor = new MaskAlgorithmChangedProcessor(); - ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap()); - RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(mock(ShardingSphereRule.class))); - ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()); - MaskRuleConfiguration maskRuleConfiguration = processor.findRuleConfiguration(database); - assertThat(maskRuleConfiguration.getMaskAlgorithms().size(), is(0)); - + void assertFindRuleConfigurationWhenRuleDoesNotExist() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.empty()); + assertTrue(new MaskAlgorithmChangedProcessor().findRuleConfiguration(database).getMaskAlgorithms().isEmpty()); + } + + @Test + void assertFindRuleConfigurationWhenMaskAlgorithmDoesNotExist() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(new MaskRule(new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap())))); + assertTrue(new MaskAlgorithmChangedProcessor().findRuleConfiguration(database).getMaskAlgorithms().isEmpty()); + } + + @Test + void assertFindRuleConfigurationWhenRuleExists() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + MaskRule maskRule = mock(MaskRule.class, RETURNS_DEEP_STUBS); + when(maskRule.getConfiguration().getMaskAlgorithms()).thenReturn(Collections.singletonMap("foo", new AlgorithmConfiguration("FOO", new Properties()))); + when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(maskRule)); + assertFalse(new MaskAlgorithmChangedProcessor().findRuleConfiguration(database).getMaskAlgorithms().isEmpty()); } @Test void assertDropRuleItemConfiguration() { - MaskAlgorithmChangedProcessor processor = mock(MaskAlgorithmChangedProcessor.class); - DropNamedRuleItemEvent event = mock(DropNamedRuleItemEvent.class); - MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(Collections.emptyList(), Collections.singletonMap("type: TEST", mock(AlgorithmConfiguration.class))); - processor.dropRuleItemConfiguration(event, currentRuleConfig); - verify(processor).dropRuleItemConfiguration(event, currentRuleConfig); + MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(Collections.emptyList(), new HashMap<>(Collections.singletonMap("type: TEST", mock(AlgorithmConfiguration.class)))); + new MaskAlgorithmChangedProcessor().dropRuleItemConfiguration(new DropNamedRuleItemEvent("foo_db", "type: TEST", ""), currentRuleConfig); + assertTrue(currentRuleConfig.getMaskAlgorithms().isEmpty()); } @Test void assertGetType() { - MaskAlgorithmChangedProcessor processor = new MaskAlgorithmChangedProcessor(); - String type = processor.getType(); - assertEquals("mask.mask_algorithms", type); + assertThat(new MaskAlgorithmChangedProcessor().getType(), is("mask.mask_algorithms")); } } diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessorTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessorTest.java index 9e51f8c1b163d..8480006d2bb18 100644 --- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessorTest.java +++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/changed/MaskTableChangedProcessorTest.java @@ -17,60 +17,67 @@ package org.apache.shardingsphere.mask.rule.changed; -import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration; import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration; import org.apache.shardingsphere.mask.metadata.nodepath.MaskRuleNodePathProvider; +import org.apache.shardingsphere.mask.rule.MaskRule; import org.junit.jupiter.api.Test; import java.util.Collections; +import java.util.LinkedList; +import java.util.Optional; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; class MaskTableChangedProcessorTest { @Test void assertSwapRuleItemConfiguration() { - AlterRuleItemEvent event = mock(AlterRuleItemEvent.class); - MaskTableChangedProcessor processor = new MaskTableChangedProcessor(); - MaskTableRuleConfiguration maskTableRuleConfiguration = processor.swapRuleItemConfiguration(event, "name: test_table"); - assertThat(maskTableRuleConfiguration.getName(), is("test_table")); + assertThat(new MaskTableChangedProcessor().swapRuleItemConfiguration(mock(AlterRuleItemEvent.class), "name: test_table").getName(), is("test_table")); } @Test - void assertFindRuleConfiguration() { - MaskTableChangedProcessor processor = new MaskTableChangedProcessor(); - ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.emptyMap()); - RuleMetaData ruleMetaData = new RuleMetaData(Collections.singleton(mock(ShardingSphereRule.class))); - ShardingSphereDatabase database = new ShardingSphereDatabase("foo_db", mock(DatabaseType.class), resourceMetaData, ruleMetaData, Collections.emptyMap()); - MaskRuleConfiguration maskRuleConfiguration = processor.findRuleConfiguration(database); - assertThat(maskRuleConfiguration.getTables().size(), is(0)); + void assertFindRuleConfigurationWhenRuleDoesNotExist() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.empty()); + assertTrue(new MaskTableChangedProcessor().findRuleConfiguration(database).getMaskAlgorithms().isEmpty()); + } + + @Test + void assertFindRuleConfigurationWhenTableDoesNotExist() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(new MaskRule(new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap())))); + assertTrue(new MaskTableChangedProcessor().findRuleConfiguration(database).getTables().isEmpty()); + } + + @Test + void assertFindRuleConfigurationWhenRuleExists() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + MaskRule maskRule = mock(MaskRule.class, RETURNS_DEEP_STUBS); + when(maskRule.getConfiguration().getTables()).thenReturn(Collections.singleton(new MaskTableRuleConfiguration("foo_tbl", Collections.emptyList()))); + when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.of(maskRule)); + assertFalse(new MaskTableChangedProcessor().findRuleConfiguration(database).getTables().isEmpty()); } @Test void assertDropRuleItemConfiguration() { - MaskTableChangedProcessor processor = mock(MaskTableChangedProcessor.class); - DropNamedRuleItemEvent event = mock(DropNamedRuleItemEvent.class); - MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(Collections.emptyList(), Collections.singletonMap("name: test_table", mock(AlgorithmConfiguration.class))); - processor.dropRuleItemConfiguration(event, currentRuleConfig); - verify(processor).dropRuleItemConfiguration(event, currentRuleConfig); + MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration( + new LinkedList<>(Collections.singleton(new MaskTableRuleConfiguration("foo_tbl", Collections.emptyList()))), Collections.emptyMap()); + new MaskTableChangedProcessor().dropRuleItemConfiguration(new DropNamedRuleItemEvent("foo_db", "foo_tbl", ""), currentRuleConfig); + assertTrue(currentRuleConfig.getTables().isEmpty()); } @Test void assertGetType() { - MaskTableChangedProcessor processor = new MaskTableChangedProcessor(); - String result = processor.getType(); - assertEquals(MaskRuleNodePathProvider.RULE_TYPE + "." + MaskRuleNodePathProvider.TABLES, result); + assertThat(new MaskTableChangedProcessor().getType(), is(MaskRuleNodePathProvider.RULE_TYPE + "." + MaskRuleNodePathProvider.TABLES)); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/reviser/constraint/ShardingConstraintReviserTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/reviser/constraint/ShardingConstraintReviserTest.java index 5574e82f3f9ab..f4b294420e4b8 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/reviser/constraint/ShardingConstraintReviserTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/reviser/constraint/ShardingConstraintReviserTest.java @@ -17,14 +17,6 @@ package org.apache.shardingsphere.sharding.metadata.reviser.constraint; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; - -import java.util.Arrays; -import java.util.Collections; -import java.util.Optional; -import org.apache.groovy.util.Maps; import org.apache.shardingsphere.infra.database.core.metadata.data.model.ConstraintMetaData; import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.instance.InstanceContext; @@ -36,6 +28,14 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.Arrays; +import java.util.Collections; +import java.util.Optional; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -46,36 +46,30 @@ class ShardingConstraintReviserTest { private ShardingRule shardingRule; @BeforeEach - public void setUp() { - shardingRule = mockShardingRule(); + void setUp() { + shardingRule = createShardingRule(); ShardingTable shardingTable = mock(ShardingTable.class); + when(shardingTable.getActualDataNodes()).thenReturn(Arrays.asList(new DataNode("schema_name", "table_name_0"), new DataNode("schema_name", "table_name_1"))); reviser = new ShardingConstraintReviser(shardingTable); - when(shardingTable.getActualDataNodes()).thenReturn(Arrays.asList(new DataNode[]{ - new DataNode("schema_name", "table_name_0"), - new DataNode("schema_name", "table_name_1") - })); } - private ShardingRule mockShardingRule() { + private ShardingRule createShardingRule() { ShardingRuleConfiguration ruleConfig = new ShardingRuleConfiguration(); - ShardingTableRuleConfiguration shardingTableRuleConfig = new ShardingTableRuleConfiguration("table_name", "ds.table_name"); - ruleConfig.setTables(Collections.singleton(shardingTableRuleConfig)); - return new ShardingRule(ruleConfig, Maps.of("ds", new MockedDataSource()), mock(InstanceContext.class)); + ruleConfig.setTables(Collections.singleton(new ShardingTableRuleConfiguration("table_name", "ds.table_name"))); + return new ShardingRule(ruleConfig, Collections.singletonMap("ds", new MockedDataSource()), mock(InstanceContext.class)); } @Test - public void testReviseWhenTableMatches() { + void assertReviseWhenTableMatches() { ConstraintMetaData originalMetaData = new ConstraintMetaData("test_table_name_1", "referenced_table_name"); - Optional result = reviser.revise("table_name_1", originalMetaData, shardingRule); - assertTrue(result.isPresent()); - assertEquals("test", result.get().getName()); - assertEquals("referenced_table_name", result.get().getReferencedTableName()); + Optional actual = reviser.revise("table_name_1", originalMetaData, shardingRule); + assertTrue(actual.isPresent()); + assertThat(actual.get().getName(), is("test")); + assertThat(actual.get().getReferencedTableName(), is("referenced_table_name")); } @Test - public void testReviseWhenTableDoesNotMatch() { - ConstraintMetaData originalMetaData = new ConstraintMetaData("test_table_name_2", "referenced_table_name"); - Optional result = reviser.revise("table_name_1", originalMetaData, shardingRule); - assertFalse(result.isPresent()); + void assertReviseWhenTableDoesNotMatch() { + assertFalse(reviser.revise("table_name_1", new ConstraintMetaData("test_table_name_2", "referenced_table_name"), shardingRule).isPresent()); } } diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/SubqueryExpressionSegmentBinderTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/SubqueryExpressionSegmentBinderTest.java index 5ae275f016e2f..43cd08f40d833 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/SubqueryExpressionSegmentBinderTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/SubqueryExpressionSegmentBinderTest.java @@ -37,15 +37,15 @@ class SubqueryExpressionSegmentBinderTest { @Test - public void testBind() { + void assertBind() { SelectStatement selectStatement = new SQLServerSelectStatement(); selectStatement.setProjections(new ProjectionsSegment(0, 0)); SubquerySegment subquery = new SubquerySegment(0, 10, selectStatement, "subquery"); SubqueryExpressionSegment segment = new SubqueryExpressionSegment(subquery); SQLStatementBinderContext statementBinderContext = new SQLStatementBinderContext(null, null, null, null); Map tableBinderContexts = new HashMap<>(); - SubqueryExpressionSegment expected = new SubqueryExpressionSegment(SubquerySegmentBinder.bind(segment.getSubquery(), statementBinderContext, tableBinderContexts)); SubqueryExpressionSegment actual = SubqueryExpressionSegmentBinder.bind(segment, statementBinderContext, tableBinderContexts); + SubqueryExpressionSegment expected = new SubqueryExpressionSegment(SubquerySegmentBinder.bind(segment.getSubquery(), statementBinderContext, tableBinderContexts)); assertThat(actual, isA(SubqueryExpressionSegment.class)); assertThat(actual.getStartIndex(), is(expected.getStartIndex())); assertThat(actual.getStopIndex(), is(expected.getStopIndex())); diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/constraint/ConstraintReviseEngineTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/constraint/ConstraintReviseEngineTest.java index a47bb0fa45764..c629d74fa575b 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/constraint/ConstraintReviseEngineTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/constraint/ConstraintReviseEngineTest.java @@ -26,8 +26,9 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Arrays; import java.util.Collection; -import java.util.LinkedHashSet; +import java.util.Collections; import java.util.Optional; import static org.hamcrest.CoreMatchers.is; @@ -37,49 +38,36 @@ import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) -class ConstraintReviseEngineTest { +class ConstraintReviseEngineTest { @Mock - private T mockRule; + private ShardingSphereRule mockRule; @Mock - private MetaDataReviseEntry mockMetaDataReviseEntry; + private MetaDataReviseEntry mockMetaDataReviseEntry; @InjectMocks - private ConstraintReviseEngine engine; + private ConstraintReviseEngine engine; @Test - void assertNotPresentReviserReturnsOriginalConstraints() { + void assertReviseWithReturnsOriginalConstraints() { String tableName = "tableName"; - doReturn(Optional.empty()).when(mockMetaDataReviseEntry).getConstraintReviser(mockRule, tableName); - Collection expectedConstraints = new LinkedHashSet<>(); - expectedConstraints.add(new ConstraintMetaData("constraint1", tableName)); - expectedConstraints.add(new ConstraintMetaData("constraint2", tableName)); - - Collection actualConstraints = engine.revise(tableName, expectedConstraints); - assertThat(actualConstraints, is(expectedConstraints)); + when(mockMetaDataReviseEntry.getConstraintReviser(mockRule, tableName)).thenReturn(Optional.empty()); + Collection expectedConstraints = Arrays.asList(new ConstraintMetaData("constraint1", tableName), new ConstraintMetaData("constraint2", tableName)); + assertThat(engine.revise(tableName, expectedConstraints), is(expectedConstraints)); } + @SuppressWarnings("unchecked") @Test - void assertReviserReturnsRevisedConstraints() { + void assertReviseWithReturnsRevisedConstraints() { String tableName = "tableName"; - ConstraintReviser reviser = mock(ConstraintReviser.class); + ConstraintReviser reviser = mock(ConstraintReviser.class); doReturn(Optional.of(reviser)).when(mockMetaDataReviseEntry).getConstraintReviser(mockRule, tableName); - - Collection originalConstraints = new LinkedHashSet<>(); ConstraintMetaData constraint1 = new ConstraintMetaData("constraint1", tableName); ConstraintMetaData constraint2 = new ConstraintMetaData("constraint2", tableName); - originalConstraints.add(constraint1); - originalConstraints.add(constraint2); - ConstraintMetaData constraint3 = new ConstraintMetaData("constraint3", tableName); when(reviser.revise(tableName, constraint1, mockRule)).thenReturn(Optional.of(constraint3)); when(reviser.revise(tableName, constraint2, mockRule)).thenReturn(Optional.empty()); - Collection expectedConstraints = new LinkedHashSet<>(); - expectedConstraints.add(constraint3); - - Collection actualConstraints = engine.revise(tableName, originalConstraints); - assertThat(actualConstraints, is(expectedConstraints)); + assertThat(engine.revise(tableName, Arrays.asList(constraint1, constraint2)), is(Collections.singleton(constraint3))); } - } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/schema/SchemaMetaDataReviseEngineTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/schema/SchemaMetaDataReviseEngineTest.java index e6096bc361b79..f1b0d8c0b1155 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/schema/SchemaMetaDataReviseEngineTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/reviser/schema/SchemaMetaDataReviseEngineTest.java @@ -53,9 +53,8 @@ void assertReviseWithoutMetaDataReviseEntry() { @Test void assertReviseWithMetaDataReviseEntry() { SchemaMetaData schemaMetaData = new SchemaMetaData("expected", Collections.singletonList(createTableMetaData())); - SchemaMetaData actual = new SchemaMetaDataReviseEngine(Collections.singleton(new FixtureGlobalRule()), - new ConfigurationProperties(new Properties()), mock(DatabaseType.class), mock(DataSource.class)) - .revise(schemaMetaData); + SchemaMetaData actual = new SchemaMetaDataReviseEngine( + Collections.singleton(new FixtureGlobalRule()), new ConfigurationProperties(new Properties()), mock(DatabaseType.class), mock(DataSource.class)).revise(schemaMetaData); assertThat(actual.getName(), is(schemaMetaData.getName())); assertThat(actual.getTables(), is(schemaMetaData.getTables())); } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/ShardingSphereSchemaDataTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/ShardingSphereSchemaDataTest.java index a9508c03bfd02..4eb8ea2610db7 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/ShardingSphereSchemaDataTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/statistics/ShardingSphereSchemaDataTest.java @@ -19,12 +19,13 @@ import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -public class ShardingSphereSchemaDataTest { +class ShardingSphereSchemaDataTest { private static final String TEST_TABLE_NAME = "TEST_TABLE_NAME"; @@ -36,23 +37,20 @@ public class ShardingSphereSchemaDataTest { void assertGetTable() { ShardingSphereSchemaData shardingSphereSchemaData = new ShardingSphereSchemaData(); shardingSphereSchemaData.putTable(TEST_TABLE_NAME, new ShardingSphereTableData(TEST_TABLE_NAME)); - ShardingSphereTableData tableData = shardingSphereSchemaData.getTable(TEST_TABLE_NAME); assertTrue(TEST_TABLE_NAME.equalsIgnoreCase(tableData.getName())); - ShardingSphereTableData nonExistentTableData = shardingSphereSchemaData.getTable(NON_EXISTENT_TABLE); - assertNull(nonExistentTableData); + assertNull(shardingSphereSchemaData.getTable(NON_EXISTENT_TABLE)); } @Test void assertPutTable() { ShardingSphereSchemaData shardingSphereSchemaData = new ShardingSphereSchemaData(); shardingSphereSchemaData.putTable(TEST_TABLE_NAME, new ShardingSphereTableData(TEST_TABLE_NAME)); - - assertEquals(1, shardingSphereSchemaData.getTableData().size()); + assertThat(shardingSphereSchemaData.getTableData().size(), is(1)); assertFalse(shardingSphereSchemaData.getTableData().containsKey(TEST_TABLE_2)); ShardingSphereTableData newTable = new ShardingSphereTableData(TEST_TABLE_2); shardingSphereSchemaData.putTable(TEST_TABLE_2, newTable); - assertEquals(2, shardingSphereSchemaData.getTableData().size()); + assertThat(shardingSphereSchemaData.getTableData().size(), is(2)); assertTrue(shardingSphereSchemaData.containsTable(TEST_TABLE_2)); } @@ -60,18 +58,15 @@ void assertPutTable() { void assertRemoveTable() { ShardingSphereSchemaData shardingSphereSchemaData = new ShardingSphereSchemaData(); shardingSphereSchemaData.putTable(TEST_TABLE_NAME, new ShardingSphereTableData(TEST_TABLE_NAME)); - assertTrue(shardingSphereSchemaData.containsTable(TEST_TABLE_NAME)); shardingSphereSchemaData.removeTable(TEST_TABLE_NAME); - assertEquals(0, shardingSphereSchemaData.getTableData().size()); - assertFalse(shardingSphereSchemaData.getTableData().containsKey(TEST_TABLE_NAME)); + assertTrue(shardingSphereSchemaData.getTableData().isEmpty()); } @Test void assertContainsTable() { ShardingSphereSchemaData shardingSphereSchemaData = new ShardingSphereSchemaData(); shardingSphereSchemaData.putTable(TEST_TABLE_NAME, new ShardingSphereTableData(TEST_TABLE_NAME)); - assertTrue(shardingSphereSchemaData.containsTable(TEST_TABLE_NAME)); assertFalse(shardingSphereSchemaData.containsTable(NON_EXISTENT_TABLE)); } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/FixtureMetaDataRevise.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/FixtureMetaDataRevise.java index 69b602f1a9abe..ed848ac957bc6 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/FixtureMetaDataRevise.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/FixtureMetaDataRevise.java @@ -25,6 +25,11 @@ public final class FixtureMetaDataRevise implements MetaDataReviseEntry { + @Override + public Optional> getSchemaTableAggregationReviser(final ConfigurationProperties props) { + return Optional.of(new FixtureSchemaTableAggregationReviser()); + } + @Override public int getOrder() { return 0; @@ -34,9 +39,4 @@ public int getOrder() { public Class getTypeClass() { return FixtureGlobalRule.class; } - - @Override - public Optional> getSchemaTableAggregationReviser(final ConfigurationProperties props) { - return Optional.of(new FixtureSchemaTableAggregationReviser()); - } } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/FixtureSchemaTableAggregationReviser.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/FixtureSchemaTableAggregationReviser.java index 7912925594e3b..66ad61c214f49 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/FixtureSchemaTableAggregationReviser.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/rule/builder/fixture/FixtureSchemaTableAggregationReviser.java @@ -23,7 +23,7 @@ import java.util.Collection; import java.util.LinkedList; -public class FixtureSchemaTableAggregationReviser implements SchemaTableAggregationReviser { +public final class FixtureSchemaTableAggregationReviser implements SchemaTableAggregationReviser { private final Collection tables = new LinkedList<>();