YamlEngine { public static <T extends YamlConfiguration> T unmarshal(final File yamlFile, final Class<T> classType) throws IOException { try ( FileInputStream fileInputStream = new FileInputStream(yamlFile); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream) ) { return new Yaml(new ShardingSphereYamlConstructor(classType)).loadAs(inputStreamReader, classType); } }  static T unmarshal(final File yamlFile, final Class<T> classType); static T unmarshal(final byte[] yamlBytes, final Class<T> classType); static T unmarshal(final String yamlContent, final Class<T> classType); static Map<?, ?> unmarshal(final String yamlContent); static Properties unmarshalProperties(final String yamlContent); static String marshal(final Object value);  }
@Test public void assertUnmarshalWithFile() throws IOException { URL url = getClass().getClassLoader().getResource("yaml/fixture-rule.yaml"); assertNotNull(url); FixtureYamlRuleConfiguration actual = YamlEngine.unmarshal(new File(url.getFile()), FixtureYamlRuleConfiguration.class); assertThat(actual.getName(), is("test")); }
@Test public void assertUnmarshalWithYamlBytes() throws IOException { URL url = getClass().getClassLoader().getResource("yaml/fixture-rule.yaml"); assertNotNull(url); StringBuilder yamlContent = new StringBuilder(); try ( FileReader fileReader = new FileReader(url.getFile()); BufferedReader reader = new BufferedReader(fileReader)) { String line; while (null != (line = reader.readLine())) { yamlContent.append(line).append("\n"); } } FixtureYamlRuleConfiguration actual = YamlEngine.unmarshal(yamlContent.toString().getBytes(), FixtureYamlRuleConfiguration.class); assertThat(actual.getName(), is("test")); }
@Test public void assertUnmarshalWithYamlContentClassType() { FixtureYamlRuleConfiguration actual = YamlEngine.unmarshal("name: test", FixtureYamlRuleConfiguration.class); assertThat(actual.getName(), is("test")); }
@Test @SuppressWarnings("unchecked") public void assertUnmarshalWithYamlContent() { Map<String, Object> actual = (Map<String, Object>) YamlEngine.unmarshal("name: test"); assertThat(actual.get("name").toString(), is("test")); }