From 8b19d81195b211557844707dfcecb1332b154edd Mon Sep 17 00:00:00 2001 From: ilyas ahsan Date: Fri, 10 May 2024 22:56:22 +0700 Subject: [PATCH] Refactor AlterSQLParserRuleExecutorTest Test Case (#31199) --- .../AlterSQLParserRuleExecutorTest.java | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleExecutorTest.java b/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleExecutorTest.java index 0e0ac8abcddab..209e5d179a86b 100644 --- a/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleExecutorTest.java +++ b/kernel/sql-parser/distsql/handler/src/test/java/org/apache/shardingsphere/parser/distsql/handler/update/AlterSQLParserRuleExecutorTest.java @@ -17,6 +17,9 @@ package org.apache.shardingsphere.parser.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.mode.manager.ContextManager; import org.apache.shardingsphere.parser.config.SQLParserRuleConfiguration; import org.apache.shardingsphere.parser.distsql.segment.CacheOptionSegment; import org.apache.shardingsphere.parser.distsql.statement.updatable.AlterSQLParserRuleStatement; @@ -24,25 +27,45 @@ import org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder; import org.junit.jupiter.api.Test; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +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 AlterSQLParserRuleExecutorTest { + private DistSQLUpdateExecuteEngine engine; + @Test void assertExecute() { - AlterSQLParserRuleExecutor executor = new AlterSQLParserRuleExecutor(); AlterSQLParserRuleStatement sqlStatement = new AlterSQLParserRuleStatement(new CacheOptionSegment(64, 512L), new CacheOptionSegment(1000, 1000L)); + engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager()); + assertDoesNotThrow(() -> engine.executeUpdate()); + } + + @Test + void assertExecuteWithNullStatement() { + AlterSQLParserRuleStatement sqlStatement = new AlterSQLParserRuleStatement(null, null); + engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager()); + assertDoesNotThrow(() -> engine.executeUpdate()); + } + + @Test + void assertExecuteWithNullCacheOptionSegment() { + AlterSQLParserRuleStatement sqlStatement = new AlterSQLParserRuleStatement(new CacheOptionSegment(null, null), new CacheOptionSegment(null, null)); + engine = new DistSQLUpdateExecuteEngine(sqlStatement, null, mockContextManager()); + assertDoesNotThrow(() -> engine.executeUpdate()); + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + private ContextManager mockContextManager() { + ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); SQLParserRule rule = mock(SQLParserRule.class); + GlobalRuleDefinitionExecutor executor = mock(GlobalRuleDefinitionExecutor.class); + when(executor.getRuleClass()).thenReturn(SQLParserRule.class); when(rule.getConfiguration()).thenReturn(getSQLParserRuleConfiguration()); - executor.setRule(rule); - SQLParserRuleConfiguration actual = executor.buildToBeAlteredRuleConfiguration(sqlStatement); - assertThat(actual.getSqlStatementCache().getInitialCapacity(), is(1000)); - assertThat(actual.getSqlStatementCache().getMaximumSize(), is(1000L)); - assertThat(actual.getParseTreeCache().getInitialCapacity(), is(64)); - assertThat(actual.getParseTreeCache().getMaximumSize(), is(512L)); + when(result.getMetaDataContexts().getMetaData().getGlobalRuleMetaData().getSingleRule(executor.getRuleClass())).thenReturn(rule); + return result; } private SQLParserRuleConfiguration getSQLParserRuleConfiguration() {