Skip to content

Commit

Permalink
Refactor AlterStorageUnitExecutorTest (#30029)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Feb 6, 2024
1 parent 9e994cb commit b0d1b08
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
import org.apache.shardingsphere.test.mock.AutoMockExtension;
import org.apache.shardingsphere.test.mock.StaticMockSettings;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -50,60 +48,51 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(AutoMockExtension.class)
@StaticMockSettings(ProxyContext.class)
@ExtendWith(MockitoExtension.class)
class AlterStorageUnitExecutorTest {

private final AlterStorageUnitExecutor executor = new AlterStorageUnitExecutor();

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private ShardingSphereDatabase database;

private AlterStorageUnitExecutor executor;

@BeforeEach
void setUp() throws ReflectiveOperationException {
executor = new AlterStorageUnitExecutor();
executor.setDatabase(database);
Plugins.getMemberAccessor().set(executor.getClass().getDeclaredField("validateHandler"), executor, mock(DataSourcePoolPropertiesValidator.class));
}

@Test
void assertExecuteUpdate() {
void assertExecuteUpdateSuccess() {
ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS);
StorageUnit storageUnit = mock(StorageUnit.class, RETURNS_DEEP_STUBS);
ConnectionProperties connectionProps = mockConnectionProperties("ds_0");
when(storageUnit.getConnectionProperties()).thenReturn(connectionProps);
when(resourceMetaData.getStorageUnits()).thenReturn(Collections.singletonMap("ds_0", storageUnit));
when(database.getResourceMetaData()).thenReturn(resourceMetaData);
executor.setDatabase(database);
assertDoesNotThrow(() -> executor.executeUpdate(createAlterStorageUnitStatement("ds_0"), mockContextManager(mock(MetaDataContexts.class, RETURNS_DEEP_STUBS))));
}

@Test
void assertExecuteUpdateWithDuplicateStorageUnitNames() {
executor.setDatabase(database);
assertThrows(DuplicateStorageUnitException.class, () -> executor.executeUpdate(createAlterStorageUnitStatementWithDuplicateStorageUnitNames(), mock(ContextManager.class)));
}

@Test
void assertExecuteUpdateWithNotExistedStorageUnitNames() {
MetaDataContexts metaDataContexts = mock(MetaDataContexts.class, RETURNS_DEEP_STUBS);
ContextManager contextManager = mockContextManager(metaDataContexts);
when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
executor.setDatabase(database);
assertThrows(MissingRequiredStorageUnitsException.class, () -> executor.executeUpdate(createAlterStorageUnitStatement("not_existed"), mock(ContextManager.class)));
assertThrows(MissingRequiredStorageUnitsException.class,
() -> executor.executeUpdate(createAlterStorageUnitStatement("not_existed"), mockContextManager(mock(MetaDataContexts.class, RETURNS_DEEP_STUBS))));
}

@Test
void assertExecuteUpdateWithAlterDatabase() {
ContextManager contextManager = mockContextManager(mock(MetaDataContexts.class, RETURNS_DEEP_STUBS));
when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager);
ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS);
StorageUnit storageUnit = mock(StorageUnit.class, RETURNS_DEEP_STUBS);
ConnectionProperties connectionProps = mockConnectionProperties("ds_1");
when(storageUnit.getConnectionProperties()).thenReturn(connectionProps);
when(resourceMetaData.getStorageUnits()).thenReturn(Collections.singletonMap("ds_0", storageUnit));
when(database.getResourceMetaData()).thenReturn(resourceMetaData);
executor.setDatabase(database);
assertThrows(InvalidStorageUnitsException.class, () -> executor.executeUpdate(createAlterStorageUnitStatement("ds_0"), mock(ContextManager.class)));
assertThrows(InvalidStorageUnitsException.class, () -> executor.executeUpdate(createAlterStorageUnitStatement("ds_0"), mockContextManager(mock(MetaDataContexts.class, RETURNS_DEEP_STUBS))));
}

private ContextManager mockContextManager(final MetaDataContexts metaDataContexts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.shardingsphere.distsql.handler.exception.storageunit.DuplicateStorageUnitException;
import org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException;
import org.apache.shardingsphere.distsql.handler.validate.DataSourcePoolPropertiesValidator;
import org.apache.shardingsphere.distsql.segment.DataSourceSegment;
import org.apache.shardingsphere.distsql.segment.HostnameAndPortBasedDataSourceSegment;
import org.apache.shardingsphere.distsql.segment.URLBasedDataSourceSegment;
import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.RegisterStorageUnitStatement;
Expand All @@ -38,9 +37,8 @@
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import java.util.Collection;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
Expand All @@ -53,38 +51,33 @@
@MockitoSettings(strictness = Strictness.LENIENT)
class RegisterStorageUnitExecutorTest {

private final RegisterStorageUnitExecutor executor = new RegisterStorageUnitExecutor();

@Mock
private ShardingSphereDatabase database;

private RegisterStorageUnitExecutor executor;

@BeforeEach
void setUp() throws ReflectiveOperationException {
when(database.getName()).thenReturn("foo_db");
when(database.getRuleMetaData()).thenReturn(mock(RuleMetaData.class));
executor = new RegisterStorageUnitExecutor();
executor.setDatabase(database);
Plugins.getMemberAccessor().set(executor.getClass().getDeclaredField("validateHandler"), executor, mock(DataSourcePoolPropertiesValidator.class));
}

@Test
void assertExecute() {
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(contextManager.getMetaDataContexts()).thenReturn(mock(MetaDataContexts.class, RETURNS_DEEP_STUBS));
executor.setDatabase(database);
assertDoesNotThrow(() -> executor.executeUpdate(createRegisterStorageUnitStatement(), contextManager));
void assertExecuteUpdateSuccess() {
assertDoesNotThrow(() -> executor.executeUpdate(createRegisterStorageUnitStatement(), mock(ContextManager.class, RETURNS_DEEP_STUBS)));
}

@Test
void assertExecuteUpdateWithDuplicateStorageUnitNamesInStatement() {
executor.setDatabase(database);
assertThrows(DuplicateStorageUnitException.class, () -> executor.executeUpdate(createRegisterStorageUnitStatementWithDuplicateStorageUnitNames(), mock(ContextManager.class)));
}

@Test
void assertExecuteUpdateWithDuplicateStorageUnitNamesWithResourceMetaData() {
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(contextManager.getStorageUnits("foo_db").keySet()).thenReturn(Collections.singleton("ds_0"));
executor.setDatabase(database);
assertThrows(DuplicateStorageUnitException.class, () -> executor.executeUpdate(createRegisterStorageUnitStatement(), contextManager));
}

Expand All @@ -95,7 +88,6 @@ void assertExecuteUpdateWithDuplicateStorageUnitNamesWithDataSourceContainedRule
DataSourceContainedRule rule = mock(DataSourceContainedRule.class);
when(rule.getDataSourceMapper()).thenReturn(Collections.singletonMap("ds_0", Collections.emptyList()));
when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.singleton(rule));
executor.setDatabase(database);
assertThrows(InvalidStorageUnitsException.class, () -> executor.executeUpdate(createRegisterStorageUnitStatement(), contextManager));
}

Expand All @@ -104,9 +96,8 @@ private RegisterStorageUnitStatement createRegisterStorageUnitStatement() {
}

private RegisterStorageUnitStatement createRegisterStorageUnitStatementWithDuplicateStorageUnitNames() {
Collection<DataSourceSegment> result = new LinkedList<>();
result.add(new HostnameAndPortBasedDataSourceSegment("ds_0", "127.0.0.1", "3306", "ds_0", "root", "", new Properties()));
result.add(new URLBasedDataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/ds_1", "root", "", new Properties()));
return new RegisterStorageUnitStatement(false, result);
return new RegisterStorageUnitStatement(false, Arrays.asList(
new HostnameAndPortBasedDataSourceSegment("ds_0", "127.0.0.1", "3306", "ds_0", "root", "", new Properties()),
new URLBasedDataSourceSegment("ds_0", "jdbc:mysql://127.0.0.1:3306/ds_1", "root", "", new Properties())));
}
}

0 comments on commit b0d1b08

Please sign in to comment.