Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DropMaskRuleExecutorTest #33116

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,70 @@

package org.apache.shardingsphere.mask.distsql.handler.update;

import org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine;
import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.mask.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.config.rule.MaskColumnRuleConfiguration;
import org.apache.shardingsphere.mask.config.rule.MaskTableRuleConfiguration;
import org.apache.shardingsphere.mask.distsql.statement.DropMaskRuleStatement;
import org.apache.shardingsphere.mask.rule.MaskRule;
import org.junit.jupiter.api.BeforeEach;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;

import java.sql.SQLException;
import java.util.Collections;
import java.util.LinkedList;

import static org.hamcrest.CoreMatchers.is;
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.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class DropMaskRuleExecutorTest {

private final DropMaskRuleExecutor executor = new DropMaskRuleExecutor();

@BeforeEach
void setUp() {
executor.setDatabase(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS));
}

@Test
void assertCheckSQLStatementWithoutToBeDroppedRule() {
void assertExecuteUpdateWithoutToBeDroppedRule() {
MaskRule rule = mock(MaskRule.class);
when(rule.getConfiguration()).thenReturn(new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap()));
executor.setRule(rule);
assertThrows(MissingRequiredRuleException.class, () -> executor.checkBeforeUpdate(createSQLStatement(false, "t_mask")));
assertThrows(MissingRequiredRuleException.class, () -> new DistSQLUpdateExecuteEngine(createSQLStatement(false, "t_mask"), "foo_db", mockContextManager(rule)).executeUpdate());
}

@Test
void assertUpdateCurrentRuleConfiguration() {
MaskRuleConfiguration ruleConfig = createCurrentRuleConfiguration();
void assertExecuteUpdateWithoutIfExists() throws SQLException {
MaskRule rule = mock(MaskRule.class);
MaskRuleConfiguration ruleConfig = createCurrentRuleConfiguration();
when(rule.getConfiguration()).thenReturn(ruleConfig);
executor.setRule(rule);
MaskRuleConfiguration toBeDroppedRuleConfig = executor.buildToBeDroppedRuleConfiguration(createSQLStatement(false, "T_MASK"));
assertThat(toBeDroppedRuleConfig.getTables().size(), is(1));
assertTrue(toBeDroppedRuleConfig.getMaskAlgorithms().isEmpty());
ContextManager contextManager = mockContextManager(rule);
new DistSQLUpdateExecuteEngine(createSQLStatement(false, "T_MASK"), "foo_db", contextManager).executeUpdate();
MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService();
metaDataManagerPersistService.alterRuleConfiguration(eq("foo_db"), ArgumentMatchers.argThat(this::assertRuleConfigurationWithoutIfExists));
}

private boolean assertRuleConfigurationWithoutIfExists(final MaskRuleConfiguration actual) {
assertThat(actual.getTables().size(), is(1));
assertTrue(actual.getMaskAlgorithms().isEmpty());
return true;
}

@Test
void assertUpdateCurrentRuleConfigurationWithIfExists() {
DropMaskRuleStatement statement = createSQLStatement(true, "T_USER");
void assertExecuteUpdateWithIfExists() throws SQLException {
MaskRule rule = mock(MaskRule.class);
when(rule.getConfiguration()).thenReturn(new MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap()));
executor.setRule(rule);
executor.checkBeforeUpdate(statement);
MaskRuleConfiguration ruleConfig = createCurrentRuleConfiguration();
when(rule.getConfiguration()).thenReturn(ruleConfig);
executor.setRule(rule);
MaskRuleConfiguration toBeDroppedRuleConfig = executor.buildToBeDroppedRuleConfiguration(statement);
assertThat(toBeDroppedRuleConfig.getTables().size(), is(1));
ContextManager contextManager = mockContextManager(rule);
new DistSQLUpdateExecuteEngine(createSQLStatement(true, "T_USER"), "foo_db", contextManager).executeUpdate();
MetaDataManagerPersistService metaDataManagerPersistService = contextManager.getPersistServiceFacade().getMetaDataManagerPersistService();
metaDataManagerPersistService.alterRuleConfiguration(eq("foo_db"), ArgumentMatchers.argThat(this::assertRuleConfigurationWithoutIfExists));
metaDataManagerPersistService.alterRuleConfiguration(eq("foo_db"), ArgumentMatchers.argThat(this::assertRuleConfigurationWithIfExists));
}

private DropMaskRuleStatement createSQLStatement(final boolean ifExists, final String tableName) {
Expand All @@ -89,4 +92,18 @@ private MaskRuleConfiguration createCurrentRuleConfiguration() {
MaskTableRuleConfiguration tableRuleConfig = new MaskTableRuleConfiguration("t_mask", Collections.singleton(columnRuleConfig));
return new MaskRuleConfiguration(new LinkedList<>(Collections.singleton(tableRuleConfig)), Collections.emptyMap());
}

private boolean assertRuleConfigurationWithIfExists(final MaskRuleConfiguration actual) {
assertThat(actual.getTables().size(), is(1));
return true;
}

private ContextManager mockContextManager(final MaskRule rule) {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getName()).thenReturn("foo_db");
when(database.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(rule)));
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);
return result;
}
}