You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2021/09/01 11:58:15 UTC

[shardingsphere] branch master updated: Load from YAML file. (#12155)

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 aff2d7b  Load from YAML file. (#12155)
aff2d7b is described below

commit aff2d7b01fcb83a0802dd02624acc2075b3f7a53
Author: Guocheng Tang <to...@qq.com>
AuthorDate: Wed Sep 1 19:57:44 2021 +0800

    Load from YAML file. (#12155)
    
    * Load from YAML file.
---
 .../service/impl/DataSourcePersistServiceTest.java | 19 ++++++---------
 .../resources/yaml/persist/data-source-foo.yaml    | 28 ++++++++++++++++++++++
 2 files changed, 35 insertions(+), 12 deletions(-)

diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/persist/service/impl/DataSourcePersistServiceTest.java b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/persist/service/impl/DataSourcePersistServiceTest.java
index 806227b..27e748c 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/persist/service/impl/DataSourcePersistServiceTest.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/test/java/org/apache/shardingsphere/mode/persist/service/impl/DataSourcePersistServiceTest.java
@@ -18,6 +18,7 @@
 package org.apache.shardingsphere.mode.persist.service.impl;
 
 import lombok.SneakyThrows;
+import org.apache.commons.lang3.StringUtils;
 import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
 import org.apache.shardingsphere.mode.persist.PersistRepository;
 import org.apache.shardingsphere.test.mock.MockedDataSource;
@@ -50,7 +51,7 @@ public final class DataSourcePersistServiceTest {
     
     @Test
     public void assertLoad() {
-        when(repository.get("/metadata/foo_db/dataSources")).thenReturn(readDataSourceYaml());
+        when(repository.get("/metadata/foo_db/dataSources")).thenReturn(readDataSourceYaml("yaml/persist/data-source.yaml"));
         Map<String, DataSourceConfiguration> actual = new DataSourcePersistService(repository).load("foo_db");
         assertThat(actual.size(), is(2));
         assertDataSourceConfiguration(actual.get("ds_0"), DataSourceConfiguration.getDataSourceConfiguration(createDataSource("ds_0")));
@@ -58,9 +59,9 @@ public final class DataSourcePersistServiceTest {
     }
     
     @SneakyThrows({IOException.class, URISyntaxException.class})
-    private String readDataSourceYaml() {
-        return Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/persist/data-source.yaml").toURI()))
-                .stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining());
+    private String readDataSourceYaml(final String path) {
+        return Files.readAllLines(Paths.get(ClassLoader.getSystemResource(path).toURI()))
+                .stream().filter(each -> StringUtils.isNotBlank(each) && !each.startsWith("#")).map(each -> each + System.lineSeparator()).collect(Collectors.joining());
     }
     
     private void assertDataSourceConfiguration(final DataSourceConfiguration actual, final DataSourceConfiguration expected) {
@@ -82,19 +83,13 @@ public final class DataSourcePersistServiceTest {
     public void assertAppend() {
         when(repository.get("/metadata/foo_db/dataSources")).thenReturn("");
         new DataSourcePersistService(repository).append("foo_db", Collections.singletonMap("foo_ds", DataSourceConfiguration.getDataSourceConfiguration(createDataSource("foo_ds"))));
-        // TODO load from YAML file
-        String expected = "foo_ds:\n" + "  driverClassName: com.mysql.jdbc.Driver\n" + "  password: root\n"
-                + "  dataSourceClassName: org.apache.shardingsphere.test.mock.MockedDataSource\n" + "  connectionInitSqls:\n" + "  - set names utf8mb4;\n"
-                + "  - set names utf8;\n" + "  url: jdbc:mysql://localhost:3306/foo_ds\n" + "  username: root\n";
+        String expected = readDataSourceYaml("yaml/persist/data-source-foo.yaml");
         verify(repository).persist("/metadata/foo_db/dataSources", expected);
     }
     
     @Test
     public void assertDrop() {
-        // TODO load from YAML file
-        String actual = "foo_ds:\n" + "  driverClassName: com.mysql.jdbc.Driver\n" + "  password: root\n"
-                + "  dataSourceClassName: org.apache.shardingsphere.test.mock.MockedDataSource\n" + "  connectionInitSqls:\n" + "  - set names utf8mb4;\n"
-                + "  - set names utf8;\n" + "  url: jdbc:mysql://localhost:3306/foo_ds\n" + "  username: root\n";
+        String actual = readDataSourceYaml("yaml/persist/data-source-foo.yaml");
         when(repository.get("/metadata/foo_db/dataSources")).thenReturn(actual);
         new DataSourcePersistService(repository).drop("foo_db", Collections.singleton("foo_ds"));
         verify(repository).persist("/metadata/foo_db/dataSources", "{}\n");
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/persist/data-source-foo.yaml b/shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/persist/data-source-foo.yaml
new file mode 100644
index 0000000..bccc459
--- /dev/null
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/test/resources/yaml/persist/data-source-foo.yaml
@@ -0,0 +1,28 @@
+#
+# 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.
+#
+
+foo_ds:
+  driverClassName: com.mysql.jdbc.Driver
+  password: root
+  dataSourceClassName: org.apache.shardingsphere.test.mock.MockedDataSource
+  connectionInitSqls:
+# No spaces after conversion
+  - set names utf8mb4;
+  - set names utf8;
+  url: jdbc:mysql://localhost:3306/foo_ds
+  username: root
+  
\ No newline at end of file