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 9d2a86a296b Add test cases for GlobalRuleConfigurationEventSubscriber
(#32882)
9d2a86a296b is described below
commit 9d2a86a296b38d2302cb96e911360978e8d15b0f
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Sep 15 18:04:15 2024 +0800
Add test cases for GlobalRuleConfigurationEventSubscriber (#32882)
---
.../GlobalRuleConfigurationEventSubscriber.java | 7 +--
...GlobalRuleConfigurationEventSubscriberTest.java | 57 ++++++++++++++++++++++
2 files changed, 61 insertions(+), 3 deletions(-)
diff --git
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriber.java
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriber.java
index 6e5c847b29b..d098cb6aba8 100644
---
a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriber.java
+++
b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriber.java
@@ -42,11 +42,12 @@ public final class GlobalRuleConfigurationEventSubscriber
implements EventSubscr
*
* @param event global rule alter event
*/
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({"unchecked", "unused"})
@Subscribe
public synchronized void renew(final AlterGlobalRuleConfigurationEvent
event) {
-
Preconditions.checkArgument(event.getActiveVersion().equals(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService()
- .getActiveVersionByFullPath(event.getActiveVersionKey())),
"Invalid active version: %s of key: %s", event.getActiveVersion(),
event.getActiveVersionKey());
+ Preconditions.checkArgument(event.getActiveVersion().equals(
+
contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath(event.getActiveVersionKey())),
+ "Invalid active version: %s of key: %s",
event.getActiveVersion(), event.getActiveVersionKey());
Optional<RuleConfiguration> ruleConfig =
contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().load(event.getRuleSimpleName());
Preconditions.checkArgument(ruleConfig.isPresent(), "Can not find rule
configuration with name: %s", event.getRuleSimpleName());
contextManager.getMetaDataContextManager().getGlobalConfigurationManager().alterGlobalRuleConfiguration(
diff --git
a/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriberTest.java
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriberTest.java
new file mode 100644
index 00000000000..aafa9f41f82
--- /dev/null
+++
b/mode/type/cluster/core/src/test/java/org/apache/shardingsphere/mode/manager/cluster/event/subscriber/dispatch/GlobalRuleConfigurationEventSubscriberTest.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package
org.apache.shardingsphere.mode.manager.cluster.event.subscriber.dispatch;
+
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import
org.apache.shardingsphere.mode.event.dispatch.config.AlterGlobalRuleConfigurationEvent;
+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 java.util.Optional;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class GlobalRuleConfigurationEventSubscriberTest {
+
+ private GlobalRuleConfigurationEventSubscriber subscriber;
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private ContextManager contextManager;
+
+ @BeforeEach
+ void setUp() {
+ subscriber = new
GlobalRuleConfigurationEventSubscriber(contextManager);
+ }
+
+ @Test
+ void assertRenew() {
+
when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getMetaDataVersionPersistService().getActiveVersionByFullPath("key")).thenReturn("value");
+
when(contextManager.getPersistServiceFacade().getMetaDataPersistService().getGlobalRuleService().load("foo")).thenReturn(Optional.of(mock(RuleConfiguration.class)));
+ subscriber.renew(new AlterGlobalRuleConfigurationEvent("foo", "key",
"value"));
+
verify(contextManager.getMetaDataContextManager().getGlobalConfigurationManager()).alterGlobalRuleConfiguration(any());
+ }
+}