ShardingRuleConfigurationYamlSwapper implements YamlRuleConfigurationSwapper<YamlShardingRuleConfiguration, ShardingRuleConfiguration> { @Override public YamlShardingRuleConfiguration swapToYamlConfiguration(final ShardingRuleConfiguration data) { YamlShardingRuleConfiguration result = new YamlShardingRuleConfiguration(); data.getTables().forEach(each -> result.getTables().put(each.getLogicTable(), tableYamlSwapper.swapToYamlConfiguration(each))); data.getAutoTables().forEach(each -> result.getAutoTables().put(each.getLogicTable(), autoTableYamlSwapper.swapToYamlConfiguration(each))); result.getBindingTables().addAll(data.getBindingTableGroups()); result.getBroadcastTables().addAll(data.getBroadcastTables()); setYamlDefaultStrategies(data, result); setYamlAlgorithms(data, 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 assertSwapToYamlWithMinProperties() { ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); shardingRuleConfig.getTables().add(mock(ShardingTableRuleConfiguration.class)); YamlShardingRuleConfiguration actual = shardingRuleConfigurationYamlSwapper.swapToYamlConfiguration(shardingRuleConfig); assertThat(actual.getTables().size(), is(1)); assertTrue(actual.getBindingTables().isEmpty()); assertTrue(actual.getBroadcastTables().isEmpty()); assertNull(actual.getDefaultDatabaseStrategy()); assertNull(actual.getDefaultTableStrategy()); assertNull(actual.getDefaultKeyGenerateStrategy()); }
@Test public void assertSwapToYamlWithMaxProperties() { ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); shardingRuleConfig.getTables().add(mock(ShardingTableRuleConfiguration.class)); shardingRuleConfig.getBindingTableGroups().add("tbl, sub_tbl"); shardingRuleConfig.getBroadcastTables().add("dict"); shardingRuleConfig.setDefaultDatabaseShardingStrategy(mock(ShardingStrategyConfiguration.class)); shardingRuleConfig.setDefaultTableShardingStrategy(mock(ShardingStrategyConfiguration.class)); shardingRuleConfig.setDefaultKeyGenerateStrategy(mock(KeyGenerateStrategyConfiguration.class)); YamlShardingRuleConfiguration actual = shardingRuleConfigurationYamlSwapper.swapToYamlConfiguration(shardingRuleConfig); assertThat(actual.getTables().size(), is(1)); assertThat(actual.getBindingTables().size(), is(1)); assertThat(actual.getBindingTables().iterator().next(), is("tbl, sub_tbl")); assertThat(actual.getBroadcastTables().size(), is(1)); assertThat(actual.getBroadcastTables().iterator().next(), is("dict")); assertNotNull(actual.getDefaultDatabaseStrategy()); assertNotNull(actual.getDefaultTableStrategy()); assertNotNull(actual.getDefaultKeyGenerateStrategy()); }