From e84ed3e9494ea47f65e26af893532b781171efc1 Mon Sep 17 00:00:00 2001 From: Ilyas Ahsan Date: Sat, 11 May 2024 08:05:29 +0700 Subject: [PATCH] Refactor AlterSQLTranslatorRuleExecutorTest Test Case --- .../AlterSQLTranslatorRuleExecutorTest.java | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutorTest.java b/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutorTest.java index 3adef17ca7d9f..91a947cdc36a3 100644 --- a/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutorTest.java +++ b/kernel/sql-translator/distsql/handler/src/test/java/org/apache/shardingsphere/sqltranslator/distsql/handler/update/AlterSQLTranslatorRuleExecutorTest.java @@ -17,7 +17,10 @@ package org.apache.shardingsphere.sqltranslator.distsql.handler.update; +import org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine; +import org.apache.shardingsphere.distsql.handler.engine.update.rdl.rule.spi.global.GlobalRuleDefinitionExecutor; import org.apache.shardingsphere.distsql.segment.AlgorithmSegment; +import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.sqltranslator.api.config.SQLTranslatorRuleConfiguration; import org.apache.shardingsphere.sqltranslator.distsql.statement.updateable.AlterSQLTranslatorRuleStatement; import org.apache.shardingsphere.sqltranslator.rule.SQLTranslatorRule; @@ -27,26 +30,38 @@ import java.util.Properties; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; class AlterSQLTranslatorRuleExecutorTest { + private DistSQLUpdateExecuteEngine engine; + @Test void assertExecute() { - AlterSQLTranslatorRuleExecutor executor = new AlterSQLTranslatorRuleExecutor(); + AlterSQLTranslatorRuleStatement sqlStatement = new AlterSQLTranslatorRuleStatement(new AlgorithmSegment("Native", PropertiesBuilder.build(new Property("foo", "bar"))), Boolean.TRUE); + engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager()); + assertDoesNotThrow(() -> engine.executeUpdate()); + } + + @Test + void assertExecuteWithNullOriginalSQLWhenTranslatingFailed() { + AlterSQLTranslatorRuleStatement sqlStatement = new AlterSQLTranslatorRuleStatement(new AlgorithmSegment("Native", PropertiesBuilder.build(new Property("foo", "bar"))), null); + engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager()); + assertDoesNotThrow(() -> engine.executeUpdate()); + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + private ContextManager mockContextManager() { + ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); SQLTranslatorRule rule = mock(SQLTranslatorRule.class); + GlobalRuleDefinitionExecutor executor = mock(GlobalRuleDefinitionExecutor.class); + when(executor.getRuleClass()).thenReturn(SQLTranslatorRule.class); when(rule.getConfiguration()).thenReturn(createSQLTranslatorRuleConfiguration()); - executor.setRule(rule); - SQLTranslatorRuleConfiguration actual = executor.buildToBeAlteredRuleConfiguration( - new AlterSQLTranslatorRuleStatement(new AlgorithmSegment("Native", PropertiesBuilder.build(new Property("foo", "bar"))), null)); - assertThat(actual.getType(), is("Native")); - assertThat(actual.getProps().size(), is(1)); - assertThat(actual.getProps().getProperty("foo"), is("bar")); - assertTrue(actual.isUseOriginalSQLWhenTranslatingFailed()); + when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(executor.getRuleClass())).thenReturn(rule); + return result; } private SQLTranslatorRuleConfiguration createSQLTranslatorRuleConfiguration() {