Skip to content

Commit

Permalink
Refactor AlterSQLParserRuleExecutorTest Test Case (#31199)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasahsan123 authored May 10, 2024
1 parent 425c8c4 commit 8b19d81
Showing 1 changed file with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,55 @@

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;
import org.apache.shardingsphere.parser.rule.SQLParserRule;
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() {
Expand Down

0 comments on commit 8b19d81

Please sign in to comment.