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 e9a34ae610c Add coverage for meta data import and lock/txn operations
(#37495)
e9a34ae610c is described below
commit e9a34ae610c80c474202875faba65664e4c097c8
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Dec 24 20:39:30 2025 +0800
Add coverage for meta data import and lock/txn operations (#37495)
* Add coverage for meta data import and lock/txn operations
* Add coverage for meta data import and lock/txn operations
---
.../proxy/backend/util/MetaDataImportExecutor.java | 2 -
.../TransactionCommitOperationTest.java | 31 ++++++
.../updatable/lock/LockClusterOperationTest.java | 31 ++++++
.../backend/util/MetaDataImportExecutorTest.java | 115 +++++++++++++++++++++
4 files changed, 177 insertions(+), 2 deletions(-)
diff --git
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/MetaDataImportExecutor.java
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/MetaDataImportExecutor.java
index 01954ab7b02..115292ed26a 100644
---
a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/MetaDataImportExecutor.java
+++
b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/MetaDataImportExecutor.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.proxy.backend.util;
-import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine;
@@ -35,7 +34,6 @@ import java.util.stream.Collectors;
/**
* Metadata import executor.
*/
-@RequiredArgsConstructor
public final class MetaDataImportExecutor {
private final YamlRuleConfigurationSwapperEngine ruleConfigSwapperEngine =
new YamlRuleConfigurationSwapperEngine();
diff --git
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/TransactionCommitOperationTest.java
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/TransactionCommitOperationTest.java
new file mode 100644
index 00000000000..baebb544c13
--- /dev/null
+++
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/transaction/TransactionCommitOperationTest.java
@@ -0,0 +1,31 @@
+/*
+ * 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.proxy.backend.connector.jdbc.transaction;
+
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class TransactionCommitOperationTest {
+
+ @Test
+ void assertGetName() {
+ assertThat(new TransactionCommitOperation().getName(),
is("transaction_commit"));
+ }
+}
diff --git
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/lock/LockClusterOperationTest.java
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/lock/LockClusterOperationTest.java
new file mode 100644
index 00000000000..022c60b93a0
--- /dev/null
+++
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/lock/LockClusterOperationTest.java
@@ -0,0 +1,31 @@
+/*
+ * 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.proxy.backend.handler.distsql.ral.updatable.lock;
+
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class LockClusterOperationTest {
+
+ @Test
+ void assertGetName() {
+ assertThat(new LockClusterOperation().getName(), is("lock_cluster"));
+ }
+}
diff --git
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/util/MetaDataImportExecutorTest.java
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/util/MetaDataImportExecutorTest.java
new file mode 100644
index 00000000000..e67a6bfceee
--- /dev/null
+++
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/util/MetaDataImportExecutorTest.java
@@ -0,0 +1,115 @@
+/*
+ * 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.proxy.backend.util;
+
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import
org.apache.shardingsphere.mode.persist.service.MetaDataManagerPersistService;
+import
org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConfiguration;
+import
org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyServerConfiguration;
+import org.apache.shardingsphere.proxy.backend.distsql.export.ExportedMetaData;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Answers;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.internal.configuration.plugins.Plugins;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Collections;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.ArgumentMatchers.anyCollection;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class MetaDataImportExecutorTest {
+
+ @Mock
+ private YamlDatabaseConfigurationImportExecutor
databaseConfigImportExecutor;
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private ContextManager contextManager;
+
+ @Test
+ void assertImportClusterConfigurationsWithoutServerConfiguration() {
+ ExportedMetaData exportedMetaData = createExportedMetaData();
+ MetaDataImportExecutor executor = new
MetaDataImportExecutor(contextManager);
+ setField(executor, "databaseConfigImportExecutor",
databaseConfigImportExecutor);
+ YamlProxyDatabaseConfiguration databaseConfig = new
YamlProxyDatabaseConfiguration();
+ databaseConfig.setDatabaseName("logic_db");
+ try (MockedStatic<YamlEngine> mockedStatic =
mockStatic(YamlEngine.class)) {
+ mockedStatic.when(() -> YamlEngine.unmarshal(anyString(),
eq(YamlProxyServerConfiguration.class))).thenReturn(null);
+ mockedStatic.when(() -> YamlEngine.unmarshal(anyString(),
eq(YamlProxyDatabaseConfiguration.class))).thenReturn(databaseConfig);
+ executor.importClusterConfigurations(exportedMetaData);
+ }
+
verify(databaseConfigImportExecutor).importDatabaseConfiguration(databaseConfig);
+ }
+
+ @Test
+ void assertImportClusterConfigurations() {
+ MetaDataManagerPersistService metaDataManagerService =
mock(MetaDataManagerPersistService.class);
+
when(contextManager.getPersistServiceFacade().getModeFacade().getMetaDataManagerService()).thenReturn(metaDataManagerService);
+ ExportedMetaData exportedMetaData = createExportedMetaData();
+ MetaDataImportExecutor executor = new
MetaDataImportExecutor(contextManager);
+ setField(executor, "databaseConfigImportExecutor",
databaseConfigImportExecutor);
+ YamlRuleConfigurationSwapperEngine swapperEngine =
mock(YamlRuleConfigurationSwapperEngine.class);
+ RuleConfiguration ruleConfig = mock(RuleConfiguration.class);
+
when(swapperEngine.swapToRuleConfigurations(anyCollection())).thenReturn(Collections.singleton(ruleConfig));
+ setField(executor, "ruleConfigSwapperEngine", swapperEngine);
+ Properties props = new Properties();
+ YamlProxyServerConfiguration serverConfig = new
YamlProxyServerConfiguration();
+ serverConfig.setProps(props);
+ YamlProxyDatabaseConfiguration databaseConfig = new
YamlProxyDatabaseConfiguration();
+ databaseConfig.setDatabaseName("logic_db");
+ try (MockedStatic<YamlEngine> mockedStatic =
mockStatic(YamlEngine.class)) {
+ mockedStatic.when(() -> YamlEngine.unmarshal(anyString(),
eq(YamlProxyServerConfiguration.class))).thenReturn(serverConfig);
+ mockedStatic.when(() -> YamlEngine.unmarshal(anyString(),
eq(YamlProxyDatabaseConfiguration.class))).thenReturn(databaseConfig);
+ executor.importClusterConfigurations(exportedMetaData);
+ }
+
verify(metaDataManagerService).alterGlobalRuleConfiguration(ruleConfig);
+ verify(metaDataManagerService).alterProperties(props);
+ ArgumentCaptor<YamlProxyDatabaseConfiguration> captor =
ArgumentCaptor.forClass(YamlProxyDatabaseConfiguration.class);
+
verify(databaseConfigImportExecutor).importDatabaseConfiguration(captor.capture());
+ assertThat(captor.getValue().getDatabaseName(), is("logic_db"));
+ }
+
+ private ExportedMetaData createExportedMetaData() {
+ ExportedMetaData result = new ExportedMetaData();
+ result.setDatabases(Collections.singletonMap("logic_db",
"databaseName: logic_db"));
+ result.setProps("");
+ result.setRules("");
+ return result;
+ }
+
+ @SneakyThrows(ReflectiveOperationException.class)
+ private void setField(final Object target, final String fieldName, final
Object value) {
+
Plugins.getMemberAccessor().set(MetaDataImportExecutor.class.getDeclaredField(fieldName),
target, value);
+ }
+}