You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2024/09/25 02:28:08 UTC

(shardingsphere) branch master updated: Add test cases on DataSourceUnitPersistService (#32991)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 f3b30101c3d Add test cases on DataSourceUnitPersistService (#32991)
f3b30101c3d is described below

commit f3b30101c3dc5f59840498ae8ca6977ec7e5922a
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Sep 25 10:28:02 2024 +0800

    Add test cases on DataSourceUnitPersistService (#32991)
---
 .../database/DataSourceUnitPersistService.java     | 46 ++++++++---------
 .../database/DataSourceUnitPersistServiceTest.java | 57 ++++++++++++++++++++++
 2 files changed, 80 insertions(+), 23 deletions(-)

diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/DataSourceUnitPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/DataSourceUnitPersistService.java
index 18c3a13eb42..164eac5f16f 100644
--- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/DataSourceUnitPersistService.java
+++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/database/DataSourceUnitPersistService.java
@@ -41,25 +41,6 @@ public final class DataSourceUnitPersistService {
     
     private final PersistRepository repository;
     
-    /**
-     * Persist data source pool configurations.
-     *
-     * @param databaseName database name
-     * @param dataSourceConfigs data source pool configurations
-     */
-    public void persist(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
-        for (Entry<String, DataSourcePoolProperties> entry : dataSourceConfigs.entrySet()) {
-            String activeVersion = getDataSourceActiveVersion(databaseName, entry.getKey());
-            List<String> versions = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceUnitVersionsNode(databaseName, entry.getKey()));
-            repository.persist(DataSourceMetaDataNode.getDataSourceUnitVersionNode(databaseName, entry.getKey(), versions.isEmpty()
-                    ? MetaDataVersion.DEFAULT_VERSION
-                    : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue())));
-            if (Strings.isNullOrEmpty(activeVersion)) {
-                repository.persist(DataSourceMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, entry.getKey()), MetaDataVersion.DEFAULT_VERSION);
-            }
-        }
-    }
-    
     /**
      * Load data source pool configurations.
      *
@@ -97,13 +78,22 @@ public final class DataSourceUnitPersistService {
     }
     
     /**
-     * Delete data source pool configurations.
+     * Persist data source pool configurations.
      *
      * @param databaseName database name
-     * @param dataSourceName data source name
+     * @param dataSourceConfigs data source pool configurations
      */
-    public void delete(final String databaseName, final String dataSourceName) {
-        repository.delete(DataSourceMetaDataNode.getDataSourceUnitNode(databaseName, dataSourceName));
+    public void persist(final String databaseName, final Map<String, DataSourcePoolProperties> dataSourceConfigs) {
+        for (Entry<String, DataSourcePoolProperties> entry : dataSourceConfigs.entrySet()) {
+            String activeVersion = getDataSourceActiveVersion(databaseName, entry.getKey());
+            List<String> versions = repository.getChildrenKeys(DataSourceMetaDataNode.getDataSourceUnitVersionsNode(databaseName, entry.getKey()));
+            repository.persist(DataSourceMetaDataNode.getDataSourceUnitVersionNode(databaseName, entry.getKey(), versions.isEmpty()
+                    ? MetaDataVersion.DEFAULT_VERSION
+                    : String.valueOf(Integer.parseInt(versions.get(0)) + 1)), YamlEngine.marshal(new YamlDataSourceConfigurationSwapper().swapToMap(entry.getValue())));
+            if (Strings.isNullOrEmpty(activeVersion)) {
+                repository.persist(DataSourceMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, entry.getKey()), MetaDataVersion.DEFAULT_VERSION);
+            }
+        }
     }
     
     /**
@@ -131,4 +121,14 @@ public final class DataSourceUnitPersistService {
     private String getDataSourceActiveVersion(final String databaseName, final String dataSourceName) {
         return repository.query(DataSourceMetaDataNode.getDataSourceUnitActiveVersionNode(databaseName, dataSourceName));
     }
+    
+    /**
+     * Delete data source pool configurations.
+     *
+     * @param databaseName database name
+     * @param dataSourceName data source name
+     */
+    public void delete(final String databaseName, final String dataSourceName) {
+        repository.delete(DataSourceMetaDataNode.getDataSourceUnitNode(databaseName, dataSourceName));
+    }
 }
diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/database/DataSourceUnitPersistServiceTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/database/DataSourceUnitPersistServiceTest.java
new file mode 100644
index 00000000000..3da5fb90201
--- /dev/null
+++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/service/config/database/DataSourceUnitPersistServiceTest.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.metadata.persist.service.config.database;
+
+import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
+import org.apache.shardingsphere.mode.spi.PersistRepository;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Arrays;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class DataSourceUnitPersistServiceTest {
+    
+    private DataSourceUnitPersistService persistService;
+    
+    @Mock
+    private PersistRepository repository;
+    
+    @BeforeEach
+    void setUp() {
+        persistService = new DataSourceUnitPersistService(repository);
+        when(repository.query("/metadata/foo_db/data_sources/units/foo_ds/active_version")).thenReturn("10");
+    }
+    
+    @Test
+    void assertLoad() {
+        when(repository.getChildrenKeys("/metadata/foo_db/data_sources/units")).thenReturn(Arrays.asList("foo_ds", "bar_ds"));
+        when(repository.query("/metadata/foo_db/data_sources/units/foo_ds/versions/10")).thenReturn("{dataSourceClassName: org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource}");
+        Map<String, DataSourcePoolProperties> actual = persistService.load("foo_db");
+        assertThat(actual.size(), is(1));
+        assertThat(actual.get("foo_ds").getPoolClassName(), is("org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource"));
+    }
+}