ConfigCenter { public Collection<RuleConfiguration> loadRuleConfigurations(final String schemaName) { return hasRuleConfiguration(schemaName) ? new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations( YamlEngine.unmarshal(repository.get(node.getRulePath(schemaName)), YamlRootRuleConfigurations.class).getRules()) : new LinkedList<>(); } 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 assertLoadShardingAndEncryptRuleConfiguration() { when(configurationRepository.get("/schemas/sharding_db/rule")).thenReturn(readYAML(SHARDING_AND_ENCRYPT_RULE_YAML)); ConfigCenter configCenter = new ConfigCenter(configurationRepository); Collection<RuleConfiguration> ruleConfigurations = configCenter.loadRuleConfigurations("sharding_db"); assertThat(ruleConfigurations.size(), is(2)); for (RuleConfiguration each : ruleConfigurations) { if (each instanceof ShardingRuleConfiguration) { ShardingRuleConfiguration shardingRuleConfig = (ShardingRuleConfiguration) each; assertThat(shardingRuleConfig.getTables().size(), is(1)); assertThat(shardingRuleConfig.getTables().iterator().next().getLogicTable(), is("t_order")); } else if (each instanceof EncryptRuleConfiguration) { EncryptRuleConfiguration encryptRuleConfig = (EncryptRuleConfiguration) each; assertThat(encryptRuleConfig.getEncryptors().size(), is(2)); ShardingSphereAlgorithmConfiguration encryptAlgorithmConfig = encryptRuleConfig.getEncryptors().get("aes_encryptor"); assertThat(encryptAlgorithmConfig.getType(), is("AES")); assertThat(encryptAlgorithmConfig.getProps().get("aes-key-value").toString(), is("123456abcd")); } } }
@Test public void assertLoadShardingRuleConfiguration() { when(configurationRepository.get("/schemas/sharding_db/rule")).thenReturn(readYAML(SHARDING_RULE_YAML)); ConfigCenter configCenter = new ConfigCenter(configurationRepository); Collection<RuleConfiguration> actual = configCenter.loadRuleConfigurations("sharding_db"); assertThat(actual.size(), is(1)); ShardingRuleConfiguration actualShardingRuleConfig = (ShardingRuleConfiguration) actual.iterator().next(); assertThat(actualShardingRuleConfig.getTables().size(), is(1)); assertThat(actualShardingRuleConfig.getTables().iterator().next().getLogicTable(), is("t_order")); }
@Test public void assertLoadPrimaryReplicaReplicationRuleConfiguration() { when(configurationRepository.get("/schemas/sharding_db/rule")).thenReturn(readYAML(PRIMARY_REPLICA_REPLICATION_RULE_YAML)); ConfigCenter configCenter = new ConfigCenter(configurationRepository); Collection<RuleConfiguration> actual = configCenter.loadRuleConfigurations("sharding_db"); PrimaryReplicaReplicationRuleConfiguration config = (PrimaryReplicaReplicationRuleConfiguration) actual.iterator().next(); assertThat(config.getDataSources().size(), is(1)); assertThat(config.getDataSources().iterator().next().getPrimaryDataSourceName(), is("primary_ds")); assertThat(config.getDataSources().iterator().next().getReplicaDataSourceNames().size(), is(2)); }
@Test public void assertLoadEncryptRuleConfiguration() { when(configurationRepository.get("/schemas/sharding_db/rule")).thenReturn(readYAML(ENCRYPT_RULE_YAML)); ConfigCenter configCenter = new ConfigCenter(configurationRepository); EncryptRuleConfiguration actual = (EncryptRuleConfiguration) configCenter.loadRuleConfigurations("sharding_db").iterator().next(); assertThat(actual.getEncryptors().size(), is(1)); ShardingSphereAlgorithmConfiguration encryptAlgorithmConfig = actual.getEncryptors().get("order_encryptor"); assertThat(encryptAlgorithmConfig.getType(), is("AES")); assertThat(encryptAlgorithmConfig.getProps().get("aes-key-value").toString(), is("123456")); }
@Test public void assertLoadShadowRuleConfiguration() { when(configurationRepository.get("/schemas/sharding_db/rule")).thenReturn(readYAML(SHADOW_RULE_YAML)); ConfigCenter configCenter = new ConfigCenter(configurationRepository); ShadowRuleConfiguration actual = (ShadowRuleConfiguration) configCenter.loadRuleConfigurations("sharding_db").iterator().next(); assertThat(actual.getSourceDataSourceNames(), is(Arrays.asList("ds", "ds1"))); assertThat(actual.getShadowDataSourceNames(), is(Arrays.asList("shadow_ds", "shadow_ds1"))); assertThat(actual.getColumn(), is("shadow")); }