This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 1cd6892eda2 Add test cases on QualifiedDataSourceSubscriber (#32889)
1cd6892eda2 is described below
commit 1cd6892eda24a3577c8fbac002a955a5b085ff9c
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Sep 15 21:08:43 2024 +0800
Add test cases on QualifiedDataSourceSubscriber (#32889)
---
.../QualifiedDataSourceSubscriberTest.java | 69 ++++------------------
1 file changed, 12 insertions(+), 57 deletions(-)
diff --git
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/QualifiedDataSourceSubscriberTest.java
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/QualifiedDataSourceSubscriberTest.java
index 5de411f0785..6af27c79fb1 100644
---
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/QualifiedDataSourceSubscriberTest.java
+++
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/QualifiedDataSourceSubscriberTest.java
@@ -17,91 +17,46 @@
package
org.apache.shardingsphere.mode.manager.cluster.event.subscriber.dispatch;
-import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
-import
org.apache.shardingsphere.infra.instance.metadata.proxy.ProxyInstanceMetaData;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDataSource;
-import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import
org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.state.datasource.DataSourceState;
-import org.apache.shardingsphere.infra.util.eventbus.EventBusContext;
-import org.apache.shardingsphere.mode.manager.ContextManager;
-import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
-import
org.apache.shardingsphere.mode.manager.cluster.ClusterContextManagerBuilder;
-import
org.apache.shardingsphere.mode.event.dispatch.state.storage.QualifiedDataSourceStateEvent;
-import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory;
-import
org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;
import
org.apache.shardingsphere.infra.state.datasource.qualified.QualifiedDataSourceState;
+import
org.apache.shardingsphere.mode.event.dispatch.state.storage.QualifiedDataSourceStateEvent;
+import org.apache.shardingsphere.mode.manager.ContextManager;
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.junit.jupiter.MockitoExtension;
-import org.mockito.junit.jupiter.MockitoSettings;
-import org.mockito.quality.Strictness;
-import java.sql.SQLException;
import java.util.Collections;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.Objects;
-import java.util.Properties;
-import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
-@MockitoSettings(strictness = Strictness.LENIENT)
class QualifiedDataSourceSubscriberTest {
private QualifiedDataSourceSubscriber subscriber;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
- private ShardingSphereDatabase database;
+ private ContextManager contextManager;
@BeforeEach
- void setUp() throws SQLException {
- EventBusContext eventBusContext = new EventBusContext();
- ContextManager contextManager = new
ClusterContextManagerBuilder().build(createContextManagerBuilderParameter(),
eventBusContext);
-
contextManager.renewMetaDataContexts(MetaDataContextsFactory.create(contextManager.getPersistServiceFacade().getMetaDataPersistService(),
new ShardingSphereMetaData(createDatabases(),
-
contextManager.getMetaDataContexts().getMetaData().getGlobalResourceMetaData(),
contextManager.getMetaDataContexts().getMetaData().getGlobalRuleMetaData(),
- new ConfigurationProperties(new Properties()))));
+ void setUp() {
subscriber = new QualifiedDataSourceSubscriber(contextManager);
}
@Test
- void assertRenewForDisableStateChanged() {
- StaticDataSourceRuleAttribute ruleAttribute =
mock(StaticDataSourceRuleAttribute.class);
-
when(database.getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class)).thenReturn(Collections.singleton(ruleAttribute));
- QualifiedDataSourceStateEvent event = new
QualifiedDataSourceStateEvent(new QualifiedDataSource("db.readwrite_ds.ds_0"),
new QualifiedDataSourceState(DataSourceState.DISABLED));
- subscriber.renew(event);
- verify(ruleAttribute).updateStatus(
- argThat(qualifiedDataSource ->
Objects.equals(event.getQualifiedDataSource(), qualifiedDataSource)),
- argThat(dataSourceState -> event.getStatus().getState() ==
dataSourceState));
- }
-
- private ContextManagerBuilderParameter
createContextManagerBuilderParameter() {
- ModeConfiguration modeConfig = new ModeConfiguration("Cluster", new
ClusterPersistRepositoryConfiguration("FIXTURE", "", "", new Properties()));
- InstanceMetaData instanceMetaData = new
ProxyInstanceMetaData("foo_instance_id", 3307);
- return new ContextManagerBuilderParameter(modeConfig,
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyList(),
- new Properties(), Collections.emptyList(), instanceMetaData,
false);
- }
-
- private Map<String, ShardingSphereDatabase> createDatabases() {
-
when(database.getSchemas()).thenReturn(Collections.singletonMap("foo_schema",
new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME)));
-
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class,
"FIXTURE"));
-
when(database.getSchema("foo_schema")).thenReturn(mock(ShardingSphereSchema.class));
- when(database.getRuleMetaData().getRules()).thenReturn(new
LinkedList<>());
-
when(database.getRuleMetaData().getConfigurations()).thenReturn(Collections.emptyList());
- return Collections.singletonMap("db", database);
+ void assertRenew() {
+
when(contextManager.getMetaDataContexts().getMetaData().containsDatabase("foo_db")).thenReturn(true);
+ StaticDataSourceRuleAttribute staticDataSourceRuleAttribute =
mock(StaticDataSourceRuleAttribute.class);
+
when(contextManager.getMetaDataContexts().getMetaData().getDatabase("foo_db").getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class))
+
.thenReturn(Collections.singleton(staticDataSourceRuleAttribute));
+ QualifiedDataSource qualifiedDataSource = new
QualifiedDataSource("foo_db.readwrite_ds.ds_0");
+ subscriber.renew(new
QualifiedDataSourceStateEvent(qualifiedDataSource, new
QualifiedDataSourceState(DataSourceState.DISABLED)));
+
verify(staticDataSourceRuleAttribute).updateStatus(qualifiedDataSource,
DataSourceState.DISABLED);
}
}