/*
 * 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.shardingproxy.util;

import org.apache.shardingsphere.core.config.DataSourceConfiguration;
import org.apache.shardingsphere.shardingproxy.config.yaml.YamlDataSourceParameter;
import org.hamcrest.CoreMatchers;
import org.junit.Test;

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

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

public final class DataSourceConverterTest {
    
    @Test
    public void assertGetDataSourceParameterMap() {
        Map<String, DataSourceConfiguration> dataSourceConfigurationMap = new HashMap<>(2, 1);
        DataSourceConfiguration dataSourceConfiguration0 = mock(DataSourceConfiguration.class);
        dataSourceConfigurationMap.put("ds_0", dataSourceConfiguration0);
        DataSourceConfiguration dataSourceConfiguration1 = mock(DataSourceConfiguration.class);
        dataSourceConfigurationMap.put("ds_1", dataSourceConfiguration1);
        Map<String, YamlDataSourceParameter> actual = DataSourceConverter.getDataSourceParameterMap(dataSourceConfigurationMap);
        assertThat(actual.size(), is(2));
        assertTrue(actual.containsKey("ds_0"));
        assertTrue(actual.containsKey("ds_1"));
    }
    
    @Test
    public void assertGetDataSourceConfigurationMap() {
        Map<String, YamlDataSourceParameter> dataSourceParameterMap = new HashMap<>(2, 1);
        dataSourceParameterMap.put("ds_0", crateDataSourceParameter());
        dataSourceParameterMap.put("ds_1", crateDataSourceParameter());
        Map<String, DataSourceConfiguration> actual = DataSourceConverter.getDataSourceConfigurationMap(dataSourceParameterMap);
        assertThat(actual.size(), is(2));
        assertNotNull(actual.get("ds_0"));
        assertNotNull(actual.get("ds_1"));
        assertThatParameter(actual.get("ds_0"));
        assertThatParameter(actual.get("ds_1"));
    }
    
    private YamlDataSourceParameter crateDataSourceParameter() {
        YamlDataSourceParameter result = new YamlDataSourceParameter();
        result.setUsername("root");
        result.setUrl("jdbc:mysql://localhost:3306/demo_ds");
        result.setPassword("root");
        return result;
    }
    
    private void assertThatParameter(final DataSourceConfiguration actual) {
        Map<String, Object> properties = actual.getProperties();
        assertThat(properties.get("maxPoolSize"), CoreMatchers.<Object>is(50));
        assertThat(properties.get("minPoolSize"), CoreMatchers.<Object>is(1));
        assertThat(properties.get("connectionTimeoutMilliseconds"), CoreMatchers.<Object>is(30 * 1000L));
        assertThat(properties.get("idleTimeoutMilliseconds"), CoreMatchers.<Object>is(60 * 1000L));
        assertThat(properties.get("maxLifetimeMilliseconds"), CoreMatchers.<Object>is(0L));
        assertThat(properties.get("maintenanceIntervalMilliseconds"), CoreMatchers.<Object>is(30 * 1000L));
        assertThat(properties.get("url"), CoreMatchers.<Object>is("jdbc:mysql://localhost:3306/demo_ds"));
        assertThat(properties.get("username"), CoreMatchers.<Object>is("root"));
        assertThat(properties.get("password"), CoreMatchers.<Object>is("root"));
    }
}

@@ -851,16 +851,59 @@
 .util;%0A%0A
+import com.zaxxer.hikari.HikariDataSource;%0A
 import o
@@ -1240,141 +1240,12 @@
 sert
-NotNull;%0Aimport static org.junit.Assert.assertThat;%0Aimport static org.junit.Assert.assertTrue;%0Aimport static org.mockito.Mockito.mock
+That
 ;%0A%0Ap
@@ -1513,212 +1513,468 @@
 0 = 
-mock(DataSourceConfiguration.class);%0A        dataSourceConfigurationMap.put(%22ds_0%22, dataSourceConfiguration0);%0A        DataSourceConfiguration dataSourceConfiguration1 = mock(DataSourceConfiguration.class
+new DataSourceConfiguration(HikariDataSource.class.getName());%0A        dataSourceConfiguration0.getProperties().put(%22url%22, %22jdbc:mysql://localhost:3306/demo_ds_0%22);%0A        dataSourceConfigurationMap.put(%22ds_0%22, dataSourceConfiguration0);%0A        DataSourceConfiguration dataSourceConfiguration1 = new DataSourceConfiguration(HikariDataSource.class.getName());%0A        dataSourceConfiguration1.getProperties().put(%22url%22, %22jdbc:mysql://localhost:3306/demo_ds_1%22
 );%0A 
@@ -2228,35 +2228,35 @@
      assertT
-rue
+hat
 (actual.
 containsKey(
@@ -2243,29 +2243,75 @@
 (actual.
-containsKey(%22
+get(%22ds_0%22).getUrl(), is(%22jdbc:mysql://localhost:3306/demo_
 ds_0%22));
@@ -2330,19 +2330,19 @@
 ertT
-rue
+hat
 (actual.
 cont
@@ -2341,21 +2341,67 @@
 ual.
-containsKey(%22
+get(%22ds_1%22).getUrl(), is(%22jdbc:mysql://localhost:3306/demo_
 ds_1
@@ -2906,98 +2906,8 @@
 sert
-NotNull(actual.get(%22ds_0%22));%0A        assertNotNull(actual.get(%22ds_1%22));%0A        assertThat
 Para
@@ -2943,28 +2943,24 @@
       assert
-That
 Parameter(ac
@@ -3130,44 +3130,8 @@
 ();%0A
-        result.setUsername(%22root%22);%0A
     
@@ -3184,24 +3184,60 @@
 /demo_ds%22);%0A
+        result.setUsername(%22root%22);%0A
         resu
@@ -3317,20 +3317,16 @@
 d assert
-That
 Paramete

test coverage for DataSourceConverterTest