Skip to content

Commit

Permalink
Refactor AlterSQLTranslatorRuleExecutorTest Test Case (#31202)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyasahsan123 authored May 11, 2024
1 parent 81ec147 commit ed211e7
Showing 1 changed file with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down

0 comments on commit ed211e7

Please sign in to comment.