ConfigCenter { public Map<String, DataSourceConfiguration> loadDataSourceConfigurations(final String schemaName) { if (!hasDataSourceConfiguration(schemaName)) { return new LinkedHashMap<>(); } YamlDataSourceConfigurationWrap result = YamlEngine.unmarshal(repository.get(node.getDataSourcePath(schemaName)), YamlDataSourceConfigurationWrap.class); return result.getDataSources().entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> new DataSourceConfigurationYamlSwapper().swapToObject(entry.getValue()))); } ConfigCenter(final ConfigurationRepository repository); void persistConfigurations(final String schemaName, final Map<String, DataSourceConfiguration> dataSourceConfigs,
                                      final Collection<RuleConfiguration> ruleConfigurations, final boolean isOverwrite); void persistGlobalConfiguration(final Authentication authentication, final Properties props, final boolean isOverwrite); @Subscribe synchronized void renew(final DataSourcePersistEvent event); @Subscribe synchronized void renew(final RulePersistEvent event); @Subscribe synchronized void renew(final SchemaNamePersistEvent event); @Subscribe synchronized void renew(final MetaDataPersistEvent event); Map<String, DataSourceConfiguration> loadDataSourceConfigurations(final String schemaName); Collection<RuleConfiguration> loadRuleConfigurations(final String schemaName); Authentication loadAuthentication(); Properties loadProperties(); Collection<String> getAllSchemaNames(); boolean hasRuleConfiguration(final String schemaName); boolean hasDataSourceConfiguration(final String schemaName); void persistMetaData(final String schemaName, final RuleSchemaMetaData ruleSchemaMetaData); Optional<RuleSchemaMetaData> loadMetaData(final String schemaName); void deleteSchema(final String schemaName);  }
@Test public void assertLoadDataSourceConfigurations() { when(configurationRepository.get("/schemas/sharding_db/datasource")).thenReturn(readYAML(DATA_SOURCE_YAM)); ConfigCenter configCenter = new ConfigCenter(configurationRepository); Map<String, DataSourceConfiguration> actual = configCenter.loadDataSourceConfigurations("sharding_db"); assertThat(actual.size(), is(2)); assertDataSourceConfiguration(actual.get("ds_0"), createDataSourceConfiguration(createDataSource("ds_0"))); assertDataSourceConfiguration(actual.get("ds_1"), createDataSourceConfiguration(createDataSource("ds_1"))); }
@Test public void assertLoadDataSourceConfigurationsNotExistPath() { when(configurationRepository.get("/schemas/sharding_db/datasource")).thenReturn(""); ConfigCenter configCenter = new ConfigCenter(configurationRepository); Map<String, DataSourceConfiguration> actual = configCenter.loadDataSourceConfigurations("sharding_db"); assertThat(actual.size(), is(0)); }
@Test public void assertLoadDataSourceConfigurationsWithConnectionInitSqls() { when(configurationRepository.get("/schemas/sharding_db/datasource")).thenReturn(readYAML(DATA_SOURCE_YAML_WITH_CONNECTION_INIT_SQL)); ConfigCenter configCenter = new ConfigCenter(configurationRepository); Map<String, DataSourceConfiguration> actual = configCenter.loadDataSourceConfigurations("sharding_db"); assertThat(actual.size(), is(2)); assertDataSourceConfigurationWithConnectionInitSqls(actual.get("ds_0"), createDataSourceConfiguration(createDataSourceWithConnectionInitSqls("ds_0"))); assertDataSourceConfigurationWithConnectionInitSqls(actual.get("ds_1"), createDataSourceConfiguration(createDataSourceWithConnectionInitSqls("ds_1"))); }