diff --git a/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/java/org/apache/shardingsphere/orchestration/core/configcenter/listener/SchemaChangedListenerTest.java b/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/java/org/apache/shardingsphere/orchestration/core/configcenter/listener/SchemaChangedListenerTest.java index 3fcb0da99b4627cb079fa8ebeeac5cf3d6f00100..f4a1f11a973f535654526fbbd5b6886d7d6dd5f8 100644 --- a/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/java/org/apache/shardingsphere/orchestration/core/configcenter/listener/SchemaChangedListenerTest.java +++ b/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/java/org/apache/shardingsphere/orchestration/core/configcenter/listener/SchemaChangedListenerTest.java @@ -17,7 +17,7 @@ package org.apache.shardingsphere.orchestration.core.configcenter.listener; -import com.google.common.base.Preconditions; +import lombok.SneakyThrows; import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration; import org.apache.shardingsphere.encrypt.api.config.EncryptorRuleConfiguration; import org.apache.shardingsphere.masterslave.api.config.MasterSlaveRuleConfiguration; @@ -28,24 +28,24 @@ import org.apache.shardingsphere.orchestration.core.common.event.DataSourceChang import org.apache.shardingsphere.orchestration.core.common.event.EncryptRuleChangedEvent; import org.apache.shardingsphere.orchestration.core.common.event.IgnoredShardingOrchestrationEvent; import org.apache.shardingsphere.orchestration.core.common.event.MasterSlaveRuleChangedEvent; +import org.apache.shardingsphere.orchestration.core.common.event.RuleConfigurationsChangedEvent; import org.apache.shardingsphere.orchestration.core.common.event.SchemaAddedEvent; import org.apache.shardingsphere.orchestration.core.common.event.SchemaDeletedEvent; import org.apache.shardingsphere.orchestration.core.common.event.ShardingOrchestrationEvent; -import org.apache.shardingsphere.orchestration.core.common.event.RuleConfigurationsChangedEvent; import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.underlying.common.config.RuleConfiguration; -import org.apache.shardingsphere.underlying.common.yaml.representer.ShardingSphereYAMLRepresenter; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; -import org.yaml.snakeyaml.Yaml; -import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collection; import java.util.Map.Entry; +import java.util.stream.Collectors; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; @@ -54,15 +54,15 @@ import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public final class SchemaChangedListenerTest { - - private static final String DATA_SOURCE_YAML = "data-source.yaml"; - - private static final String SHARDING_RULE_YAML = "sharding-rule.yaml"; - - private static final String MASTER_SLAVE_RULE_YAML = "master-slave-rule.yaml"; - - private static final String ENCRYPT_RULE_YAML = "encrypt-rule.yaml"; - + + private static final String DATA_SOURCE_FILE = "yaml/data-source.yaml"; + + private static final String SHARDING_RULE_FILE = "yaml/sharding-rule.yaml"; + + private static final String MASTER_SLAVE_RULE_FILE = "yaml/master-slave-rule.yaml"; + + private static final String ENCRYPT_RULE_FILE = "yaml/encrypt-rule.yaml"; + private SchemaChangedListener schemaChangedListener; @Mock @@ -83,23 +83,16 @@ public final class SchemaChangedListenerTest { @Test public void assertCreateDataSourceChangedEventForExistedSchema() { - String dataSource = readYamlIntoString(DATA_SOURCE_YAML); + String dataSource = readYAML(DATA_SOURCE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/test/config/schema/sharding_db/datasource", dataSource, ChangedType.UPDATED); ShardingOrchestrationEvent actual = schemaChangedListener.createShardingOrchestrationEvent(dataChangedEvent); assertThat(actual, instanceOf(DataSourceChangedEvent.class)); assertThat(((DataSourceChangedEvent) actual).getShardingSchemaName(), is("sharding_db")); } - - private String readYamlIntoString(final String yamlFile) { - InputStream input = getClass().getClassLoader().getResourceAsStream(yamlFile); - Preconditions.checkNotNull(input, "file resource `%s` must not be null.", yamlFile); - Yaml yaml = new Yaml(new ShardingSphereYAMLRepresenter()); - return yaml.dumpAsMap(yaml.load(input)); - } @Test public void assertCreateRuleConfigurationsChangedEventForExistedSchema() { - String shardingRule = readYamlIntoString(SHARDING_RULE_YAML); + String shardingRule = readYAML(SHARDING_RULE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/test/config/schema/sharding_db/rule", shardingRule, ChangedType.UPDATED); ShardingOrchestrationEvent actual = schemaChangedListener.createShardingOrchestrationEvent(dataChangedEvent); assertThat(actual, instanceOf(RuleConfigurationsChangedEvent.class)); @@ -111,7 +104,7 @@ public final class SchemaChangedListenerTest { @Test public void assertCreateMasterSlaveRuleChangedEventForExistedSchema() { - String masterSlaveRule = readYamlIntoString(MASTER_SLAVE_RULE_YAML); + String masterSlaveRule = readYAML(MASTER_SLAVE_RULE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/test/config/schema/masterslave_db/rule", masterSlaveRule, ChangedType.UPDATED); ShardingOrchestrationEvent actual = schemaChangedListener.createShardingOrchestrationEvent(dataChangedEvent); assertThat(actual, instanceOf(MasterSlaveRuleChangedEvent.class)); @@ -121,7 +114,7 @@ public final class SchemaChangedListenerTest { @Test public void assertCreateEncryptRuleChangedEventForExistedSchema() { - String encryptRule = readYamlIntoString(ENCRYPT_RULE_YAML); + String encryptRule = readYAML(ENCRYPT_RULE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/test/config/schema/encrypt_db/rule", encryptRule, ChangedType.UPDATED); ShardingOrchestrationEvent actual = schemaChangedListener.createShardingOrchestrationEvent(dataChangedEvent); assertThat(actual, instanceOf(EncryptRuleChangedEvent.class)); @@ -143,8 +136,8 @@ public final class SchemaChangedListenerTest { @Test public void assertCreateShardingSchemaAddedEventForNewSchema() { - String shardingRule = readYamlIntoString(SHARDING_RULE_YAML); - String dataSource = readYamlIntoString(DATA_SOURCE_YAML); + String shardingRule = readYAML(SHARDING_RULE_FILE); + String dataSource = readYAML(DATA_SOURCE_FILE); when(configCenterRepository.get("/test/config/schema/logic_db/rule")).thenReturn(shardingRule); when(configCenterRepository.get("/test/config/schema/logic_db/datasource")).thenReturn(dataSource); when(configCenterRepository.get("/test/config/schema/logic_db/rule")).thenReturn(shardingRule); @@ -157,8 +150,8 @@ public final class SchemaChangedListenerTest { @Test public void assertCreateMasterSlaveSchemaAddedEventForNewSchema() { - String dataSource = readYamlIntoString(DATA_SOURCE_YAML); - String masterSlaveRule = readYamlIntoString(MASTER_SLAVE_RULE_YAML); + String dataSource = readYAML(DATA_SOURCE_FILE); + String masterSlaveRule = readYAML(MASTER_SLAVE_RULE_FILE); when(configCenterRepository.get("/test/config/schema/logic_db/rule")).thenReturn(masterSlaveRule); when(configCenterRepository.get("/test/config/schema/logic_db/datasource")).thenReturn(dataSource); when(configCenterRepository.get("/test/config/schema/logic_db/rule")).thenReturn(masterSlaveRule); @@ -171,8 +164,8 @@ public final class SchemaChangedListenerTest { @Test public void assertCreateEncryptSchemaAddedEventForNewSchema() { - String dataSource = readYamlIntoString(DATA_SOURCE_YAML); - String encryptRule = readYamlIntoString(ENCRYPT_RULE_YAML); + String dataSource = readYAML(DATA_SOURCE_FILE); + String encryptRule = readYAML(ENCRYPT_RULE_FILE); when(configCenterRepository.get("/test/config/schema/logic_db/rule")).thenReturn(encryptRule); when(configCenterRepository.get("/test/config/schema/logic_db/datasource")).thenReturn(dataSource); when(configCenterRepository.get("/test/config/schema/logic_db/rule")).thenReturn(encryptRule); @@ -185,7 +178,7 @@ public final class SchemaChangedListenerTest { @Test public void assertCreateSchemaDeletedEventForNewSchema() { - String dataSource = readYamlIntoString(DATA_SOURCE_YAML); + String dataSource = readYAML(DATA_SOURCE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/test/config/schema/logic_db/datasource", dataSource, ChangedType.DELETED); ShardingOrchestrationEvent actual = schemaChangedListener.createShardingOrchestrationEvent(dataChangedEvent); assertThat(actual, instanceOf(SchemaDeletedEvent.class)); @@ -194,7 +187,7 @@ public final class SchemaChangedListenerTest { @Test public void assertCreateWithInvalidNodeChangedEvent() { - String dataSource = readYamlIntoString(DATA_SOURCE_YAML); + String dataSource = readYAML(DATA_SOURCE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/test/config/schema/logic_db", dataSource, ChangedType.DELETED); ShardingOrchestrationEvent actual = schemaChangedListener.createShardingOrchestrationEvent(dataChangedEvent); assertThat(actual, instanceOf(IgnoredShardingOrchestrationEvent.class)); @@ -202,7 +195,7 @@ public final class SchemaChangedListenerTest { @Test public void assertCreateWithNullShardingSchemaName() { - String dataSource = readYamlIntoString(DATA_SOURCE_YAML); + String dataSource = readYAML(DATA_SOURCE_FILE); DataChangedEvent dataChangedEvent = new DataChangedEvent("/test/config/schema/datasource", dataSource, ChangedType.DELETED); ShardingOrchestrationEvent actual = schemaChangedListener.createShardingOrchestrationEvent(dataChangedEvent); assertThat(actual, instanceOf(IgnoredShardingOrchestrationEvent.class)); @@ -210,8 +203,8 @@ public final class SchemaChangedListenerTest { @Test public void assertCreateAddedEventWithEncryptRuleConfigurationForNewSchema() { - String dataSource = readYamlIntoString(DATA_SOURCE_YAML); - String encryptRule = readYamlIntoString(ENCRYPT_RULE_YAML); + String dataSource = readYAML(DATA_SOURCE_FILE); + String encryptRule = readYAML(ENCRYPT_RULE_FILE); when(configCenterRepository.get("/test/config/schema/logic_db/rule")).thenReturn(encryptRule); when(configCenterRepository.get("/test/config/schema/logic_db/datasource")).thenReturn(dataSource); DataChangedEvent dataChangedEvent = new DataChangedEvent("/test/config/schema/logic_db/rule", encryptRule, ChangedType.UPDATED); @@ -222,8 +215,8 @@ public final class SchemaChangedListenerTest { @Test public void assertCreateAddedEventWithShardingRuleConfigurationForNewSchema() { - String dataSource = readYamlIntoString(DATA_SOURCE_YAML); - String shardingRule = readYamlIntoString(SHARDING_RULE_YAML); + String dataSource = readYAML(DATA_SOURCE_FILE); + String shardingRule = readYAML(SHARDING_RULE_FILE); when(configCenterRepository.get("/test/config/schema/logic_db/rule")).thenReturn(shardingRule); when(configCenterRepository.get("/test/config/schema/logic_db/datasource")).thenReturn(dataSource); DataChangedEvent dataChangedEvent = new DataChangedEvent("/test/config/schema/logic_db/rule", shardingRule, ChangedType.UPDATED); @@ -234,8 +227,8 @@ public final class SchemaChangedListenerTest { @Test public void assertCreateAddedEventWithMasterSlaveRuleConfigurationForNewSchema() { - String dataSource = readYamlIntoString(DATA_SOURCE_YAML); - String masterSlaveRule = readYamlIntoString(MASTER_SLAVE_RULE_YAML); + String dataSource = readYAML(DATA_SOURCE_FILE); + String masterSlaveRule = readYAML(MASTER_SLAVE_RULE_FILE); when(configCenterRepository.get("/test/config/schema/logic_db/rule")).thenReturn(masterSlaveRule); when(configCenterRepository.get("/test/config/schema/logic_db/datasource")).thenReturn(dataSource); DataChangedEvent dataChangedEvent = new DataChangedEvent("/test/config/schema/logic_db/rule", masterSlaveRule, ChangedType.UPDATED); @@ -243,4 +236,9 @@ public final class SchemaChangedListenerTest { assertThat(actual, instanceOf(SchemaAddedEvent.class)); assertThat(((SchemaAddedEvent) actual).getRuleConfigurations().iterator().next(), instanceOf(MasterSlaveRuleConfiguration.class)); } + + @SneakyThrows + private String readYAML(final String yamlFile) { + return Files.readAllLines(Paths.get(ClassLoader.getSystemResource(yamlFile).toURI())).stream().map(each -> each + System.lineSeparator()).collect(Collectors.joining()); + } } diff --git a/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/data-source.yaml b/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/yaml/data-source.yaml similarity index 100% rename from sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/data-source.yaml rename to sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/yaml/data-source.yaml diff --git a/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/encrypt-rule.yaml b/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/yaml/encrypt-rule.yaml similarity index 92% rename from sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/encrypt-rule.yaml rename to sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/yaml/encrypt-rule.yaml index fa27886279a12d04dd9cf5d9a72c1280c3ba7bbb..ff59e37e21730cc5c64df6685bb237bc7bd28e7a 100644 --- a/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/encrypt-rule.yaml +++ b/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/yaml/encrypt-rule.yaml @@ -16,7 +16,7 @@ # rules: -- !!org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration +- !ENCRYPT tables: t_order: columns: diff --git a/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/master-slave-rule.yaml b/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/yaml/master-slave-rule.yaml similarity index 91% rename from sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/master-slave-rule.yaml rename to sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/yaml/master-slave-rule.yaml index 1dbc92c71765125efcd963aa6886c4c8c5035afb..186fc3accf802fc5d9354cddb366a0ded2b52a93 100644 --- a/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/master-slave-rule.yaml +++ b/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/yaml/master-slave-rule.yaml @@ -16,7 +16,7 @@ # rules: -- !!org.apache.shardingsphere.masterslave.yaml.config.YamlMasterSlaveRuleConfiguration +- !MASTER_SLAVE dataSources: ms_ds: masterDataSourceName: master_ds diff --git a/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/sharding-rule.yaml b/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/yaml/sharding-rule.yaml similarity index 93% rename from sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/sharding-rule.yaml rename to sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/yaml/sharding-rule.yaml index 6efe557c5fd0bc135b48de3ed3699faca323bd09..a9a4e2ccbec83111b106225baca07372f51f5bd6 100644 --- a/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/sharding-rule.yaml +++ b/sharding-orchestration/sharding-orchestration-core/sharding-orchestration-core-configcenter/src/test/resources/yaml/sharding-rule.yaml @@ -16,7 +16,7 @@ # rules: -- !!org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration +- !SHARDING tables: t_order: logicTable: t_order