PrimaryReplicaReplicationRuleConfigurationYamlSwapper implements YamlRuleConfigurationSwapper<YamlPrimaryReplicaReplicationRuleConfiguration, PrimaryReplicaReplicationRuleConfiguration> { @Override public YamlPrimaryReplicaReplicationRuleConfiguration swapToYamlConfiguration(final PrimaryReplicaReplicationRuleConfiguration data) { YamlPrimaryReplicaReplicationRuleConfiguration result = new YamlPrimaryReplicaReplicationRuleConfiguration(); result.setDataSources(data.getDataSources().stream().collect( Collectors.toMap(PrimaryReplicaReplicationDataSourceRuleConfiguration::getName, this::swapToYamlConfiguration, (a, b) -> b, LinkedHashMap::new))); if (null != data.getLoadBalancers()) { data.getLoadBalancers().forEach((key, value) -> result.getLoadBalancers().put(key, algorithmSwapper.swapToYamlConfiguration(value))); } return result; }  @Override YamlPrimaryReplicaReplicationRuleConfiguration swapToYamlConfiguration(final PrimaryReplicaReplicationRuleConfiguration data); @Override PrimaryReplicaReplicationRuleConfiguration swapToObject(final YamlPrimaryReplicaReplicationRuleConfiguration yamlConfig); @Override Class<PrimaryReplicaReplicationRuleConfiguration> getTypeClass(); @Override String getRuleTagName(); @Override int getOrder();  }
@Test public void assertSwapToYamlWithLoadBalanceAlgorithm() { PrimaryReplicaReplicationDataSourceRuleConfiguration dataSourceConfig = new PrimaryReplicaReplicationDataSourceRuleConfiguration("ds", "primary", Collections.singletonList("replica"), "roundRobin"); YamlPrimaryReplicaReplicationRuleConfiguration actual = getPrimaryReplicaReplicationRuleConfigurationYamlSwapper().swapToYamlConfiguration(new PrimaryReplicaReplicationRuleConfiguration( Collections.singleton(dataSourceConfig), ImmutableMap.of("roundRobin", new ShardingSphereAlgorithmConfiguration("ROUND_ROBIN", new Properties())))); assertThat(actual.getDataSources().get("ds").getName(), is("ds")); assertThat(actual.getDataSources().get("ds").getPrimaryDataSourceName(), is("primary")); assertThat(actual.getDataSources().get("ds").getReplicaDataSourceNames(), is(Collections.singletonList("replica"))); assertThat(actual.getDataSources().get("ds").getLoadBalancerName(), is("roundRobin")); }
@Test public void assertSwapToYamlWithoutLoadBalanceAlgorithm() { PrimaryReplicaReplicationDataSourceRuleConfiguration dataSourceConfig = new PrimaryReplicaReplicationDataSourceRuleConfiguration("ds", "primary", Collections.singletonList("replica"), null); YamlPrimaryReplicaReplicationRuleConfiguration actual = getPrimaryReplicaReplicationRuleConfigurationYamlSwapper().swapToYamlConfiguration( new PrimaryReplicaReplicationRuleConfiguration(Collections.singleton(dataSourceConfig), Collections.emptyMap())); assertThat(actual.getDataSources().get("ds").getName(), is("ds")); assertThat(actual.getDataSources().get("ds").getPrimaryDataSourceName(), is("primary")); assertThat(actual.getDataSources().get("ds").getReplicaDataSourceNames(), is(Collections.singletonList("replica"))); assertNull(actual.getDataSources().get("ds").getLoadBalancerName()); }