/*
 * 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.infra.yaml.swapper;

import org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public final class YamlDataSourceConfigurationSwapperTest {
    
    private final YamlDataSourceConfigurationSwapper swapper = new YamlDataSourceConfigurationSwapper();
    
    @Test
    public void assertSwapToDataSourceConfiguration() {
        Map<String, Object> yamlConfig = new HashMap<>(3, 1);
        yamlConfig.put("dataSourceClassName", "xxx.jdbc.driver");
        yamlConfig.put("url", "xx:xxx");
        yamlConfig.put("username", "root");
        DataSourceConfiguration actual = swapper.swapToDataSourceConfiguration(yamlConfig);
        assertThat(actual.getDataSourceClassName(), is("xxx.jdbc.driver"));
        assertThat(actual.getProps().size(), is(2));
        assertThat(actual.getProps().get("url").toString(), is("xx:xxx"));
        assertThat(actual.getProps().get("username").toString(), is("root"));
    }
    
    @Test
    public void assertSwapToMap() {
        DataSourceConfiguration dataSourceConfig = new DataSourceConfiguration("xxx.jdbc.driver");
        dataSourceConfig.getProps().put("url", "xx:xxx");
        dataSourceConfig.getProps().put("username", "root");
        Map<String, Object> actual = swapper.swapToMap(dataSourceConfig);
        assertThat(actual.get("dataSourceClassName"), is("xxx.jdbc.driver"));
        assertThat(actual.get("url").toString(), is("xx:xxx"));
        assertThat(actual.get("username").toString(), is("root"));
    }
}

@@ -851,16 +851,59 @@
 apper;%0A%0A
+import com.zaxxer.hikari.HikariDataSource;%0A
 import o
@@ -1011,22 +1011,83 @@
 ort java
-.util.
+x.sql.DataSource;%0Aimport java.util.HashMap;%0Aimport java.util.Linked
 HashMap;
@@ -1368,20 +1368,920 @@
 pper();%0A
+%0A
     
+@Test%0A    public void assertSwapToDataSources() %7B%0A        Map%3CString, Map%3CString, Object%3E%3E yamlConfig = createYamlConfig();%0A        Map%3CString, DataSource%3E dataSources = swapper.swapToDataSources(yamlConfig);%0A        HikariDataSource actual0 = (HikariDataSource) dataSources.get(%22ds_0%22);%0A        assertThat(actual0.getDriverClassName(), is(%22org.h2.Driver%22));%0A        assertThat(actual0.getJdbcUrl(), is(%22jdbc:h2:mem:test_ds_0;MODE=MySQL%22));%0A        assertThat(actual0.getUsername(), is(%22root%22));%0A        assertThat(actual0.getPassword(), is(%22root%22));%0A        HikariDataSource actual1 = (HikariDataSource) dataSources.get(%22ds_1%22);%0A        assertThat(actual1.getDriverClassName(), is(%22org.h2.Driver%22));%0A        assertThat(actual1.getJdbcUrl(), is(%22jdbc:h2:mem:test_ds_1;MODE=MySQL%22));%0A        assertThat(actual1.getUsername(), is(%22root%22));%0A        assertThat(actual1.getPassword(), is(%22root%22));%0A    %7D%0A
 %0A    @Te
@@ -3489,11 +3489,762 @@
 );%0A    %7D
+%0A%0A    private Map%3CString, Map%3CString, Object%3E%3E createYamlConfig() %7B%0A        Map%3CString, Map%3CString, Object%3E%3E result = new LinkedHashMap%3C%3E(2, 1);%0A        result.put(%22ds_0%22, createPropertyMap(%22ds_0%22));%0A        result.put(%22ds_1%22, createPropertyMap(%22ds_1%22));%0A        return result;%0A    %7D%0A%0A    private Map%3CString, Object%3E createPropertyMap(final String name) %7B%0A        Map%3CString, Object%3E result = new LinkedHashMap%3C%3E(5, 1);%0A        result.put(%22dataSourceClassName%22, %22com.zaxxer.hikari.HikariDataSource%22);%0A        result.put(%22driverClassName%22, %22org.h2.Driver%22);%0A        result.put(%22jdbcUrl%22, String.format(%22jdbc:h2:mem:test_%25s;MODE=MySQL%22, name));%0A        result.put(%22username%22, %22root%22);%0A        result.put(%22password%22, %22root%22);%0A        return result;%0A    %7D
 %0A%7D%0A

Add test case for YamlDataSourceConfigurationSwapper.swapToDataSources (#9560)