ConfigCenter { public void persistConfigurations(final String schemaName, final Map<String, DataSourceConfiguration> dataSourceConfigs, final Collection<RuleConfiguration> ruleConfigurations, final boolean isOverwrite) { persistDataSourceConfigurations(schemaName, dataSourceConfigs, isOverwrite); persistRuleConfigurations(schemaName, ruleConfigurations, isOverwrite); persistSchemaName(schemaName); } 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 assertPersistConfigurationForShardingRuleWithoutAuthenticationAndIsNotOverwriteAndConfigurationIsExisted() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), false); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository).persist(eq("/schemas/sharding_db/rule"), ArgumentMatchers.any()); }
@Test public void assertMoreSchema() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), false); verify(configurationRepository, times(0)).persist("/schemas", "myTest1,myTest2,sharding_db"); }
@Test public void assertMoreAndContainsSchema() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), false); verify(configurationRepository, times(0)).persist("/schemas", "myTest1,sharding_db"); }
@Test public void assertPersistConfigurationForShardingRuleWithoutAuthenticationAndIsNotOverwriteAndConfigurationIsNotExisted() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), false); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository).persist(eq("/schemas/sharding_db/rule"), ArgumentMatchers.any()); }
@Test public void assertPersistConfigurationForShardingRuleWithoutAuthenticationAndIsOverwrite() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), true); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository, times(0)).persist("/schemas/sharding_db/rule", readYAML(SHARDING_RULE_YAML)); }
@Test public void assertPersistConfigurationForPrimaryReplicaReplicationRuleWithoutAuthenticationAndIsNotOverwriteAndConfigurationIsExisted() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createPrimaryReplicaReplicationRuleConfiguration(), false); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository).persist(eq("/schemas/sharding_db/rule"), ArgumentMatchers.any()); }
@Test public void assertPersistConfigurationForPrimaryReplicaReplicationRuleWithoutAuthenticationAndIsNotOverwriteAndConfigurationIsNotExisted() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createPrimaryReplicaReplicationRuleConfiguration(), false); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository).persist(eq("/schemas/sharding_db/rule"), ArgumentMatchers.any()); }
@Test public void assertPersistConfigurationForPrimaryReplicaReplicationRuleWithoutAuthenticationAndIsOverwrite() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createPrimaryReplicaReplicationRuleConfiguration(), true); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository, times(0)).persist("/schemas/sharding_db/rule", readYAML(PRIMARY_REPLICA_REPLICATION_RULE_YAML)); }
@Test public void assertPersistConfigurationForShardingRuleWithAuthenticationAndIsNotOverwriteAndConfigurationIsExisted() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), false); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository).persist(eq("/schemas/sharding_db/rule"), ArgumentMatchers.any()); }
@Test public void assertPersistConfigurationForShardingRuleWithAuthenticationAndIsNotOverwriteAndConfigurationIsNotExisted() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), false); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository).persist(eq("/schemas/sharding_db/rule"), ArgumentMatchers.any()); }
@Test public void assertPersistConfigurationForShardingRuleWithAuthenticationAndIsOverwrite() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), true); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository, times(0)).persist("/schemas/sharding_db/rule", readYAML(SHARDING_RULE_YAML)); }
@Test public void assertPersistConfigurationForPrimaryReplicaReplicationRuleWithAuthenticationAndIsNotOverwriteAndConfigurationIsExisted() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createPrimaryReplicaReplicationRuleConfiguration(), false); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository).persist(eq("/schemas/sharding_db/rule"), ArgumentMatchers.any()); }
@Test public void assertPersistConfigurationForPrimaryReplicaReplicationRuleWithAuthenticationAndIsNotOverwriteAndConfigurationIsNotExisted() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createPrimaryReplicaReplicationRuleConfiguration(), false); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository).persist(eq("/schemas/sharding_db/rule"), ArgumentMatchers.any()); }
@Test public void assertPersistConfigurationForPrimaryReplicaReplicationRuleWithAuthenticationAndIsOverwrite() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createPrimaryReplicaReplicationRuleConfiguration(), true); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository, times(0)).persist("/schemas/sharding_db/rule", readYAML(PRIMARY_REPLICA_REPLICATION_RULE_YAML)); }
@Test public void assertPersistConfigurationForEncrypt() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createEncryptRuleConfiguration(), true); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository, times(0)).persist("/schemas/sharding_db/rule", readYAML(ENCRYPT_RULE_YAML)); }
@Test public void assertNullRuleConfiguration() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), Collections.emptyList(), true); }
@Test public void assertPersistConfigurationForShadow() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createShadowRuleConfiguration(), true); verify(configurationRepository).persist(eq("/schemas/sharding_db/datasource"), ArgumentMatchers.any()); verify(configurationRepository, times(0)).persist("/schemas/sharding_db/rule", readYAML(SHADOW_RULE_YAML)); }
@Test public void assertPersistSchemaNameWithExistSchema() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); when(configurationRepository.get("/schemas")).thenReturn("sharding_db"); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), true); verify(configurationRepository, times(0)).persist(eq("/schemas"), eq("sharding_db")); }
@Test public void assertPersistSchemaNameWithExistAndNewSchema() { ConfigCenter configCenter = new ConfigCenter(configurationRepository); when(configurationRepository.get("/schemas")).thenReturn("primary_replica_db"); configCenter.persistConfigurations("sharding_db", createDataSourceConfigurations(), createRuleConfigurations(), true); verify(configurationRepository).persist(eq("/schemas"), eq("primary_replica_db,sharding_db")); }