Skip to content

Commit

Permalink
Add AlgorithmChangedProcessor (#33486)
Browse files Browse the repository at this point in the history
* Add AlgorithmChangedProcessor

* Add AlgorithmChangedProcessor
  • Loading branch information
terrymanu authored Oct 31, 2024
1 parent f1dbeca commit c18b9c8
Show file tree
Hide file tree
Showing 18 changed files with 458 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,29 @@
import org.apache.shardingsphere.encrypt.metadata.nodepath.EncryptRuleNodePathProvider;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
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.event.dispatch.rule.drop.DropRuleItemEvent;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
import org.apache.shardingsphere.mode.processor.AlgorithmChangedProcessor;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

/**
* Encryptor changed processor.
*/
public final class EncryptorChangedProcessor implements RuleItemConfigurationChangedProcessor<EncryptRuleConfiguration, AlgorithmConfiguration> {
public final class EncryptorChangedProcessor extends AlgorithmChangedProcessor<EncryptRuleConfiguration> {

@Override
public AlgorithmConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent event, final String yamlContent) {
return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class));
}

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

private EncryptRuleConfiguration getEncryptRuleConfiguration(final EncryptRuleConfiguration config) {
return null == config.getTables() ? new EncryptRuleConfiguration(new LinkedList<>(), config.getEncryptors()) : config;
public EncryptorChangedProcessor() {
super(EncryptRule.class);
}

@Override
public void changeRuleItemConfiguration(final AlterRuleItemEvent event, final EncryptRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) {
currentRuleConfig.getEncryptors().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig);
protected EncryptRuleConfiguration createEmptyRuleConfiguration() {
return new EncryptRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>());
}

@Override
public void dropRuleItemConfiguration(final DropRuleItemEvent event, final EncryptRuleConfiguration currentRuleConfig) {
currentRuleConfig.getEncryptors().remove(((DropNamedRuleItemEvent) event).getItemName());
protected Map<String, AlgorithmConfiguration> getAlgorithmConfigurations(final EncryptRuleConfiguration currentRuleConfig) {
return currentRuleConfig.getEncryptors();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@
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.LinkedHashMap;
import java.util.LinkedList;
import java.util.Properties;

import static org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
Expand All @@ -48,45 +47,36 @@ class EncryptorChangedProcessorTest {
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())));
void assertFindRuleConfigurationWhenAbsent() {
assertThat(processor.findRuleConfiguration(mockDatabase()), deepEqual(new EncryptRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())));
}

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);
private ShardingSphereDatabase mockDatabase() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule)));
when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.emptyList()));
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"));
AlterNamedRuleItemEvent event = new AlterNamedRuleItemEvent("", "bar_algo", "", "", "");
EncryptRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
AlgorithmConfiguration toBeChangedItemConfig = new AlgorithmConfiguration("BAR_FIXTURE", new Properties());
processor.changeRuleItemConfiguration(event, currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getEncryptors().size(), is(2));
assertThat(currentRuleConfig.getEncryptors().get("foo_algo").getType(), is("FOO_FIXTURE"));
assertThat(currentRuleConfig.getEncryptors().get("bar_algo").getType(), is("BAR_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);
DropNamedRuleItemEvent event = new DropNamedRuleItemEvent("", "foo_algo", "");
EncryptRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
processor.dropRuleItemConfiguration(event, currentRuleConfig);
assertTrue(currentRuleConfig.getEncryptors().isEmpty());
}

private EncryptRuleConfiguration createCurrentRuleConfiguration() {
return new EncryptRuleConfiguration(Collections.emptyList(), new HashMap<>(Collections.singletonMap("foo_algo", new AlgorithmConfiguration("FOO_FIXTURE", new Properties()))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,32 @@
package org.apache.shardingsphere.mask.rule.changed;

import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
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.event.dispatch.rule.drop.DropRuleItemEvent;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfiguration;
import org.apache.shardingsphere.infra.algorithm.core.yaml.YamlAlgorithmConfigurationSwapper;
import org.apache.shardingsphere.mask.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.metadata.nodepath.MaskRuleNodePathProvider;
import org.apache.shardingsphere.mask.rule.MaskRule;
import org.apache.shardingsphere.mode.spi.RuleItemConfigurationChangedProcessor;
import org.apache.shardingsphere.mode.processor.AlgorithmChangedProcessor;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

/**
* Mask algorithm changed processor.
*/
public final class MaskAlgorithmChangedProcessor implements RuleItemConfigurationChangedProcessor<MaskRuleConfiguration, AlgorithmConfiguration> {
public final class MaskAlgorithmChangedProcessor extends AlgorithmChangedProcessor<MaskRuleConfiguration> {

@Override
public AlgorithmConfiguration swapRuleItemConfiguration(final AlterRuleItemEvent event, final String yamlContent) {
return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class));
}

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

@Override
public void changeRuleItemConfiguration(final AlterRuleItemEvent event, final MaskRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) {
currentRuleConfig.getMaskAlgorithms().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig);
protected MaskRuleConfiguration createEmptyRuleConfiguration() {
return new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>());
}

@Override
public void dropRuleItemConfiguration(final DropRuleItemEvent event, final MaskRuleConfiguration currentRuleConfig) {
currentRuleConfig.getMaskAlgorithms().remove(((DropNamedRuleItemEvent) event).getItemName());
protected Map<String, AlgorithmConfiguration> getAlgorithmConfigurations(final MaskRuleConfiguration currentRuleConfig) {
return currentRuleConfig.getMaskAlgorithms();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@
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;
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.LinkedHashMap;
import java.util.LinkedList;
import java.util.Properties;

Expand All @@ -50,46 +47,36 @@ class MaskAlgorithmChangedProcessorTest {
RuleItemConfigurationChangedProcessor.class, "mask.mask_algorithms");

@Test
void assertSwapRuleItemConfiguration() {
AlgorithmConfiguration actual = processor.swapRuleItemConfiguration(new AlterNamedRuleItemEvent("", "foo", "", "", ""), createYAMLContent());
assertThat(actual, deepEqual(new AlgorithmConfiguration("foo_algo", new Properties())));
void assertFindRuleConfigurationWhenAbsent() {
assertThat(processor.findRuleConfiguration(mockDatabase()), deepEqual(new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())));
}

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

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

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

@Test
void assertChangeRuleItemConfiguration() {
MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(
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", "", "", ""), currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getMaskAlgorithms().size(), is(1));
assertThat(currentRuleConfig.getMaskAlgorithms().get("foo_algo").getType(), is("FIXTURE"));
AlterNamedRuleItemEvent event = new AlterNamedRuleItemEvent("", "bar_algo", "", "", "");
MaskRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
AlgorithmConfiguration toBeChangedItemConfig = new AlgorithmConfiguration("BAR_FIXTURE", new Properties());
processor.changeRuleItemConfiguration(event, currentRuleConfig, toBeChangedItemConfig);
assertThat(currentRuleConfig.getMaskAlgorithms().size(), is(2));
assertThat(currentRuleConfig.getMaskAlgorithms().get("foo_algo").getType(), is("FOO_FIXTURE"));
assertThat(currentRuleConfig.getMaskAlgorithms().get("bar_algo").getType(), is("BAR_FIXTURE"));
}

@Test
void assertDropRuleItemConfiguration() {
MaskRuleConfiguration currentRuleConfig = new MaskRuleConfiguration(Collections.emptyList(), new HashMap<>(Collections.singletonMap("foo_algo", mock(AlgorithmConfiguration.class))));
processor.dropRuleItemConfiguration(new DropNamedRuleItemEvent("", "foo_algo", ""), currentRuleConfig);
DropNamedRuleItemEvent event = new DropNamedRuleItemEvent("", "foo_algo", "");
MaskRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration();
processor.dropRuleItemConfiguration(event, currentRuleConfig);
assertTrue(currentRuleConfig.getMaskAlgorithms().isEmpty());
}

private MaskRuleConfiguration createCurrentRuleConfiguration() {
return new MaskRuleConfiguration(Collections.emptyList(), new HashMap<>(Collections.singletonMap("foo_algo", new AlgorithmConfiguration("FOO_FIXTURE", new Properties()))));
}
}
Loading

0 comments on commit c18b9c8

Please sign in to comment.