From b0d1b085577f0e794bb40dd9115add9aee0a0004 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Tue, 6 Feb 2024 23:25:24 +0800 Subject: [PATCH] Refactor AlterStorageUnitExecutorTest (#30029) --- .../AlterStorageUnitExecutorTest.java | 29 ++++++------------- .../RegisterStorageUnitExecutorTest.java | 27 ++++++----------- 2 files changed, 18 insertions(+), 38 deletions(-) diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/resource/AlterStorageUnitExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/resource/AlterStorageUnitExecutorTest.java index 9a95d3925291d..0ba7ca15b211b 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/resource/AlterStorageUnitExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/resource/AlterStorageUnitExecutorTest.java @@ -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; @@ -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) { diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/resource/RegisterStorageUnitExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/resource/RegisterStorageUnitExecutorTest.java index 4a6790ffa7310..2aa60d75cb8ed 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/resource/RegisterStorageUnitExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/resource/RegisterStorageUnitExecutorTest.java @@ -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; @@ -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; @@ -53,30 +51,26 @@ @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))); } @@ -84,7 +78,6 @@ void assertExecuteUpdateWithDuplicateStorageUnitNamesInStatement() { 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)); } @@ -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)); } @@ -104,9 +96,8 @@ private RegisterStorageUnitStatement createRegisterStorageUnitStatement() { } private RegisterStorageUnitStatement createRegisterStorageUnitStatementWithDuplicateStorageUnitNames() { - Collection 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()))); } }