diff --git a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutor.java b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutor.java index 3ede2baf2aedb..4e47807f2aa46 100644 --- a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutor.java +++ b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.broadcast.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; import org.apache.shardingsphere.broadcast.distsql.statement.CreateBroadcastTableRuleStatement; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; @@ -31,10 +32,13 @@ /** * Create broadcast table rule executor. */ +@Setter public final class CreateBroadcastTableRuleExecutor implements DatabaseRuleCreateExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final CreateBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnits().isEmpty(), () -> new EmptyStorageUnitException(database.getName())); if (!sqlStatement.isIfNotExists()) { checkDuplicate(sqlStatement, currentRuleConfig); diff --git a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleExecutor.java b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleExecutor.java index 9c2c30e4c2ed5..8eca31ab3a18b 100644 --- a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleExecutor.java +++ b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.broadcast.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; import org.apache.shardingsphere.broadcast.distsql.statement.DropBroadcastTableRuleStatement; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; @@ -31,29 +32,31 @@ /** * Drop broadcast table rule executor. */ +@Setter public final class DropBroadcastTableRuleExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { if (!isExistRuleConfig(currentRuleConfig) && sqlStatement.isIfExists()) { return; } - String databaseName = database.getName(); - checkCurrentRuleConfiguration(databaseName, currentRuleConfig); - checkBroadcastTableRuleExist(databaseName, sqlStatement, currentRuleConfig); + checkCurrentRuleConfiguration(currentRuleConfig); + checkBroadcastTableRuleExist(sqlStatement, currentRuleConfig); } - private void checkCurrentRuleConfiguration(final String databaseName, final BroadcastRuleConfiguration currentRuleConfig) { - ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Broadcast", databaseName)); + private void checkCurrentRuleConfiguration(final BroadcastRuleConfiguration currentRuleConfig) { + ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Broadcast", database.getName())); } - private void checkBroadcastTableRuleExist(final String databaseName, final DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { + private void checkBroadcastTableRuleExist(final DropBroadcastTableRuleStatement sqlStatement, final BroadcastRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists()) { return; } Collection currentRules = currentRuleConfig.getTables(); Collection notExistRules = sqlStatement.getTables().stream().filter(each -> !containsIgnoreCase(currentRules, each)).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(notExistRules.isEmpty(), () -> new MissingRequiredRuleException("Broadcast", databaseName, notExistRules)); + ShardingSpherePreconditions.checkState(notExistRules.isEmpty(), () -> new MissingRequiredRuleException("Broadcast", database.getName(), notExistRules)); } private boolean containsIgnoreCase(final Collection currentRules, final String ruleName) { diff --git a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleStatementUpdaterTest.java b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleStatementUpdaterTest.java index b1a0006014187..ba01dffe80bdf 100644 --- a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleStatementUpdaterTest.java +++ b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/CreateBroadcastTableRuleStatementUpdaterTest.java @@ -43,26 +43,29 @@ class CreateBroadcastTableRuleStatementUpdaterTest { void assertCheckSQLStatementWithEmptyStorageUnit() { BroadcastRuleConfiguration currentConfig = mock(BroadcastRuleConfiguration.class); when(currentConfig.getTables()).thenReturn(Collections.singleton("t_address")); - CreateBroadcastTableRuleStatement statement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); + CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.emptyMap()); - assertThrows(EmptyStorageUnitException.class, () -> executor.checkBeforeUpdate(database, statement, currentConfig)); + executor.setDatabase(database); + assertThrows(EmptyStorageUnitException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); } @Test void assertCheckSQLStatementWithDuplicateBroadcastRule() { BroadcastRuleConfiguration currentConfig = mock(BroadcastRuleConfiguration.class); when(currentConfig.getTables()).thenReturn(Collections.singleton("t_address")); - CreateBroadcastTableRuleStatement statement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(mockShardingSphereDatabase(), statement, currentConfig)); + CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); + executor.setDatabase(mockShardingSphereDatabase()); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); } @Test void assertBuildToBeCreatedRuleConfiguration() { BroadcastRuleConfiguration currentConfig = new BroadcastRuleConfiguration(new LinkedList<>()); - CreateBroadcastTableRuleStatement statement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); - executor.checkBeforeUpdate(mockShardingSphereDatabase(), statement, currentConfig); - BroadcastRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentConfig, statement); + CreateBroadcastTableRuleStatement sqlStatement = new CreateBroadcastTableRuleStatement(false, Collections.singleton("t_address")); + executor.setDatabase(mockShardingSphereDatabase()); + executor.checkBeforeUpdate(sqlStatement, currentConfig); + BroadcastRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentConfig, sqlStatement); executor.updateCurrentRuleConfiguration(currentConfig, toBeCreatedRuleConfig); assertThat(currentConfig.getTables().size(), is(1)); assertThat(currentConfig.getTables().iterator().next(), is("t_address")); diff --git a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleStatementUpdaterTest.java b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleStatementUpdaterTest.java index 2e15f35f6e4d5..8b9480c021a8a 100644 --- a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleStatementUpdaterTest.java +++ b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/update/DropBroadcastTableRuleStatementUpdaterTest.java @@ -35,34 +35,33 @@ class DropBroadcastTableRuleStatementUpdaterTest { - private ShardingSphereDatabase database; - private final DropBroadcastTableRuleExecutor executor = new DropBroadcastTableRuleExecutor(); @BeforeEach void setUp() { - database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("sharding_db"); + executor.setDatabase(database); } @Test void assertCheckSQLStatementWithoutCurrentRule() { - DropBroadcastTableRuleStatement statement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address")); - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, statement, null)); + DropBroadcastTableRuleStatement sqlStatement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address")); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, null)); } @Test void assertCheckSQLStatementWithoutToBeDroppedRule() { - DropBroadcastTableRuleStatement statement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address")); - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, statement, new BroadcastRuleConfiguration(Collections.emptyList()))); + DropBroadcastTableRuleStatement sqlStatement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address")); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, new BroadcastRuleConfiguration(Collections.emptyList()))); } @Test void assertUpdateCurrentRuleConfiguration() { BroadcastRuleConfiguration config = new BroadcastRuleConfiguration(new LinkedList<>()); config.getTables().add("t_address"); - DropBroadcastTableRuleStatement statement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address")); - assertTrue(executor.updateCurrentRuleConfiguration(statement, config)); + DropBroadcastTableRuleStatement sqlStatement = new DropBroadcastTableRuleStatement(false, Collections.singleton("t_address")); + assertTrue(executor.updateCurrentRuleConfiguration(sqlStatement, config)); assertTrue(config.getTables().isEmpty()); } } diff --git a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleExecutor.java b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleExecutor.java index 65f76b9be6452..578a39721da56 100644 --- a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleExecutor.java +++ b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleExecutor.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.encrypt.distsql.handler.update; import com.google.common.base.Preconditions; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor; @@ -42,26 +43,28 @@ /** * Alter encrypt rule executor. */ +@Setter public final class AlterEncryptRuleExecutor implements DatabaseRuleAlterExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final AlterEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { - String databaseName = database.getName(); - checkCurrentRuleConfiguration(databaseName, currentRuleConfig); - checkToBeAlteredRules(databaseName, sqlStatement, currentRuleConfig); + public void checkBeforeUpdate(final AlterEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { + checkCurrentRuleConfiguration(currentRuleConfig); + checkToBeAlteredRules(sqlStatement, currentRuleConfig); checkColumnNames(sqlStatement); checkToBeAlteredEncryptors(sqlStatement); } - private void checkCurrentRuleConfiguration(final String databaseName, final EncryptRuleConfiguration currentRuleConfig) { - ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Encrypt", databaseName)); + private void checkCurrentRuleConfiguration(final EncryptRuleConfiguration currentRuleConfig) { + ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Encrypt", database.getName())); } - private void checkToBeAlteredRules(final String databaseName, final AlterEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { + private void checkToBeAlteredRules(final AlterEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { Collection currentEncryptTableNames = currentRuleConfig.getTables().stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toList()); Collection notExistEncryptTableNames = getToBeAlteredEncryptTableNames(sqlStatement).stream().filter(each -> !currentEncryptTableNames.contains(each)).collect(Collectors.toList()); if (!notExistEncryptTableNames.isEmpty()) { - throw new MissingRequiredRuleException("Encrypt", databaseName, notExistEncryptTableNames); + throw new MissingRequiredRuleException("Encrypt", database.getName(), notExistEncryptTableNames); } } diff --git a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutor.java b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutor.java index 4b20e244660aa..f4cd5c976b33f 100644 --- a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutor.java +++ b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.encrypt.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; import org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException; @@ -42,17 +43,32 @@ /** * Create encrypt rule executor. */ +@Setter public final class CreateEncryptRuleExecutor implements DatabaseRuleCreateExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { if (!sqlStatement.isIfNotExists()) { - checkDuplicateRuleNames(database.getName(), sqlStatement, currentRuleConfig); + checkDuplicateRuleNames(sqlStatement, currentRuleConfig); } checkColumnNames(sqlStatement); checkAlgorithmTypes(sqlStatement); checkToBeCreatedEncryptors(sqlStatement); - checkDataSources(database); + checkDataSources(); + } + + private void checkColumnNames(final CreateEncryptRuleStatement sqlStatement) { + for (EncryptRuleSegment each : sqlStatement.getRules()) { + ShardingSpherePreconditions.checkState(isColumnNameNotConflicts(each), + () -> new InvalidRuleConfigurationException("encrypt", "assisted query column or like query column conflicts with logic column")); + } + } + + private boolean isColumnNameNotConflicts(final EncryptRuleSegment rule) { + return rule.getColumns().stream().noneMatch(each -> null != each.getLikeQuery() && each.getName().equals(each.getLikeQuery().getName()) + || null != each.getAssistedQuery() && each.getName().equals(each.getAssistedQuery().getName())); } private void checkAlgorithmTypes(final CreateEncryptRuleStatement sqlStatement) { @@ -88,9 +104,9 @@ private void checkAssistedAlgorithmType(final EncryptColumnItemSegment itemSegme () -> new InvalidAlgorithmConfigurationException("assisted encrypt", encryptAlgorithm.getType())); } - private void checkDuplicateRuleNames(final String databaseName, final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { + private void checkDuplicateRuleNames(final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig); - ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("encrypt", databaseName, duplicatedRuleNames)); + ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("encrypt", database.getName(), duplicatedRuleNames)); } private Collection getDuplicatedRuleNames(final CreateEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { @@ -101,18 +117,6 @@ private Collection getDuplicatedRuleNames(final CreateEncryptRuleStateme return sqlStatement.getRules().stream().map(EncryptRuleSegment::getTableName).filter(currentRuleNames::contains).collect(Collectors.toSet()); } - private void checkColumnNames(final CreateEncryptRuleStatement sqlStatement) { - for (EncryptRuleSegment each : sqlStatement.getRules()) { - ShardingSpherePreconditions.checkState(isColumnNameNotConflicts(each), - () -> new InvalidRuleConfigurationException("encrypt", "assisted query column or like query column conflicts with logic column")); - } - } - - private boolean isColumnNameNotConflicts(final EncryptRuleSegment rule) { - return rule.getColumns().stream().noneMatch(each -> null != each.getLikeQuery() && each.getName().equals(each.getLikeQuery().getName()) - || null != each.getAssistedQuery() && each.getName().equals(each.getAssistedQuery().getName())); - } - private void checkToBeCreatedEncryptors(final CreateEncryptRuleStatement sqlStatement) { Collection encryptors = new LinkedHashSet<>(); sqlStatement.getRules().forEach(each -> each.getColumns().forEach(column -> { @@ -127,7 +131,7 @@ private void checkToBeCreatedEncryptors(final CreateEncryptRuleStatement sqlStat encryptors.stream().filter(Objects::nonNull).forEach(each -> TypedSPILoader.checkService(EncryptAlgorithm.class, each.getName(), each.getProps())); } - private void checkDataSources(final ShardingSphereDatabase database) { + private void checkDataSources() { ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnits().isEmpty(), () -> new EmptyStorageUnitException(database.getName())); } diff --git a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleExecutor.java b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleExecutor.java index f0bf6b6bc8717..8becbedffad3f 100644 --- a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleExecutor.java +++ b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.encrypt.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration; @@ -38,21 +39,24 @@ /** * Drop encrypt rule executor. */ +@Setter public final class DropEncryptRuleExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { - checkToBeDroppedEncryptTableNames(database.getName(), sqlStatement, currentRuleConfig); + public void checkBeforeUpdate(final DropEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { + checkToBeDroppedEncryptTableNames(sqlStatement, currentRuleConfig); } - private void checkToBeDroppedEncryptTableNames(final String databaseName, final DropEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { + private void checkToBeDroppedEncryptTableNames(final DropEncryptRuleStatement sqlStatement, final EncryptRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists()) { return; } - ShardingSpherePreconditions.checkState(isExistRuleConfig(currentRuleConfig), () -> new MissingRequiredRuleException("Encrypt", databaseName)); + ShardingSpherePreconditions.checkState(isExistRuleConfig(currentRuleConfig), () -> new MissingRequiredRuleException("Encrypt", database.getName())); Collection currentEncryptTableNames = currentRuleConfig.getTables().stream().map(EncryptTableRuleConfiguration::getName).collect(Collectors.toList()); Collection notExistedTableNames = sqlStatement.getTables().stream().filter(each -> !currentEncryptTableNames.contains(each)).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(notExistedTableNames.isEmpty(), () -> new MissingRequiredRuleException("Encrypt", databaseName, notExistedTableNames)); + ShardingSpherePreconditions.checkState(notExistedTableNames.isEmpty(), () -> new MissingRequiredRuleException("Encrypt", database.getName(), notExistedTableNames)); } @Override diff --git a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleStatementUpdaterTest.java b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleStatementUpdaterTest.java index 7212e1a8b69a6..d73d9299ca17b 100644 --- a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleStatementUpdaterTest.java +++ b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/AlterEncryptRuleStatementUpdaterTest.java @@ -31,11 +31,8 @@ import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.Arrays; import java.util.Collections; @@ -46,34 +43,37 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; -@ExtendWith(MockitoExtension.class) class AlterEncryptRuleStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - private final AlterEncryptRuleExecutor executor = new AlterEncryptRuleExecutor(); + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } + @Test void assertCheckSQLStatementWithoutCurrentRule() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("MD5"), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("MD5"), null)); } @Test void assertCheckSQLStatementWithoutToBeAlteredRules() { assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(database, createSQLStatement("MD5"), new EncryptRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); + () -> executor.checkBeforeUpdate(createSQLStatement("MD5"), new EncryptRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); } @Test void assertCheckSQLStatementWithoutToBeAlteredEncryptors() { - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("INVALID_TYPE"), createCurrentRuleConfiguration())); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(createSQLStatement("INVALID_TYPE"), createCurrentRuleConfiguration())); } @Test void assertCheckSQLStatementWithConflictColumnNames() { - assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(database, createConflictColumnNameSQLStatement(), createCurrentRuleConfiguration())); + assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createConflictColumnNameSQLStatement(), createCurrentRuleConfiguration())); } @Test diff --git a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleStatementUpdaterTest.java b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleStatementUpdaterTest.java index f3dd1dbbdd01d..3c5187ca64206 100644 --- a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleStatementUpdaterTest.java +++ b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleStatementUpdaterTest.java @@ -31,11 +31,8 @@ import org.apache.shardingsphere.infra.spi.exception.ServiceProviderNotFoundException; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.Collection; import java.util.Collections; @@ -47,35 +44,38 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; -@ExtendWith(MockitoExtension.class) class CreateEncryptRuleStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - private final CreateEncryptRuleExecutor executor = new CreateEncryptRuleExecutor(); + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } + @Test void assertCheckSQLStatementWithDuplicateEncryptRule() { - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement(false, "MD5"), getCurrentRuleConfig())); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "MD5"), getCurrentRuleConfig())); } @Test void assertCheckSQLStatementWithoutToBeCreatedEncryptors() { - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement(false, "INVALID_TYPE"), null)); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "INVALID_TYPE"), null)); } @Test void assertCheckSQLStatementWithConflictColumnNames() { - assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(database, createConflictColumnNameSQLStatement(), getCurrentRuleConfig())); + assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createConflictColumnNameSQLStatement(), getCurrentRuleConfig())); } @Test void assertCreateEncryptRuleWithIfNotExists() { EncryptRuleConfiguration currentRuleConfig = getCurrentRuleConfig(); CreateEncryptRuleStatement sqlStatement = createAESEncryptRuleSQLStatement(true); - executor.checkBeforeUpdate(database, sqlStatement, currentRuleConfig); + executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); EncryptRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, sqlStatement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(2)); @@ -129,7 +129,7 @@ private EncryptRuleConfiguration getCurrentRuleConfig() { void assertCreateAESEncryptRuleWithPropertiesNotExists() { EncryptRuleConfiguration currentRuleConfig = getCurrentRuleConfig(); CreateEncryptRuleStatement sqlStatement = createWrongAESEncryptorSQLStatement(); - assertThrows(EncryptAlgorithmInitializationException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, currentRuleConfig)); + assertThrows(EncryptAlgorithmInitializationException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentRuleConfig)); } private CreateEncryptRuleStatement createWrongAESEncryptorSQLStatement() { diff --git a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleStatementUpdaterTest.java b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleStatementUpdaterTest.java index 1a29c2b949087..94255028fd616 100644 --- a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleStatementUpdaterTest.java +++ b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/DropEncryptRuleStatementUpdaterTest.java @@ -25,11 +25,8 @@ import org.apache.shardingsphere.encrypt.distsql.statement.DropEncryptRuleStatement; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.Arrays; import java.util.Collections; @@ -43,25 +40,27 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -@ExtendWith(MockitoExtension.class) class DropEncryptRuleStatementUpdaterTest { private final DropEncryptRuleExecutor executor = new DropEncryptRuleExecutor(); - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } @Test void assertCheckSQLStatementWithoutCurrentRule() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("t_encrypt"), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_encrypt"), null)); } @Test void assertCheckSQLStatementWithoutToBeDroppedRule() { assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(database, createSQLStatement("t_encrypt"), new EncryptRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); + () -> executor.checkBeforeUpdate(createSQLStatement("t_encrypt"), new EncryptRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); } @Test @@ -82,7 +81,7 @@ void assertUpdateCurrentRuleConfigurationWithInUsedEncryptor() { void assertUpdateCurrentRuleConfigurationWithIfExists() { EncryptRuleConfiguration ruleConfig = createCurrentRuleConfiguration(); DropEncryptRuleStatement statement = createSQLStatement(true, "t_encrypt_1"); - executor.checkBeforeUpdate(database, statement, mock(EncryptRuleConfiguration.class)); + executor.checkBeforeUpdate(statement, mock(EncryptRuleConfiguration.class)); assertFalse(executor.updateCurrentRuleConfiguration(statement, ruleConfig)); assertThat(ruleConfig.getEncryptors().size(), is(3)); } diff --git a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleExecutor.java b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleExecutor.java index b0a662b779d99..18eceed6ce07a 100644 --- a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleExecutor.java +++ b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.mask.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; @@ -38,23 +39,25 @@ /** * Alter mask rule executor. */ +@Setter public final class AlterMaskRuleExecutor implements DatabaseRuleAlterExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final AlterMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { - String databaseName = database.getName(); - checkCurrentRuleConfiguration(databaseName, currentRuleConfig); - checkToBeAlteredRules(databaseName, sqlStatement, currentRuleConfig); + public void checkBeforeUpdate(final AlterMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { + checkCurrentRuleConfiguration(currentRuleConfig); + checkToBeAlteredRules(sqlStatement, currentRuleConfig); } - private void checkCurrentRuleConfiguration(final String databaseName, final MaskRuleConfiguration currentRuleConfig) { - ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Mask", databaseName)); + private void checkCurrentRuleConfiguration(final MaskRuleConfiguration currentRuleConfig) { + ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Mask", database.getName())); } - private void checkToBeAlteredRules(final String databaseName, final AlterMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { + private void checkToBeAlteredRules(final AlterMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { Collection currentMaskTableNames = currentRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList()); Collection notExistedMaskTableNames = getToBeAlteredMaskTableNames(sqlStatement).stream().filter(each -> !currentMaskTableNames.contains(each)).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(notExistedMaskTableNames.isEmpty(), () -> new MissingRequiredRuleException("Mask", databaseName, notExistedMaskTableNames)); + ShardingSpherePreconditions.checkState(notExistedMaskTableNames.isEmpty(), () -> new MissingRequiredRuleException("Mask", database.getName(), notExistedMaskTableNames)); } private Collection getToBeAlteredMaskTableNames(final AlterMaskRuleStatement sqlStatement) { diff --git a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleExecutor.java b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleExecutor.java index bf9fb55fdc9d1..99cd567c8ef23 100644 --- a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleExecutor.java +++ b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.mask.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -38,24 +39,27 @@ /** * Create mask rule executor. */ +@Setter public final class CreateMaskRuleExecutor implements DatabaseRuleCreateExecutor { + private ShardingSphereDatabase database; + private boolean ifNotExists; @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final CreateMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { ifNotExists = sqlStatement.isIfNotExists(); if (!ifNotExists) { - checkDuplicatedRuleNames(database.getName(), sqlStatement, currentRuleConfig); + checkDuplicatedRuleNames(sqlStatement, currentRuleConfig); } checkAlgorithms(sqlStatement); } - private void checkDuplicatedRuleNames(final String databaseName, final CreateMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { + private void checkDuplicatedRuleNames(final CreateMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { if (null != currentRuleConfig) { Collection currentRuleNames = currentRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList()); Collection duplicatedRuleNames = sqlStatement.getRules().stream().map(MaskRuleSegment::getTableName).filter(currentRuleNames::contains).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("mask", databaseName, duplicatedRuleNames)); + ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("mask", database.getName(), duplicatedRuleNames)); } } diff --git a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleExecutor.java b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleExecutor.java index 338c7c747342b..eca3aadefb4bc 100644 --- a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleExecutor.java +++ b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.mask.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; @@ -38,21 +39,24 @@ /** * Drop mask rule statement executor. */ +@Setter public final class DropMaskRuleExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { - checkToBeDroppedMaskTableNames(database.getName(), sqlStatement, currentRuleConfig); + public void checkBeforeUpdate(final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { + checkToBeDroppedMaskTableNames(sqlStatement, currentRuleConfig); } - private void checkToBeDroppedMaskTableNames(final String databaseName, final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { + private void checkToBeDroppedMaskTableNames(final DropMaskRuleStatement sqlStatement, final MaskRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists()) { return; } - ShardingSpherePreconditions.checkState(isExistRuleConfig(currentRuleConfig), () -> new MissingRequiredRuleException("Mask", databaseName)); + ShardingSpherePreconditions.checkState(isExistRuleConfig(currentRuleConfig), () -> new MissingRequiredRuleException("Mask", database.getName())); Collection currentMaskTableNames = currentRuleConfig.getTables().stream().map(MaskTableRuleConfiguration::getName).collect(Collectors.toList()); Collection notExistedTableNames = sqlStatement.getTables().stream().filter(each -> !currentMaskTableNames.contains(each)).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(notExistedTableNames.isEmpty(), () -> new MissingRequiredRuleException("Mask", databaseName, notExistedTableNames)); + ShardingSpherePreconditions.checkState(notExistedTableNames.isEmpty(), () -> new MissingRequiredRuleException("Mask", database.getName(), notExistedTableNames)); } @Override diff --git a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdaterTest.java b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdaterTest.java index 2bd4bb2101c2d..ee8ed9b5737e5 100644 --- a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdaterTest.java +++ b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdaterTest.java @@ -25,11 +25,8 @@ import org.apache.shardingsphere.mask.distsql.segment.MaskColumnSegment; import org.apache.shardingsphere.mask.distsql.segment.MaskRuleSegment; import org.apache.shardingsphere.mask.distsql.statement.AlterMaskRuleStatement; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.Collection; import java.util.Collections; @@ -41,29 +38,32 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; -@ExtendWith(MockitoExtension.class) class AlterMaskRuleStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - private final AlterMaskRuleExecutor executor = new AlterMaskRuleExecutor(); + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } + @Test void assertCheckSQLStatementWithoutCurrentRule() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("MD5"), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("MD5"), null)); } @Test void assertCheckSQLStatementWithoutToBeAlteredRules() { assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(database, createSQLStatement("MD5"), new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); + () -> executor.checkBeforeUpdate(createSQLStatement("MD5"), new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); } @Test void assertCheckSQLStatementWithoutToBeAlteredAlgorithm() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("INVALID_TYPE"), createCurrentRuleConfig())); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("INVALID_TYPE"), createCurrentRuleConfig())); } @Test @@ -71,7 +71,7 @@ void assertCheckSQLStatementWithIncompleteDataType() { MaskColumnSegment columnSegment = new MaskColumnSegment("user_id", new AlgorithmSegment("test", new Properties())); MaskRuleSegment ruleSegment = new MaskRuleSegment("t_mask", Collections.singleton(columnSegment)); AlterMaskRuleStatement statement = new AlterMaskRuleStatement(Collections.singleton(ruleSegment)); - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, statement, createCurrentRuleConfig())); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(statement, createCurrentRuleConfig())); } @Test diff --git a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleStatementUpdaterTest.java b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleStatementUpdaterTest.java index 3199b00fc97e3..752bc01405118 100644 --- a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleStatementUpdaterTest.java +++ b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleStatementUpdaterTest.java @@ -26,11 +26,8 @@ import org.apache.shardingsphere.mask.distsql.segment.MaskColumnSegment; import org.apache.shardingsphere.mask.distsql.segment.MaskRuleSegment; import org.apache.shardingsphere.mask.distsql.statement.CreateMaskRuleStatement; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.Collection; import java.util.Collections; @@ -42,30 +39,33 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; -@ExtendWith(MockitoExtension.class) class CreateMaskRuleStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - private final CreateMaskRuleExecutor executor = new CreateMaskRuleExecutor(); + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } + @Test void assertCheckSQLStatementWithDuplicateMaskRule() { - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(database, createDuplicatedSQLStatement(false, "MD5"), getCurrentRuleConfig())); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(createDuplicatedSQLStatement(false, "MD5"), getCurrentRuleConfig())); } @Test void assertCheckSQLStatementWithInvalidAlgorithm() { - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement(false, "INVALID_TYPE"), null)); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "INVALID_TYPE"), null)); } @Test void assertCreateMaskRule() { MaskRuleConfiguration currentRuleConfig = getCurrentRuleConfig(); CreateMaskRuleStatement sqlStatement = createSQLStatement(false, "MD5"); - executor.checkBeforeUpdate(database, sqlStatement, currentRuleConfig); + executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); MaskRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, sqlStatement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(4)); @@ -77,11 +77,11 @@ void assertCreateMaskRule() { void assertCreateMaskRuleWithIfNotExists() { MaskRuleConfiguration currentRuleConfig = getCurrentRuleConfig(); CreateMaskRuleStatement sqlStatement = createSQLStatement(false, "MD5"); - executor.checkBeforeUpdate(database, sqlStatement, currentRuleConfig); + executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); MaskRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, sqlStatement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); sqlStatement = createSQLStatement(true, "MD5"); - executor.checkBeforeUpdate(database, sqlStatement, currentRuleConfig); + executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, sqlStatement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(4)); diff --git a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleStatementUpdaterTest.java b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleStatementUpdaterTest.java index ed404944ce1ef..49ba7e2adf15b 100644 --- a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleStatementUpdaterTest.java +++ b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/DropMaskRuleStatementUpdaterTest.java @@ -23,11 +23,8 @@ import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration; import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration; import org.apache.shardingsphere.mask.distsql.statement.DropMaskRuleStatement; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.Collections; import java.util.HashMap; @@ -38,25 +35,27 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -@ExtendWith(MockitoExtension.class) class DropMaskRuleStatementUpdaterTest { private final DropMaskRuleExecutor executor = new DropMaskRuleExecutor(); - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } @Test void assertCheckSQLStatementWithoutCurrentRule() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement(false, "t_mask"), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "t_mask"), null)); } @Test void assertCheckSQLStatementWithoutToBeDroppedRule() { assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(database, createSQLStatement(false, "t_mask"), new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); + () -> executor.checkBeforeUpdate(createSQLStatement(false, "t_mask"), new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); } @Test @@ -71,7 +70,7 @@ void assertUpdateCurrentRuleConfiguration() { void assertUpdateCurrentRuleConfigurationWithIfExists() { MaskRuleConfiguration ruleConfig = createCurrentRuleConfiguration(); DropMaskRuleStatement statement = createSQLStatement(true, "t_user"); - executor.checkBeforeUpdate(database, statement, mock(MaskRuleConfiguration.class)); + executor.checkBeforeUpdate(statement, mock(MaskRuleConfiguration.class)); assertFalse(executor.updateCurrentRuleConfiguration(statement, ruleConfig)); assertThat(ruleConfig.getTables().size(), is(1)); } diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleExecutor.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleExecutor.java index f93c9cd3421bd..ee65dd993814e 100644 --- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleExecutor.java +++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleExecutor.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.readwritesplitting.distsql.handler.update; import com.google.common.base.Preconditions; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -38,10 +39,13 @@ /** * Alter readwrite-splitting rule executor. */ +@Setter public final class AlterReadwriteSplittingRuleExecutor implements DatabaseRuleAlterExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final AlterReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final AlterReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { ReadwriteSplittingRuleStatementChecker.checkAlteration(database, sqlStatement.getRules(), currentRuleConfig); } diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleExecutor.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleExecutor.java index 1014586f42585..22a8c6c10f861 100644 --- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleExecutor.java +++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.readwritesplitting.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration; @@ -33,10 +34,13 @@ /** * Create readwrite-splitting rule executor. */ +@Setter public final class CreateReadwriteSplittingRuleExecutor implements DatabaseRuleCreateExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final CreateReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { ReadwriteSplittingRuleStatementChecker.checkCreation(database, sqlStatement.getRules(), currentRuleConfig, sqlStatement.isIfNotExists()); } diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutor.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutor.java index c2301ad110e01..75adc344c6aba 100644 --- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutor.java +++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.readwritesplitting.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.exception.rule.RuleInUsedException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; @@ -44,35 +45,37 @@ /** * Drop readwrite-splitting rule executor. */ +@Setter public final class DropReadwriteSplittingRuleExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { if (!isExistRuleConfig(currentRuleConfig) && sqlStatement.isIfExists()) { return; } - String databaseName = database.getName(); ReadwriteSplittingRuleStatementChecker.checkRuleConfigurationExist(database, currentRuleConfig); - checkToBeDroppedRuleNames(databaseName, sqlStatement, currentRuleConfig); - checkToBeDroppedInUsed(database, sqlStatement); + checkToBeDroppedRuleNames(sqlStatement, currentRuleConfig); + checkToBeDroppedInUsed(sqlStatement); } - private void checkToBeDroppedRuleNames(final String databaseName, final DropReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { + private void checkToBeDroppedRuleNames(final DropReadwriteSplittingRuleStatement sqlStatement, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists()) { return; } Collection currentRuleNames = currentRuleConfig.getDataSources().stream().map(ReadwriteSplittingDataSourceRuleConfiguration::getName).collect(Collectors.toList()); Collection notExistedRuleNames = sqlStatement.getNames().stream().filter(each -> !currentRuleNames.contains(each)).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(notExistedRuleNames.isEmpty(), () -> new MissingRequiredRuleException("Readwrite-splitting", databaseName, sqlStatement.getNames())); + ShardingSpherePreconditions.checkState(notExistedRuleNames.isEmpty(), () -> new MissingRequiredRuleException("Readwrite-splitting", database.getName(), sqlStatement.getNames())); } - private void checkToBeDroppedInUsed(final ShardingSphereDatabase database, final DropReadwriteSplittingRuleStatement sqlStatement) { - Collection resourceBeUsed = getInUsedResources(database); + private void checkToBeDroppedInUsed(final DropReadwriteSplittingRuleStatement sqlStatement) { + Collection resourceBeUsed = getInUsedResources(); Collection ruleInUsed = sqlStatement.getNames().stream().filter(resourceBeUsed::contains).collect(Collectors.toSet()); ShardingSpherePreconditions.checkState(ruleInUsed.isEmpty(), () -> new RuleInUsedException("Readwrite-splitting", database.getName(), ruleInUsed)); } - private Collection getInUsedResources(final ShardingSphereDatabase database) { + private Collection getInUsedResources() { Collection result = new HashSet<>(); for (DataSourceContainedRule each : database.getRuleMetaData().findRules(DataSourceContainedRule.class)) { if (each instanceof ReadwriteSplittingRule) { diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdaterTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdaterTest.java index 9a1a21cc0bc89..fced83bf6636f 100644 --- a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdaterTest.java +++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/AlterReadwriteSplittingRuleStatementUpdaterTest.java @@ -59,31 +59,33 @@ class AlterReadwriteSplittingRuleStatementUpdaterTest { private final AlterReadwriteSplittingRuleExecutor executor = new AlterReadwriteSplittingRuleExecutor(); @BeforeEach - void before() { + void setUp() { when(database.getResourceMetaData()).thenReturn(resourceMetaData); + executor.setDatabase(database); } @Test void assertCheckSQLStatementWithoutCurrentRule() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("TEST"), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("TEST"), null)); } @Test void assertCheckSQLStatementWithoutToBeAlteredRules() { assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(database, createSQLStatement("TEST"), new ReadwriteSplittingRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); + () -> executor.checkBeforeUpdate(createSQLStatement("TEST"), new ReadwriteSplittingRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); } @Test void assertCheckSQLStatementWithoutExistedResources() { when(resourceMetaData.getNotExistedDataSources(any())).thenReturn(Collections.singleton("read_ds_0")); - assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("TEST"), createCurrentRuleConfiguration())); + assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(createSQLStatement("TEST"), createCurrentRuleConfiguration())); } @Test void assertCheckSQLStatementWithoutToBeAlteredLoadBalancers() { when(database.getRuleMetaData().findRules(any())).thenReturn(Collections.emptyList()); - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("INVALID_TYPE"), createCurrentRuleConfiguration())); + executor.setDatabase(database); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(createSQLStatement("INVALID_TYPE"), createCurrentRuleConfiguration())); } @Test @@ -91,8 +93,7 @@ void assertCheckSQLStatementWithDuplicateWriteResourceNamesInStatement() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData()).thenReturn(resourceMetaData); assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate(database, - createSQLStatementWithDuplicateWriteResourceNames("readwrite_ds_0", "readwrite_ds_1", "TEST"), createCurrentRuleConfigurationWithMultipleRules())); + () -> executor.checkBeforeUpdate(createSQLStatementWithDuplicateWriteResourceNames("readwrite_ds_0", "readwrite_ds_1", "TEST"), createCurrentRuleConfigurationWithMultipleRules())); } @Test @@ -100,7 +101,7 @@ void assertCheckSQLStatementWithDuplicateWriteResourceNames() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData()).thenReturn(resourceMetaData); assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate(database, + () -> executor.checkBeforeUpdate( createSQLStatement("readwrite_ds_0", "ds_write_1", Arrays.asList("read_ds_0", "read_ds_1"), "TEST"), createCurrentRuleConfigurationWithMultipleRules())); } @@ -109,8 +110,7 @@ void assertCheckSQLStatementWithDuplicateReadResourceNamesInStatement() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData()).thenReturn(resourceMetaData); assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate(database, - createSQLStatementWithDuplicateReadResourceNames("readwrite_ds_0", "readwrite_ds_1", "TEST"), createCurrentRuleConfigurationWithMultipleRules())); + () -> executor.checkBeforeUpdate(createSQLStatementWithDuplicateReadResourceNames("readwrite_ds_0", "readwrite_ds_1", "TEST"), createCurrentRuleConfigurationWithMultipleRules())); } @Test @@ -118,7 +118,7 @@ void assertCheckSQLStatementWithDuplicateReadResourceNames() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData()).thenReturn(resourceMetaData); assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate(database, + () -> executor.checkBeforeUpdate( createSQLStatement("readwrite_ds_1", "write_ds_1", Arrays.asList("read_ds_0_0", "read_ds_0_1"), "TEST"), createCurrentRuleConfigurationWithMultipleRules())); } diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java index 7dd3c86b9e4b7..e14597d897e52 100644 --- a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java +++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/CreateReadwriteSplittingRuleStatementUpdaterTest.java @@ -65,27 +65,28 @@ class CreateReadwriteSplittingRuleStatementUpdaterTest { private final CreateReadwriteSplittingRuleExecutor executor = new CreateReadwriteSplittingRuleExecutor(); @BeforeEach - void before() { + void setUp() { when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.emptyList()); + executor.setDatabase(database); } @Test void assertCheckSQLStatementWithDuplicateRuleNames() { when(resourceMetaData.getStorageUnits()).thenReturn(Collections.emptyMap()); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("TEST"), createCurrentRuleConfiguration())); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("TEST"), createCurrentRuleConfiguration())); } @Test void assertCheckSQLStatementWithDuplicateResource() { when(resourceMetaData.getStorageUnits()).thenReturn(Collections.singletonMap("write_ds", null)); - assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("write_ds", "TEST"), createCurrentRuleConfiguration())); + assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createSQLStatement("write_ds", "TEST"), createCurrentRuleConfiguration())); } @Test void assertCheckSQLStatementWithoutExistedResources() { when(resourceMetaData.getNotExistedDataSources(any())).thenReturn(Arrays.asList("read_ds_0", "read_ds_1")); - assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("TEST"), null)); + assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(createSQLStatement("TEST"), null)); } @Test @@ -95,37 +96,38 @@ void assertCheckSQLStatementWithDuplicateLogicResource() { when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.singleton(dataSourceContainedRule)); ReadwriteSplittingRuleSegment ruleSegment = new ReadwriteSplittingRuleSegment("duplicate_ds", "write_ds_0", Arrays.asList("read_ds_0", "read_ds_1"), new AlgorithmSegment(null, new Properties())); - assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement(false, ruleSegment), null)); + executor.setDatabase(database); + assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, ruleSegment), null)); } @Test void assertCheckSQLStatementWithDuplicateWriteResourceNamesInStatement() { assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate(database, createSQLStatementWithDuplicateWriteResourceNames("write_ds_0", "write_ds_1", "TEST"), null)); + () -> executor.checkBeforeUpdate(createSQLStatementWithDuplicateWriteResourceNames("write_ds_0", "write_ds_1", "TEST"), null)); } @Test void assertCheckSQLStatementWithDuplicateWriteResourceNames() { assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate(database, createSQLStatement("readwrite_ds_1", "ds_write", Arrays.asList("read_ds_0", "read_ds_1"), "TEST"), createCurrentRuleConfiguration())); + () -> executor.checkBeforeUpdate(createSQLStatement("readwrite_ds_1", "ds_write", Arrays.asList("read_ds_0", "read_ds_1"), "TEST"), createCurrentRuleConfiguration())); } @Test void assertCheckSQLStatementWithDuplicateReadResourceNamesInStatement() { - assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(database, createSQLStatementWithDuplicateReadResourceNames("write_ds_0", "write_ds_1", "TEST"), null)); + assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(createSQLStatementWithDuplicateReadResourceNames("write_ds_0", "write_ds_1", "TEST"), null)); } @Test void assertCheckSQLStatementWithDuplicateReadResourceNames() { assertThrows(InvalidRuleConfigurationException.class, - () -> executor.checkBeforeUpdate(database, createSQLStatement("readwrite_ds_1", "write_ds_1", Arrays.asList("read_ds_0", "read_ds_1"), "TEST"), createCurrentRuleConfiguration())); + () -> executor.checkBeforeUpdate(createSQLStatement("readwrite_ds_1", "write_ds_1", Arrays.asList("read_ds_0", "read_ds_1"), "TEST"), createCurrentRuleConfiguration())); } @Test void assertCheckSQLStatementWithIfNotExists() { ReadwriteSplittingRuleSegment staticSegment = new ReadwriteSplittingRuleSegment("readwrite_ds_0", "write_ds_0", Arrays.asList("read_ds_2", "read_ds_3"), new AlgorithmSegment(null, new Properties())); - executor.checkBeforeUpdate(database, createSQLStatement(true, staticSegment), createCurrentRuleConfiguration()); + executor.checkBeforeUpdate(createSQLStatement(true, staticSegment), createCurrentRuleConfiguration()); } @Test @@ -136,7 +138,8 @@ void assertUpdateSuccess() { ReadwriteSplittingRuleSegment staticSegment = new ReadwriteSplittingRuleSegment("static_rule", "write_ds_0", Arrays.asList("read_ds_0", "read_ds_1"), new AlgorithmSegment("TEST", new Properties())); CreateReadwriteSplittingRuleStatement statement = createSQLStatement(false, staticSegment); - executor.checkBeforeUpdate(database, statement, null); + executor.setDatabase(database); + executor.checkBeforeUpdate(statement, null); ReadwriteSplittingRuleConfiguration currentRuleConfig = new ReadwriteSplittingRuleConfiguration(new ArrayList<>(), new HashMap<>()); ReadwriteSplittingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdaterTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdaterTest.java index b73975d159f52..09b229f183b0b 100644 --- a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdaterTest.java +++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdaterTest.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration; import org.apache.shardingsphere.readwritesplitting.distsql.statement.DropReadwriteSplittingRuleStatement; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Answers; @@ -57,22 +58,27 @@ class DropReadwriteSplittingRuleStatementUpdaterTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ShardingSphereDatabase database; + @BeforeEach + void setUp() { + executor.setDatabase(database); + } + @Test void assertCheckSQLStatementWithoutCurrentRule() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement(), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(), null)); } @Test void assertCheckSQLStatementWithoutToBeDroppedRule() throws RuleDefinitionViolationException { assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(database, createSQLStatement(), new ReadwriteSplittingRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); + () -> executor.checkBeforeUpdate(createSQLStatement(), new ReadwriteSplittingRuleConfiguration(Collections.emptyList(), Collections.emptyMap()))); } @Test void assertCheckSQLStatementWithIfExists() throws RuleDefinitionViolationException { - executor.checkBeforeUpdate(database, new DropReadwriteSplittingRuleStatement(true, Collections.singleton("readwrite_ds")), + executor.checkBeforeUpdate(new DropReadwriteSplittingRuleStatement(true, Collections.singleton("readwrite_ds")), new ReadwriteSplittingRuleConfiguration(Collections.emptyList(), Collections.emptyMap())); - executor.checkBeforeUpdate(database, new DropReadwriteSplittingRuleStatement(true, Collections.singleton("readwrite_ds")), null); + executor.checkBeforeUpdate(new DropReadwriteSplittingRuleStatement(true, Collections.singleton("readwrite_ds")), null); } @Test @@ -83,7 +89,8 @@ void assertCheckSQLStatementWithInUsed() throws RuleDefinitionViolationException DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class); when(dataNodeContainedRule.getAllDataNodes()).thenReturn(Collections.singletonMap("foo_ds", Collections.singleton(new DataNode("readwrite_ds.tbl")))); when(database.getRuleMetaData().findRules(DataNodeContainedRule.class)).thenReturn(Collections.singleton(dataNodeContainedRule)); - assertThrows(RuleInUsedException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement(), createCurrentRuleConfiguration())); + executor.setDatabase(database); + assertThrows(RuleInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement(), createCurrentRuleConfiguration())); } @Test diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterDefaultShadowAlgorithmExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterDefaultShadowAlgorithmExecutor.java index 7984fbebb3a26..c1d47fd66c5c6 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterDefaultShadowAlgorithmExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterDefaultShadowAlgorithmExecutor.java @@ -18,30 +18,54 @@ package org.apache.shardingsphere.shadow.distsql.handler.update; import com.google.common.base.Strings; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor; import org.apache.shardingsphere.distsql.segment.AlgorithmSegment; import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.distsql.handler.checker.ShadowRuleStatementChecker; import org.apache.shardingsphere.shadow.distsql.statement.AlterDefaultShadowAlgorithmStatement; import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm; -import java.util.Collection; import java.util.Collections; import java.util.Map; /** * Alter default shadow algorithm executor. */ +@Setter public final class AlterDefaultShadowAlgorithmExecutor implements DatabaseRuleAlterExecutor { private static final String DEFAULT_ALGORITHM_NAME = "default_shadow_algorithm"; + private ShardingSphereDatabase database; + + @Override + public void checkBeforeUpdate(final AlterDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + ShadowRuleStatementChecker.checkRuleConfigurationExists(database.getName(), currentRuleConfig); + checkAlgorithms(sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment(), currentRuleConfig); + } + + private void checkAlgorithms(final AlgorithmSegment algorithmSegment, final ShadowRuleConfiguration currentRuleConfig) { + checkAlgorithmCompleteness(algorithmSegment); + checkAlgorithmType(algorithmSegment); + ShadowRuleStatementChecker.checkExisted(Collections.singleton(DEFAULT_ALGORITHM_NAME), + currentRuleConfig.getShadowAlgorithms().keySet(), notExistedAlgorithms -> new MissingRequiredAlgorithmException("shadow", database.getName(), notExistedAlgorithms)); + } + + private void checkAlgorithmCompleteness(final AlgorithmSegment algorithmSegment) { + ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(algorithmSegment.getName()), () -> new InvalidAlgorithmConfigurationException("shadow")); + } + + private void checkAlgorithmType(final AlgorithmSegment algorithmSegment) { + TypedSPILoader.checkService(ShadowAlgorithm.class, algorithmSegment.getName(), algorithmSegment.getProps()); + } + @Override public ShadowRuleConfiguration buildToBeAlteredRuleConfiguration(final ShadowRuleConfiguration currentRuleConfig, final AlterDefaultShadowAlgorithmStatement sqlStatement) { ShadowRuleConfiguration result = new ShadowRuleConfiguration(); @@ -61,29 +85,6 @@ public void updateCurrentRuleConfiguration(final ShadowRuleConfiguration current currentRuleConfig.setDefaultShadowAlgorithmName(toBeAlteredRuleConfig.getDefaultShadowAlgorithmName()); } - @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final AlterDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { - ShadowRuleStatementChecker.checkRuleConfigurationExists(database.getName(), currentRuleConfig); - checkAlgorithms(database.getName(), sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment(), currentRuleConfig); - } - - private void checkAlgorithms(final String databaseName, final AlgorithmSegment algorithmSegment, final ShadowRuleConfiguration currentRuleConfig) { - checkAlgorithmCompleteness(algorithmSegment); - checkAlgorithmType(algorithmSegment); - Collection requiredAlgorithmNames = Collections.singleton(DEFAULT_ALGORITHM_NAME); - ShadowRuleStatementChecker.checkExisted(requiredAlgorithmNames, - currentRuleConfig.getShadowAlgorithms().keySet(), notExistedAlgorithms -> new MissingRequiredAlgorithmException("shadow", databaseName, notExistedAlgorithms)); - } - - private void checkAlgorithmCompleteness(final AlgorithmSegment algorithmSegment) { - boolean isCompleteAlgorithm = !Strings.isNullOrEmpty(algorithmSegment.getName()); - ShardingSpherePreconditions.checkState(isCompleteAlgorithm, () -> new InvalidAlgorithmConfigurationException("shadow")); - } - - private void checkAlgorithmType(final AlgorithmSegment algorithmSegment) { - TypedSPILoader.checkService(ShadowAlgorithm.class, algorithmSegment.getName(), algorithmSegment.getProps()); - } - @Override public Class getRuleConfigurationClass() { return ShadowRuleConfiguration.class; diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleExecutor.java index dba41939ffdaa..a63ed78c2e4d8 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.shadow.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.algorithm.AlgorithmInUsedException; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; @@ -40,8 +41,37 @@ /** * Alter shadow rule executor. */ +@Setter public final class AlterShadowRuleExecutor implements DatabaseRuleAlterExecutor { + private ShardingSphereDatabase database; + + @Override + public void checkBeforeUpdate(final AlterShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + ShadowRuleStatementChecker.checkRuleConfigurationExists(database.getName(), currentRuleConfig); + checkRuleNames(sqlStatement.getRules(), currentRuleConfig); + ShadowRuleStatementChecker.checkStorageUnitsExist(ShadowRuleStatementSupporter.getStorageUnitNames(sqlStatement.getRules()), database); + checkAlgorithms(sqlStatement.getRules()); + checkAlgorithmType(sqlStatement); + } + + private void checkRuleNames(final Collection segments, final ShadowRuleConfiguration currentRuleConfig) { + Collection currentRuleNames = ShadowRuleStatementSupporter.getRuleNames(currentRuleConfig); + Collection requiredRuleNames = ShadowRuleStatementSupporter.getRuleNames(segments); + ShadowRuleStatementChecker.checkDuplicated(requiredRuleNames, duplicated -> new DuplicateRuleException("shadow", database.getName(), duplicated)); + ShadowRuleStatementChecker.checkExisted(requiredRuleNames, currentRuleNames, notExistedRules -> new MissingRequiredRuleException("Shadow", notExistedRules)); + } + + private void checkAlgorithms(final Collection segments) { + Collection requiredAlgorithms = ShadowRuleStatementSupporter.getAlgorithmNames(segments); + ShadowRuleStatementChecker.checkDuplicated(requiredAlgorithms, duplicated -> new AlgorithmInUsedException("Shadow", database.getName(), duplicated)); + } + + private void checkAlgorithmType(final AlterShadowRuleStatement sqlStatement) { + sqlStatement.getRules().stream().flatMap(each -> each.getShadowTableRules().values().stream()).flatMap(Collection::stream) + .map(ShadowAlgorithmSegment::getAlgorithmSegment).forEach(each -> TypedSPILoader.checkService(ShadowAlgorithm.class, each.getName(), each.getProps())); + } + @Override public ShadowRuleConfiguration buildToBeAlteredRuleConfiguration(final ShadowRuleConfiguration currentRuleConfig, final AlterShadowRuleStatement sqlStatement) { return ShadowRuleStatementConverter.convert(sqlStatement.getRules()); @@ -62,37 +92,6 @@ private void updateTables(final Map currentTab toBeAlteredTables.forEach(currentTables::replace); } - @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final AlterShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { - String databaseName = database.getName(); - ShadowRuleStatementChecker.checkRuleConfigurationExists(databaseName, currentRuleConfig); - checkRuleNames(databaseName, sqlStatement.getRules(), currentRuleConfig); - checkStorageUnits(database, sqlStatement.getRules()); - checkAlgorithms(databaseName, sqlStatement.getRules()); - checkAlgorithmType(sqlStatement); - } - - private void checkRuleNames(final String databaseName, final Collection segments, final ShadowRuleConfiguration currentRuleConfig) { - Collection currentRuleNames = ShadowRuleStatementSupporter.getRuleNames(currentRuleConfig); - Collection requiredRuleNames = ShadowRuleStatementSupporter.getRuleNames(segments); - ShadowRuleStatementChecker.checkDuplicated(requiredRuleNames, duplicated -> new DuplicateRuleException("shadow", databaseName, duplicated)); - ShadowRuleStatementChecker.checkExisted(requiredRuleNames, currentRuleNames, notExistedRules -> new MissingRequiredRuleException("Shadow", notExistedRules)); - } - - private void checkStorageUnits(final ShardingSphereDatabase database, final Collection segments) { - ShadowRuleStatementChecker.checkStorageUnitsExist(ShadowRuleStatementSupporter.getStorageUnitNames(segments), database); - } - - private void checkAlgorithmType(final AlterShadowRuleStatement sqlStatement) { - sqlStatement.getRules().stream().flatMap(each -> each.getShadowTableRules().values().stream()).flatMap(Collection::stream) - .map(ShadowAlgorithmSegment::getAlgorithmSegment).forEach(each -> TypedSPILoader.checkService(ShadowAlgorithm.class, each.getName(), each.getProps())); - } - - private void checkAlgorithms(final String databaseName, final Collection segments) { - Collection requiredAlgorithms = ShadowRuleStatementSupporter.getAlgorithmNames(segments); - ShadowRuleStatementChecker.checkDuplicated(requiredAlgorithms, duplicated -> new AlgorithmInUsedException("Shadow", databaseName, duplicated)); - } - @Override public Class getRuleConfigurationClass() { return ShadowRuleConfiguration.class; diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmExecutor.java index 5b0fa3bb3856d..a1aa64c740b84 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateDefaultShadowAlgorithmExecutor.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.shadow.distsql.handler.update; import com.google.common.base.Strings; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.algorithm.DuplicateAlgorithmException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; @@ -39,10 +40,13 @@ /** * Create default shadow algorithm statement executor. */ +@Setter public final class CreateDefaultShadowAlgorithmExecutor implements DatabaseRuleCreateExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final CreateDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { if (!sqlStatement.isIfNotExists()) { checkExisted(database.getName(), currentRuleConfig); } diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleExecutor.java index 0ff51becaf6b1..aca2c9151aa56 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.shadow.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -38,17 +39,42 @@ /** * Create shadow rule executor. */ +@Setter public final class CreateShadowRuleExecutor implements DatabaseRuleCreateExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { - checkDuplicatedRules(database, sqlStatement, currentRuleConfig); - String databaseName = database.getName(); - checkStorageUnits(database, sqlStatement.getRules()); - checkAlgorithms(databaseName, sqlStatement.getRules()); + public void checkBeforeUpdate(final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + checkDuplicatedRules(sqlStatement, currentRuleConfig); + checkStorageUnits(sqlStatement.getRules()); + checkAlgorithms(sqlStatement.getRules()); checkAlgorithmType(sqlStatement.getRules()); } + private void checkDuplicatedRules(final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + Collection toBeCreatedRuleNames = ShadowRuleStatementSupporter.getRuleNames(sqlStatement.getRules()); + ShadowRuleStatementChecker.checkDuplicated(toBeCreatedRuleNames, duplicated -> new DuplicateRuleException("shadow", database.getName(), duplicated)); + ShadowRuleStatementChecker.checkDuplicatedWithLogicDataSource(toBeCreatedRuleNames, database); + if (!sqlStatement.isIfNotExists()) { + toBeCreatedRuleNames.retainAll(ShadowRuleStatementSupporter.getRuleNames(currentRuleConfig)); + ShardingSpherePreconditions.checkState(toBeCreatedRuleNames.isEmpty(), () -> new DuplicateRuleException("shadow", database.getName(), toBeCreatedRuleNames)); + } + } + + private void checkStorageUnits(final Collection segments) { + ShadowRuleStatementChecker.checkStorageUnitsExist(ShadowRuleStatementSupporter.getStorageUnitNames(segments), database); + } + + private void checkAlgorithms(final Collection segments) { + ShadowRuleStatementChecker.checkDuplicated(ShadowRuleStatementSupporter.getAlgorithmNames(segments), duplicated -> new DuplicateRuleException("shadow", database.getName(), duplicated)); + } + + private void checkAlgorithmType(final Collection segments) { + segments.stream().flatMap(each -> each.getShadowTableRules().values().stream()).flatMap(Collection::stream) + .map(ShadowAlgorithmSegment::getAlgorithmSegment).forEach(each -> TypedSPILoader.checkService(ShadowAlgorithm.class, each.getName(), each.getProps())); + } + @Override public ShadowRuleConfiguration buildToBeCreatedRuleConfiguration(final ShadowRuleConfiguration currentRuleConfig, final CreateShadowRuleStatement sqlStatement) { Collection segments = sqlStatement.getRules(); @@ -71,29 +97,6 @@ private void updateTables(final Map currentTab toBeCreateTables.forEach((key, value) -> currentTables.merge(key, value, ShadowRuleStatementSupporter::mergeConfiguration)); } - private void checkDuplicatedRules(final ShardingSphereDatabase database, final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { - Collection toBeCreatedRuleNames = ShadowRuleStatementSupporter.getRuleNames(sqlStatement.getRules()); - ShadowRuleStatementChecker.checkDuplicated(toBeCreatedRuleNames, duplicated -> new DuplicateRuleException("shadow", database.getName(), duplicated)); - ShadowRuleStatementChecker.checkDuplicatedWithLogicDataSource(toBeCreatedRuleNames, database); - if (!sqlStatement.isIfNotExists()) { - toBeCreatedRuleNames.retainAll(ShadowRuleStatementSupporter.getRuleNames(currentRuleConfig)); - ShardingSpherePreconditions.checkState(toBeCreatedRuleNames.isEmpty(), () -> new DuplicateRuleException("shadow", database.getName(), toBeCreatedRuleNames)); - } - } - - private void checkStorageUnits(final ShardingSphereDatabase database, final Collection segments) { - ShadowRuleStatementChecker.checkStorageUnitsExist(ShadowRuleStatementSupporter.getStorageUnitNames(segments), database); - } - - private void checkAlgorithms(final String databaseName, final Collection segments) { - ShadowRuleStatementChecker.checkDuplicated(ShadowRuleStatementSupporter.getAlgorithmNames(segments), duplicated -> new DuplicateRuleException("shadow", databaseName, duplicated)); - } - - private void checkAlgorithmType(final Collection segments) { - segments.stream().flatMap(each -> each.getShadowTableRules().values().stream()).flatMap(Collection::stream) - .map(ShadowAlgorithmSegment::getAlgorithmSegment).forEach(each -> TypedSPILoader.checkService(ShadowAlgorithm.class, each.getName(), each.getProps())); - } - @Override public Class getRuleConfigurationClass() { return ShadowRuleConfiguration.class; diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmExecutor.java index 3e8db5f09fbae..2ed3a668af18b 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.shadow.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -30,21 +31,24 @@ /** * Drop default shadow algorithm executor. */ +@Setter public final class DropDefaultShadowAlgorithmExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists() && !isExistRuleConfig(currentRuleConfig)) { return; } ShadowRuleStatementChecker.checkRuleConfigurationExists(database.getName(), currentRuleConfig); - checkAlgorithm(database.getName(), sqlStatement, currentRuleConfig); + checkAlgorithm(sqlStatement, currentRuleConfig); } - private void checkAlgorithm(final String databaseName, final DropDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + private void checkAlgorithm(final DropDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { if (!sqlStatement.isIfExists()) { ShardingSpherePreconditions.checkNotNull(currentRuleConfig.getDefaultShadowAlgorithmName(), - () -> new MissingRequiredAlgorithmException("shadow", databaseName, Collections.singleton("default"))); + () -> new MissingRequiredAlgorithmException("shadow", database.getName(), Collections.singleton("default"))); } } diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmExecutor.java index a5e8a4e5adc72..0625fa7be775d 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.shadow.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.DistSQLException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.AlgorithmInUsedException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException; @@ -39,27 +40,31 @@ /** * Drop shadow algorithm executor. */ +@Setter public final class DropShadowAlgorithmExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists() && !isExistRuleConfig(currentRuleConfig)) { return; } ShadowRuleStatementChecker.checkRuleConfigurationExists(database.getName(), currentRuleConfig); - checkAlgorithm(database.getName(), sqlStatement, currentRuleConfig); + checkAlgorithm(sqlStatement, currentRuleConfig); } - private void checkAlgorithm(final String databaseName, final DropShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + private void checkAlgorithm(final DropShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { Collection currentAlgorithms = ShadowRuleStatementSupporter.getAlgorithmNames(currentRuleConfig); Collection requiredAlgorithms = sqlStatement.getNames(); String defaultShadowAlgorithmName = currentRuleConfig.getDefaultShadowAlgorithmName(); if (!sqlStatement.isIfExists()) { - ShadowRuleStatementChecker.checkExisted(requiredAlgorithms, currentAlgorithms, notExistedAlgorithms -> new MissingRequiredAlgorithmException("shadow", databaseName, notExistedAlgorithms)); + ShadowRuleStatementChecker.checkExisted( + requiredAlgorithms, currentAlgorithms, notExistedAlgorithms -> new MissingRequiredAlgorithmException("shadow", database.getName(), notExistedAlgorithms)); } - checkAlgorithmInUsed(requiredAlgorithms, getAlgorithmInUse(currentRuleConfig), identical -> new AlgorithmInUsedException("Sharding", databaseName, identical)); + checkAlgorithmInUsed(requiredAlgorithms, getAlgorithmInUse(currentRuleConfig), identical -> new AlgorithmInUsedException("Sharding", database.getName(), identical)); ShardingSpherePreconditions.checkState(!requiredAlgorithms.contains(defaultShadowAlgorithmName), - () -> new AlgorithmInUsedException("Shadow", databaseName, Collections.singleton(defaultShadowAlgorithmName))); + () -> new AlgorithmInUsedException("Shadow", database.getName(), Collections.singleton(defaultShadowAlgorithmName))); } private void checkAlgorithmInUsed(final Collection requiredAlgorithms, final Collection currentAlgorithms, final Function, DistSQLException> thrower) { diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleExecutor.java index 316502c3e7032..f508ba1837afb 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleExecutor.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.shadow.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; @@ -39,25 +40,28 @@ /** * Drop shadow rule executor. */ +@Setter public final class DropShadowRuleExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists() && !isExistRuleConfig(currentRuleConfig)) { return; } - checkConfigurationExisted(database.getName(), currentRuleConfig); - checkRuleExisted(database.getName(), sqlStatement, currentRuleConfig); + checkConfigurationExisted(currentRuleConfig); + checkRuleExisted(sqlStatement, currentRuleConfig); } - private void checkConfigurationExisted(final String databaseName, final ShadowRuleConfiguration currentRuleConfig) { - ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Shadow", databaseName)); + private void checkConfigurationExisted(final ShadowRuleConfiguration currentRuleConfig) { + ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Shadow", database.getName())); } - private void checkRuleExisted(final String databaseName, final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + private void checkRuleExisted(final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { if (!sqlStatement.isIfExists()) { ShadowRuleStatementChecker.checkExisted(sqlStatement.getNames(), getDataSourceNames(currentRuleConfig), - notExistedRuleNames -> new MissingRequiredRuleException("Shadow", databaseName, notExistedRuleNames)); + notExistedRuleNames -> new MissingRequiredRuleException("Shadow", database.getName(), notExistedRuleNames)); } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterDefaultShadowAlgorithmStatementUpdaterTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterDefaultShadowAlgorithmStatementUpdaterTest.java index 1da6e1d88d995..8c05c195a0754 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterDefaultShadowAlgorithmStatementUpdaterTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterDefaultShadowAlgorithmStatementUpdaterTest.java @@ -30,9 +30,9 @@ import org.apache.shardingsphere.shadow.distsql.statement.AlterDefaultShadowAlgorithmStatement; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -40,24 +40,28 @@ import java.util.Properties; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class AlterDefaultShadowAlgorithmStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; + private final AlterDefaultShadowAlgorithmExecutor executor = new AlterDefaultShadowAlgorithmExecutor(); @Mock private ShadowRuleConfiguration currentConfig; - private final AlterDefaultShadowAlgorithmExecutor executor = new AlterDefaultShadowAlgorithmExecutor(); + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } @Test void assertExecuteAlgorithmWithoutConfiguration() { AlterDefaultShadowAlgorithmStatement sqlStatement = new AlterDefaultShadowAlgorithmStatement( new ShadowAlgorithmSegment("sqlHintAlgorithm", new AlgorithmSegment("SQL_HINT", PropertiesBuilder.build(new Property("type", "value"))))); - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, null)); } @Test @@ -66,21 +70,21 @@ void assertExecuteAlgorithmNotInMetaData() { when(currentConfig.getShadowAlgorithms()).thenReturn(Collections.singletonMap("sqlHintAlgorithm", new AlgorithmConfiguration("type", props))); AlterDefaultShadowAlgorithmStatement sqlStatement = new AlterDefaultShadowAlgorithmStatement( new ShadowAlgorithmSegment("default_shadow_algorithm", new AlgorithmSegment("SQL_HINT", props))); - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, currentConfig)); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); } @Test void assertExecuteInvalidAlgorithmType() { AlterDefaultShadowAlgorithmStatement sqlStatement = new AlterDefaultShadowAlgorithmStatement( new ShadowAlgorithmSegment("default_shadow_algorithm", new AlgorithmSegment("NOT_EXIST_SQL_HINT", PropertiesBuilder.build(new Property("type", "value"))))); - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, currentConfig)); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); } @Test void assertExecuteIncompletenessAlgorithm() { AlterDefaultShadowAlgorithmStatement sqlStatement = new AlterDefaultShadowAlgorithmStatement( new ShadowAlgorithmSegment("default_shadow_algorithm", new AlgorithmSegment("", PropertiesBuilder.build(new Property("type", "value"))))); - assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, currentConfig)); + assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); } @Test @@ -89,6 +93,6 @@ void assertExecuteSuccess() { when(currentConfig.getShadowAlgorithms()).thenReturn(Collections.singletonMap("default_shadow_algorithm", new AlgorithmConfiguration("type", props))); AlterDefaultShadowAlgorithmStatement sqlStatement = new AlterDefaultShadowAlgorithmStatement( new ShadowAlgorithmSegment("default_shadow_algorithm", new AlgorithmSegment("SQL_HINT", props))); - executor.checkBeforeUpdate(database, sqlStatement, currentConfig); + executor.checkBeforeUpdate(sqlStatement, currentConfig); } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterShadowRuleStatementUpdaterTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterShadowRuleStatementUpdaterTest.java index c98f74b6d8838..48c6eddc119f3 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterShadowRuleStatementUpdaterTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterShadowRuleStatementUpdaterTest.java @@ -35,7 +35,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; @@ -50,15 +49,14 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) class AlterShadowRuleStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - @Mock private ResourceMetaData resourceMetaData; @@ -68,30 +66,32 @@ class AlterShadowRuleStatementUpdaterTest { private final AlterShadowRuleExecutor executor = new AlterShadowRuleExecutor(); @BeforeEach - void before() { + void setUp() { Collection shadowDataSource = new LinkedList<>(); shadowDataSource.add(new ShadowDataSourceConfiguration("initRuleName1", "ds1", "ds_shadow1")); shadowDataSource.add(new ShadowDataSourceConfiguration("initRuleName2", "ds2", "ds_shadow2")); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(currentConfig.getDataSources()).thenReturn(shadowDataSource); + executor.setDatabase(database); } @Test void assertExecuteWithoutCurrentConfiguration() { ShadowRuleSegment ruleSegment = new ShadowRuleSegment("ruleName", null, null, null); - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, new AlterShadowRuleStatement(Arrays.asList(ruleSegment, ruleSegment)), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(new AlterShadowRuleStatement(Arrays.asList(ruleSegment, ruleSegment)), null)); } @Test void assertExecuteWithDuplicateRuleName() { ShadowRuleSegment ruleSegment = new ShadowRuleSegment("ruleName", null, null, null); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(database, new AlterShadowRuleStatement(Arrays.asList(ruleSegment, ruleSegment)), currentConfig)); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(new AlterShadowRuleStatement(Arrays.asList(ruleSegment, ruleSegment)), currentConfig)); } @Test void assertExecuteWithRuleNameNotInMetaData() { ShadowRuleSegment ruleSegment = new ShadowRuleSegment("ruleName", null, null, null); - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, new AlterShadowRuleStatement(Collections.singleton(ruleSegment)), currentConfig)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(new AlterShadowRuleStatement(Collections.singleton(ruleSegment)), currentConfig)); } @Test @@ -99,7 +99,7 @@ void assertExecuteWithNotExistResource() { List dataSources = Arrays.asList("ds", "ds0"); when(resourceMetaData.getNotExistedDataSources(any())).thenReturn(dataSources); AlterShadowRuleStatement sqlStatement = new AlterShadowRuleStatement(Collections.singleton(new ShadowRuleSegment("initRuleName1", "ds3", null, null))); - assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, currentConfig)); + assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); } @Test @@ -108,7 +108,7 @@ void assertExecuteDuplicateAlgorithm() { AlterShadowRuleStatement sqlStatement = new AlterShadowRuleStatement(Arrays.asList( new ShadowRuleSegment("initRuleName1", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))), new ShadowRuleSegment("initRuleName2", "ds1", null, Collections.singletonMap("t_order_1", Collections.singleton(segment))))); - assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, currentConfig)); + assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); } @Test @@ -117,7 +117,7 @@ void assertExecuteDuplicateAlgorithmWithoutConfiguration() { AlterShadowRuleStatement sqlStatement = new AlterShadowRuleStatement(Arrays.asList( new ShadowRuleSegment("initRuleName1", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))), new ShadowRuleSegment("initRuleName2", "ds1", null, Collections.singletonMap("t_order_1", Collections.singleton(segment))))); - assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, currentConfig)); + assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); } @Test @@ -128,7 +128,7 @@ void assertExecuteSuccess() { AlterShadowRuleStatement sqlStatement = new AlterShadowRuleStatement(Arrays.asList( new ShadowRuleSegment("initRuleName1", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment1))), new ShadowRuleSegment("initRuleName2", "ds1", null, Collections.singletonMap("t_order_1", Collections.singleton(segment2))))); - executor.checkBeforeUpdate(database, sqlStatement, currentConfig); + executor.checkBeforeUpdate(sqlStatement, currentConfig); } @Test @@ -138,6 +138,6 @@ void assertExecuteSuccessWithoutProps() { AlterShadowRuleStatement sqlStatement = new AlterShadowRuleStatement(Arrays.asList( new ShadowRuleSegment("initRuleName1", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment1))), new ShadowRuleSegment("initRuleName2", "ds1", null, Collections.singletonMap("t_order_1", Collections.singleton(segment2))))); - executor.checkBeforeUpdate(database, sqlStatement, currentConfig); + executor.checkBeforeUpdate(sqlStatement, currentConfig); } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmStatementUpdaterTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmStatementUpdaterTest.java index b6b0078dca2d5..65c62d734dbbb 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmStatementUpdaterTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateDefaultShadowAlgorithmStatementUpdaterTest.java @@ -26,32 +26,35 @@ import org.apache.shardingsphere.shadow.distsql.statement.CreateDefaultShadowAlgorithmStatement; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class CreateDefaultShadowAlgorithmStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; + private final CreateDefaultShadowAlgorithmExecutor executor = new CreateDefaultShadowAlgorithmExecutor(); @Mock private ShadowRuleConfiguration currentConfig; - private final CreateDefaultShadowAlgorithmExecutor executor = new CreateDefaultShadowAlgorithmExecutor(); + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } @Test void assertExecuteWithInvalidAlgorithm() { CreateDefaultShadowAlgorithmStatement statement = mock(CreateDefaultShadowAlgorithmStatement.class); when(statement.getShadowAlgorithmSegment()).thenReturn(new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("name", PropertiesBuilder.build(new Property("type", "value"))))); - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(database, statement, currentConfig)); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(statement, currentConfig)); } @Test @@ -59,13 +62,13 @@ void assertExecuteSuccess() { CreateDefaultShadowAlgorithmStatement statement = mock(CreateDefaultShadowAlgorithmStatement.class); when(statement.getShadowAlgorithmSegment()).thenReturn( new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("SQL_HINT", PropertiesBuilder.build(new Property("type", "value"))))); - executor.checkBeforeUpdate(database, statement, currentConfig); + executor.checkBeforeUpdate(statement, currentConfig); } @Test void assertExecuteWithIfNotExists() { ShadowAlgorithmSegment shadowAlgorithmSegment = new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("SQL_HINT", PropertiesBuilder.build(new Property("type", "value")))); CreateDefaultShadowAlgorithmStatement statement = new CreateDefaultShadowAlgorithmStatement(true, shadowAlgorithmSegment); - executor.checkBeforeUpdate(database, statement, currentConfig); + executor.checkBeforeUpdate(statement, currentConfig); } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateShadowRuleStatementUpdaterTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateShadowRuleStatementUpdaterTest.java index 7be5b2063dd26..ed731986b7424 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateShadowRuleStatementUpdaterTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/CreateShadowRuleStatementUpdaterTest.java @@ -66,24 +66,25 @@ class CreateShadowRuleStatementUpdaterTest { private final CreateShadowRuleExecutor executor = new CreateShadowRuleExecutor(); @BeforeEach - void before() { + void setUp() { when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(database.getName()).thenReturn("shadow_db"); when(currentConfig.getDataSources()).thenReturn(Collections.singleton(new ShadowDataSourceConfiguration("initRuleName", "initDs0", "initDs0Shadow"))); when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.emptyList()); + executor.setDatabase(database); } @Test void assertExecuteWithDuplicateRuleName() { ShadowRuleSegment ruleSegment = new ShadowRuleSegment("ruleName", null, null, null); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(database, new CreateShadowRuleStatement(false, Arrays.asList(ruleSegment, ruleSegment)), null)); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(new CreateShadowRuleStatement(false, Arrays.asList(ruleSegment, ruleSegment)), null)); } @Test void assertExecuteWithDuplicateRuleNameInMetaData() { when(currentConfig.getDataSources()).thenReturn(Collections.singleton(new ShadowDataSourceConfiguration("ruleName", "ds", "ds_shadow"))); ShadowRuleSegment ruleSegment = new ShadowRuleSegment("ruleName", null, null, null); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(database, new CreateShadowRuleStatement(false, Collections.singleton(ruleSegment)), currentConfig)); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(new CreateShadowRuleStatement(false, Collections.singleton(ruleSegment)), currentConfig)); } @Test @@ -91,15 +92,16 @@ void assertExecuteWithDuplicateLogicResource() { DataSourceContainedRule dataSourceContainedRule = mock(DataSourceContainedRule.class); when(dataSourceContainedRule.getDataSourceMapper()).thenReturn(Collections.singletonMap("duplicate_ds", Collections.singleton("ds_0"))); when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.singleton(dataSourceContainedRule)); + executor.setDatabase(database); ShadowRuleSegment ruleSegment = new ShadowRuleSegment("duplicate_ds", null, null, null); - assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(database, new CreateShadowRuleStatement(false, Collections.singleton(ruleSegment)), null)); + assertThrows(InvalidRuleConfigurationException.class, () -> executor.checkBeforeUpdate(new CreateShadowRuleStatement(false, Collections.singleton(ruleSegment)), null)); } @Test void assertExecuteWithNotExistResource() { when(resourceMetaData.getNotExistedDataSources(any())).thenReturn(Arrays.asList("ds0", "ds1")); CreateShadowRuleStatement sqlStatement = new CreateShadowRuleStatement(false, Collections.singleton(new ShadowRuleSegment("ruleName", "ds1", null, null))); - assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, currentConfig)); + assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); } @Test @@ -108,7 +110,7 @@ void assertExecuteDuplicateAlgorithm() { CreateShadowRuleStatement sqlStatement = new CreateShadowRuleStatement(false, Arrays.asList( new ShadowRuleSegment("ruleName", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))), new ShadowRuleSegment("ruleName", "ds1", null, Collections.singletonMap("t_order_1", Collections.singleton(segment))))); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, currentConfig)); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); } @Test @@ -117,7 +119,7 @@ void assertExecuteDuplicateAlgorithmWithoutConfiguration() { CreateShadowRuleStatement sqlStatement = new CreateShadowRuleStatement(false, Arrays.asList( new ShadowRuleSegment("ruleName", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))), new ShadowRuleSegment("ruleName1", "ds1", null, Collections.singletonMap("t_order_1", Collections.singleton(segment))))); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, null)); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(sqlStatement, null)); } @Test @@ -125,7 +127,7 @@ void assertInvalidAlgorithmConfiguration() { ShadowAlgorithmSegment segment = new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("type", PropertiesBuilder.build(new Property("type", "value")))); CreateShadowRuleStatement sqlStatement = new CreateShadowRuleStatement(false, Collections.singleton(new ShadowRuleSegment("ruleName", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))))); - assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, currentConfig)); + assertThrows(ServiceProviderNotFoundException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentConfig)); } @Test @@ -133,7 +135,7 @@ void assertExecuteWithoutProps() { ShadowAlgorithmSegment segment = new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("SQL_HINT", null)); CreateShadowRuleStatement sqlStatement = new CreateShadowRuleStatement(false, Collections.singleton(new ShadowRuleSegment("initRuleNameWithoutProps", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))))); - executor.checkBeforeUpdate(database, sqlStatement, currentConfig); + executor.checkBeforeUpdate(sqlStatement, currentConfig); } @Test @@ -141,6 +143,6 @@ void assertExecuteWithIfNotExists() { ShadowAlgorithmSegment segment = new ShadowAlgorithmSegment("algorithmName", new AlgorithmSegment("SQL_HINT", PropertiesBuilder.build(new Property("type", "value")))); CreateShadowRuleStatement sqlStatement = new CreateShadowRuleStatement(true, Collections.singleton(new ShadowRuleSegment("initRuleName", "ds", null, Collections.singletonMap("t_order", Collections.singleton(segment))))); - executor.checkBeforeUpdate(database, sqlStatement, currentConfig); + executor.checkBeforeUpdate(sqlStatement, currentConfig); } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropDefaultShadowAlgorithmStatementUpdaterTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropDefaultShadowAlgorithmStatementUpdaterTest.java index 403e6f80dcfb5..4260c2bda4da7 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropDefaultShadowAlgorithmStatementUpdaterTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropDefaultShadowAlgorithmStatementUpdaterTest.java @@ -23,37 +23,40 @@ import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.distsql.handler.update.DropDefaultShadowAlgorithmExecutor; import org.apache.shardingsphere.shadow.distsql.statement.DropDefaultShadowAlgorithmStatement; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; @ExtendWith(MockitoExtension.class) class DropDefaultShadowAlgorithmStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; + private final DropDefaultShadowAlgorithmExecutor executor = new DropDefaultShadowAlgorithmExecutor(); @Mock private ShadowRuleConfiguration currentConfig; - private final DropDefaultShadowAlgorithmExecutor executor = new DropDefaultShadowAlgorithmExecutor(); + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } @Test void assertCheckWithoutDefaultAlgorithm() { - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(database, new DropDefaultShadowAlgorithmStatement(false), currentConfig)); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(new DropDefaultShadowAlgorithmStatement(false), currentConfig)); } @Test void assertCheckWithIfExists() { - executor.checkBeforeUpdate(database, new DropDefaultShadowAlgorithmStatement(true), currentConfig); - executor.checkBeforeUpdate(database, new DropDefaultShadowAlgorithmStatement(true), null); + executor.checkBeforeUpdate(new DropDefaultShadowAlgorithmStatement(true), currentConfig); + executor.checkBeforeUpdate(new DropDefaultShadowAlgorithmStatement(true), null); } @Test @@ -62,7 +65,7 @@ void assertUpdate() { ruleConfig.setDefaultShadowAlgorithmName("default"); ruleConfig.getShadowAlgorithms().put(ruleConfig.getDefaultShadowAlgorithmName(), mock(AlgorithmConfiguration.class)); DropDefaultShadowAlgorithmStatement statement = new DropDefaultShadowAlgorithmStatement(false); - executor.checkBeforeUpdate(database, new DropDefaultShadowAlgorithmStatement(true), ruleConfig); + executor.checkBeforeUpdate(new DropDefaultShadowAlgorithmStatement(true), ruleConfig); assertTrue(executor.hasAnyOneToBeDropped(statement, ruleConfig)); executor.updateCurrentRuleConfiguration(statement, ruleConfig); assertNull(ruleConfig.getDefaultShadowAlgorithmName()); diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowAlgorithmStatementUpdaterTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowAlgorithmStatementUpdaterTest.java index 18a247f140294..727f58142aeeb 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowAlgorithmStatementUpdaterTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowAlgorithmStatementUpdaterTest.java @@ -23,35 +23,34 @@ import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.distsql.handler.update.DropShadowAlgorithmExecutor; import org.apache.shardingsphere.shadow.distsql.statement.DropShadowAlgorithmStatement; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.Arrays; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -@ExtendWith(MockitoExtension.class) class DropShadowAlgorithmStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - private final DropShadowAlgorithmExecutor executor = new DropShadowAlgorithmExecutor(); + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } + @Test void assertExecuteWithoutAlgorithmNameInMetaData() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("ruleSegment"), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("ruleSegment"), null)); } @Test void assertExecuteWithIfExists() { DropShadowAlgorithmStatement sqlStatement = createSQLStatement(true, "ruleSegment"); - executor.checkBeforeUpdate(database, sqlStatement, mock(ShadowRuleConfiguration.class)); + executor.checkBeforeUpdate(sqlStatement, mock(ShadowRuleConfiguration.class)); } @Test @@ -59,7 +58,7 @@ void assertUpdate() { DropShadowAlgorithmStatement sqlStatement = createSQLStatement("shadow_algorithm"); ShadowRuleConfiguration ruleConfig = new ShadowRuleConfiguration(); ruleConfig.getShadowAlgorithms().put("shadow_algorithm", new AlgorithmConfiguration("type", null)); - executor.checkBeforeUpdate(database, sqlStatement, ruleConfig); + executor.checkBeforeUpdate(sqlStatement, ruleConfig); executor.updateCurrentRuleConfiguration(sqlStatement, ruleConfig); assertTrue(ruleConfig.getShadowAlgorithms().isEmpty()); } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleStatementUpdaterTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleStatementUpdaterTest.java index 08e8f3b0d2158..85a47dcc266c6 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleStatementUpdaterTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleStatementUpdaterTest.java @@ -25,11 +25,8 @@ import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration; import org.apache.shardingsphere.shadow.distsql.handler.update.DropShadowRuleExecutor; import org.apache.shardingsphere.shadow.distsql.statement.DropShadowRuleStatement; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.ArrayList; import java.util.Arrays; @@ -40,36 +37,39 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -@ExtendWith(MockitoExtension.class) class DropShadowRuleStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; + private final DropShadowRuleExecutor executor = new DropShadowRuleExecutor(); + + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } @Test void assertCheckWithNullConfiguration() { - assertThrows(MissingRequiredRuleException.class, () -> new DropShadowRuleExecutor().checkBeforeUpdate(database, createSQLStatement("anyRuleName"), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("anyRuleName"), null)); } @Test void assertCheckWithRuleNotExisted() { assertThrows(MissingRequiredRuleException.class, - () -> new DropShadowRuleExecutor().checkBeforeUpdate(database, createSQLStatement("notExistedRuleName"), mock(ShadowRuleConfiguration.class))); + () -> executor.checkBeforeUpdate(createSQLStatement("notExistedRuleName"), mock(ShadowRuleConfiguration.class))); } @Test void assertCheckWithIfExists() { - new DropShadowRuleExecutor().checkBeforeUpdate(database, createSQLStatement(true, "notExistedRuleName"), mock(ShadowRuleConfiguration.class)); + executor.checkBeforeUpdate(createSQLStatement(true, "notExistedRuleName"), mock(ShadowRuleConfiguration.class)); } @Test void assertUpdateCurrentRuleConfigurationWithUnusedAlgorithms() { DropShadowRuleStatement sqlStatement = createSQLStatement("shadow_group"); ShadowRuleConfiguration ruleConfig = createCurrentRuleConfiguration(); - DropShadowRuleExecutor executor = new DropShadowRuleExecutor(); - executor.checkBeforeUpdate(database, sqlStatement, ruleConfig); + executor.checkBeforeUpdate(sqlStatement, ruleConfig); assertTrue(executor.updateCurrentRuleConfiguration(sqlStatement, ruleConfig)); assertTrue(ruleConfig.getDataSources().isEmpty()); assertTrue(ruleConfig.getTables().isEmpty()); @@ -80,8 +80,7 @@ void assertUpdateCurrentRuleConfigurationWithUnusedAlgorithms() { void assertUpdateMultipleCurrentRuleConfigurationWithInUsedAlgorithms() { DropShadowRuleStatement sqlStatement = createSQLStatement("shadow_group"); ShadowRuleConfiguration ruleConfig = createMultipleCurrentRuleConfiguration(); - DropShadowRuleExecutor executor = new DropShadowRuleExecutor(); - executor.checkBeforeUpdate(database, sqlStatement, ruleConfig); + executor.checkBeforeUpdate(sqlStatement, ruleConfig); assertTrue(executor.updateCurrentRuleConfiguration(sqlStatement, ruleConfig)); assertTrue(ruleConfig.getDataSources().isEmpty()); assertTrue(ruleConfig.getTables().isEmpty()); diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterDefaultShardingStrategyExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterDefaultShardingStrategyExecutor.java index 5d9d321f74726..62ea9c0c1870d 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterDefaultShardingStrategyExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterDefaultShardingStrategyExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; @@ -38,20 +39,22 @@ /** * Alter default sharding strategy executor. */ +@Setter public final class AlterDefaultShardingStrategyExecutor implements DatabaseRuleAlterExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final AlterDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - String databaseName = database.getName(); - checkCurrentRuleConfiguration(databaseName, currentRuleConfig); + public void checkBeforeUpdate(final AlterDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + checkCurrentRuleConfiguration(currentRuleConfig); if (!"none".equalsIgnoreCase(sqlStatement.getStrategyType())) { checkAlgorithm(sqlStatement); } - checkExist(databaseName, sqlStatement, currentRuleConfig); + checkExist(sqlStatement, currentRuleConfig); } - private void checkCurrentRuleConfiguration(final String databaseName, final ShardingRuleConfiguration currentRuleConfig) { - ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Sharding", databaseName)); + private void checkCurrentRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig) { + ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Sharding", database.getName())); } private void checkAlgorithm(final AlterDefaultShardingStrategyStatement sqlStatement) { @@ -65,10 +68,10 @@ private boolean isAlgorithmDefinitionExists(final AlterDefaultShardingStrategySt return null != sqlStatement.getAlgorithmSegment(); } - private void checkExist(final String databaseName, final AlterDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkExist(final AlterDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { Optional strategyConfig = getStrategyConfiguration(currentRuleConfig, sqlStatement.getDefaultType()); ShardingSpherePreconditions.checkState(strategyConfig.isPresent(), - () -> new MissingRequiredRuleException(String.format("Default sharding %s strategy", sqlStatement.getDefaultType().toLowerCase()), databaseName)); + () -> new MissingRequiredRuleException(String.format("Default sharding %s strategy", sqlStatement.getDefaultType().toLowerCase()), database.getName())); } private Optional getStrategyConfiguration(final ShardingRuleConfiguration currentRuleConfig, final String type) { diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableReferenceRuleExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableReferenceRuleExecutor.java index 1c2979160f737..db3055d9344a4 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableReferenceRuleExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableReferenceRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; import org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; @@ -40,33 +41,44 @@ /** * Alter sharding table reference rule executor. */ +@Setter public final class AlterShardingTableReferenceRuleExecutor implements DatabaseRuleAlterExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - String databaseName = database.getName(); - checkCurrentRuleConfiguration(databaseName, currentRuleConfig); - checkToBeAlteredRulesExisted(databaseName, sqlStatement, currentRuleConfig); - checkDuplicatedTablesInShardingTableReferenceRules(databaseName, sqlStatement, currentRuleConfig); - checkToBeReferencedShardingTablesExisted(databaseName, sqlStatement, currentRuleConfig); + public void checkBeforeUpdate(final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + checkCurrentRuleConfiguration(currentRuleConfig); + checkToBeAlteredRulesExisted(sqlStatement, currentRuleConfig); + checkDuplicatedTablesInShardingTableReferenceRules(sqlStatement, currentRuleConfig); + checkToBeReferencedShardingTablesExisted(sqlStatement, currentRuleConfig); checkShardingTableReferenceRulesValid(sqlStatement, currentRuleConfig); } - private void checkCurrentRuleConfiguration(final String databaseName, final ShardingRuleConfiguration currentRuleConfig) { - ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Sharding", databaseName)); + private void checkCurrentRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig) { + ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Sharding", database.getName())); } - private void checkToBeAlteredRulesExisted(final String databaseName, final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkToBeAlteredRulesExisted(final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { Collection currentRuleNames = currentRuleConfig.getBindingTableGroups().stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toSet()); Collection notExistedRuleNames = sqlStatement.getRules().stream().map(TableReferenceRuleSegment::getName).filter(each -> !currentRuleNames.contains(each)).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(notExistedRuleNames.isEmpty(), () -> new MissingRequiredRuleException("Sharding table reference", databaseName, notExistedRuleNames)); + ShardingSpherePreconditions.checkState(notExistedRuleNames.isEmpty(), () -> new MissingRequiredRuleException("Sharding table reference", database.getName(), notExistedRuleNames)); } - private void checkDuplicatedTablesInShardingTableReferenceRules(final String databaseName, - final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkDuplicatedTablesInShardingTableReferenceRules(final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { Collection currentReferencedTableNames = getReferencedTableNames(currentRuleConfig, getToBeAlteredRuleNames(sqlStatement)); Collection duplicatedTableNames = sqlStatement.getTableNames().stream().filter(currentReferencedTableNames::contains).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(duplicatedTableNames.isEmpty(), () -> new DuplicateRuleException("sharding table reference", databaseName, duplicatedTableNames)); + ShardingSpherePreconditions.checkState(duplicatedTableNames.isEmpty(), () -> new DuplicateRuleException("sharding table reference", database.getName(), duplicatedTableNames)); + } + + private Collection getReferencedTableNames(final ShardingRuleConfiguration currentRuleConfig, final Collection getToBeAlteredRuleNames) { + Collection result = new HashSet<>(); + currentRuleConfig.getBindingTableGroups().forEach(each -> { + if (!getToBeAlteredRuleNames.contains(each.getName())) { + result.addAll(Arrays.stream(each.getReference().split(",")).map(String::trim).collect(Collectors.toSet())); + } + }); + return result; } private Collection getToBeAlteredRuleNames(final AlterShardingTableReferenceRuleStatement sqlStatement) { @@ -77,27 +89,10 @@ private Collection getToBeAlteredRuleNames(final ShardingRuleConfigurati return ruleConfig.getBindingTableGroups().stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toSet()); } - private void checkToBeReferencedShardingTablesExisted(final String databaseName, final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkToBeReferencedShardingTablesExisted(final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { Collection existedShardingTables = getCurrentShardingTables(currentRuleConfig); Collection notExistedShardingTables = sqlStatement.getTableNames().stream().filter(each -> !containsIgnoreCase(existedShardingTables, each)).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(notExistedShardingTables.isEmpty(), () -> new MissingRequiredRuleException("Sharding", databaseName, notExistedShardingTables)); - } - - private void checkShardingTableReferenceRulesValid(final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection toBeAlteredShardingTableReferenceRules = buildToBeAlteredRuleConfiguration(currentRuleConfig, sqlStatement).getBindingTableGroups(); - Collection ruleNames = toBeAlteredShardingTableReferenceRules.stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(ShardingTableRuleStatementChecker.isValidBindingTableGroups(toBeAlteredShardingTableReferenceRules, currentRuleConfig), - () -> new InvalidRuleConfigurationException("sharding table", ruleNames, Collections.singleton("invalid sharding table reference."))); - } - - private Collection getReferencedTableNames(final ShardingRuleConfiguration currentRuleConfig, final Collection getToBeAlteredRuleNames) { - Collection result = new HashSet<>(); - currentRuleConfig.getBindingTableGroups().forEach(each -> { - if (!getToBeAlteredRuleNames.contains(each.getName())) { - result.addAll(Arrays.stream(each.getReference().split(",")).map(String::trim).collect(Collectors.toSet())); - } - }); - return result; + ShardingSpherePreconditions.checkState(notExistedShardingTables.isEmpty(), () -> new MissingRequiredRuleException("Sharding", database.getName(), notExistedShardingTables)); } private Collection getCurrentShardingTables(final ShardingRuleConfiguration currentRuleConfig) { @@ -111,6 +106,13 @@ private boolean containsIgnoreCase(final Collection currentRules, final return currentRules.stream().anyMatch(each -> each.equalsIgnoreCase(ruleName)); } + private void checkShardingTableReferenceRulesValid(final AlterShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + Collection toBeAlteredShardingTableReferenceRules = buildToBeAlteredRuleConfiguration(currentRuleConfig, sqlStatement).getBindingTableGroups(); + Collection ruleNames = toBeAlteredShardingTableReferenceRules.stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toList()); + ShardingSpherePreconditions.checkState(ShardingTableRuleStatementChecker.isValidBindingTableGroups(toBeAlteredShardingTableReferenceRules, currentRuleConfig), + () -> new InvalidRuleConfigurationException("sharding table", ruleNames, Collections.singleton("invalid sharding table reference."))); + } + @Override public ShardingRuleConfiguration buildToBeAlteredRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig, final AlterShardingTableReferenceRuleStatement sqlStatement) { ShardingRuleConfiguration result = new ShardingRuleConfiguration(); diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleExecutor.java index 405eb0e82b7cd..dfc7d365c78a8 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/AlterShardingTableRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; @@ -34,10 +35,13 @@ /** * Alter sharding table rule executor. */ +@Setter public final class AlterShardingTableRuleExecutor implements DatabaseRuleAlterExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final AlterShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final AlterShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Sharding", database.getName())); ShardingTableRuleStatementChecker.checkAlteration(database, sqlStatement.getRules(), currentRuleConfig); } diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateDefaultShardingStrategyExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateDefaultShardingStrategyExecutor.java index 8a2e9d662e25e..7f624a0a8a5dd 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateDefaultShardingStrategyExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateDefaultShardingStrategyExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; @@ -38,16 +39,18 @@ /** * Create default sharding strategy executor. */ +@Setter public final class CreateDefaultShardingStrategyExecutor implements DatabaseRuleCreateExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final CreateDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - String databaseName = database.getName(); + public void checkBeforeUpdate(final CreateDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (!"none".equalsIgnoreCase(sqlStatement.getStrategyType())) { checkAlgorithm(sqlStatement); } if (!sqlStatement.isIfNotExists()) { - checkExist(databaseName, sqlStatement, currentRuleConfig); + checkExist(sqlStatement, currentRuleConfig); } } @@ -62,13 +65,13 @@ private boolean isAlgorithmDefinitionExists(final CreateDefaultShardingStrategyS return null != sqlStatement.getAlgorithmSegment(); } - private void checkExist(final String databaseName, final CreateDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkExist(final CreateDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (null == currentRuleConfig) { return; } Optional strategyConfig = getStrategyConfiguration(currentRuleConfig, sqlStatement.getDefaultType()); ShardingSpherePreconditions.checkState(!strategyConfig.isPresent(), - () -> new DuplicateRuleException(String.format("default sharding %s strategy", sqlStatement.getDefaultType().toLowerCase()), databaseName)); + () -> new DuplicateRuleException(String.format("default sharding %s strategy", sqlStatement.getDefaultType().toLowerCase()), database.getName())); } private Optional getStrategyConfiguration(final ShardingRuleConfiguration currentRuleConfig, final String type) { diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableReferenceRuleExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableReferenceRuleExecutor.java index 5cd8fcf85a3ba..b68fd66379ff0 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableReferenceRuleExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableReferenceRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; import org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; @@ -40,62 +41,41 @@ /** * Create sharding table reference rule executor. */ +@Setter public final class CreateShardingTableReferenceRuleExecutor implements DatabaseRuleCreateExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - String databaseName = database.getName(); - checkCurrentRuleConfiguration(databaseName, currentRuleConfig); + public void checkBeforeUpdate(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + checkCurrentRuleConfiguration(currentRuleConfig); if (!sqlStatement.isIfNotExists()) { - checkDuplicatedRuleNames(databaseName, sqlStatement, currentRuleConfig); + checkDuplicatedRuleNames(sqlStatement, currentRuleConfig); } - checkDuplicatedTablesInShardingTableReferenceRules(databaseName, sqlStatement, currentRuleConfig); - checkToBeReferencedShardingTablesExisted(databaseName, sqlStatement, currentRuleConfig); + checkDuplicatedTablesInShardingTableReferenceRules(sqlStatement, currentRuleConfig); + checkToBeReferencedShardingTablesExisted(sqlStatement, currentRuleConfig); checkShardingTableReferenceRulesValid(sqlStatement, currentRuleConfig); } - @Override - public ShardingRuleConfiguration buildToBeCreatedRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig, final CreateShardingTableReferenceRuleStatement sqlStatement) { - Collection segments = sqlStatement.getRules(); - if (sqlStatement.isIfNotExists()) { - Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig); - segments.removeIf(each -> duplicatedRuleNames.contains(each.getName())); - } - ShardingRuleConfiguration result = new ShardingRuleConfiguration(); - segments.forEach(each -> result.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration(each.getName(), each.getReference()))); - return result; - } - - @Override - public void updateCurrentRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig, final ShardingRuleConfiguration toBeCreatedRuleConfig) { - currentRuleConfig.getBindingTableGroups().addAll(toBeCreatedRuleConfig.getBindingTableGroups()); - } - - private Collection getDuplicatedRuleNames(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { - Collection currentRuleNames = currentRuleConfig.getBindingTableGroups().stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toSet()); - return sqlStatement.getRules().stream().map(TableReferenceRuleSegment::getName).filter(currentRuleNames::contains).collect(Collectors.toSet()); - } - - private void checkCurrentRuleConfiguration(final String databaseName, final ShardingRuleConfiguration currentRuleConfig) { - ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Sharding", databaseName)); + private void checkCurrentRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig) { + ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Sharding", database.getName())); } - private void checkDuplicatedRuleNames(final String databaseName, final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkDuplicatedRuleNames(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig); - ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("sharding table reference", databaseName, duplicatedRuleNames)); + ShardingSpherePreconditions.checkState(duplicatedRuleNames.isEmpty(), () -> new DuplicateRuleException("sharding table reference", database.getName(), duplicatedRuleNames)); } - private void checkDuplicatedTablesInShardingTableReferenceRules(final String databaseName, final CreateShardingTableReferenceRuleStatement sqlStatement, - final ShardingRuleConfiguration currentRuleConfig) { + private void checkDuplicatedTablesInShardingTableReferenceRules(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { Collection currentReferencedTableNames = getReferencedTableNames(currentRuleConfig); Collection duplicatedTableNames = sqlStatement.getTableNames().stream().filter(currentReferencedTableNames::contains).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(duplicatedTableNames.isEmpty(), () -> new DuplicateRuleException("sharding table reference", databaseName, duplicatedTableNames)); + ShardingSpherePreconditions.checkState(duplicatedTableNames.isEmpty(), () -> new DuplicateRuleException("sharding table reference", database.getName(), duplicatedTableNames)); } - private void checkToBeReferencedShardingTablesExisted(final String databaseName, final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkToBeReferencedShardingTablesExisted(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { Collection existedShardingTables = getCurrentLogicTables(currentRuleConfig); Collection notExistedShardingTables = sqlStatement.getTableNames().stream().filter(each -> !containsIgnoreCase(existedShardingTables, each)).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(notExistedShardingTables.isEmpty(), () -> new MissingRequiredRuleException("Sharding", databaseName, notExistedShardingTables)); + ShardingSpherePreconditions.checkState(notExistedShardingTables.isEmpty(), () -> new MissingRequiredRuleException("Sharding", database.getName(), notExistedShardingTables)); } private Collection getCurrentLogicTables(final ShardingRuleConfiguration currentRuleConfig) { @@ -120,6 +100,28 @@ private boolean containsIgnoreCase(final Collection currentRules, final return currentRules.stream().anyMatch(each -> each.equalsIgnoreCase(ruleName)); } + @Override + public ShardingRuleConfiguration buildToBeCreatedRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig, final CreateShardingTableReferenceRuleStatement sqlStatement) { + Collection segments = sqlStatement.getRules(); + if (sqlStatement.isIfNotExists()) { + Collection duplicatedRuleNames = getDuplicatedRuleNames(sqlStatement, currentRuleConfig); + segments.removeIf(each -> duplicatedRuleNames.contains(each.getName())); + } + ShardingRuleConfiguration result = new ShardingRuleConfiguration(); + segments.forEach(each -> result.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration(each.getName(), each.getReference()))); + return result; + } + + private Collection getDuplicatedRuleNames(final CreateShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + Collection currentRuleNames = currentRuleConfig.getBindingTableGroups().stream().map(ShardingTableReferenceRuleConfiguration::getName).collect(Collectors.toSet()); + return sqlStatement.getRules().stream().map(TableReferenceRuleSegment::getName).filter(currentRuleNames::contains).collect(Collectors.toSet()); + } + + @Override + public void updateCurrentRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig, final ShardingRuleConfiguration toBeCreatedRuleConfig) { + currentRuleConfig.getBindingTableGroups().addAll(toBeCreatedRuleConfig.getBindingTableGroups()); + } + @Override public Class getRuleConfigurationClass() { return ShardingRuleConfiguration.class; diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableRuleExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableRuleExecutor.java index cf13eaff79eb2..2ff822e737268 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableRuleExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/CreateShardingTableRuleExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; @@ -35,10 +36,13 @@ /** * Create sharding table rule executor. */ +@Setter public final class CreateShardingTableRuleExecutor implements DatabaseRuleCreateExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final CreateShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final CreateShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { ShardingTableRuleStatementChecker.checkCreation(database, sqlStatement.getRules(), sqlStatement.isIfNotExists(), currentRuleConfig); } diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyExecutor.java index 10fc1432f7b7f..3cc14be1bd58b 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropDefaultShardingStrategyExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; @@ -31,26 +32,31 @@ /** * Drop default sharding strategy executor. */ +@Setter public final class DropDefaultShardingStrategyExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropDefaultShardingStrategyStatement sqlStatement, - final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (!isExistRuleConfig(currentRuleConfig) && sqlStatement.isIfExists()) { return; } - String databaseName = database.getName(); - checkCurrentRuleConfiguration(databaseName, currentRuleConfig); - checkExist(databaseName, sqlStatement, currentRuleConfig); + checkCurrentRuleConfiguration(currentRuleConfig); + checkExist(sqlStatement, currentRuleConfig); + } + + private void checkCurrentRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig) { + ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Sharding", database.getName())); } - private void checkExist(final String databaseName, final DropDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkExist(final DropDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists()) { return; } Optional strategyConfig = getStrategyConfiguration(currentRuleConfig, sqlStatement.getDefaultType()); ShardingSpherePreconditions.checkState(strategyConfig.isPresent(), () -> new MissingRequiredRuleException( - String.format("Default sharding %s strategy", sqlStatement.getDefaultType().toLowerCase()), databaseName)); + String.format("Default sharding %s strategy", sqlStatement.getDefaultType().toLowerCase()), database.getName())); } private Optional getStrategyConfiguration(final ShardingRuleConfiguration currentRuleConfig, final String type) { @@ -60,10 +66,6 @@ private Optional getStrategyConfiguration(final S return Optional.ofNullable(result); } - private void checkCurrentRuleConfiguration(final String databaseName, final ShardingRuleConfiguration currentRuleConfig) { - ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Sharding", databaseName)); - } - @Override public boolean hasAnyOneToBeDropped(final DropDefaultShardingStrategyStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (sqlStatement.getDefaultType().equalsIgnoreCase(ShardingStrategyLevelType.TABLE.name())) { diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmExecutor.java index 51c0cec53b353..90afda28f08c6 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAlgorithmExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.algorithm.AlgorithmInUsedException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; @@ -33,38 +34,40 @@ /** * Drop sharding algorithm executor. */ +@Setter public final class DropShardingAlgorithmExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (null == currentRuleConfig && sqlStatement.isIfExists()) { return; } - String databaseName = database.getName(); - checkCurrentRuleConfiguration(databaseName, currentRuleConfig); - checkToBeDroppedShardingAlgorithms(databaseName, sqlStatement, currentRuleConfig); - checkShardingAlgorithmsInUsed(databaseName, sqlStatement, currentRuleConfig); + checkCurrentRuleConfiguration(currentRuleConfig); + checkToBeDroppedShardingAlgorithms(sqlStatement, currentRuleConfig); + checkShardingAlgorithmsInUsed(sqlStatement, currentRuleConfig); } - private void checkCurrentRuleConfiguration(final String databaseName, final ShardingRuleConfiguration currentRuleConfig) { - ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredAlgorithmException("Sharding", databaseName)); + private void checkCurrentRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig) { + ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredAlgorithmException("Sharding", database.getName())); } - private void checkToBeDroppedShardingAlgorithms(final String databaseName, final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkToBeDroppedShardingAlgorithms(final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists()) { return; } Collection currentShardingAlgorithms = getCurrentShardingAlgorithms(currentRuleConfig); Collection notExistedAlgorithms = sqlStatement.getNames().stream().filter(each -> !currentShardingAlgorithms.contains(each)).collect(Collectors.toList()); if (!notExistedAlgorithms.isEmpty()) { - throw new MissingRequiredAlgorithmException(databaseName, notExistedAlgorithms); + throw new MissingRequiredAlgorithmException(database.getName(), notExistedAlgorithms); } } - private void checkShardingAlgorithmsInUsed(final String databaseName, final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkShardingAlgorithmsInUsed(final DropShardingAlgorithmStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { Collection allInUsed = getAllOfAlgorithmsInUsed(currentRuleConfig); Collection usedAlgorithms = sqlStatement.getNames().stream().filter(allInUsed::contains).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(usedAlgorithms.isEmpty(), () -> new AlgorithmInUsedException("Sharding", databaseName, usedAlgorithms)); + ShardingSpherePreconditions.checkState(usedAlgorithms.isEmpty(), () -> new AlgorithmInUsedException("Sharding", database.getName(), usedAlgorithms)); } private Collection getAllOfAlgorithmsInUsed(final ShardingRuleConfiguration shardingRuleConfig) { diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAuditorExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAuditorExecutor.java index e11e4152faa47..ad4c92041875b 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAuditorExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingAuditorExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.algorithm.AlgorithmInUsedException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; @@ -34,31 +35,33 @@ /** * Drop sharding auditor statement executor. */ +@Setter public final class DropShardingAuditorExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropShardingAuditorStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShardingAuditorStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (null == currentRuleConfig && sqlStatement.isIfExists()) { return; } - String databaseName = database.getName(); Collection auditorNames = new LinkedList<>(sqlStatement.getNames()); - checkExist(databaseName, auditorNames, currentRuleConfig, sqlStatement); - checkInUsed(databaseName, auditorNames, currentRuleConfig); + checkExist(auditorNames, currentRuleConfig, sqlStatement); + checkInUsed(auditorNames, currentRuleConfig); } - private void checkExist(final String databaseName, final Collection auditorNames, final ShardingRuleConfiguration currentRuleConfig, final DropShardingAuditorStatement sqlStatement) { + private void checkExist(final Collection auditorNames, final ShardingRuleConfiguration currentRuleConfig, final DropShardingAuditorStatement sqlStatement) { if (sqlStatement.isIfExists()) { return; } Collection notExistAuditors = auditorNames.stream().filter(each -> !currentRuleConfig.getAuditors().containsKey(each)).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(notExistAuditors.isEmpty(), () -> new MissingRequiredAlgorithmException("Sharding auditor", databaseName, notExistAuditors)); + ShardingSpherePreconditions.checkState(notExistAuditors.isEmpty(), () -> new MissingRequiredAlgorithmException("Sharding auditor", database.getName(), notExistAuditors)); } - private void checkInUsed(final String databaseName, final Collection auditorNames, final ShardingRuleConfiguration currentRuleConfig) { + private void checkInUsed(final Collection auditorNames, final ShardingRuleConfiguration currentRuleConfig) { Collection usedAuditors = getUsedAuditors(currentRuleConfig); Collection inUsedNames = auditorNames.stream().filter(usedAuditors::contains).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(inUsedNames.isEmpty(), () -> new AlgorithmInUsedException("Sharding auditor", databaseName, inUsedNames)); + ShardingSpherePreconditions.checkState(inUsedNames.isEmpty(), () -> new AlgorithmInUsedException("Sharding auditor", database.getName(), inUsedNames)); } private Collection getUsedAuditors(final ShardingRuleConfiguration shardingRuleConfig) { diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingKeyGeneratorExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingKeyGeneratorExecutor.java index 01eaa64509ba2..4cfbedd013a96 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingKeyGeneratorExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingKeyGeneratorExecutor.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.sharding.distsql.handler.update; import com.google.common.base.Strings; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.algorithm.AlgorithmInUsedException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; @@ -35,32 +36,33 @@ /** * Drop sharding key generator executor. */ +@Setter public final class DropShardingKeyGeneratorExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropShardingKeyGeneratorStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShardingKeyGeneratorStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (null == currentRuleConfig && sqlStatement.isIfExists()) { return; } - String databaseName = database.getName(); Collection keyGeneratorNames = new LinkedList<>(sqlStatement.getNames()); - checkExist(databaseName, keyGeneratorNames, currentRuleConfig, sqlStatement); - checkInUsed(databaseName, keyGeneratorNames, currentRuleConfig); + checkExist(keyGeneratorNames, currentRuleConfig, sqlStatement); + checkInUsed(keyGeneratorNames, currentRuleConfig); } - private void checkExist(final String databaseName, final Collection keyGeneratorNames, - final ShardingRuleConfiguration currentRuleConfig, final DropShardingKeyGeneratorStatement sqlStatement) { + private void checkExist(final Collection keyGeneratorNames, final ShardingRuleConfiguration currentRuleConfig, final DropShardingKeyGeneratorStatement sqlStatement) { if (sqlStatement.isIfExists()) { return; } Collection notExistKeyGenerators = keyGeneratorNames.stream().filter(each -> !currentRuleConfig.getKeyGenerators().containsKey(each)).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(notExistKeyGenerators.isEmpty(), () -> new MissingRequiredAlgorithmException("Key generator", databaseName, notExistKeyGenerators)); + ShardingSpherePreconditions.checkState(notExistKeyGenerators.isEmpty(), () -> new MissingRequiredAlgorithmException("Key generator", database.getName(), notExistKeyGenerators)); } - private void checkInUsed(final String databaseName, final Collection keyGeneratorNames, final ShardingRuleConfiguration currentRuleConfig) { + private void checkInUsed(final Collection keyGeneratorNames, final ShardingRuleConfiguration currentRuleConfig) { Collection usedKeyGenerators = getUsedKeyGenerators(currentRuleConfig); Collection inUsedNames = keyGeneratorNames.stream().filter(usedKeyGenerators::contains).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(inUsedNames.isEmpty(), () -> new AlgorithmInUsedException("Key generator", databaseName, inUsedNames)); + ShardingSpherePreconditions.checkState(inUsedNames.isEmpty(), () -> new AlgorithmInUsedException("Key generator", database.getName(), inUsedNames)); } private Collection getUsedKeyGenerators(final ShardingRuleConfiguration shardingRuleConfig) { diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableReferenceExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableReferenceExecutor.java index 09db910461b32..fb7fa4cf1531a 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableReferenceExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableReferenceExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; @@ -31,31 +32,32 @@ /** * Drop sharding table reference executor. */ +@Setter public final class DropShardingTableReferenceExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (!isExistRuleConfig(currentRuleConfig) && sqlStatement.isIfExists()) { return; } - String databaseName = database.getName(); - checkCurrentRuleConfiguration(databaseName, currentRuleConfig); - checkToBeDroppedShardingTableReferenceRules(databaseName, sqlStatement, currentRuleConfig); + checkCurrentRuleConfiguration(currentRuleConfig); + checkToBeDroppedShardingTableReferenceRules(sqlStatement, currentRuleConfig); } - private void checkCurrentRuleConfiguration(final String databaseName, final ShardingRuleConfiguration currentRuleConfig) { + private void checkCurrentRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig) { ShardingSpherePreconditions.checkState(null != currentRuleConfig && !currentRuleConfig.getBindingTableGroups().isEmpty(), - () -> new MissingRequiredRuleException("Sharding table reference", databaseName)); + () -> new MissingRequiredRuleException("Sharding table reference", database.getName())); } - private void checkToBeDroppedShardingTableReferenceRules(final String databaseName, - final DropShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkToBeDroppedShardingTableReferenceRules(final DropShardingTableReferenceRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists()) { return; } Collection currentRuleNames = getCurrentShardingTableReferenceRuleNames(currentRuleConfig); Collection notExistedRuleNames = sqlStatement.getNames().stream().filter(each -> !containsIgnoreCase(currentRuleNames, each)).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(notExistedRuleNames.isEmpty(), () -> new MissingRequiredRuleException("Sharding table reference", databaseName, notExistedRuleNames)); + ShardingSpherePreconditions.checkState(notExistedRuleNames.isEmpty(), () -> new MissingRequiredRuleException("Sharding table reference", database.getName(), notExistedRuleNames)); } private Collection getCurrentShardingTableReferenceRuleNames(final ShardingRuleConfiguration currentRuleConfig) { diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleExecutor.java index a5679475b2343..fa23e557a68e9 100644 --- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleExecutor.java +++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleExecutor.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.sharding.distsql.handler.update; import com.google.common.base.Splitter; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.exception.rule.RuleInUsedException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleDropExecutor; @@ -38,35 +39,37 @@ /** * Drop sharding table rule executor. */ +@Setter public final class DropShardingTableRuleExecutor implements DatabaseRuleDropExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final DropShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final DropShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (!isExistRuleConfig(currentRuleConfig) && sqlStatement.isIfExists()) { return; } - String databaseName = database.getName(); - checkCurrentRuleConfiguration(databaseName, currentRuleConfig); - checkToBeDroppedShardingTableNames(databaseName, sqlStatement, currentRuleConfig); - checkBindingTables(databaseName, sqlStatement, currentRuleConfig); + checkCurrentRuleConfiguration(currentRuleConfig); + checkToBeDroppedShardingTableNames(sqlStatement, currentRuleConfig); + checkBindingTables(sqlStatement, currentRuleConfig); } - private void checkCurrentRuleConfiguration(final String databaseName, final ShardingRuleConfiguration currentRuleConfig) { - ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Sharding", databaseName)); + private void checkCurrentRuleConfiguration(final ShardingRuleConfiguration currentRuleConfig) { + ShardingSpherePreconditions.checkNotNull(currentRuleConfig, () -> new MissingRequiredRuleException("Sharding", database.getName())); } private Collection getToBeDroppedShardingTableNames(final DropShardingTableRuleStatement sqlStatement) { return sqlStatement.getTableNames().stream().map(each -> each.getIdentifier().getValue()).collect(Collectors.toList()); } - private void checkToBeDroppedShardingTableNames(final String databaseName, final DropShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkToBeDroppedShardingTableNames(final DropShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists()) { return; } Collection currentShardingTableNames = getCurrentShardingTableNames(currentRuleConfig); Collection notExistedTableNames = getToBeDroppedShardingTableNames(sqlStatement).stream().filter(each -> !containsIgnoreCase(currentShardingTableNames, each)).collect(Collectors.toList()); - ShardingSpherePreconditions.checkState(notExistedTableNames.isEmpty(), () -> new MissingRequiredRuleException("sharding", databaseName, notExistedTableNames)); + ShardingSpherePreconditions.checkState(notExistedTableNames.isEmpty(), () -> new MissingRequiredRuleException("sharding", database.getName(), notExistedTableNames)); } private boolean containsIgnoreCase(final Collection collection, final String str) { @@ -80,11 +83,11 @@ private Collection getCurrentShardingTableNames(final ShardingRuleConfig return result; } - private void checkBindingTables(final String databaseName, final DropShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { + private void checkBindingTables(final DropShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration currentRuleConfig) { Collection bindingTables = getBindingTables(currentRuleConfig); Collection usedTableNames = getToBeDroppedShardingTableNames(sqlStatement).stream().filter(each -> containsIgnoreCase(bindingTables, each)).collect(Collectors.toList()); if (!usedTableNames.isEmpty()) { - throw new RuleInUsedException("Sharding", databaseName, usedTableNames, "sharding table reference"); + throw new RuleInUsedException("Sharding", database.getName(), usedTableNames, "sharding table reference"); } } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterDefaultShardingStrategyStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterDefaultShardingStrategyStatementUpdaterTest.java index dfc0b62b960de..9d280463001d0 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterDefaultShardingStrategyStatementUpdaterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterDefaultShardingStrategyStatementUpdaterTest.java @@ -31,41 +31,37 @@ import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.Properties; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@ExtendWith(MockitoExtension.class) class AlterDefaultShardingStrategyStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - private final AlterDefaultShardingStrategyExecutor executor = new AlterDefaultShardingStrategyExecutor(); @BeforeEach void before() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("test"); + executor.setDatabase(database); } @Test void assertExecuteWithInvalidStrategyType() { assertThrows(InvalidAlgorithmConfigurationException.class, - () -> executor.checkBeforeUpdate(database, new AlterDefaultShardingStrategyStatement("TABLE", "invalidType", null, null), new ShardingRuleConfiguration())); + () -> executor.checkBeforeUpdate(new AlterDefaultShardingStrategyStatement("TABLE", "invalidType", null, null), new ShardingRuleConfiguration())); } @Test void assertExecuteWithoutCurrentConfiguration() { AlterDefaultShardingStrategyStatement statement = new AlterDefaultShardingStrategyStatement("TABLE", "standard", "order_id", null); - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, statement, null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(statement, null)); } @Test @@ -74,7 +70,7 @@ void assertExecuteWithNotExist() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("not_exist_algorithm", null); - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(database, statement, currentRuleConfig)); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(statement, currentRuleConfig)); } @Test @@ -83,7 +79,7 @@ void assertExecuteWithUnmatchedStrategy() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(database, statement, currentRuleConfig)); + assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(statement, currentRuleConfig)); } @Test @@ -93,7 +89,7 @@ void assertAlterDefaultTableShardingStrategy() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); StandardShardingStrategyConfiguration defaultTableShardingStrategy = (StandardShardingStrategyConfiguration) currentRuleConfig.getDefaultTableShardingStrategy(); @@ -107,7 +103,7 @@ void assertAlterDefaultDatabaseShardingStrategy() { AlterDefaultShardingStrategyStatement statement = new AlterDefaultShardingStrategyStatement("DATABASE", "standard", "user_id", databaseAlgorithmSegment); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); StandardShardingStrategyConfiguration defaultDatabaseShardingStrategy = (StandardShardingStrategyConfiguration) currentRuleConfig.getDefaultDatabaseShardingStrategy(); @@ -121,7 +117,7 @@ void assertAlterDefaultTableShardingStrategyWithNoneShardingStrategyType() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); NoneShardingStrategyConfiguration defaultTableShardingStrategy = (NoneShardingStrategyConfiguration) currentRuleConfig.getDefaultTableShardingStrategy(); @@ -134,7 +130,7 @@ void assertAlterDefaultDatabaseShardingStrategyWithNoneShardingStrategyType() { AlterDefaultShardingStrategyStatement statement = new AlterDefaultShardingStrategyStatement("DATABASE", "none", null, null); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); NoneShardingStrategyConfiguration defaultDatabaseShardingStrategy = (NoneShardingStrategyConfiguration) currentRuleConfig.getDefaultDatabaseShardingStrategy(); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableReferenceRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableReferenceRuleStatementUpdaterTest.java index 1a5a58b0428ef..5d92f9614beaa 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableReferenceRuleStatementUpdaterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableReferenceRuleStatementUpdaterTest.java @@ -25,46 +25,45 @@ import org.apache.shardingsphere.sharding.distsql.handler.update.AlterShardingTableReferenceRuleExecutor; import org.apache.shardingsphere.sharding.distsql.segment.table.TableReferenceRuleSegment; import org.apache.shardingsphere.sharding.distsql.statement.AlterShardingTableReferenceRuleStatement; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.Collections; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; -@ExtendWith(MockitoExtension.class) class AlterShardingTableReferenceRuleStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; + private final AlterShardingTableReferenceRuleExecutor executor = new AlterShardingTableReferenceRuleExecutor(); + + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } @Test void assertCheckWithoutCurrentRuleConfig() { - assertThrows(MissingRequiredRuleException.class, - () -> new AlterShardingTableReferenceRuleExecutor().checkBeforeUpdate(database, createSQLStatement("foo", "t_order,t_order_item"), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("foo", "t_order,t_order_item"), null)); } @Test void assertCheckWithNotExistedRule() { - assertThrows(MissingRequiredRuleException.class, - () -> new AlterShardingTableReferenceRuleExecutor().checkBeforeUpdate(database, createSQLStatement("notExisted", "t_1,t_2"), createCurrentRuleConfiguration())); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("notExisted", "t_1,t_2"), createCurrentRuleConfiguration())); } @Test void assertCheckWithNotExistedTables() { - assertThrows(MissingRequiredRuleException.class, - () -> new AlterShardingTableReferenceRuleExecutor().checkBeforeUpdate(database, createSQLStatement("reference_0", "t_3,t_4"), createCurrentRuleConfiguration())); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("reference_0", "t_3,t_4"), createCurrentRuleConfiguration())); } @Test void assertUpdate() { ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); - new AlterShardingTableReferenceRuleExecutor().updateCurrentRuleConfiguration(currentRuleConfig, createToBeAlteredRuleConfig()); + executor.updateCurrentRuleConfiguration(currentRuleConfig, createToBeAlteredRuleConfig()); assertThat(currentRuleConfig.getBindingTableGroups().size(), is(1)); assertThat(currentRuleConfig.getBindingTableGroups().iterator().next().getName(), is("reference_0")); assertThat(currentRuleConfig.getBindingTableGroups().iterator().next().getReference(), is("t_order,t_order_item,t_1,t_2")); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableRuleStatementUpdaterTest.java index dfbb3964120fa..56263beeb469b 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableRuleStatementUpdaterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/AlterShardingTableRuleStatementUpdaterTest.java @@ -38,10 +38,6 @@ import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import javax.sql.DataSource; import java.util.Arrays; @@ -53,14 +49,12 @@ import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@ExtendWith(MockitoExtension.class) class AlterShardingTableRuleStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - private final ShardingRuleConfiguration currentRuleConfig = createCurrentShardingRuleConfiguration(); private final ResourceMetaData resourceMetaData = new ResourceMetaData(createDataSource()); @@ -68,16 +62,18 @@ class AlterShardingTableRuleStatementUpdaterTest { private final AlterShardingTableRuleExecutor executor = new AlterShardingTableRuleExecutor(); @BeforeEach - void before() { + void setUp() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("schema"); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.emptyList())); + executor.setDatabase(database); } @Test void assertUpdate() { AlterShardingTableRuleStatement statement = new AlterShardingTableRuleStatement(Arrays.asList(createCompleteAutoTableRule("t_order_item"), createCompleteTableRule("t_order"))); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(1)); @@ -102,7 +98,7 @@ void assertUpdate() { @Test void assertUpdateWithDifferentCase() { AlterShardingTableRuleStatement statement = new AlterShardingTableRuleStatement(Arrays.asList(createCompleteAutoTableRule("T_ORDER_ITEM"), createCompleteTableRule("T_ORDER"))); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(1)); @@ -127,7 +123,7 @@ void assertUpdateWithDifferentCase() { @Test void assertUpdateTableType() { AlterShardingTableRuleStatement statement = new AlterShardingTableRuleStatement(Arrays.asList(createCompleteAutoTableRule("t_order"), createCompleteTableRule("t_order_item"))); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeAlteredRuleConfig = executor.buildToBeAlteredRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeAlteredRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(1)); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateDefaultShardingStrategyStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateDefaultShardingStrategyStatementUpdaterTest.java index 67b74a8b7914f..8ec77dc060152 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateDefaultShardingStrategyStatementUpdaterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateDefaultShardingStrategyStatementUpdaterTest.java @@ -31,41 +31,37 @@ import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.Properties; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@ExtendWith(MockitoExtension.class) class CreateDefaultShardingStrategyStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - private final CreateDefaultShardingStrategyExecutor executor = new CreateDefaultShardingStrategyExecutor(); @BeforeEach void before() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("foo_db"); + executor.setDatabase(database); } @Test void assertExecuteWithInvalidStrategyType() { CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "invalidType", null, null); - assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(database, statement, new ShardingRuleConfiguration())); + assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(statement, new ShardingRuleConfiguration())); } @Test void assertExecuteWithoutAlgorithm() { CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "TABLE", "standard", "order_id", null); - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(database, statement, null)); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(statement, null)); } @Test @@ -75,7 +71,7 @@ void assertExecuteWithExist() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(database, statement, currentRuleConfig)); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(statement, currentRuleConfig)); } @Test @@ -84,7 +80,7 @@ void assertExecuteWithUnmatchedStrategy() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(database, statement, currentRuleConfig)); + assertThrows(InvalidAlgorithmConfigurationException.class, () -> executor.checkBeforeUpdate(statement, currentRuleConfig)); } @Test @@ -94,7 +90,7 @@ void assertCreateDefaultTableShardingStrategy() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); StandardShardingStrategyConfiguration defaultTableShardingStrategy = (StandardShardingStrategyConfiguration) currentRuleConfig.getDefaultTableShardingStrategy(); @@ -107,7 +103,7 @@ void assertCreateDefaultDatabaseShardingStrategy() { AlgorithmSegment databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}"))); CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "DATABASE", "standard", "user_id", databaseAlgorithmSegment); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); StandardShardingStrategyConfiguration defaultDatabaseShardingStrategy = (StandardShardingStrategyConfiguration) currentRuleConfig.getDefaultDatabaseShardingStrategy(); @@ -122,12 +118,12 @@ void assertCreateDefaultTableShardingStrategyWithIfNotExists() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); algorithm = new AlgorithmSegment("user_id_algorithm", new Properties()); CreateDefaultShardingStrategyStatement statementWithIfNotExists = new CreateDefaultShardingStrategyStatement(true, "TABLE", "standard", "order_id", algorithm); - executor.checkBeforeUpdate(database, statementWithIfNotExists, currentRuleConfig); + executor.checkBeforeUpdate(statementWithIfNotExists, currentRuleConfig); toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, statementWithIfNotExists); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); StandardShardingStrategyConfiguration defaultTableShardingStrategy = (StandardShardingStrategyConfiguration) currentRuleConfig.getDefaultTableShardingStrategy(); @@ -140,12 +136,12 @@ void assertCreateDefaultDatabaseShardingStrategyWithIfNotExists() { AlgorithmSegment databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}"))); CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "DATABASE", "standard", "user_id", databaseAlgorithmSegment); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); databaseAlgorithmSegment = new AlgorithmSegment("inline", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${order_id % 2}"))); CreateDefaultShardingStrategyStatement statementWithIfNotExists = new CreateDefaultShardingStrategyStatement(true, "TABLE", "standard", "order_id", databaseAlgorithmSegment); - executor.checkBeforeUpdate(database, statementWithIfNotExists, currentRuleConfig); + executor.checkBeforeUpdate(statementWithIfNotExists, currentRuleConfig); toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, statementWithIfNotExists); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); StandardShardingStrategyConfiguration defaultDatabaseShardingStrategy = (StandardShardingStrategyConfiguration) currentRuleConfig.getDefaultDatabaseShardingStrategy(); @@ -159,7 +155,7 @@ void assertCreateDefaultTableShardingStrategyWithNoneShardingStrategyType() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "orderAlgorithm")); currentRuleConfig.getShardingAlgorithms().put("order_id_algorithm", null); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); NoneShardingStrategyConfiguration defaultTableShardingStrategy = (NoneShardingStrategyConfiguration) currentRuleConfig.getDefaultTableShardingStrategy(); @@ -171,7 +167,7 @@ void assertCreateDefaultTableShardingStrategyWithNoneShardingStrategyType() { void assertCreateDefaultDatabaseShardingStrategyWithNoneShardingStrategyType() { CreateDefaultShardingStrategyStatement statement = new CreateDefaultShardingStrategyStatement(false, "DATABASE", "none", null, null); ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); NoneShardingStrategyConfiguration defaultDatabaseShardingStrategy = (NoneShardingStrategyConfiguration) currentRuleConfig.getDefaultDatabaseShardingStrategy(); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableReferenceRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableReferenceRuleStatementUpdaterTest.java index ac3c99eab0aa6..2f6478d59dc88 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableReferenceRuleStatementUpdaterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableReferenceRuleStatementUpdaterTest.java @@ -26,11 +26,8 @@ import org.apache.shardingsphere.sharding.distsql.handler.update.CreateShardingTableReferenceRuleExecutor; import org.apache.shardingsphere.sharding.distsql.segment.table.TableReferenceRuleSegment; import org.apache.shardingsphere.sharding.distsql.statement.CreateShardingTableReferenceRuleStatement; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.Collection; import java.util.Iterator; @@ -39,18 +36,21 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; -@ExtendWith(MockitoExtension.class) class CreateShardingTableReferenceRuleStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - private final CreateShardingTableReferenceRuleExecutor executor = new CreateShardingTableReferenceRuleExecutor(); + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } + @Test void assertCheckSQLStatementWithoutCurrentTableRule() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement(false, "foo", "t_order,t_order_item"), new ShardingRuleConfiguration())); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "foo", "t_order,t_order_item"), new ShardingRuleConfiguration())); } private CreateShardingTableReferenceRuleStatement createSQLStatement(final boolean ifNotExists, final String name, final String reference) { @@ -61,14 +61,14 @@ private CreateShardingTableReferenceRuleStatement createSQLStatement(final boole @Test void assertCheckSQLStatementWithDuplicateTables() { - assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement(false, "foo", "t_order,t_order_item"), getCurrentRuleConfig())); + assertThrows(DuplicateRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "foo", "t_order,t_order_item"), getCurrentRuleConfig())); } @Test void assertUpdateWithIfNotExists() { CreateShardingTableReferenceRuleStatement sqlStatement = createSQLStatement(true, "foo", "t_order,t_order_item"); ShardingRuleConfiguration currentRuleConfig = getCurrentRuleConfig(); - executor.checkBeforeUpdate(database, sqlStatement, currentRuleConfig); + executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, sqlStatement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); Collection referenceRuleConfigs = currentRuleConfig.getBindingTableGroups(); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleStatementUpdaterTest.java index e89b114c9e761..a717812ff7d9b 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleStatementUpdaterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingTableRuleStatementUpdaterTest.java @@ -49,10 +49,6 @@ import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import javax.sql.DataSource; import java.util.Arrays; @@ -68,31 +64,30 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -@ExtendWith(MockitoExtension.class) class CreateShardingTableRuleStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - private final ShardingRuleConfiguration currentRuleConfig = createCurrentShardingRuleConfiguration(); private final CreateShardingTableRuleExecutor executor = new CreateShardingTableRuleExecutor(); @BeforeEach void before() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("schema"); ResourceMetaData resourceMetaData = new ResourceMetaData(createDataSource()); when(database.getResourceMetaData()).thenReturn(resourceMetaData); when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(new MockDataSourceContainedRule()))); + executor.setDatabase(database); } @Test void assertUpdate() { CreateShardingTableRuleStatement statement = new CreateShardingTableRuleStatement(false, Arrays.asList(createCompleteAutoTableRule(), createCompleteTableRule())); - executor.checkBeforeUpdate(database, statement, currentRuleConfig); + executor.checkBeforeUpdate(statement, currentRuleConfig); ShardingRuleConfiguration toBeCreatedRuleConfig = executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, statement); executor.updateCurrentRuleConfiguration(currentRuleConfig, toBeCreatedRuleConfig); assertThat(currentRuleConfig.getTables().size(), is(2)); @@ -137,7 +132,7 @@ void assertCheckCreateShardingStatement() { + "TYPE(NAME='hash_mod',PROPERTIES('sharding-count'='6'))," + "KEY_GENERATE_STRATEGY(COLUMN=order_id,TYPE(NAME='snowflake')))"; CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql); - executor.checkBeforeUpdate(database, distSQLStatement, null); + executor.checkBeforeUpdate(distSQLStatement, null); } @Test @@ -148,7 +143,7 @@ void assertCheckCreateShardingStatementThrows() { + "TYPE(NAME='inline',PROPERTIES('algorithm-expression'='t_order_item_${order_id % 4}'))," + "KEY_GENERATE_STRATEGY(COLUMN=order_id,TYPE(NAME='snowflake')))"; CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql); - assertThrows(DistSQLException.class, () -> executor.checkBeforeUpdate(database, distSQLStatement, null)); + assertThrows(DistSQLException.class, () -> executor.checkBeforeUpdate(distSQLStatement, null)); } @Test @@ -159,7 +154,7 @@ void assertCheckCreateShardingStatementWithNoneDatabaseStrategy() { + "TABLE_STRATEGY(TYPE='standard',SHARDING_COLUMN=order_id,SHARDING_ALGORITHM(TYPE(NAME='inline',PROPERTIES('algorithm-expression'='t_order_${order_id % 2}'))))" + ");"; CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql); - executor.checkBeforeUpdate(database, distSQLStatement, null); + executor.checkBeforeUpdate(distSQLStatement, null); } @Test @@ -170,7 +165,7 @@ void assertCheckCreateShardingStatementWithNoneDatabaseStrategyThrows() { + "TABLE_STRATEGY(TYPE='standard',SHARDING_COLUMN=order_id,SHARDING_ALGORITHM(TYPE(NAME='inline',PROPERTIES('algorithm-expression'='t_order_${order_id % 2}'))))" + ");"; CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql); - assertThrows(InvalidShardingStrategyConfigurationException.class, () -> executor.checkBeforeUpdate(database, distSQLStatement, null)); + assertThrows(InvalidShardingStrategyConfigurationException.class, () -> executor.checkBeforeUpdate(distSQLStatement, null)); } @Test @@ -181,7 +176,7 @@ void assertCheckCreateShardingStatementWithNoneTableStrategy() { + "TABLE_STRATEGY(TYPE='NONE')" + ");"; CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql); - executor.checkBeforeUpdate(database, distSQLStatement, null); + executor.checkBeforeUpdate(distSQLStatement, null); } @Test @@ -192,7 +187,7 @@ void assertCheckCreateShardingStatementWithNoneTableStrategyThrows() { + "TABLE_STRATEGY(TYPE='NONE')" + ");"; CreateShardingTableRuleStatement distSQLStatement = (CreateShardingTableRuleStatement) getDistSQLStatement(sql); - assertThrows(InvalidShardingStrategyConfigurationException.class, () -> executor.checkBeforeUpdate(database, distSQLStatement, null)); + assertThrows(InvalidShardingStrategyConfigurationException.class, () -> executor.checkBeforeUpdate(distSQLStatement, null)); } @Test @@ -201,7 +196,7 @@ void assertUpdateWithIfNotExistsStatement() { segments.add(createCompleteAutoTableRule()); segments.add(createCompleteTableRule()); CreateShardingTableRuleStatement statementWithIfNotExists = new CreateShardingTableRuleStatement(true, segments); - executor.checkBeforeUpdate(database, statementWithIfNotExists, currentRuleConfig); + executor.checkBeforeUpdate(statementWithIfNotExists, currentRuleConfig); executor.updateCurrentRuleConfiguration(currentRuleConfig, executor.buildToBeCreatedRuleConfiguration(currentRuleConfig, statementWithIfNotExists)); assertThat(currentRuleConfig.getTables().size(), is(2)); Iterator tableRuleIterator = currentRuleConfig.getTables().iterator(); diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropDefaultShardingStrategyStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropDefaultShardingStrategyStatementUpdaterTest.java index d343c32976bb2..f4caec49ec969 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropDefaultShardingStrategyStatementUpdaterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropDefaultShardingStrategyStatementUpdaterTest.java @@ -24,11 +24,8 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.handler.update.DropDefaultShardingStrategyExecutor; import org.apache.shardingsphere.sharding.distsql.statement.DropDefaultShardingStrategyStatement; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.LinkedHashMap; import java.util.Map; @@ -40,29 +37,32 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; -@ExtendWith(MockitoExtension.class) class DropDefaultShardingStrategyStatementUpdaterTest { private final DropDefaultShardingStrategyExecutor executor = new DropDefaultShardingStrategyExecutor(); - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } @Test void assertCheckSQLStatementWithoutCurrentRule() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, new DropDefaultShardingStrategyStatement(false, "TABLE"), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(new DropDefaultShardingStrategyStatement(false, "TABLE"), null)); } @Test void assertCheckSQLStatementWithoutExistedAlgorithm() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("table"), new ShardingRuleConfiguration())); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("table"), new ShardingRuleConfiguration())); } @Test void assertCheckSQLStatementWithIfExists() { - executor.checkBeforeUpdate(database, new DropDefaultShardingStrategyStatement(true, "table"), new ShardingRuleConfiguration()); - executor.checkBeforeUpdate(database, new DropDefaultShardingStrategyStatement(true, "table"), null); + executor.checkBeforeUpdate(new DropDefaultShardingStrategyStatement(true, "table"), new ShardingRuleConfiguration()); + executor.checkBeforeUpdate(new DropDefaultShardingStrategyStatement(true, "table"), null); } @Test diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAlgorithmStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAlgorithmStatementUpdaterTest.java index 6d618b66ec9a0..5d7f6477d1c2c 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAlgorithmStatementUpdaterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAlgorithmStatementUpdaterTest.java @@ -52,33 +52,39 @@ class DropShardingAlgorithmStatementUpdaterTest { @Test void assertCheckSQLStatementWithoutCurrentRule() throws RuleDefinitionViolationException { - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(database, new DropShardingAlgorithmStatement(false, Collections.emptyList()), null)); + executor.setDatabase(database); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(new DropShardingAlgorithmStatement(false, Collections.emptyList()), null)); } @Test void assertCheckSQLStatementWithoutCurrentRuleWithIfExists() throws RuleDefinitionViolationException { DropShardingAlgorithmStatement dropShardingAlgorithmStatement = new DropShardingAlgorithmStatement(true, Collections.emptyList()); - executor.checkBeforeUpdate(database, dropShardingAlgorithmStatement, null); + executor.setDatabase(database); + executor.checkBeforeUpdate(dropShardingAlgorithmStatement, null); } @Test void assertCheckSQLStatementWithoutExistedAlgorithm() throws RuleDefinitionViolationException { - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("t_order"), new ShardingRuleConfiguration())); + executor.setDatabase(database); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order"), new ShardingRuleConfiguration())); } @Test void assertCheckSQLStatementWithoutExistedAlgorithmWithIfExists() throws RuleDefinitionViolationException { - executor.checkBeforeUpdate(database, createSQLStatementWithIfExists("t_order"), new ShardingRuleConfiguration()); + executor.setDatabase(database); + executor.checkBeforeUpdate(createSQLStatementWithIfExists("t_order"), new ShardingRuleConfiguration()); } @Test void assertCheckSQLStatementWithBindingTableRule() throws RuleDefinitionViolationException { - assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("t_order_tb_inline"), createCurrentRuleConfiguration())); + executor.setDatabase(database); + assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order_tb_inline"), createCurrentRuleConfiguration())); } @Test void assertCheckSQLStatementWithBindingTableRuleWithIfExists() throws RuleDefinitionViolationException { - assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(database, createSQLStatementWithIfExists("t_order_tb_inline"), createCurrentRuleConfiguration())); + executor.setDatabase(database); + assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatementWithIfExists("t_order_tb_inline"), createCurrentRuleConfiguration())); } @Test diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAuditorStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAuditorStatementUpdaterTest.java index e1446cab95404..83abcaf94ef29 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAuditorStatementUpdaterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingAuditorStatementUpdaterTest.java @@ -28,12 +28,6 @@ import org.apache.shardingsphere.sharding.distsql.statement.DropShardingAuditorStatement; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; -import org.mockito.junit.jupiter.MockitoSettings; -import org.mockito.quality.Strictness; import java.util.Arrays; import java.util.Collections; @@ -41,31 +35,30 @@ import static org.junit.jupiter.api.Assertions.assertThrows; 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; -@ExtendWith(MockitoExtension.class) -@MockitoSettings(strictness = Strictness.LENIENT) class DropShardingAuditorStatementUpdaterTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; - private final DropShardingAuditorExecutor executor = new DropShardingAuditorExecutor(); @BeforeEach void before() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(database.getName()).thenReturn("foo_db"); + executor.setDatabase(database); } @Test void assertExecuteWithNotExist() { - assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("sharding_key_required_auditor"), new ShardingRuleConfiguration())); + assertThrows(MissingRequiredAlgorithmException.class, () -> executor.checkBeforeUpdate(createSQLStatement("sharding_key_required_auditor"), new ShardingRuleConfiguration())); } @Test void assertExecuteWithNotExistWithIfExists() { DropShardingAuditorStatement sqlStatement = new DropShardingAuditorStatement(true, Collections.singleton("sharding_key_required_auditor")); - executor.checkBeforeUpdate(database, sqlStatement, new ShardingRuleConfiguration()); + executor.checkBeforeUpdate(sqlStatement, new ShardingRuleConfiguration()); } @Test @@ -81,7 +74,7 @@ void assertExecuteWithUsed() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", null)); currentRuleConfig.getAutoTables().add(createShardingAutoTableRuleConfiguration()); - assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(database, createSQLStatement("sharding_key_required_auditor"), currentRuleConfig)); + assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement("sharding_key_required_auditor"), currentRuleConfig)); } private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorStatementUpdaterTest.java index 2f74a3e7765ed..a5619996d5deb 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorStatementUpdaterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorStatementUpdaterTest.java @@ -26,6 +26,7 @@ import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; import org.apache.shardingsphere.sharding.distsql.handler.update.DropShardingKeyGeneratorExecutor; import org.apache.shardingsphere.sharding.distsql.statement.DropShardingKeyGeneratorStatement; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.Collections; @@ -33,21 +34,29 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; class DropShardingKeyGeneratorStatementUpdaterTest { + private final DropShardingKeyGeneratorExecutor executor = new DropShardingKeyGeneratorExecutor(); + + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } + @Test void assertExecuteWithNotExist() { DropShardingKeyGeneratorStatement sqlStatement = new DropShardingKeyGeneratorStatement(false, Collections.singleton("uuid_key_generator")); assertThrows(MissingRequiredAlgorithmException.class, - () -> new DropShardingKeyGeneratorExecutor().checkBeforeUpdate(mock(ShardingSphereDatabase.class), sqlStatement, new ShardingRuleConfiguration())); + () -> executor.checkBeforeUpdate(sqlStatement, new ShardingRuleConfiguration())); } @Test void assertExecuteWithNotExistWithIfExists() { DropShardingKeyGeneratorStatement sqlStatement = new DropShardingKeyGeneratorStatement(true, Collections.singleton("uuid_key_generator")); - new DropShardingKeyGeneratorExecutor().checkBeforeUpdate(mock(ShardingSphereDatabase.class), sqlStatement, new ShardingRuleConfiguration()); + executor.checkBeforeUpdate(sqlStatement, new ShardingRuleConfiguration()); } @Test @@ -65,7 +74,7 @@ void assertExecuteWithUsed() { ShardingRuleConfiguration currentRuleConfig = new ShardingRuleConfiguration(); currentRuleConfig.getKeyGenerators().put("uuid_key_generator", new AlgorithmConfiguration("UUID", null)); currentRuleConfig.getAutoTables().add(createShardingAutoTableRuleConfiguration()); - assertThrows(AlgorithmInUsedException.class, () -> new DropShardingKeyGeneratorExecutor().checkBeforeUpdate(mock(ShardingSphereDatabase.class), sqlStatement, currentRuleConfig)); + assertThrows(AlgorithmInUsedException.class, () -> executor.checkBeforeUpdate(sqlStatement, currentRuleConfig)); } private ShardingAutoTableRuleConfiguration createShardingAutoTableRuleConfiguration() { diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableReferenceRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableReferenceRuleStatementUpdaterTest.java index ddaf855ef71e4..5e1420edb1fba 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableReferenceRuleStatementUpdaterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableReferenceRuleStatementUpdaterTest.java @@ -25,11 +25,8 @@ import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; import org.apache.shardingsphere.sharding.distsql.handler.update.DropShardingTableReferenceExecutor; import org.apache.shardingsphere.sharding.distsql.statement.DropShardingTableReferenceRuleStatement; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; import java.util.Arrays; import java.util.Collections; @@ -39,33 +36,36 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; -@ExtendWith(MockitoExtension.class) class DropShardingTableReferenceRuleStatementUpdaterTest { private final DropShardingTableReferenceExecutor executor = new DropShardingTableReferenceExecutor(); - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private ShardingSphereDatabase database; + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); + } @Test void assertCheckWithoutCurrentRule() { - assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(database, new DropShardingTableReferenceRuleStatement(false, Collections.singleton("notExisted")), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(new DropShardingTableReferenceRuleStatement(false, Collections.singleton("notExisted")), null)); } @Test void assertCheckWithNotExistedShardingTableReferenceRule() { assertThrows(MissingRequiredRuleException.class, - () -> executor.checkBeforeUpdate(database, new DropShardingTableReferenceRuleStatement(false, Collections.singleton("notExisted")), new ShardingRuleConfiguration())); + () -> executor.checkBeforeUpdate(new DropShardingTableReferenceRuleStatement(false, Collections.singleton("notExisted")), new ShardingRuleConfiguration())); } @Test void assertCheckWithIfExists() { DropShardingTableReferenceRuleStatement statement = new DropShardingTableReferenceRuleStatement(true, Collections.singleton("notExisted")); - executor.checkBeforeUpdate(database, statement, null); + executor.checkBeforeUpdate(statement, null); ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); shardingRuleConfig.setBindingTableGroups(Collections.singleton(new ShardingTableReferenceRuleConfiguration("foo", "t_3,t_4"))); - executor.checkBeforeUpdate(database, statement, shardingRuleConfig); + executor.checkBeforeUpdate(statement, shardingRuleConfig); } @Test @@ -95,7 +95,7 @@ void assertDropSpecifiedReferenceRuleConfiguration() { ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); currentRuleConfig.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("reference_1", "t_1,t_2")); DropShardingTableReferenceRuleStatement sqlStatement = new DropShardingTableReferenceRuleStatement(false, Collections.singleton("reference_1")); - executor.checkBeforeUpdate(database, sqlStatement, currentRuleConfig); + executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(sqlStatement, currentRuleConfig); assertThat(currentRuleConfig.getBindingTableGroups().size(), is(1)); assertThat(currentRuleConfig.getBindingTableGroups().iterator().next().getReference(), is("t_order,t_order_item")); @@ -106,7 +106,7 @@ void assertDropMultipleReferenceRules() { ShardingRuleConfiguration currentRuleConfig = createCurrentRuleConfiguration(); currentRuleConfig.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("reference_1", "t_1,t_2,t_3")); DropShardingTableReferenceRuleStatement sqlStatement = new DropShardingTableReferenceRuleStatement(false, Arrays.asList("reference_0", "reference_1")); - executor.checkBeforeUpdate(database, sqlStatement, currentRuleConfig); + executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); executor.updateCurrentRuleConfiguration(sqlStatement, currentRuleConfig); assertTrue(currentRuleConfig.getBindingTableGroups().isEmpty()); } diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java index a883eaea2b049..509d513e4252f 100644 --- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java +++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java @@ -33,6 +33,7 @@ import org.apache.shardingsphere.sharding.distsql.statement.DropShardingTableRuleStatement; import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment; import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Answers; @@ -51,37 +52,39 @@ class DropShardingTableRuleStatementUpdaterTest { + private final DropShardingTableRuleExecutor executor = new DropShardingTableRuleExecutor(); + + @BeforeEach + void setUp() { + executor.setDatabase(mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS)); + } + @Test void assertCheckSQLStatementWithoutCurrentRule() throws RuleDefinitionViolationException { - assertThrows(MissingRequiredRuleException.class, - () -> new DropShardingTableRuleExecutor().checkBeforeUpdate( - mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS), new DropShardingTableRuleStatement(false, Collections.emptyList()), null)); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(new DropShardingTableRuleStatement(false, Collections.emptyList()), null)); } @Test void assertCheckSQLStatementWithoutExistedTableRule() throws RuleDefinitionViolationException { - assertThrows(MissingRequiredRuleException.class, - () -> new DropShardingTableRuleExecutor().checkBeforeUpdate( - mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS), createSQLStatement("t_order"), new ShardingRuleConfiguration())); + assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order"), new ShardingRuleConfiguration())); } @Test void assertCheckSQLStatementIfExistsWithNullCurrentRuleConfiguration() throws RuleDefinitionViolationException { DropShardingTableRuleStatement statement = new DropShardingTableRuleStatement(true, Collections.singleton(new TableNameSegment(0, 3, new IdentifierValue("t_order_if_exists")))); - new DropShardingTableRuleExecutor().checkBeforeUpdate(mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS), statement, null); + executor.checkBeforeUpdate(statement, null); } @Test void assertCheckSQLStatementIfExists() throws RuleDefinitionViolationException { DropShardingTableRuleStatement statement = new DropShardingTableRuleStatement(true, Collections.singleton(new TableNameSegment(0, 3, new IdentifierValue("t_order_if_exists")))); - new DropShardingTableRuleExecutor().checkBeforeUpdate(mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS), statement, new ShardingRuleConfiguration()); + executor.checkBeforeUpdate(statement, new ShardingRuleConfiguration()); } @Test void assertCheckSQLStatementWithBindingTableRule() throws RuleDefinitionViolationException { - assertThrows(RuleInUsedException.class, () -> new DropShardingTableRuleExecutor().checkBeforeUpdate( - mock(ShardingSphereDatabase.class, Answers.RETURNS_DEEP_STUBS), createSQLStatement("t_order_item"), createCurrentRuleConfiguration())); + assertThrows(RuleInUsedException.class, () -> executor.checkBeforeUpdate(createSQLStatement("t_order_item"), createCurrentRuleConfiguration())); } @Test diff --git a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java index 5a5adae05e7fa..457afbab48008 100644 --- a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java +++ b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/engine/database/DatabaseRuleDefinitionExecuteEngine.java @@ -49,7 +49,8 @@ public final class DatabaseRuleDefinitionExecuteEngine { public void executeUpdate() { Class ruleConfigClass = executor.getRuleConfigurationClass(); RuleConfiguration currentRuleConfig = findCurrentRuleConfiguration(database, ruleConfigClass).orElse(null); - executor.checkBeforeUpdate(database, sqlStatement, currentRuleConfig); + executor.setDatabase(database); + executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); if (getRefreshStatus(currentRuleConfig)) { contextManager.getMetaDataContexts().getPersistService().getMetaDataVersionPersistService() .switchActiveVersion(DatabaseRuleOperatorFactory.newInstance(contextManager, executor).operate(sqlStatement, database, currentRuleConfig)); diff --git a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleDefinitionExecutor.java b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleDefinitionExecutor.java index e07660d807549..59ff976c8f905 100644 --- a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleDefinitionExecutor.java +++ b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/database/DatabaseRuleDefinitionExecutor.java @@ -17,8 +17,8 @@ package org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database; +import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI; import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement; @@ -30,16 +30,15 @@ * @param type of rule configuration */ @SingletonSPI -public interface DatabaseRuleDefinitionExecutor extends TypedSPI { +public interface DatabaseRuleDefinitionExecutor extends TypedSPI, DistSQLExecutorDatabaseAware { /** - * Check SQL statement. + * Check before update. * - * @param database database * @param sqlStatement SQL statement * @param currentRuleConfig current rule configuration */ - void checkBeforeUpdate(ShardingSphereDatabase database, T sqlStatement, R currentRuleConfig); + void checkBeforeUpdate(T sqlStatement, R currentRuleConfig); /** * Get rule configuration class. diff --git a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/global/GlobalRuleDefinitionExecutor.java b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/global/GlobalRuleDefinitionExecutor.java index b3fc8664c9c75..a277ef8fa2cb3 100644 --- a/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/global/GlobalRuleDefinitionExecutor.java +++ b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rdl/rule/spi/global/GlobalRuleDefinitionExecutor.java @@ -32,7 +32,7 @@ public interface GlobalRuleDefinitionExecutor extends TypedSPI { /** - * Check SQL statement. + * Check before update. * * @param currentRuleConfig current rule configuration * @param sqlStatement SQL statement diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java index 2107216d4e774..0d341224af7a7 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableExecutor.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.single.distsql.handler.update; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; import org.apache.shardingsphere.infra.database.DatabaseTypeEngine; @@ -48,17 +49,20 @@ /** * Load single table statement executor. */ +@Setter public final class LoadSingleTableExecutor implements DatabaseRuleCreateExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final LoadSingleTableStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) { + public void checkBeforeUpdate(final LoadSingleTableStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) { String defaultSchemaName = new DatabaseTypeRegistry(database.getProtocolType()).getDefaultSchemaName(database.getName()); - checkDuplicatedTables(database, sqlStatement, defaultSchemaName); - checkStorageUnits(database, sqlStatement); - checkActualTableExist(database, sqlStatement, defaultSchemaName); + checkDuplicatedTables(sqlStatement, defaultSchemaName); + checkStorageUnits(sqlStatement); + checkActualTableExist(sqlStatement, defaultSchemaName); } - private void checkDuplicatedTables(final ShardingSphereDatabase database, final LoadSingleTableStatement sqlStatement, final String defaultSchemaName) { + private void checkDuplicatedTables(final LoadSingleTableStatement sqlStatement, final String defaultSchemaName) { Collection tableSegments = sqlStatement.getTables(); DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(database.getProtocolType()).getDialectDatabaseMetaData(); boolean isSchemaSupportedDatabaseType = dialectDatabaseMetaData.getDefaultSchema().isPresent(); @@ -97,7 +101,7 @@ private Collection getRequiredDataSources(final LoadSingleTableStatement .filter(each -> !SingleTableConstants.ASTERISK.equals(each)).collect(Collectors.toSet()); } - private void checkStorageUnits(final ShardingSphereDatabase database, final LoadSingleTableStatement sqlStatement) { + private void checkStorageUnits(final LoadSingleTableStatement sqlStatement) { Collection requiredDataSources = getRequiredDataSources(sqlStatement); if (requiredDataSources.isEmpty()) { return; @@ -108,7 +112,7 @@ private void checkStorageUnits(final ShardingSphereDatabase database, final Load ShardingSpherePreconditions.checkState(notExistedDataSources.isEmpty(), () -> new MissingRequiredStorageUnitsException(database.getName(), notExistedDataSources)); } - private void checkActualTableExist(final ShardingSphereDatabase database, final LoadSingleTableStatement sqlStatement, final String defaultSchemaName) { + private void checkActualTableExist(final LoadSingleTableStatement sqlStatement, final String defaultSchemaName) { Collection requiredDataSources = getRequiredDataSources(sqlStatement); if (requiredDataSources.isEmpty()) { return; diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutor.java index 5d271fbeffcbc..e2cf32fd7c39f 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutor.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitExecutor.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.single.distsql.handler.update; import com.google.common.base.Strings; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleCreateExecutor; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; @@ -31,14 +32,17 @@ /** * Set default single table storage unit executor. */ +@Setter public final class SetDefaultSingleTableStorageUnitExecutor implements DatabaseRuleCreateExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final SetDefaultSingleTableStorageUnitStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) { - checkStorageUnitExist(database, sqlStatement); + public void checkBeforeUpdate(final SetDefaultSingleTableStorageUnitStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) { + checkStorageUnitExist(sqlStatement); } - private void checkStorageUnitExist(final ShardingSphereDatabase database, final SetDefaultSingleTableStorageUnitStatement sqlStatement) { + private void checkStorageUnitExist(final SetDefaultSingleTableStorageUnitStatement sqlStatement) { if (!Strings.isNullOrEmpty(sqlStatement.getDefaultStorageUnit())) { Collection storageUnitNames = database.getResourceMetaData().getStorageUnits().keySet(); ShardingSpherePreconditions.checkState(storageUnitNames.contains(sqlStatement.getDefaultStorageUnit()), diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableExecutor.java index 3e74eff01064e..49fe04368716d 100644 --- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableExecutor.java +++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/update/UnloadSingleTableExecutor.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.single.distsql.handler.update; import com.google.common.base.Splitter; +import lombok.Setter; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.type.rdl.rule.spi.database.DatabaseRuleAlterExecutor; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; @@ -39,19 +40,22 @@ /** * Unload single table statement executor. */ +@Setter public final class UnloadSingleTableExecutor implements DatabaseRuleAlterExecutor { + private ShardingSphereDatabase database; + @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final UnloadSingleTableStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) { - checkCurrentRuleConfig(database.getName(), currentRuleConfig); - checkTables(database, sqlStatement, currentRuleConfig); + public void checkBeforeUpdate(final UnloadSingleTableStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) { + checkCurrentRuleConfig(currentRuleConfig); + checkTables(sqlStatement, currentRuleConfig); } - private void checkCurrentRuleConfig(final String databaseName, final SingleRuleConfiguration currentRuleConfig) { - ShardingSpherePreconditions.checkState(null != currentRuleConfig, () -> new MissingRequiredRuleException("Single", databaseName)); + private void checkCurrentRuleConfig(final SingleRuleConfiguration currentRuleConfig) { + ShardingSpherePreconditions.checkState(null != currentRuleConfig, () -> new MissingRequiredRuleException("Single", database.getName())); } - private void checkTables(final ShardingSphereDatabase database, final UnloadSingleTableStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) { + private void checkTables(final UnloadSingleTableStatement sqlStatement, final SingleRuleConfiguration currentRuleConfig) { if (sqlStatement.isUnloadAllTables()) { return; } @@ -65,6 +69,11 @@ private void checkTables(final ShardingSphereDatabase database, final UnloadSing } } + private Collection getAllTableNames(final ShardingSphereDatabase database) { + String defaultSchemaName = new DatabaseTypeRegistry(database.getProtocolType()).getDefaultSchemaName(database.getName()); + return database.getSchema(defaultSchemaName).getTables().values().stream().map(ShardingSphereTable::getName).collect(Collectors.toList()); + } + private void checkTableExist(final Collection allTables, final String tableName) { ShardingSpherePreconditions.checkState(allTables.contains(tableName), () -> new NoSuchTableException(tableName)); } @@ -73,11 +82,6 @@ private void checkIsSingleTable(final Collection singleTables, final Str ShardingSpherePreconditions.checkState(singleTables.contains(tableName), () -> new SingleTableNotFoundException(tableName)); } - private Collection getAllTableNames(final ShardingSphereDatabase database) { - String defaultSchemaName = new DatabaseTypeRegistry(database.getProtocolType()).getDefaultSchemaName(database.getName()); - return database.getSchema(defaultSchemaName).getTables().values().stream().map(ShardingSphereTable::getName).collect(Collectors.toList()); - } - private void checkTableRuleExist(final String databaseName, final DatabaseType databaseType, final SingleRuleConfiguration currentRuleConfig, final Collection dataNodes, final String tableName) { ShardingSpherePreconditions.checkState(!dataNodes.isEmpty(), () -> new MissingRequiredRuleException("Single", databaseName, tableName)); diff --git a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdaterTest.java b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdaterTest.java index e5a52bbd2c2d6..ffb9502bb580e 100644 --- a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdaterTest.java +++ b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/LoadSingleTableStatementUpdaterTest.java @@ -67,14 +67,16 @@ void assertCheckWithDuplicatedTables() { when(database.getName()).thenReturn("foo_db"); when(schema.containsTable("foo")).thenReturn(true); LoadSingleTableStatement sqlStatement = new LoadSingleTableStatement(Collections.singletonList(new SingleTableSegment("ds_0", null, "foo"))); - assertThrows(TableExistsException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, mock(SingleRuleConfiguration.class))); + executor.setDatabase(database); + assertThrows(TableExistsException.class, () -> executor.checkBeforeUpdate(sqlStatement, mock(SingleRuleConfiguration.class))); } @Test void assertCheckWithInvalidStorageUnit() { - when(database.getName()).thenReturn("foo_db"); LoadSingleTableStatement sqlStatement = new LoadSingleTableStatement(Collections.singletonList(new SingleTableSegment("ds_0", null, "foo"))); - assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(database, sqlStatement, mock(SingleRuleConfiguration.class))); + when(database.getName()).thenReturn("foo_db"); + executor.setDatabase(database); + assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.checkBeforeUpdate(sqlStatement, mock(SingleRuleConfiguration.class))); } @Test diff --git a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitStatementUpdaterTest.java b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitStatementUpdaterTest.java index 2db6e91557baf..719cbe6e21350 100644 --- a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitStatementUpdaterTest.java +++ b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/update/SetDefaultSingleTableStorageUnitStatementUpdaterTest.java @@ -40,8 +40,9 @@ class SetDefaultSingleTableStorageUnitStatementUpdaterTest { @Test void assertCheckWithInvalidResource() { + executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); assertThrows(MissingRequiredStorageUnitsException.class, - () -> executor.checkBeforeUpdate(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS), new SetDefaultSingleTableStorageUnitStatement("bar_ds"), mock(SingleRuleConfiguration.class))); + () -> executor.checkBeforeUpdate(new SetDefaultSingleTableStorageUnitStatement("bar_ds"), mock(SingleRuleConfiguration.class))); } @Test diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandler.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandler.java index 1272f0f1b2612..6559ab04e1827 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandler.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/rule/LegacyRuleDefinitionBackendHandler.java @@ -64,7 +64,8 @@ public ResponseHeader execute() { DatabaseRuleDefinitionExecutor executor = TypedSPILoader.getService(DatabaseRuleDefinitionExecutor.class, sqlStatement.getClass()); Class ruleConfigClass = executor.getRuleConfigurationClass(); RuleConfiguration currentRuleConfig = findCurrentRuleConfiguration(database, ruleConfigClass).orElse(null); - executor.checkBeforeUpdate(database, sqlStatement, currentRuleConfig); + executor.setDatabase(database); + executor.checkBeforeUpdate(sqlStatement, currentRuleConfig); if (getRefreshStatus(sqlStatement, currentRuleConfig, executor)) { ProxyContext.getInstance().getContextManager().getInstanceContext().getModeContextManager() .alterRuleConfiguration(database.getName(), processSQLStatement(database, sqlStatement, executor, currentRuleConfig)); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/FixtureDatabaseRuleCreateExecutor.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/FixtureDatabaseRuleCreateExecutor.java index 07587002b50b9..f6fd55298195b 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/FixtureDatabaseRuleCreateExecutor.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/fixture/FixtureDatabaseRuleCreateExecutor.java @@ -23,16 +23,16 @@ public final class FixtureDatabaseRuleCreateExecutor implements DatabaseRuleCreateExecutor { @Override - public FixtureRuleConfiguration buildToBeCreatedRuleConfiguration(final FixtureRuleConfiguration currentRuleConfig, final CreateFixtureRuleStatement sqlStatement) { - return new FixtureRuleConfiguration(); + public void checkBeforeUpdate(final CreateFixtureRuleStatement sqlStatement, final FixtureRuleConfiguration currentRuleConfig) { } @Override - public void updateCurrentRuleConfiguration(final FixtureRuleConfiguration currentRuleConfig, final FixtureRuleConfiguration toBeCreatedRuleConfig) { + public FixtureRuleConfiguration buildToBeCreatedRuleConfiguration(final FixtureRuleConfiguration currentRuleConfig, final CreateFixtureRuleStatement sqlStatement) { + return new FixtureRuleConfiguration(); } @Override - public void checkBeforeUpdate(final ShardingSphereDatabase database, final CreateFixtureRuleStatement sqlStatement, final FixtureRuleConfiguration currentRuleConfig) { + public void updateCurrentRuleConfiguration(final FixtureRuleConfiguration currentRuleConfig, final FixtureRuleConfiguration toBeCreatedRuleConfig) { } @Override @@ -44,4 +44,8 @@ public Class getRuleConfigurationClass() { public Class getType() { return CreateFixtureRuleStatement.class; } + + @Override + public void setDatabase(final ShardingSphereDatabase database) { + } }