You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/07/05 03:41:38 UTC
[shardingsphere] branch master updated: repair audit config empty bug, add unit test for sharding audit (#18839)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new d0fa017b411 repair audit config empty bug, add unit test for sharding audit (#18839)
d0fa017b411 is described below
commit d0fa017b411e79512f3c2de8299fc25f99da380d
Author: natehuang <na...@tencent.com>
AuthorDate: Tue Jul 5 11:41:32 2022 +0800
repair audit config empty bug, add unit test for sharding audit (#18839)
---
.../factory/ShardingAuditAlgorithmFactoryTest.java | 42 +++++++++++++++
.../fixture/ShardingAuditAlgorithmFixture.java | 49 ++++++++++++++++++
...rdingsphere.sharding.spi.ShardingAuditAlgorithm | 18 +++++++
...rdingAuditStrategyConfigurationYamlSwapper.java | 2 +-
.../sharding/rule/ShardingRuleTest.java | 5 ++
...gAuditStrategyConfigurationYamlSwapperTest.java | 59 ++++++++++++++++++++++
6 files changed, 174 insertions(+), 1 deletion(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/test/java/org/apache/shardingsphere/sharding/factory/ShardingAuditAlgorithmFactoryTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/test/java/org/apache/shardingsphere/sharding/factory/ShardingAuditAlgorithmFactoryTest.java
new file mode 100644
index 00000000000..ba6fc87d6ae
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/test/java/org/apache/shardingsphere/sharding/factory/ShardingAuditAlgorithmFactoryTest.java
@@ -0,0 +1,42 @@
+/*
+ * 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.sharding.factory;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.sharding.fixture.ShardingAuditAlgorithmFixture;
+import org.junit.Test;
+
+import java.util.Properties;
+
+public final class ShardingAuditAlgorithmFactoryTest {
+
+ @Test
+ public void assertNewInstance() {
+ ShardingSphereAlgorithmConfiguration configuration = new ShardingSphereAlgorithmConfiguration("FIXTURE", new Properties());
+ assertThat(ShardingAuditAlgorithmFactory.newInstance(configuration), instanceOf(ShardingAuditAlgorithmFixture.class));
+ }
+
+ @Test
+ public void assertContains() {
+ assertTrue(ShardingAuditAlgorithmFactory.contains("FIXTURE"));
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/test/java/org/apache/shardingsphere/sharding/fixture/ShardingAuditAlgorithmFixture.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/test/java/org/apache/shardingsphere/sharding/fixture/ShardingAuditAlgorithmFixture.java
new file mode 100644
index 00000000000..a8cb37416d1
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/test/java/org/apache/shardingsphere/sharding/fixture/ShardingAuditAlgorithmFixture.java
@@ -0,0 +1,49 @@
+/*
+ * 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.sharding.fixture;
+
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.check.SQLCheckResult;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.user.Grantee;
+import org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm;
+
+import java.util.List;
+import java.util.Properties;
+
+public final class ShardingAuditAlgorithmFixture implements ShardingAuditAlgorithm {
+
+ @Override
+ public Properties getProps() {
+ return new Properties();
+ }
+
+ @Override
+ public void init(final Properties props) {
+ }
+
+ @Override
+ public SQLCheckResult check(final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters, final Grantee grantee, final ShardingSphereDatabase database) {
+ return new SQLCheckResult(true, "");
+ }
+
+ @Override
+ public String getType() {
+ return "FIXTURE";
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/test/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/test/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm
new file mode 100644
index 00000000000..35e9e19632a
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/test/resources/META-INF/services/org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.sharding.fixture.ShardingAuditAlgorithmFixture
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/strategy/ShardingAuditStrategyConfigurationYamlSwapper.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/strategy/ShardingAuditStrategyConfigurationYamlSwapper.java
index 2a526c9ec69..deacc8a5b13 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/strategy/ShardingAuditStrategyConfigurationYamlSwapper.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/yaml/swapper/strategy/ShardingAuditStrategyConfigurationYamlSwapper.java
@@ -29,7 +29,7 @@ public final class ShardingAuditStrategyConfigurationYamlSwapper implements Yaml
@Override
public YamlShardingAuditStrategyConfiguration swapToYamlConfiguration(final ShardingAuditStrategyConfiguration data) {
YamlShardingAuditStrategyConfiguration result = new YamlShardingAuditStrategyConfiguration();
- result.getAuditAlgorithmNames().addAll(data.getAuditAlgorithmNames());
+ result.setAuditAlgorithmNames(data.getAuditAlgorithmNames());
result.setAllowHintDisable(data.isAllowHintDisable());
return result;
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
index b2b01cb4aee..ba43d6cfd28 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
@@ -30,11 +30,13 @@ import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereSchema;
+import org.apache.shardingsphere.sharding.algorithm.audit.DMLShardingConditionsShardingAuditAlgorithm;
import org.apache.shardingsphere.sharding.algorithm.keygen.SnowflakeKeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.algorithm.keygen.UUIDKeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration;
@@ -88,6 +90,7 @@ public final class ShardingRuleTest {
assertThat(actual.getBindingTableRules().values().iterator().next().getTableRules().size(), is(2));
assertThat(actual.getBroadcastTables(), is(new TreeSet<>(Collections.singletonList("BROADCAST_TABLE"))));
assertThat(actual.getDefaultKeyGenerateAlgorithm(), instanceOf(UUIDKeyGenerateAlgorithm.class));
+ assertThat(actual.getAuditAlgorithms().get("audit_algorithm"), instanceOf(DMLShardingConditionsShardingAuditAlgorithm.class));
assertThat(actual.getDefaultShardingColumn(), is("table_id"));
}
@@ -469,9 +472,11 @@ public final class ShardingRuleTest {
shardingRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("table_id", "standard"));
shardingRuleConfig.setDefaultShardingColumn("table_id");
shardingRuleConfig.setDefaultKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("id", "default"));
+ shardingRuleConfig.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singletonList("audit_algorithm"), false));
shardingRuleConfig.getShardingAlgorithms().put("core_standard_fixture", new ShardingSphereAlgorithmConfiguration("CORE.STANDARD.FIXTURE", new Properties()));
shardingRuleConfig.getKeyGenerators().put("uuid", new ShardingSphereAlgorithmConfiguration("UUID", new Properties()));
shardingRuleConfig.getKeyGenerators().put("default", new ShardingSphereAlgorithmConfiguration("UUID", new Properties()));
+ shardingRuleConfig.getAuditAlgorithms().put("audit_algorithm", new ShardingSphereAlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties()));
return new ShardingRule(shardingRuleConfig, createDataSourceNames(), mock(InstanceContext.class));
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/strategy/ShardingAuditStrategyConfigurationYamlSwapperTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/strategy/ShardingAuditStrategyConfigurationYamlSwapperTest.java
new file mode 100644
index 00000000000..dd82481b651
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/swapper/strategy/ShardingAuditStrategyConfigurationYamlSwapperTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.sharding.swapper.strategy;
+
+import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration;
+import org.apache.shardingsphere.sharding.yaml.config.strategy.audit.YamlShardingAuditStrategyConfiguration;
+import org.apache.shardingsphere.sharding.yaml.swapper.strategy.ShardingAuditStrategyConfigurationYamlSwapper;
+import org.junit.Test;
+
+import java.util.Arrays;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public final class ShardingAuditStrategyConfigurationYamlSwapperTest {
+
+ @Test
+ public void assertSwapToYaml() {
+ YamlShardingAuditStrategyConfiguration actual = new ShardingAuditStrategyConfigurationYamlSwapper()
+ .swapToYamlConfiguration(new ShardingAuditStrategyConfiguration(Arrays.asList("audit_algorithm1", "audit_algorithm2"), false));
+ assertThat(actual.getAuditAlgorithmNames().size(), is(2));
+ assertTrue(actual.getAuditAlgorithmNames().containsAll(Arrays.asList("audit_algorithm1", "audit_algorithm2")));
+ assertFalse(actual.isAllowHintDisable());
+ }
+
+ @Test
+ public void assertSwapToObject() {
+ YamlShardingAuditStrategyConfiguration yamlShardingAuditStrategyConfig = new YamlShardingAuditStrategyConfiguration();
+ yamlShardingAuditStrategyConfig.setAuditAlgorithmNames(Arrays.asList("audit_algorithm1", "audit_algorithm2"));
+ yamlShardingAuditStrategyConfig.setAllowHintDisable(false);
+ ShardingAuditStrategyConfiguration actual = new ShardingAuditStrategyConfigurationYamlSwapper().swapToObject(yamlShardingAuditStrategyConfig);
+ assertThat(actual.getAuditAlgorithmNames().size(), is(2));
+ assertTrue(actual.getAuditAlgorithmNames().containsAll(Arrays.asList("audit_algorithm1", "audit_algorithm2")));
+ assertFalse(actual.isAllowHintDisable());
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void assertSwapToObjectWithNull() {
+ YamlShardingAuditStrategyConfiguration yamlShardingAuditStrategyConfig = new YamlShardingAuditStrategyConfiguration();
+ new ShardingAuditStrategyConfigurationYamlSwapper().swapToObject(yamlShardingAuditStrategyConfig);
+ }
+}