diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/feature/sharding/ShardingSQLRewriteEngineTest.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/feature/sharding/ShardingSQLRewriteEngineTest.java index 50e10f38266addc429412b24c90da338f14e17a0..c63c174dc686a78f118ed4489192eb34a20f0043 100644 --- a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/feature/sharding/ShardingSQLRewriteEngineTest.java +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/feature/sharding/ShardingSQLRewriteEngineTest.java @@ -657,13 +657,6 @@ public final class ShardingSQLRewriteEngineTest { return result; } - @Test - public void assertRewriteTableTokenWithoutBackQuoteForShow() { - SQLRewriteResult actual = getSQLRewriteResult("SHOW COLUMNS FROM table_x", Collections.emptyList(), true); - assertThat(actual.getSql(), is("SHOW COLUMNS FROM table_1")); - assertThat(actual.getParameters(), is(Collections.emptyList())); - } - @Test public void assertRewriteTableTokenWithoutBackQuoteFromSchemaForShow() { SQLRewriteResult actual = getSQLRewriteResult("SHOW COLUMNS FROM table_x FROM `sharding_db`", Collections.emptyList(), true); @@ -757,15 +750,15 @@ public final class ShardingSQLRewriteEngineTest { @Test public void assertRewriteSelectInWithShardingEncryptorWithCipher() { - SQLRewriteResult actual = getSQLRewriteResult("SELECT id FROM table_z WHERE id in (3,5)", Collections.emptyList(), true); - assertThat(actual.getSql(), is("SELECT cipher FROM table_z WHERE cipher IN ('encrypt_3', 'encrypt_5')")); + SQLRewriteResult actual = getSQLRewriteResult("SELECT password FROM t_cipher WHERE password in (3,5)", Collections.emptyList(), true); + assertThat(actual.getSql(), is("SELECT cipher_password FROM t_cipher WHERE cipher_password IN ('encrypt_3', 'encrypt_5')")); assertThat(actual.getParameters(), is(Collections.emptyList())); } @Test public void assertRewriteSelectInWithShardingEncryptorWithPlain() { - SQLRewriteResult actual = getSQLRewriteResult("SELECT id FROM table_z WHERE id in (3,5)", Collections.emptyList(), false); - assertThat(actual.getSql(), is("SELECT plain FROM table_z WHERE plain IN (3, 5)")); + SQLRewriteResult actual = getSQLRewriteResult("SELECT password FROM t_cipher WHERE password in (3,5)", Collections.emptyList(), false); + assertThat(actual.getSql(), is("SELECT plain_password FROM t_cipher WHERE plain_password IN (3, 5)")); assertThat(actual.getParameters(), is(Collections.emptyList())); } @@ -778,8 +771,8 @@ public final class ShardingSQLRewriteEngineTest { @Test public void assertRewriteUpdateWithShardingEncryptor() { - SQLRewriteResult actual = getSQLRewriteResult("UPDATE table_z SET id = 1 WHERE id = 2", Collections.emptyList(), true); - assertThat(actual.getSql(), is("UPDATE table_z SET cipher = 'encrypt_1', plain = 1 WHERE cipher = 'encrypt_2'")); + SQLRewriteResult actual = getSQLRewriteResult("UPDATE t_cipher SET password = 1 WHERE password = 2", Collections.emptyList(), true); + assertThat(actual.getSql(), is("UPDATE t_cipher SET cipher_password = 'encrypt_1', plain_password = 1 WHERE cipher_password = 'encrypt_2'")); assertThat(actual.getParameters(), is(Collections.emptyList())); } @@ -792,36 +785,36 @@ public final class ShardingSQLRewriteEngineTest { @Test public void assertRewriteSelectInWithAggregationDistinct() { - SQLRewriteResult actual = getSQLRewriteResult("SELECT COUNT(DISTINCT id) a, SUM(DISTINCT id) a FROM table_z WHERE id IN (3,5)", Collections.emptyList(), true); - assertThat(actual.getSql(), is("SELECT DISTINCT id a, id a FROM table_z WHERE cipher IN ('encrypt_3', 'encrypt_5')")); + SQLRewriteResult actual = getSQLRewriteResult("SELECT COUNT(DISTINCT id) a, SUM(DISTINCT id) a FROM table_x WHERE id IN (3,5)", Collections.emptyList(), true); + assertThat(actual.getSql(), is("SELECT DISTINCT id a, id a FROM table_1 WHERE id IN (3,5)")); assertThat(actual.getParameters(), is(Collections.emptyList())); } @Test public void assertRewriteSelectEqualWithShardingEncryptorWithCipher() { - SQLRewriteResult actual = getSQLRewriteResult("SELECT id FROM table_z WHERE id=? AND name=?", Arrays.asList(1, "x"), true); - assertThat(actual.getSql(), is("SELECT cipher FROM table_z WHERE cipher = ? AND name=?")); + SQLRewriteResult actual = getSQLRewriteResult("SELECT password FROM t_cipher WHERE password=? AND name=?", Arrays.asList(1, "x"), true); + assertThat(actual.getSql(), is("SELECT cipher_password FROM t_cipher WHERE cipher_password = ? AND name=?")); assertThat(actual.getParameters(), is(Arrays.asList("encrypt_1", "x"))); } @Test public void assertRewriteSelectEqualWithShardingEncryptorWithPlain() { - SQLRewriteResult actual = getSQLRewriteResult("SELECT id FROM table_z WHERE id=? AND name=?", Arrays.asList(1, "x"), false); - assertThat(actual.getSql(), is("SELECT plain FROM table_z WHERE plain = ? AND name=?")); + SQLRewriteResult actual = getSQLRewriteResult("SELECT password FROM t_cipher WHERE password=? AND name=?", Arrays.asList(1, "x"), false); + assertThat(actual.getSql(), is("SELECT plain_password FROM t_cipher WHERE plain_password = ? AND name=?")); assertThat(actual.getParameters(), is(Arrays.asList(1, "x"))); } @Test public void assertRewriteSelectInWithShardingEncryptorWithParameterWithCipher() { - SQLRewriteResult actual = getSQLRewriteResult("SELECT id FROM table_z WHERE id in (?, ?) or id = 3", Arrays.asList(1, 2), true); - assertThat(actual.getSql(), is("SELECT cipher FROM table_z WHERE cipher IN (?, ?) or cipher = 'encrypt_3'")); + SQLRewriteResult actual = getSQLRewriteResult("SELECT password FROM t_cipher WHERE password in (?, ?) or password = 3", Arrays.asList(1, 2), true); + assertThat(actual.getSql(), is("SELECT cipher_password FROM t_cipher WHERE cipher_password IN (?, ?) or cipher_password = 'encrypt_3'")); assertThat(actual.getParameters(), is(Arrays.asList("encrypt_1", "encrypt_2"))); } @Test public void assertRewriteSelectInWithShardingEncryptorWithParameterWithPlain() { - SQLRewriteResult actual = getSQLRewriteResult("SELECT id FROM table_z WHERE id in (?, ?) or id = 3", Arrays.asList(1, 2), false); - assertThat(actual.getSql(), is("SELECT plain FROM table_z WHERE plain IN (?, ?) or plain = 3")); + SQLRewriteResult actual = getSQLRewriteResult("SELECT password FROM t_cipher WHERE password in (?, ?) or password = 3", Arrays.asList(1, 2), false); + assertThat(actual.getSql(), is("SELECT plain_password FROM t_cipher WHERE plain_password IN (?, ?) or plain_password = 3")); assertThat(actual.getParameters(), is(Arrays.asList(1, 2))); } diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/TestShardingEncryptor.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/NormalEncryptorFixture.java similarity index 93% rename from sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/TestShardingEncryptor.java rename to sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/NormalEncryptorFixture.java index b05540f18f247415683b2e0130fdf77f057ab86e..a87057eeadbb6d73a2d03ca2e572d5463d15c42e 100644 --- a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/TestShardingEncryptor.java +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/NormalEncryptorFixture.java @@ -25,13 +25,13 @@ import java.util.Properties; @Getter @Setter -public final class TestShardingEncryptor implements ShardingEncryptor { +public final class NormalEncryptorFixture implements ShardingEncryptor { private Properties properties = new Properties(); @Override public String getType() { - return "TEST"; + return "NORMAL_ENCRYPT"; } @Override diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/TestQueryAssistedShardingEncryptor.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/QueryAssistedEncryptorFixture.java similarity index 92% rename from sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/TestQueryAssistedShardingEncryptor.java rename to sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/QueryAssistedEncryptorFixture.java index 643b8289fd276ca62cad1489c6f0dff86743e6c3..7da2d748e431608a15fb8bca02725afee3723659 100644 --- a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/TestQueryAssistedShardingEncryptor.java +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/QueryAssistedEncryptorFixture.java @@ -25,13 +25,13 @@ import java.util.Properties; @Getter @Setter -public final class TestQueryAssistedShardingEncryptor implements ShardingQueryAssistedEncryptor { +public final class QueryAssistedEncryptorFixture implements ShardingQueryAssistedEncryptor { private Properties properties = new Properties(); @Override public String getType() { - return "ASSISTED_QUERY_TEST"; + return "ASSISTED_QUERY_ENCRYPT"; } @Override diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/ShardingKeyGeneratorFixture.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/ShardingKeyGeneratorFixture.java new file mode 100644 index 0000000000000000000000000000000000000000..a1b0e5223a5214eb846967def4e571bd28b71846 --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/fixture/ShardingKeyGeneratorFixture.java @@ -0,0 +1,41 @@ +/* + * 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.core.rewrite.fixture; + +import lombok.Getter; +import lombok.Setter; +import org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator; + +import java.util.Properties; + +@Getter +@Setter +public final class ShardingKeyGeneratorFixture implements ShardingKeyGenerator { + + private Properties properties = new Properties(); + + @Override + public Comparable generateKey() { + return 1L; + } + + @Override + public String getType() { + return "TEST"; + } +} diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/engine/EncryptSQLRewriteEngineParameterizedTest.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/engine/EncryptSQLRewriteEngineParameterizedTest.java index e18003c7e330580e1fb1135a0a3d648de7dde18c..18f91046d4b4cdb76d7043c8fd80fc11d3925beb 100644 --- a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/engine/EncryptSQLRewriteEngineParameterizedTest.java +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/engine/EncryptSQLRewriteEngineParameterizedTest.java @@ -30,8 +30,8 @@ import org.apache.shardingsphere.core.rewrite.context.SQLRewriteContext; import org.apache.shardingsphere.core.rewrite.engine.SQLRewriteResult; import org.apache.shardingsphere.core.rewrite.engine.impl.DefaultSQLRewriteEngine; import org.apache.shardingsphere.core.rewrite.feature.encrypt.context.EncryptSQLRewriteContextDecorator; -import org.apache.shardingsphere.core.rewrite.parameterized.jaxb.entity.EncryptRewriteAssertionEntity; -import org.apache.shardingsphere.core.rewrite.parameterized.jaxb.entity.EncryptRewriteAssertionsRootEntity; +import org.apache.shardingsphere.core.rewrite.parameterized.jaxb.entity.RewriteAssertionEntity; +import org.apache.shardingsphere.core.rewrite.parameterized.jaxb.entity.RewriteAssertionsRootEntity; import org.apache.shardingsphere.core.rewrite.parameterized.jaxb.loader.EncryptRewriteAssertionsRootEntityLoader; import org.apache.shardingsphere.core.rule.EncryptRule; import org.apache.shardingsphere.core.yaml.config.encrypt.YamlRootEncryptRuleConfiguration; @@ -62,6 +62,8 @@ import static org.mockito.Mockito.mock; @RequiredArgsConstructor public final class EncryptSQLRewriteEngineParameterizedTest { + private static final String PATH = "encrypt"; + private final String fileName; private final String ruleFile; @@ -81,39 +83,39 @@ public final class EncryptSQLRewriteEngineParameterizedTest { @Parameters(name = "{2} -> {0}") public static Collection getTestParameters() { Collection result = new LinkedList<>(); - for (Entry entry : getAllEncryptRewriteAssertionsRootEntities().entrySet()) { + for (Entry entry : getAllRewriteAssertionsRootEntities().entrySet()) { result.addAll(getTestParameters(entry.getKey(), entry.getValue())); } return result; } - private static Collection getTestParameters(final String fileName, final EncryptRewriteAssertionsRootEntity rootAssertions) { + private static Collection getTestParameters(final String fileName, final RewriteAssertionsRootEntity rootAssertions) { Collection result = new LinkedList<>(); - for (EncryptRewriteAssertionEntity each : rootAssertions.getAssertions()) { + for (RewriteAssertionEntity each : rootAssertions.getAssertions()) { result.add(getTestParameter(fileName, rootAssertions, each)); } return result; } - private static Object[] getTestParameter(final String fileName, final EncryptRewriteAssertionsRootEntity rootAssertions, final EncryptRewriteAssertionEntity assertion) { + private static Object[] getTestParameter(final String fileName, final RewriteAssertionsRootEntity rootAssertions, final RewriteAssertionEntity assertion) { Object[] result = new Object[8]; result[0] = fileName; result[1] = rootAssertions.getYamlRule(); result[2] = assertion.getId(); result[3] = assertion.getInput().getSql(); result[4] = null == assertion.getInput().getParameters() ? Collections.emptyList() : Splitter.on(",").trimResults().splitToList(assertion.getInput().getParameters()); - result[5] = assertion.getOutput().getSql(); - result[6] = null == assertion.getOutput().getParameters() ? Collections.emptyList() : Splitter.on(",").trimResults().splitToList(assertion.getOutput().getParameters()); + result[5] = assertion.getOutputs().get(0).getSql(); + result[6] = null == assertion.getOutputs().get(0).getParameters() ? Collections.emptyList() : Splitter.on(",").trimResults().splitToList(assertion.getOutputs().get(0).getParameters()); result[7] = assertion.getDatabaseType(); return result; } - private static Map getAllEncryptRewriteAssertionsRootEntities() { - Map result = new LinkedHashMap<>(); - File file = new File(EncryptSQLRewriteEngineParameterizedTest.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "/encrypt"); + private static Map getAllRewriteAssertionsRootEntities() { + Map result = new LinkedHashMap<>(); + File file = new File(EncryptSQLRewriteEngineParameterizedTest.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "/" + PATH); for (File each : Objects.requireNonNull(file.listFiles())) { if (each.getName().endsWith(".xml")) { - result.put(each.getName(), new EncryptRewriteAssertionsRootEntityLoader().load("encrypt/" + each.getName())); + result.put(each.getName(), new EncryptRewriteAssertionsRootEntityLoader().load(PATH + "/" + each.getName())); } } return result; @@ -128,9 +130,9 @@ public final class EncryptSQLRewriteEngineParameterizedTest { private SQLRewriteResult getSQLRewriteResult() throws IOException { SQLRewriteContext sqlRewriteContext = getSQLRewriteContext(); - YamlRootEncryptRuleConfiguration encryptRuleConfiguration = createEncryptRuleConfiguration(); - EncryptRule encryptRule = new EncryptRule(new EncryptRuleConfigurationYamlSwapper().swap(encryptRuleConfiguration.getEncryptRule())); - boolean isQueryWithCipherColumn = (boolean) encryptRuleConfiguration.getProps().get("query.with.cipher.column"); + YamlRootEncryptRuleConfiguration ruleConfiguration = createRuleConfiguration(); + EncryptRule encryptRule = new EncryptRule(new EncryptRuleConfigurationYamlSwapper().swap(ruleConfiguration.getEncryptRule())); + boolean isQueryWithCipherColumn = (boolean) ruleConfiguration.getProps().get("query.with.cipher.column"); new EncryptSQLRewriteContextDecorator(encryptRule, isQueryWithCipherColumn).decorate(sqlRewriteContext); return new DefaultSQLRewriteEngine().rewrite(sqlRewriteContext); } @@ -141,7 +143,7 @@ public final class EncryptSQLRewriteEngineParameterizedTest { return new SQLRewriteContext(mock(TableMetas.class), sqlStatementContext, inputSQL, inputParameters); } - private YamlRootEncryptRuleConfiguration createEncryptRuleConfiguration() throws IOException { + private YamlRootEncryptRuleConfiguration createRuleConfiguration() throws IOException { URL url = EncryptSQLRewriteEngineParameterizedTest.class.getClassLoader().getResource(ruleFile); Preconditions.checkNotNull(url, "Cannot found rewrite rule yaml configuration."); return YamlEngine.unmarshal(new File(url.getFile()), YamlRootEncryptRuleConfiguration.class); diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/engine/ShardingSQLRewriteEngineParameterizedTest.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/engine/ShardingSQLRewriteEngineParameterizedTest.java new file mode 100644 index 0000000000000000000000000000000000000000..147a17448ee5da1744981cc7a3f08846b0cef2d2 --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/engine/ShardingSQLRewriteEngineParameterizedTest.java @@ -0,0 +1,209 @@ +/* + * 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.core.rewrite.parameterized.engine; + +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +import com.google.common.base.Splitter; +import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.core.database.DatabaseTypes; +import org.apache.shardingsphere.core.metadata.ShardingSphereMetaData; +import org.apache.shardingsphere.core.metadata.table.TableMetas; +import org.apache.shardingsphere.core.parse.SQLParseEngine; +import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; +import org.apache.shardingsphere.core.rewrite.context.SQLRewriteContext; +import org.apache.shardingsphere.core.rewrite.engine.SQLRewriteResult; +import org.apache.shardingsphere.core.rewrite.feature.encrypt.context.EncryptSQLRewriteContextDecorator; +import org.apache.shardingsphere.core.rewrite.feature.sharding.ShardingSQLRewriteEngineTest; +import org.apache.shardingsphere.core.rewrite.feature.sharding.context.ShardingSQLRewriteContextDecorator; +import org.apache.shardingsphere.core.rewrite.feature.sharding.engine.ShardingSQLRewriteEngine; +import org.apache.shardingsphere.core.rewrite.parameterized.jaxb.entity.RewriteAssertionEntity; +import org.apache.shardingsphere.core.rewrite.parameterized.jaxb.entity.RewriteAssertionsRootEntity; +import org.apache.shardingsphere.core.rewrite.parameterized.jaxb.entity.RewriteOutputEntity; +import org.apache.shardingsphere.core.rewrite.parameterized.jaxb.loader.EncryptRewriteAssertionsRootEntityLoader; +import org.apache.shardingsphere.core.route.SQLRouteResult; +import org.apache.shardingsphere.core.route.router.sharding.ShardingRouter; +import org.apache.shardingsphere.core.route.type.RoutingUnit; +import org.apache.shardingsphere.core.route.type.TableUnit; +import org.apache.shardingsphere.core.rule.BindingTableRule; +import org.apache.shardingsphere.core.rule.ShardingRule; +import org.apache.shardingsphere.core.yaml.config.sharding.YamlRootShardingConfiguration; +import org.apache.shardingsphere.core.yaml.engine.YamlEngine; +import org.apache.shardingsphere.core.yaml.swapper.impl.ShardingRuleConfigurationYamlSwapper; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + +@RunWith(Parameterized.class) +@RequiredArgsConstructor +public final class ShardingSQLRewriteEngineParameterizedTest { + + private static final String PATH = "sharding"; + + private final String fileName; + + private final String ruleFile; + + private final String name; + + private final String inputSQL; + + private final List inputParameters; + + private final List outputSQLs; + + private final List> outputGroupedParameters; + + private final String databaseType; + + @Parameters(name = "{2} -> {0}") + public static Collection getTestParameters() { + Collection result = new LinkedList<>(); + for (Entry entry : getAllRewriteAssertionsRootEntities().entrySet()) { + result.addAll(getTestParameters(entry.getKey(), entry.getValue())); + } + return result; + } + + private static Collection getTestParameters(final String fileName, final RewriteAssertionsRootEntity rootAssertions) { + Collection result = new LinkedList<>(); + for (RewriteAssertionEntity each : rootAssertions.getAssertions()) { + result.add(getTestParameter(fileName, rootAssertions, each)); + } + return result; + } + + private static Object[] getTestParameter(final String fileName, final RewriteAssertionsRootEntity rootAssertions, final RewriteAssertionEntity assertion) { + Object[] result = new Object[8]; + result[0] = fileName; + result[1] = rootAssertions.getYamlRule(); + result[2] = assertion.getId(); + result[3] = assertion.getInput().getSql(); + result[4] = null == assertion.getInput().getParameters() ? Collections.emptyList() : Splitter.on(",").trimResults().splitToList(assertion.getInput().getParameters()); + List outputs = assertion.getOutputs(); + List outputSQLs = new ArrayList<>(outputs.size()); + List outputGroupedParameters = new ArrayList<>(outputs.size()); + for (RewriteOutputEntity each : outputs) { + outputSQLs.add(each.getSql()); + outputGroupedParameters.add(null == each.getParameters() ? Collections.emptyList() : Splitter.on(",").trimResults().splitToList(each.getParameters())); + } + result[5] = outputSQLs; + result[6] = outputGroupedParameters; + result[7] = assertion.getDatabaseType(); + return result; + } + + private static Map getAllRewriteAssertionsRootEntities() { + Map result = new LinkedHashMap<>(); + File file = new File(ShardingSQLRewriteEngineParameterizedTest.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "/" + PATH); + for (File each : Objects.requireNonNull(file.listFiles())) { + if (each.getName().endsWith(".xml")) { + result.put(each.getName(), new EncryptRewriteAssertionsRootEntityLoader().load(PATH + "/" + each.getName())); + } + } + return result; + } + + @Test + public void assertRewrite() throws IOException { + Collection actual = getSQLRewriteResults(); + assertThat(actual.size(), is(outputSQLs.size())); + int count = 0; + for (SQLRewriteResult each : actual) { + assertThat(each.getSql(), is(outputSQLs.get(count))); + assertThat(each.getParameters(), is(outputGroupedParameters.get(count))); + count++; + } + } + + private Collection getSQLRewriteResults() throws IOException { + YamlRootShardingConfiguration ruleConfiguration = createRuleConfiguration(); + ShardingRule shardingRule = new ShardingRule(new ShardingRuleConfigurationYamlSwapper().swap(ruleConfiguration.getShardingRule()), ruleConfiguration.getDataSources().keySet()); + SQLParseEngine parseEngine = new SQLParseEngine(DatabaseTypes.getActualDatabaseType(null == databaseType ? "SQL92" : databaseType)); + ShardingRouter shardingRouter = new ShardingRouter(shardingRule, mock(ShardingSphereMetaData.class), parseEngine); + SQLStatement sqlStatement = shardingRouter.parse(inputSQL, false); + SQLRouteResult sqlRouteResult = shardingRouter.route(inputSQL, inputParameters, sqlStatement); + SQLRewriteContext sqlRewriteContext = new SQLRewriteContext(mock(TableMetas.class), sqlRouteResult.getSqlStatementContext(), inputSQL, inputParameters); + new ShardingSQLRewriteContextDecorator(shardingRule, sqlRouteResult).decorate(sqlRewriteContext); + boolean isQueryWithCipherColumn = (boolean) ruleConfiguration.getProps().get("query.with.cipher.column"); + new EncryptSQLRewriteContextDecorator(shardingRule.getEncryptRule(), isQueryWithCipherColumn).decorate(sqlRewriteContext); + Collection result = new LinkedList<>(); + for (RoutingUnit each : sqlRouteResult.getRoutingResult().getRoutingUnits()) { + result.add(new ShardingSQLRewriteEngine(sqlRouteResult.getShardingConditions(), + each, getLogicAndActualTables(shardingRule, each, sqlRouteResult.getSqlStatementContext().getTablesContext().getTableNames())).rewrite(sqlRewriteContext)); + } + return result; + } + + private YamlRootShardingConfiguration createRuleConfiguration() throws IOException { + URL url = ShardingSQLRewriteEngineTest.class.getClassLoader().getResource(ruleFile); + Preconditions.checkNotNull(url, "Cannot found rewrite rule yaml configuration."); + return YamlEngine.unmarshal(new File(url.getFile()), YamlRootShardingConfiguration.class); + } + + private Map getLogicAndActualTables(final ShardingRule shardingRule, final RoutingUnit routingUnit, final Collection parsedTableNames) { + Map result = new HashMap<>(); + for (TableUnit each : routingUnit.getTableUnits()) { + String logicTableName = each.getLogicTableName().toLowerCase(); + result.put(logicTableName, each.getActualTableName()); + result.putAll(getLogicAndActualTablesFromBindingTable(shardingRule, routingUnit.getMasterSlaveLogicDataSourceName(), each, parsedTableNames)); + } + return result; + } + + private Map getLogicAndActualTablesFromBindingTable(final ShardingRule shardingRule, + final String dataSourceName, final TableUnit tableUnit, final Collection parsedTableNames) { + Map result = new LinkedHashMap<>(); + Optional bindingTableRule = shardingRule.findBindingTableRule(tableUnit.getLogicTableName()); + if (bindingTableRule.isPresent()) { + result.putAll(getLogicAndActualTablesFromBindingTable(dataSourceName, tableUnit, parsedTableNames, bindingTableRule.get())); + } + return result; + } + + private Map getLogicAndActualTablesFromBindingTable( + final String dataSourceName, final TableUnit tableUnit, final Collection parsedTableNames, final BindingTableRule bindingTableRule) { + Map result = new LinkedHashMap<>(); + for (String each : parsedTableNames) { + String tableName = each.toLowerCase(); + if (!tableName.equals(tableUnit.getLogicTableName().toLowerCase()) && bindingTableRule.hasLogicTable(tableName)) { + result.put(tableName, bindingTableRule.getBindingActualTable(dataSourceName, tableName, tableUnit.getActualTableName())); + } + } + return result; + } +} diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/entity/RewriteAssertionEntity.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/entity/RewriteAssertionEntity.java new file mode 100644 index 0000000000000000000000000000000000000000..31cd571df3668aad3cd4e3f15961910d7c983350 --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/entity/RewriteAssertionEntity.java @@ -0,0 +1,48 @@ +/* + * 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.core.rewrite.parameterized.jaxb.entity; + +import lombok.Getter; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import java.util.List; + +/** + * Rewrite assertion entity for JAXB. + * + * @author zhangliang + */ +@XmlAccessorType(XmlAccessType.FIELD) +@Getter +public final class RewriteAssertionEntity { + + @XmlAttribute(required = true) + private String id; + + @XmlElement(required = true) + private RewriteInputEntity input; + + @XmlElement(required = true, name = "output") + private List outputs; + + @XmlAttribute(name = "db-type") + private String databaseType; +} diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/entity/RewriteAssertionsRootEntity.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/entity/RewriteAssertionsRootEntity.java new file mode 100644 index 0000000000000000000000000000000000000000..efc61fff54f732085afc8ea0124c94a1382fa9c3 --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/entity/RewriteAssertionsRootEntity.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.core.rewrite.parameterized.jaxb.entity; + +import lombok.Getter; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.Collection; +import java.util.LinkedList; + +/** + * Rewrite assertions root entity for JAXB. + * + * @author zhangliang + */ +@XmlRootElement(name = "rewrite-assertions") +@Getter +public final class RewriteAssertionsRootEntity { + + @XmlAttribute(name = "yaml-rule", required = true) + private String yamlRule; + + @XmlElement(name = "rewrite-assertion") + private Collection assertions = new LinkedList<>(); +} diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/entity/RewriteInputEntity.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/entity/RewriteInputEntity.java new file mode 100644 index 0000000000000000000000000000000000000000..be11297ce2b6b1728eac6b5cc2c683834c95ff25 --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/entity/RewriteInputEntity.java @@ -0,0 +1,40 @@ +/* + * 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.core.rewrite.parameterized.jaxb.entity; + +import lombok.Getter; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; + +/** + * Rewrite input entity for JAXB. + * + * @author zhangliang + */ +@XmlAccessorType(XmlAccessType.FIELD) +@Getter +public final class RewriteInputEntity { + + @XmlAttribute(required = true) + private String sql; + + @XmlAttribute + private String parameters; +} diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/entity/RewriteOutputEntity.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/entity/RewriteOutputEntity.java new file mode 100644 index 0000000000000000000000000000000000000000..8ebb6e26198386e64701d822badc4e1b1cf27687 --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/entity/RewriteOutputEntity.java @@ -0,0 +1,40 @@ +/* + * 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.core.rewrite.parameterized.jaxb.entity; + +import lombok.Getter; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; + +/** + * Rewrite input entity for JAXB. + * + * @author zhangliang + */ +@XmlAccessorType(XmlAccessType.FIELD) +@Getter +public final class RewriteOutputEntity { + + @XmlAttribute(required = true) + private String sql; + + @XmlAttribute + private String parameters; +} diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/loader/EncryptRewriteAssertionsRootEntityLoader.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/loader/EncryptRewriteAssertionsRootEntityLoader.java index 23f12532412cc13748a7e4b7653b0f4b7194a426..68796aaf5de46fdfe75132e16a71e285805159dd 100644 --- a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/loader/EncryptRewriteAssertionsRootEntityLoader.java +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/parameterized/jaxb/loader/EncryptRewriteAssertionsRootEntityLoader.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.core.rewrite.parameterized.jaxb.loader; import lombok.SneakyThrows; -import org.apache.shardingsphere.core.rewrite.parameterized.jaxb.entity.EncryptRewriteAssertionsRootEntity; +import org.apache.shardingsphere.core.rewrite.parameterized.jaxb.entity.RewriteAssertionsRootEntity; import javax.xml.bind.JAXBContext; import java.io.InputStream; @@ -37,9 +37,9 @@ public final class EncryptRewriteAssertionsRootEntityLoader { * @return rewrite assertions entity for JAXB */ @SneakyThrows - public EncryptRewriteAssertionsRootEntity load(final String file) { + public RewriteAssertionsRootEntity load(final String file) { InputStream inputStream = EncryptRewriteAssertionsRootEntityLoader.class.getClassLoader().getResourceAsStream(file); - return null == inputStream ? new EncryptRewriteAssertionsRootEntity() - : (EncryptRewriteAssertionsRootEntity) JAXBContext.newInstance(EncryptRewriteAssertionsRootEntity.class).createUnmarshaller().unmarshal(inputStream); + return null == inputStream ? new RewriteAssertionsRootEntity() + : (RewriteAssertionsRootEntity) JAXBContext.newInstance(RewriteAssertionsRootEntity.class).createUnmarshaller().unmarshal(inputStream); } } diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.encrypt.ShardingEncryptor b/sharding-core/sharding-core-rewrite/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.encrypt.ShardingEncryptor index 10de07e8bf7bff756e6c6a4e8d618aa5bfcac842..5f04be480ec91296a465e7e0ecd087cfe0cd136c 100644 --- a/sharding-core/sharding-core-rewrite/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.encrypt.ShardingEncryptor +++ b/sharding-core/sharding-core-rewrite/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.encrypt.ShardingEncryptor @@ -15,5 +15,5 @@ # limitations under the License. # -org.apache.shardingsphere.core.rewrite.fixture.TestShardingEncryptor -org.apache.shardingsphere.core.rewrite.fixture.TestQueryAssistedShardingEncryptor +org.apache.shardingsphere.core.rewrite.fixture.NormalEncryptorFixture +org.apache.shardingsphere.core.rewrite.fixture.QueryAssistedEncryptorFixture diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator b/sharding-core/sharding-core-rewrite/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator index 82690b6b52044edf26c17f5a12e04eaf917d8106..363ce8d448ded1e798f02ebbf75639f7d0b5d0f5 100644 --- a/sharding-core/sharding-core-rewrite/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator +++ b/sharding-core/sharding-core-rewrite/src/test/resources/META-INF/services/org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator @@ -15,4 +15,4 @@ # limitations under the License. # -org.apache.shardingsphere.core.rewrite.fixture.TestShardingKeyGenerator +org.apache.shardingsphere.core.rewrite.fixture.ShardingKeyGeneratorFixture diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/delete_for_query_with_cipher.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/delete_for_query_with_cipher.xml new file mode 100644 index 0000000000000000000000000000000000000000..fb75ceb23b955ba2ac8f0cc75ddb5bd0820155aa --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/delete_for_query_with_cipher.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/delete_for_query_with_plain.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/delete_for_query_with_plain.xml new file mode 100644 index 0000000000000000000000000000000000000000..b5e71b2ef524474380288571b1a3bd22f46b3a8c --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/delete_for_query_with_plain.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/delete_query_with_cipher.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/delete_query_with_cipher.xml deleted file mode 100644 index a0e5bbfeb674cbba643cb1760b7fe7c2b2af73bc..0000000000000000000000000000000000000000 --- a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/delete_query_with_cipher.xml +++ /dev/null @@ -1,62 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/delete_query_without_cipher.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/delete_query_without_cipher.xml deleted file mode 100644 index 6c51b1172eaede069c8fd049fb8fb70d86085929..0000000000000000000000000000000000000000 --- a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/delete_query_without_cipher.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/insert.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/insert.xml new file mode 100644 index 0000000000000000000000000000000000000000..b63998c3a829ed446649c7b5788edf8b8e8990d5 --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/insert.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/insert_set.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/insert_set.xml deleted file mode 100644 index 9e565d8e70bfccf17641bbd6b369368a7da2ef8e..0000000000000000000000000000000000000000 --- a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/insert_set.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/insert_values.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/insert_values.xml deleted file mode 100644 index 18ccc1744f449e58687ea771514a9e3f8d65f0de..0000000000000000000000000000000000000000 --- a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/insert_values.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_for_query_with_cipher.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_for_query_with_cipher.xml new file mode 100644 index 0000000000000000000000000000000000000000..0e3fd4a9a3b6ccfecfc689e09cb957acd2a7a745 --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_for_query_with_cipher.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_for_query_with_plain.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_for_query_with_plain.xml new file mode 100644 index 0000000000000000000000000000000000000000..f2d0c99a01e32ab4635b3b9758898c75c599c6f9 --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_for_query_with_plain.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_query_with_cipher.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_query_with_cipher.xml deleted file mode 100644 index 142461196f6d64786686bbb2335bc0a79ad44652..0000000000000000000000000000000000000000 --- a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_query_with_cipher.xml +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_query_without_cipher.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_query_without_cipher.xml deleted file mode 100644 index ebfcdb1d2087e5090ab57e21eec9579881536bbe..0000000000000000000000000000000000000000 --- a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/select_query_without_cipher.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/update_for_query_with_cipher.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/update_for_query_with_cipher.xml new file mode 100644 index 0000000000000000000000000000000000000000..d182bd0aeed80d9a65463d563b2bf42c41185de0 --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/update_for_query_with_cipher.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/update_for_query_with_plain.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/update_for_query_with_plain.xml new file mode 100644 index 0000000000000000000000000000000000000000..6de5122acac39a7cd3e8838b2c43bf5b920418dd --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/update_for_query_with_plain.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/update_query_with_cipher.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/update_query_with_cipher.xml deleted file mode 100644 index 565b0676a6ee331b33e5e669ce04969e12b689d7..0000000000000000000000000000000000000000 --- a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/update_query_with_cipher.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/update_query_without_cipher.xml b/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/update_query_without_cipher.xml deleted file mode 100644 index 821e627591cd1d235db08c319b1edc62ec4273b8..0000000000000000000000000000000000000000 --- a/sharding-core/sharding-core-rewrite/src/test/resources/encrypt/update_query_without_cipher.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/sharding/show.xml b/sharding-core/sharding-core-rewrite/src/test/resources/sharding/show.xml new file mode 100644 index 0000000000000000000000000000000000000000..8f8c12f606b78d87838df92d31e1735f4802879d --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/resources/sharding/show.xml @@ -0,0 +1,25 @@ + + + + + + + + + + diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/yaml/encrypt-rewrite-rule-query-with-cipher.yaml b/sharding-core/sharding-core-rewrite/src/test/resources/yaml/encrypt-rewrite-rule-query-with-cipher.yaml deleted file mode 100644 index 57e86c42cfc7bc774f5f98267cab59ba5ebd871b..0000000000000000000000000000000000000000 --- a/sharding-core/sharding-core-rewrite/src/test/resources/yaml/encrypt-rewrite-rule-query-with-cipher.yaml +++ /dev/null @@ -1,73 +0,0 @@ -# -# 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. -# - -dataSource: !!com.zaxxer.hikari.HikariDataSource - driverClassName: org.h2.Driver - jdbcUrl: jdbc:h2:mem:ds_encrypt;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL - username: sa - password: - -encryptRule: - tables: - t_cipher: - columns: - encrypt_1: - cipherColumn: cipher_1 - encryptor: test - encrypt_2: - cipherColumn: cipher_2 - encryptor: test - t_cipher_assisted_query: - columns: - encrypt_1: - cipherColumn: cipher_1 - assistedQueryColumn: assisted_query_1 - encryptor: assisted_query_test - encrypt_2: - cipherColumn: cipher_2 - assistedQueryColumn: assisted_query_2 - encryptor: assisted_query_test - t_cipher_plain: - columns: - encrypt_1: - cipherColumn: cipher_1 - plainColumn: plain_1 - encryptor: test - encrypt_2: - cipherColumn: cipher_2 - plainColumn: plain_2 - encryptor: test - t_cipher_assisted_query_plain: - columns: - encrypt_1: - cipherColumn: cipher_1 - assistedQueryColumn: assisted_query_1 - plainColumn: plain_1 - encryptor: assisted_query_test - encrypt_2: - cipherColumn: cipher_2 - assistedQueryColumn: assisted_query_2 - plainColumn: plain_2 - encryptor: assisted_query_test - encryptors: - test: - type: TEST - assisted_query_test: - type: ASSISTED_QUERY_TEST - -props: - query.with.cipher.column: true diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/yaml/encrypt-rewrite-rule-query-without-cipher.yaml b/sharding-core/sharding-core-rewrite/src/test/resources/yaml/encrypt-rewrite-rule-query-without-cipher.yaml deleted file mode 100644 index 155ff9d4e36fc83e3ab18c33c1f25464fa3ddb1f..0000000000000000000000000000000000000000 --- a/sharding-core/sharding-core-rewrite/src/test/resources/yaml/encrypt-rewrite-rule-query-without-cipher.yaml +++ /dev/null @@ -1,73 +0,0 @@ -# -# 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. -# - -dataSource: !!com.zaxxer.hikari.HikariDataSource - driverClassName: org.h2.Driver - jdbcUrl: jdbc:h2:mem:ds_encrypt;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL - username: sa - password: - -encryptRule: - tables: - t_cipher: - columns: - encrypt_1: - cipherColumn: cipher_1 - encryptor: test - encrypt_2: - cipherColumn: cipher_2 - encryptor: test - t_cipher_assisted_query: - columns: - encrypt_1: - cipherColumn: cipher_1 - assistedQueryColumn: assisted_query_1 - encryptor: assisted_query_test - encrypt_2: - cipherColumn: cipher_2 - assistedQueryColumn: assisted_query_2 - encryptor: assisted_query_test - t_cipher_plain: - columns: - encrypt_1: - cipherColumn: cipher_1 - plainColumn: plain_1 - encryptor: test - encrypt_2: - cipherColumn: cipher_2 - plainColumn: plain_2 - encryptor: test - t_cipher_assisted_query_plain: - columns: - encrypt_1: - cipherColumn: cipher_1 - assistedQueryColumn: assisted_query_1 - plainColumn: plain_1 - encryptor: assisted_query_test - encrypt_2: - cipherColumn: cipher_2 - assistedQueryColumn: assisted_query_2 - plainColumn: plain_2 - encryptor: assisted_query_test - encryptors: - test: - type: TEST - assisted_query_test: - type: ASSISTED_QUERY_TEST - -props: - query.with.cipher.column: false diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/yaml/encrypt/query-with-cipher.yaml b/sharding-core/sharding-core-rewrite/src/test/resources/yaml/encrypt/query-with-cipher.yaml new file mode 100644 index 0000000000000000000000000000000000000000..1ae295dd3c656658f32d63eca3741b7739c9adf3 --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/resources/yaml/encrypt/query-with-cipher.yaml @@ -0,0 +1,53 @@ +# +# 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. +# + +dataSource: !!com.zaxxer.hikari.HikariDataSource + driverClassName: org.h2.Driver + jdbcUrl: jdbc:h2:mem:ds_encrypt;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL + username: sa + password: + +encryptRule: + tables: + t_account: + columns: + password: + cipherColumn: cipher_password + assistedQueryColumn: assisted_query_password + encryptor: assisted_query + amount: + cipherColumn: cipher_amount + encryptor: normal + t_account_bak: + columns: + password: + cipherColumn: cipher_password + assistedQueryColumn: assisted_query_password + plainColumn: plain_password + encryptor: assisted_query + amount: + cipherColumn: cipher_amount + plainColumn: plain_amount + encryptor: normal + encryptors: + normal: + type: NORMAL_ENCRYPT + assisted_query: + type: ASSISTED_QUERY_ENCRYPT + +props: + query.with.cipher.column: true diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/yaml/encrypt/query-with-plain.yaml b/sharding-core/sharding-core-rewrite/src/test/resources/yaml/encrypt/query-with-plain.yaml new file mode 100644 index 0000000000000000000000000000000000000000..b8789ae267e007619faef92d13de1ceeca4a48ee --- /dev/null +++ b/sharding-core/sharding-core-rewrite/src/test/resources/yaml/encrypt/query-with-plain.yaml @@ -0,0 +1,53 @@ +# +# 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. +# + +dataSource: !!com.zaxxer.hikari.HikariDataSource + driverClassName: org.h2.Driver + jdbcUrl: jdbc:h2:mem:ds_encrypt;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL + username: sa + password: + +encryptRule: + tables: + t_account: + columns: + password: + cipherColumn: cipher_password + assistedQueryColumn: assisted_query_password + encryptor: assisted_query + amount: + cipherColumn: cipher_amount + encryptor: normal + t_account_bak: + columns: + password: + cipherColumn: cipher_password + assistedQueryColumn: assisted_query_password + plainColumn: plain_password + encryptor: assisted_query + amount: + cipherColumn: cipher_amount + plainColumn: plain_amount + encryptor: normal + encryptors: + normal: + type: NORMAL_ENCRYPT + assisted_query: + type: ASSISTED_QUERY_ENCRYPT + +props: + query.with.cipher.column: false diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/yaml/sharding-rewrite-rule.yaml b/sharding-core/sharding-core-rewrite/src/test/resources/yaml/sharding-rewrite-rule.yaml index 7d0e6c4c897f42d369202e93aae81aa3efe51646..6f8b827aecfd5b53559adebc517b4801c764ff55 100644 --- a/sharding-core/sharding-core-rewrite/src/test/resources/yaml/sharding-rewrite-rule.yaml +++ b/sharding-core/sharding-core-rewrite/src/test/resources/yaml/sharding-rewrite-rule.yaml @@ -38,7 +38,7 @@ shardingRule: worker.id: 123 table_y: actualDataNodes: db${0..1}.table_y - table_z: + t_cipher: actualDataNodes: db${0..1}.table_z table_k: actualDataNodes: db${0..1}.table_k @@ -51,19 +51,20 @@ shardingRule: worker.id: 123 bindingTables: - table_x, table_y + encryptRule: encryptors: - z_encryptor: - type: TEST + normal: + type: NORMAL_ENCRYPT k_encryptor: - type: ASSISTED_QUERY_TEST + type: ASSISTED_QUERY_ENCRYPT tables: - table_z: + t_cipher: columns: - id: - cipherColumn: cipher - plainColumn: plain - encryptor: z_encryptor + password: + cipherColumn: cipher_password + plainColumn: plain_password + encryptor: normal table_k: columns: id: @@ -81,3 +82,6 @@ shardingRule: cipherColumn: cipher assistedQueryColumn: query encryptor: k_encryptor + +props: + query.with.cipher.column: true