Skip to content

Commit

Permalink
Revise some test cases (#30677)
Browse files Browse the repository at this point in the history
* Revise MaskRuleTest

* Revise MaskAlgorithmChangedProcessorTest

* Revise MaskTableChangedProcessorTest

* Revise ShardingSphereSchemaDataTest

* Revise SubqueryExpressionSegmentBinderTest

* Revise ConstraintReviseEngineTest

* Revise SchemaMetaDataReviseEngineTest
  • Loading branch information
terrymanu authored Mar 27, 2024
1 parent 1095d83 commit d208a8d
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,42 @@

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;

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<MaskTable> result = maskRule.findMaskTable(tableName);
assertTrue(result.isPresent());
MaskTable maskTable = result.get();
Optional<MaskAlgorithm> 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<MaskTable> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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<ConstraintMetaData> result = reviser.revise("table_name_1", originalMetaData, shardingRule);
assertTrue(result.isPresent());
assertEquals("test", result.get().getName());
assertEquals("referenced_table_name", result.get().getReferencedTableName());
Optional<ConstraintMetaData> 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<ConstraintMetaData> 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());
}
}
Loading

0 comments on commit d208a8d

Please sign in to comment.