ShardingRuleConfigurationYamlSwapper implements YamlRuleConfigurationSwapper<YamlShardingRuleConfiguration, ShardingRuleConfiguration> { @Override public ShardingRuleConfiguration swapToObject(final YamlShardingRuleConfiguration yamlConfig) { ShardingRuleConfiguration result = new ShardingRuleConfiguration(); for (Entry<String, YamlTableRuleConfiguration> entry : yamlConfig.getTables().entrySet()) { YamlTableRuleConfiguration tableRuleConfig = entry.getValue(); tableRuleConfig.setLogicTable(entry.getKey()); result.getTables().add(tableYamlSwapper.swapToObject(tableRuleConfig)); } for (Entry<String, YamlShardingAutoTableRuleConfiguration> entry : yamlConfig.getAutoTables().entrySet()) { YamlShardingAutoTableRuleConfiguration tableRuleConfig = entry.getValue(); tableRuleConfig.setLogicTable(entry.getKey()); result.getAutoTables().add(autoTableYamlSwapper.swapToObject(tableRuleConfig)); } result.getBindingTableGroups().addAll(yamlConfig.getBindingTables()); result.getBroadcastTables().addAll(yamlConfig.getBroadcastTables()); setDefaultStrategies(yamlConfig, result); setAlgorithms(yamlConfig, result); return result; }  @Override YamlShardingRuleConfiguration swapToYamlConfiguration(final ShardingRuleConfiguration data); @Override ShardingRuleConfiguration swapToObject(final YamlShardingRuleConfiguration yamlConfig); @Override Class<ShardingRuleConfiguration> getTypeClass(); @Override String getRuleTagName(); @Override int getOrder();  }
@Test public void assertSwapToObjectWithMinProperties() { YamlShardingRuleConfiguration yamlConfig = new YamlShardingRuleConfiguration(); yamlConfig.getTables().put("tbl", mock(YamlTableRuleConfiguration.class)); ShardingRuleConfiguration actual = shardingRuleConfigurationYamlSwapper.swapToObject(yamlConfig); assertThat(actual.getTables().size(), is(1)); assertTrue(actual.getBindingTableGroups().isEmpty()); assertTrue(actual.getBroadcastTables().isEmpty()); assertNull(actual.getDefaultDatabaseShardingStrategy()); assertNull(actual.getDefaultTableShardingStrategy()); assertNull(actual.getDefaultKeyGenerateStrategy()); }
@Test public void assertSwapToObjectWithMaxProperties() { YamlShardingRuleConfiguration yamlConfig = new YamlShardingRuleConfiguration(); yamlConfig.getTables().put("tbl", mock(YamlTableRuleConfiguration.class)); yamlConfig.getBindingTables().add("tbl, sub_tbl"); yamlConfig.getBroadcastTables().add("dict"); yamlConfig.setDefaultDatabaseStrategy(mock(YamlShardingStrategyConfiguration.class)); yamlConfig.setDefaultTableStrategy(mock(YamlShardingStrategyConfiguration.class)); yamlConfig.setDefaultKeyGenerateStrategy(mock(YamlKeyGenerateStrategyConfiguration.class)); ShardingRuleConfiguration actual = shardingRuleConfigurationYamlSwapper.swapToObject(yamlConfig); assertThat(actual.getTables().size(), is(1)); assertThat(actual.getBindingTableGroups().size(), is(1)); assertThat(actual.getBindingTableGroups().iterator().next(), is("tbl, sub_tbl")); assertThat(actual.getBroadcastTables().size(), is(1)); assertThat(actual.getBroadcastTables().iterator().next(), is("dict")); assertNotNull(actual.getDefaultDatabaseShardingStrategy()); assertNotNull(actual.getDefaultTableShardingStrategy()); assertNotNull(actual.getDefaultKeyGenerateStrategy()); }