SchemaChangedListener extends PostGovernanceRepositoryEventListener { @Override protected Optional<GovernanceEvent> createGovernanceEvent(final DataChangedEvent event) { if (configurationNode.getSchemasPath().equals(event.getKey())) { return createSchemaNamesUpdatedEvent(event.getValue()); } String schemaName = configurationNode.getSchemaName(event.getKey()); if (Strings.isNullOrEmpty(schemaName) || !isValidNodeChangedEvent(schemaName, event.getKey())) { return Optional.empty(); } if (ChangedType.ADDED == event.getChangedType()) { return Optional.of(createAddedEvent(schemaName)); } if (ChangedType.UPDATED == event.getChangedType()) { return Optional.of(createUpdatedEvent(schemaName, event)); } if (ChangedType.DELETED == event.getChangedType()) { existedSchemaNames.remove(schemaName); return Optional.of(new SchemaDeletedEvent(schemaName)); } return Optional.empty(); } SchemaChangedListener(final ConfigurationRepository configurationRepository, final Collection<String> schemaNames);   }
@Test public void assertCreateIgnoredEvent() { assertFalse(schemaChangedListener.createGovernanceEvent(new DataChangedEvent("/schemas/encrypt_db", "test", ChangedType.UPDATED)).isPresent()); assertFalse(schemaChangedListener.createGovernanceEvent(new DataChangedEvent("/schemas/encrypt_db/rule", "test", ChangedType.IGNORED)).isPresent()); }
@Test public void assertCreateDataSourceChangedEventForExistedSchema() { String dataSource = readYAML(DATA_SOURCE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/sharding_db/datasource", dataSource, ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(actual.get(), instanceOf(DataSourceChangedEvent.class)); assertThat(((DataSourceChangedEvent) actual.get()).getSchemaName(), is("sharding_db")); }
@Test public void assertCreateRuleConfigurationsChangedEventForExistedSchema() { String shardingRule = readYAML(SHARDING_RULE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/sharding_db/rule", shardingRule, ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(((RuleConfigurationsChangedEvent) actual.get()).getSchemaName(), is("sharding_db")); Collection<RuleConfiguration> ruleConfigs = ((RuleConfigurationsChangedEvent) actual.get()).getRuleConfigurations(); assertThat(ruleConfigs.size(), is(1)); assertThat(((ShardingRuleConfiguration) ruleConfigs.iterator().next()).getTables().size(), is(1)); }
@Test public void assertCreatePrimaryReplicaReplicationRuleChangedEventForExistedSchema() { String rule = readYAML(PRIMARY_REPLICA_REPLICATION_RULE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/primary_replica_replication_db/rule", rule, ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); RuleConfigurationsChangedEvent event = (RuleConfigurationsChangedEvent) actual.get(); assertThat(event.getSchemaName(), is("primary_replica_replication_db")); assertThat(event.getRuleConfigurations().iterator().next(), instanceOf(PrimaryReplicaReplicationRuleConfiguration.class)); PrimaryReplicaReplicationRuleConfiguration ruleConfig = (PrimaryReplicaReplicationRuleConfiguration) event.getRuleConfigurations().iterator().next(); assertThat(ruleConfig.getDataSources().iterator().next().getPrimaryDataSourceName(), is("primary_ds")); }
@Test public void assertCreateEncryptRuleChangedEventForExistedSchema() { String encryptRule = readYAML(ENCRYPT_RULE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/encrypt_db/rule", encryptRule, ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); RuleConfigurationsChangedEvent event = (RuleConfigurationsChangedEvent) actual.get(); assertThat(event.getSchemaName(), is("encrypt_db")); assertThat(event.getRuleConfigurations().iterator().next(), instanceOf(EncryptRuleConfiguration.class)); EncryptRuleConfiguration encryptRuleConfig = (EncryptRuleConfiguration) event.getRuleConfigurations().iterator().next(); assertThat(encryptRuleConfig.getEncryptors().size(), is(1)); ShardingSphereAlgorithmConfiguration encryptAlgorithmConfig = encryptRuleConfig.getEncryptors().get("order_encryptor"); assertThat(encryptAlgorithmConfig.getType(), is("AES")); assertThat(encryptAlgorithmConfig.getProps().get("aes-key-value"), is(123456)); }
@Test public void assertCreateIgnoredGovernanceEventForNewSchema() { DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/logic_db/rule", "rule", ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(actual.get(), instanceOf(SchemaAddedEvent.class)); }
@Test public void assertCreateSchemaAddedEventForNewSchema() { String dataSource = readYAML(DATA_SOURCE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/logic_db/datasource", dataSource, ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(((SchemaAddedEvent) actual.get()).getSchemaName(), is("logic_db")); }
@Test public void assertCreatePrimaryReplicaReplicationSchemaAddedEventForNewSchema() { String dataSource = readYAML(DATA_SOURCE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/logic_db/datasource", dataSource, ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(((SchemaAddedEvent) actual.get()).getSchemaName(), is("logic_db")); }
@Test public void assertCreateEncryptSchemaAddedEventForNewSchema() { String dataSource = readYAML(DATA_SOURCE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/logic_db/datasource", dataSource, ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(((SchemaAddedEvent) actual.get()).getSchemaName(), is("logic_db")); }
@Test public void assertCreateSchemaDeletedEventForNewSchema() { String dataSource = readYAML(DATA_SOURCE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/logic_db/datasource", dataSource, ChangedType.DELETED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(((SchemaDeletedEvent) actual.get()).getSchemaName(), is("logic_db")); }
@Test public void assertCreateWithSchemaDeletedEvent() { String dataSource = readYAML(DATA_SOURCE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/logic_db", dataSource, ChangedType.DELETED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(actual.get(), instanceOf(SchemaDeletedEvent.class)); }
@Test public void assertCreateWithSchemaDeletedEventWithDataSourceNode() { String dataSource = readYAML(DATA_SOURCE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/datasource", dataSource, ChangedType.DELETED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(actual.get(), instanceOf(SchemaDeletedEvent.class)); }
@Test public void assertCreateAddedEventWithEncryptRuleConfigurationForNewSchema() { String encryptRule = readYAML(ENCRYPT_RULE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/logic_db/rule", encryptRule, ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(((SchemaAddedEvent) actual.get()).getSchemaName(), is("logic_db")); }
@Test public void assertCreateAddedEventWithShardingRuleConfigurationForNewSchema() { String shardingRule = readYAML(SHARDING_RULE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/logic_db/rule", shardingRule, ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(((SchemaAddedEvent) actual.get()).getSchemaName(), is("logic_db")); }
@Test public void assertCreateAddedEventWithPrimaryReplicaReplicationRuleConfigurationForNewSchema() { String rule = readYAML(PRIMARY_REPLICA_REPLICATION_RULE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/logic_db/rule", rule, ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(((SchemaAddedEvent) actual.get()).getSchemaName(), is("logic_db")); }
@Test public void assertCreateSchemaNamesUpdatedEventForAdd() { DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas", "sharding_db,primary_replica_replication_db,encrypt_db,shadow_db", ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(((SchemaAddedEvent) actual.get()).getSchemaName(), is("shadow_db")); }
@Test public void assertCreateSchemaNamesUpdatedEventForDelete() { DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas", "sharding_db,primary_replica_replication_db", ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(((SchemaDeletedEvent) actual.get()).getSchemaName(), is("encrypt_db")); }
@Test public void assertCreateSchemaNamesUpdatedEventForIgnore() { DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas", "sharding_db,primary_replica_replication_db,encrypt_db", ChangedType.UPDATED); assertFalse(schemaChangedListener.createGovernanceEvent(dataChangedEvent).isPresent()); }
@Test public void assertCreateSchemaNameAddEvent() { DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/shadow_db", "", ChangedType.ADDED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertThat(((SchemaAddedEvent) actual.get()).getSchemaName(), is("shadow_db")); }
@Test public void assertCreateMetaDataChangedEvent() { DataChangedEvent dataChangedEvent = new DataChangedEvent("/schemas/sharding_db/table", readYAML(META_DATA_FILE), ChangedType.UPDATED); Optional<GovernanceEvent> actual = schemaChangedListener.createGovernanceEvent(dataChangedEvent); assertTrue(actual.isPresent()); assertTrue(((MetaDataChangedEvent) actual.get()).getRuleSchemaMetaData().getConfiguredSchemaMetaData().getAllTableNames().contains("t_order")); }