Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test cases on RuleItemConfigurationChangedProcessor's impl #33485

Merged
merged 9 commits into from
Oct 31, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* 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.encrypt.rule.changed;

import org.apache.shardingsphere.encrypt.config.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent;
import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.HashMap;
import java.util.Properties;

import static org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class EncryptorChangedProcessorTest {

@SuppressWarnings("unchecked")
private final RuleItemConfigurationChangedProcessor<EncryptRuleConfiguration, AlgorithmConfiguration> processor = TypedSPILoader.getService(
RuleItemConfigurationChangedProcessor.class, "encrypt.encryptors");

@Test
void assertSwapRuleItemConfiguration() {
AlgorithmConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl", "", "", ""), createYAMLContent());
assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo", new Properties())));
}

private String createYAMLContent() {
YamlAlgorithmConfiguration yamlConfig = new YamlAlgorithmConfiguration();
yamlConfig.setType("foo_algo");
return YamlEngine.marshal(yamlConfig);
}

@Test
void assertFindRuleConfiguration() {
EncryptRuleConfiguration ruleConfig = mock(EncryptRuleConfiguration.class);
assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)), is(ruleConfig));
}

private ShardingSphereDatabase mockDatabase(final EncryptRuleConfiguration ruleConfig) {
EncryptRule rule = mock(EncryptRule.class);
when(rule.getConfiguration()).thenReturn(ruleConfig);
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule)));
return result;
}

@Test
void assertChangeRuleItemConfiguration() {
EncryptRuleConfiguration currentRuleConfig = new EncryptRuleConfiguration(Collections.emptyList(), new HashMap<>(Collections.singletonMap("foo_algo", mock(AlgorithmConfiguration.class))));
AlgorithmConfiguration toBeChangedItemConfig = new AlgorithmConfiguration("FIXTURE", new Properties());
processor.changeRuleItemConfiguration(
new AlterNamedRuleItemEvent("foo_db", "foo_algo", "", "", ""), currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getEncryptors().size(), is(1));
assertThat(currentRuleConfig.getEncryptors().get("foo_algo").getType(), is("FIXTURE"));
}

@Test
void assertDropRuleItemConfiguration() {
EncryptRuleConfiguration currentRuleConfig = new EncryptRuleConfiguration(Collections.emptyList(), new HashMap<>(Collections.singletonMap("foo_algo", mock(AlgorithmConfiguration.class))));
processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("", "foo_algo", ""), currentRuleConfig);
assertTrue(currentRuleConfig.getEncryptors().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ public AlgorithmConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent

@Override
public MaskRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) {
return database.getRuleMetaData().findSingleRule(MaskRule.class)
.map(optional -> getConfiguration(optional.getConfiguration())).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
}

private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration ruleConfig) {
return null == ruleConfig.getMaskAlgorithms() ? new MaskRuleConfiguration(ruleConfig.getTables(), new LinkedHashMap<>()) : ruleConfig;
return database.getRuleMetaData().findSingleRule(MaskRule.class).map(MaskRule::getConfiguration).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ public MaskTableRuleConfiguration swapRuleItemConfiguration(final AlterRuleItemE

@Override
public MaskRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) {
return database.getRuleMetaData().findSingleRule(MaskRule.class)
.map(optional -> getConfiguration(optional.getConfiguration())).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
}

private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration config) {
return null == config.getTables() ? new MaskRuleConfiguration(new LinkedList<>(), config.getMaskAlgorithms()) : config;
return database.getRuleMetaData().findSingleRule(MaskRule.class).map(MaskRule::getConfiguration).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
package org.apache.shardingsphere.mask.rule.changed;

import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mask.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.config.rule.MaskTableRuleConfiguration;
import org.apache.shardingsphere.mask.rule.MaskRule;
Expand All @@ -31,47 +34,45 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Properties;

import static org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
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.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class MaskAlgorithmChangedProcessorTest {

private final MaskAlgorithmChangedProcessor processor = (MaskAlgorithmChangedProcessor) TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class, "mask.mask_algorithms");
@SuppressWarnings("unchecked")
private final RuleItemConfigurationChangedProcessor<MaskRuleConfiguration, AlgorithmConfiguration> processor = TypedSPILoader.getService(
RuleItemConfigurationChangedProcessor.class, "mask.mask_algorithms");

@Test
void assertSwapRuleItemConfiguration() {
assertThat(processor.swapRuleItemConfiguration(mock(AlterNamedRuleItemEvent.class), "type: TEST").getType(), is("TEST"));
AlgorithmConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo", "", "", ""), createYAMLContent());
assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo", new Properties())));
}

@Test
void assertFindRuleConfigurationWhenRuleDoesNotExist() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.empty());
assertTrue(processor.findRuleConfiguration(database).getMaskAlgorithms().isEmpty());
private String createYAMLContent() {
YamlAlgorithmConfiguration yamlConfig = new YamlAlgorithmConfiguration();
yamlConfig.setType("foo_algo");
return YamlEngine.marshal(yamlConfig);
}

@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(processor.findRuleConfiguration(database).getMaskAlgorithms().isEmpty());
void assertFindRuleConfiguration() {
MaskRuleConfiguration ruleConfig = mock(MaskRuleConfiguration.class);
assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)), is(ruleConfig));
}

@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(processor.findRuleConfiguration(database).getMaskAlgorithms().isEmpty());
private ShardingSphereDatabase mockDatabase(final MaskRuleConfiguration ruleConfig) {
MaskRule rule = mock(MaskRule.class);
when(rule.getConfiguration()).thenReturn(ruleConfig);
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule)));
return result;
}

@Test
Expand All @@ -80,15 +81,15 @@ void assertChangeRuleItemConfiguration() {
new LinkedList<>(Collections.singleton(new MaskTableRuleConfiguration("foo_tbl", Collections.emptyList()))), new HashMap<>());
AlgorithmConfiguration toBeChangedItemConfig = new AlgorithmConfiguration("FIXTURE", new Properties());
processor.changeRuleItemConfiguration(
new AlterNamedRuleItemEvent("foo_db", "foo_algo", "key", "0", ""), currentRuleConfig, toBeChangedItemConfig);
new AlterNamedRuleItemEvent("foo_db", "foo_algo", "", "", ""), currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getMaskAlgorithms().size(), is(1));
assertThat(currentRuleConfig.getMaskAlgorithms().get("foo_algo").getType(), is("FIXTURE"));
}

@Test
void assertDropRuleItemConfiguration() {
MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(Collections.emptyList(), new HashMap<>(Collections.singletonMap("type: TEST", mock(AlgorithmConfiguration.class))));
processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("foo_db", "type: TEST", ""), currentRuleConfig);
MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(Collections.emptyList(), new HashMap<>(Collections.singletonMap("foo_algo", mock(AlgorithmConfiguration.class))));
processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("", "foo_algo", ""), currentRuleConfig);
assertTrue(currentRuleConfig.getMaskAlgorithms().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,77 +18,82 @@
package org.apache.shardingsphere.mask.rule.changed;

import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mask.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.config.rule.MaskColumnRuleConfiguration;
import org.apache.shardingsphere.mask.config.rule.MaskTableRuleConfiguration;
import org.apache.shardingsphere.mask.rule.MaskRule;
import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskColumnRuleConfiguration;
import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
import org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterNamedRuleItemEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.alter.AlterRuleItemEvent;
import org.apache.shardingsphere.mode.event.dispatch.rule.drop.DropNamedRuleItemEvent;
import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Optional;

import static org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
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.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class MaskTableChangedProcessorTest {

private final MaskTableChangedProcessor processor = (MaskTableChangedProcessor) TypedSPILoader.getService(RuleItemConfigurationChangedProcessor.class, "mask.tables");
@SuppressWarnings("unchecked")
private final RuleItemConfigurationChangedProcessor<MaskRuleConfiguration, MaskTableRuleConfiguration> processor = TypedSPILoader.getService(
RuleItemConfigurationChangedProcessor.class, "mask.tables");

@Test
void assertSwapRuleItemConfiguration() {
assertThat(processor.swapRuleItemConfiguration(mock(AlterRuleItemEvent.class), "name: test_table").getName(), is("test_table"));
MaskTableRuleConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl", "", "", ""), createYAMLContent());
assertThat(actual, deepEqual(new MaskTableRuleConfiguration("foo_tbl", Collections.singletonList(new MaskColumnRuleConfiguration("foo_col", "foo_algo")))));
}

@Test
void assertFindRuleConfigurationWhenRuleDoesNotExist() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.empty());
assertTrue(processor.findRuleConfiguration(database).getMaskAlgorithms().isEmpty());
private String createYAMLContent() {
YamlMaskTableRuleConfiguration yamlConfig = new YamlMaskTableRuleConfiguration();
yamlConfig.setName("foo_tbl");
YamlMaskColumnRuleConfiguration yamlColumnRuleConfig = new YamlMaskColumnRuleConfiguration();
yamlColumnRuleConfig.setLogicColumn("foo_col");
yamlColumnRuleConfig.setMaskAlgorithm("foo_algo");
yamlConfig.setColumns(Collections.singletonMap("foo_col", yamlColumnRuleConfig));
return YamlEngine.marshal(yamlConfig);
}

@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(processor.findRuleConfiguration(database).getTables().isEmpty());
void assertFindRuleConfiguration() {
MaskRuleConfiguration ruleConfig = mock(MaskRuleConfiguration.class);
assertThat(processor.findRuleConfiguration(mockDatabase(ruleConfig)), is(ruleConfig));
}

@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(processor.findRuleConfiguration(database).getTables().isEmpty());
private ShardingSphereDatabase mockDatabase(final MaskRuleConfiguration ruleConfig) {
MaskRule rule = mock(MaskRule.class);
when(rule.getConfiguration()).thenReturn(ruleConfig);
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule)));
return result;
}

@Test
void assertChangeRuleItemConfiguration() {
MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(
new LinkedList<>(Collections.singleton(new MaskTableRuleConfiguration("foo_tbl", Collections.emptyList()))), Collections.emptyMap());
MaskTableRuleConfiguration toBeChangedItemConfig = new MaskTableRuleConfiguration("foo_tbl", Collections.singleton(mock(MaskColumnRuleConfiguration.class)));
processor.changeRuleItemConfiguration(
new AlterNamedRuleItemEvent("foo_db", "foo_tbl", "key", "0", ""), currentRuleConfig, toBeChangedItemConfig);
processor.changeRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo_tbl", "", "", ""), currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getTables().size(), is(1));
assertThat(currentRuleConfig.getTables().iterator().next().getColumns().size(), is(1));
assertThat(new ArrayList<>(currentRuleConfig.getTables()).get(0).getColumns().size(), is(1));
}

@Test
void assertDropRuleItemConfiguration() {
MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(
new LinkedList<>(Collections.singleton(new MaskTableRuleConfiguration("foo_tbl", Collections.emptyList()))), Collections.emptyMap());
processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("foo_db", "foo_tbl", ""), currentRuleConfig);
processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("", "foo_tbl", ""), currentRuleConfig);
assertTrue(currentRuleConfig.getTables().isEmpty());
}
}
Loading