You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/08/18 03:33:02 UTC
[shardingsphere] branch master updated: Add unit test for
OrchestrationSchemaContext (#6903)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 47f68db Add unit test for OrchestrationSchemaContext (#6903)
47f68db is described below
commit 47f68db6e8109d55c4317eeb0156d374951252dd
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Tue Aug 18 11:32:49 2020 +0800
Add unit test for OrchestrationSchemaContext (#6903)
* Add unit test for OrchestrationSchemaContext
* Add unit test for OrchestrationSchemaContexts
---
.../pom.xml | 13 +
.../core/schema/OrchestrationSchemaContexts.java | 12 +-
.../schema/OrchestrationSchemaContextsTest.java | 276 +++++++++++++++++++++
.../fixture/TestOrchestrationSchemaContexts.java | 124 +++++++++
4 files changed, 421 insertions(+), 4 deletions(-)
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/pom.xml b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/pom.xml
index fcb5252..ebf01d9 100644
--- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/pom.xml
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/pom.xml
@@ -54,5 +54,18 @@
<artifactId>shardingsphere-metrics-facade</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-dbcp2</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.h2database</groupId>
+ <artifactId>h2</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.zaxxer</groupId>
+ <artifactId>HikariCP</artifactId>
+ <scope>compile</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java
index 67ef53b..ec44156 100644
--- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/main/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContexts.java
@@ -163,7 +163,7 @@ public abstract class OrchestrationSchemaContexts implements SchemaContexts {
schemas.put(schemaName, getAddedSchemaContext(schemaAddedEvent));
schemaContexts =
new StandardSchemaContexts(schemas, schemaContexts.getAuthentication(), schemaContexts.getProps(), schemaContexts.getDatabaseType());
- OrchestrationFacade.getInstance().getMetaDataCenter().persistMetaDataCenterNode(schemaName, schemaContexts.getSchemaContexts().get(schemaName).getSchema().getMetaData().getSchema());
+ orchestrationFacade.getMetaDataCenter().persistMetaDataCenterNode(schemaName, schemaContexts.getSchemaContexts().get(schemaName).getSchema().getMetaData().getSchema());
}
/**
@@ -251,7 +251,7 @@ public abstract class OrchestrationSchemaContexts implements SchemaContexts {
schemaContexts.put(schemaName, getChangedSchemaContext(this.schemaContexts.getSchemaContexts().get(schemaName), ruleConfigurationsChangedEvent.getRuleConfigurations()));
this.schemaContexts =
new StandardSchemaContexts(schemaContexts, this.schemaContexts.getAuthentication(), this.schemaContexts.getProps(), this.schemaContexts.getDatabaseType());
- OrchestrationFacade.getInstance().getMetaDataCenter().persistMetaDataCenterNode(schemaName, schemaContexts.get(schemaName).getSchema().getMetaData().getSchema());
+ orchestrationFacade.getMetaDataCenter().persistMetaDataCenterNode(schemaName, schemaContexts.get(schemaName).getSchema().getMetaData().getSchema());
}
/**
@@ -383,8 +383,12 @@ public abstract class OrchestrationSchemaContexts implements SchemaContexts {
Map<String, DataSource> result = new LinkedHashMap<>(oldDataSources);
result.keySet().removeAll(deletedDataSources);
result.keySet().removeAll(modifiedDataSources.keySet());
- result.putAll(modifiedDataSources);
- result.putAll(addedDataSources);
+ if (null != modifiedDataSources) {
+ result.putAll(modifiedDataSources);
+ }
+ if (null != addedDataSources) {
+ result.putAll(addedDataSources);
+ }
return result;
}
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/test/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContextsTest.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/test/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContextsTest.java
new file mode 100644
index 0000000..f0ee528
--- /dev/null
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/test/java/org/apache/shardingsphere/orchestration/core/schema/OrchestrationSchemaContextsTest.java
@@ -0,0 +1,276 @@
+/*
+ * 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.orchestration.core.schema;
+
+import lombok.SneakyThrows;
+import org.apache.commons.dbcp2.BasicDataSource;
+import org.apache.shardingsphere.cluster.facade.init.ClusterInitFacade;
+import org.apache.shardingsphere.infra.auth.Authentication;
+import org.apache.shardingsphere.infra.config.DataSourceConfiguration;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.database.type.dialect.H2DatabaseType;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData;
+import org.apache.shardingsphere.infra.rule.event.RuleChangedEvent;
+import org.apache.shardingsphere.kernel.context.SchemaContext;
+import org.apache.shardingsphere.kernel.context.StandardSchemaContexts;
+import org.apache.shardingsphere.kernel.context.runtime.RuntimeContext;
+import org.apache.shardingsphere.kernel.context.schema.ShardingSphereSchema;
+import org.apache.shardingsphere.masterslave.rule.MasterSlaveRule;
+import org.apache.shardingsphere.metrics.configuration.config.MetricsConfiguration;
+import org.apache.shardingsphere.metrics.facade.MetricsTrackerManagerFacade;
+import org.apache.shardingsphere.orchestration.core.common.event.AuthenticationChangedEvent;
+import org.apache.shardingsphere.orchestration.core.common.event.DataSourceChangedEvent;
+import org.apache.shardingsphere.orchestration.core.common.event.MetricsConfigurationChangedEvent;
+import org.apache.shardingsphere.orchestration.core.common.event.PropertiesChangedEvent;
+import org.apache.shardingsphere.orchestration.core.common.event.RuleConfigurationsChangedEvent;
+import org.apache.shardingsphere.orchestration.core.common.event.SchemaAddedEvent;
+import org.apache.shardingsphere.orchestration.core.common.event.SchemaDeletedEvent;
+import org.apache.shardingsphere.orchestration.core.facade.OrchestrationFacade;
+import org.apache.shardingsphere.orchestration.core.metadata.MetaDataCenter;
+import org.apache.shardingsphere.orchestration.core.metadata.event.MetaDataChangedEvent;
+import org.apache.shardingsphere.orchestration.core.registry.RegistryCenter;
+import org.apache.shardingsphere.orchestration.core.registry.event.CircuitStateChangedEvent;
+import org.apache.shardingsphere.orchestration.core.registry.event.DisabledStateChangedEvent;
+import org.apache.shardingsphere.orchestration.core.registry.schema.OrchestrationSchema;
+import org.apache.shardingsphere.orchestration.core.schema.fixture.TestOrchestrationSchemaContexts;
+import org.apache.shardingsphere.transaction.ShardingTransactionManagerEngine;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class OrchestrationSchemaContextsTest {
+
+ @Mock
+ private OrchestrationFacade orchestrationFacade;
+
+ @Mock
+ private RegistryCenter registryCenter;
+
+ @Mock
+ private MetaDataCenter metaDataCenter;
+
+ @Mock
+ private SchemaContext schemaContext;
+
+ @Mock
+ private MasterSlaveRule masterSlaveRule;
+
+ private Authentication authentication = new Authentication();
+
+ private ConfigurationProperties configurationProperties = new ConfigurationProperties(new Properties());
+
+ private TestOrchestrationSchemaContexts orchestrationSchemaContexts;
+
+ @Before
+ public void setUp() {
+ when(orchestrationFacade.getRegistryCenter()).thenReturn(registryCenter);
+ when(registryCenter.loadDisabledDataSources()).thenReturn(Arrays.asList("schema.ds_1"));
+ when(orchestrationFacade.getMetaDataCenter()).thenReturn(metaDataCenter);
+ orchestrationSchemaContexts = new TestOrchestrationSchemaContexts(new StandardSchemaContexts(getSchemaContextMap(),
+ authentication, configurationProperties, new H2DatabaseType()), orchestrationFacade);
+ }
+
+ @SneakyThrows(Exception.class)
+ private Map<String, SchemaContext> getSchemaContextMap() {
+ ShardingSphereSchema shardingSphereSchema = mock(ShardingSphereSchema.class);
+ ShardingSphereMetaData shardingSphereMetaData = mock(ShardingSphereMetaData.class);
+ RuntimeContext runtimeContext = mock(RuntimeContext.class);
+ ShardingTransactionManagerEngine shardingTransactionManagerEngine = mock(ShardingTransactionManagerEngine.class);
+ when(schemaContext.getName()).thenReturn("schema");
+ when(schemaContext.getSchema()).thenReturn(shardingSphereSchema);
+ when(schemaContext.getRuntimeContext()).thenReturn(runtimeContext);
+ when(shardingSphereSchema.getMetaData()).thenReturn(shardingSphereMetaData);
+ when(shardingSphereSchema.getRules()).thenReturn(Arrays.asList(masterSlaveRule));
+ when(runtimeContext.getTransactionManagerEngine()).thenReturn(shardingTransactionManagerEngine);
+ doNothing().when(shardingTransactionManagerEngine).close();
+ return Collections.singletonMap("schema", schemaContext);
+ }
+
+ @Test
+ public void assertGetDatabaseType() {
+ assertThat(orchestrationSchemaContexts.getDatabaseType().getName(), is("H2"));
+ }
+
+ @Test
+ public void assertGetSchemaContexts() {
+ assertThat(orchestrationSchemaContexts.getSchemaContexts().get("schema"), is(schemaContext));
+ }
+
+ @Test
+ public void assertGetDefaultSchemaContext() {
+ assertNull(orchestrationSchemaContexts.getDefaultSchemaContext());
+ }
+
+ @Test
+ public void assertGetAuthentication() {
+ assertThat(orchestrationSchemaContexts.getAuthentication(), is(authentication));
+ }
+
+ @Test
+ public void assertGetProps() {
+ assertThat(orchestrationSchemaContexts.getProps(), is(configurationProperties));
+ }
+
+ @Test
+ public void assertIsCircuitBreak() {
+ assertFalse(orchestrationSchemaContexts.isCircuitBreak());
+ }
+
+ @Test
+ @SneakyThrows(Exception.class)
+ public void assertSchemaAdd() {
+ SchemaAddedEvent event = new SchemaAddedEvent("schema_add", getDataSourceConfigurations(), new ArrayList<>());
+ orchestrationSchemaContexts.renew(event);
+ assertNotNull(orchestrationSchemaContexts.getSchemaContexts().get("schema_add"));
+ }
+
+ private Map<String, DataSourceConfiguration> getDataSourceConfigurations() {
+ BasicDataSource dataSource = new BasicDataSource();
+ dataSource.setDriverClassName("org.h2.Driver");
+ dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+ dataSource.setUsername("sa");
+ dataSource.setPassword("");
+ Map<String, DataSourceConfiguration> result = new LinkedHashMap<>();
+ result.put("ds_m", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
+ result.put("ds_0", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
+ result.put("ds_1", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
+ return result;
+ }
+
+ @Test
+ public void assertSchemaDelete() {
+ SchemaDeletedEvent event = new SchemaDeletedEvent("schema");
+ orchestrationSchemaContexts.renew(event);
+ assertNull(orchestrationSchemaContexts.getSchemaContexts().get("schema"));
+ }
+
+ @Test
+ public void assertPropertiesChanged() {
+ Properties properties = new Properties();
+ properties.setProperty("sql.show", "true");
+ PropertiesChangedEvent event = new PropertiesChangedEvent(properties);
+ orchestrationSchemaContexts.renew(event);
+ assertThat(orchestrationSchemaContexts.getProps().getProps().getProperty("sql.show"), is("true"));
+ }
+
+ @Test
+ public void assertAuthenticationChanged() {
+ Authentication authentication = new Authentication();
+ AuthenticationChangedEvent event = new AuthenticationChangedEvent(authentication);
+ orchestrationSchemaContexts.renew(event);
+ assertThat(orchestrationSchemaContexts.getAuthentication(), is(authentication));
+ }
+
+ @Test
+ public void assertMetaDataChanged() {
+ MetaDataChangedEvent event = new MetaDataChangedEvent(Arrays.asList("schema_changed"), mock(RuleSchemaMetaData.class));
+ orchestrationSchemaContexts.renew(event);
+ assertTrue(orchestrationSchemaContexts.getSchemaContexts().keySet().contains("schema"));
+ assertFalse(orchestrationSchemaContexts.getSchemaContexts().keySet().contains("schema_changed"));
+ }
+
+ @Test
+ public void assertMetaDataChangedWithExistSchema() {
+ MetaDataChangedEvent event = new MetaDataChangedEvent(Arrays.asList("schema"), mock(RuleSchemaMetaData.class));
+ orchestrationSchemaContexts.renew(event);
+ assertThat(orchestrationSchemaContexts.getSchemaContexts().get("schema"), not(schemaContext));
+ }
+
+ @Test
+ @SneakyThrows(Exception.class)
+ public void assertRuleConfigurationsChanged() {
+ assertThat(orchestrationSchemaContexts.getSchemaContexts().get("schema"), is(schemaContext));
+ RuleConfigurationsChangedEvent event = new RuleConfigurationsChangedEvent("schema", new ArrayList<>());
+ orchestrationSchemaContexts.renew(event);
+ assertThat(orchestrationSchemaContexts.getSchemaContexts().get("schema"), not(schemaContext));
+ }
+
+ @Test
+ public void assertDisableStateChanged() {
+ DisabledStateChangedEvent event = new DisabledStateChangedEvent(new OrchestrationSchema("schema.ds_0"), true);
+ orchestrationSchemaContexts.renew(event);
+ verify(masterSlaveRule, times(2)).updateRuleStatus(any(RuleChangedEvent.class));
+ }
+
+ @Test
+ @SneakyThrows(Exception.class)
+ public void assertDataSourceChanged() {
+ DataSourceChangedEvent event = new DataSourceChangedEvent("schema", getChangedDataSourceConfigurations());
+ orchestrationSchemaContexts.renew(event);
+ assertTrue(orchestrationSchemaContexts.getSchemaContexts().get("schema").getSchema().getDataSources().keySet().contains("ds_2"));
+ }
+
+ private Map<String, DataSourceConfiguration> getChangedDataSourceConfigurations() {
+ BasicDataSource dataSource = new BasicDataSource();
+ dataSource.setDriverClassName("org.h2.Driver");
+ dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+ dataSource.setUsername("sa");
+ dataSource.setPassword("");
+ Map<String, DataSourceConfiguration> result = new LinkedHashMap<>();
+ result.put("ds_m", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
+ result.put("ds_1", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
+ result.put("ds_2", DataSourceConfiguration.getDataSourceConfiguration(dataSource));
+ return result;
+ }
+
+ @Test
+ public void assertCircuitStateChanged() {
+ CircuitStateChangedEvent event = new CircuitStateChangedEvent(true);
+ orchestrationSchemaContexts.renew(event);
+ assertTrue(orchestrationSchemaContexts.isCircuitBreak());
+ }
+
+ @Test
+ public void assertMetricsConfigurationChanged() {
+ MetricsConfigurationChangedEvent event = new MetricsConfigurationChangedEvent(mock(MetricsConfiguration.class));
+ orchestrationSchemaContexts.renew(event);
+ assertFalse(MetricsTrackerManagerFacade.getEnabled());
+ }
+
+ @Test
+ public void assertEnableCluster() {
+ PropertiesChangedEvent event = new PropertiesChangedEvent(new Properties());
+ orchestrationSchemaContexts.enable(event);
+ assertFalse(ClusterInitFacade.isEnabled());
+ }
+}
diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/test/java/org/apache/shardingsphere/orchestration/core/schema/fixture/TestOrchestrationSchemaContexts.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/test/java/org/apache/shardingsphere/orchestration/core/schema/fixture/TestOrchestrationSchemaCont [...]
new file mode 100644
index 0000000..2e36f72
--- /dev/null
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-schema/src/test/java/org/apache/shardingsphere/orchestration/core/schema/fixture/TestOrchestrationSchemaContexts.java
@@ -0,0 +1,124 @@
+/*
+ * 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.orchestration.core.schema.fixture;
+
+import com.zaxxer.hikari.HikariConfig;
+import com.zaxxer.hikari.HikariDataSource;
+import org.apache.shardingsphere.infra.config.DataSourceConfiguration;
+import org.apache.shardingsphere.kernel.context.SchemaContext;
+import org.apache.shardingsphere.kernel.context.SchemaContexts;
+import org.apache.shardingsphere.kernel.context.schema.DataSourceParameter;
+import org.apache.shardingsphere.orchestration.core.facade.OrchestrationFacade;
+import org.apache.shardingsphere.orchestration.core.schema.OrchestrationSchemaContexts;
+
+import javax.sql.DataSource;
+import java.lang.reflect.Field;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public final class TestOrchestrationSchemaContexts extends OrchestrationSchemaContexts {
+
+ public TestOrchestrationSchemaContexts(final SchemaContexts schemaContexts, final OrchestrationFacade orchestrationFacade) {
+ super(schemaContexts, orchestrationFacade);
+ }
+
+ @Override
+ protected Map<String, DataSource> getAddedDataSources(final SchemaContext oldSchemaContext, final Map<String, DataSourceConfiguration> newDataSources) throws Exception {
+ return Collections.singletonMap("ds_2", buildDataSource(getDataSourceParameter()));
+ }
+
+ @Override
+ protected Map<String, DataSource> getModifiedDataSources(final SchemaContext oldSchemaContext, final Map<String, DataSourceConfiguration> newDataSources) throws Exception {
+ return Collections.singletonMap("ds_1", buildDataSource(getDataSourceParameter()));
+ }
+
+ private DataSourceParameter getDataSourceParameter() {
+ DataSourceParameter result = new DataSourceParameter();
+ result.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
+ result.setUsername("sa");
+ result.setPassword("");
+ return result;
+ }
+
+ @Override
+ protected Map<String, Map<String, DataSource>> createDataSourcesMap(final Map<String, Map<String, DataSourceConfiguration>> dataSourcesMap) throws Exception {
+ Map<String, Map<String, DataSource>> result = new LinkedHashMap<>();
+ for (Map.Entry<String, Map<String, DataSourceParameter>> entry : createDataSourceParametersMap(dataSourcesMap).entrySet()) {
+ result.put(entry.getKey(), createDataSources(entry.getValue()));
+ }
+ return result;
+ }
+
+ private Map<String, DataSource> createDataSources(final Map<String, DataSourceParameter> dataSourceParameters) throws Exception {
+ Map<String, DataSource> result = new LinkedHashMap<>();
+ for (Map.Entry<String, DataSourceParameter> entry: dataSourceParameters.entrySet()) {
+ result.put(entry.getKey(), buildDataSource(entry.getValue()));
+ }
+ return result;
+ }
+
+ @Override
+ public Map<String, Map<String, DataSourceParameter>> createDataSourceParametersMap(final Map<String, Map<String, DataSourceConfiguration>> dataSourcesMap) {
+ Map<String, Map<String, DataSourceParameter>> result = new LinkedHashMap<>();
+ for (Map.Entry<String, Map<String, DataSourceConfiguration>> entry : dataSourcesMap.entrySet()) {
+ result.put(entry.getKey(), getDataSourceParameterMap(entry.getValue()));
+ }
+ return result;
+ }
+
+ private DataSource buildDataSource(final DataSourceParameter dataSourceParameter) {
+ HikariConfig config = new HikariConfig();
+ config.setJdbcUrl(dataSourceParameter.getUrl());
+ config.setUsername(dataSourceParameter.getUsername());
+ config.setPassword(dataSourceParameter.getPassword());
+ config.setConnectionTimeout(dataSourceParameter.getConnectionTimeoutMilliseconds());
+ config.setIdleTimeout(dataSourceParameter.getIdleTimeoutMilliseconds());
+ config.setMaxLifetime(dataSourceParameter.getMaxLifetimeMilliseconds());
+ config.setMaximumPoolSize(dataSourceParameter.getMaxPoolSize());
+ config.setMinimumIdle(dataSourceParameter.getMinPoolSize());
+ config.setReadOnly(dataSourceParameter.isReadOnly());
+ return new HikariDataSource(config);
+ }
+
+ private Map<String, DataSourceParameter> getDataSourceParameterMap(final Map<String, DataSourceConfiguration> dataSourceConfigurationMap) {
+ return dataSourceConfigurationMap.entrySet().stream()
+ .collect(Collectors.toMap(Map.Entry::getKey, entry -> createDataSourceParameter(entry.getValue()), (oldVal, currVal) -> oldVal, LinkedHashMap::new));
+ }
+
+ private DataSourceParameter createDataSourceParameter(final DataSourceConfiguration dataSourceConfig) {
+ bindAlias(dataSourceConfig);
+ DataSourceParameter result = new DataSourceParameter();
+ for (Field each : result.getClass().getDeclaredFields()) {
+ try {
+ each.setAccessible(true);
+ if (dataSourceConfig.getProps().containsKey(each.getName())) {
+ each.set(result, dataSourceConfig.getProps().get(each.getName()));
+ }
+ } catch (final ReflectiveOperationException ignored) {
+ }
+ }
+ return result;
+ }
+
+ private static void bindAlias(final DataSourceConfiguration dataSourceConfiguration) {
+ dataSourceConfiguration.addPropertyAlias("url", "jdbcUrl");
+ dataSourceConfiguration.addPropertyAlias("user", "username");
+ }
+}