diff --git a/src/main/java/io/ljunggren/yaml/utils/YamlUtils.java b/src/main/java/io/ljunggren/yaml/utils/YamlUtils.java new file mode 100644 index 0000000..c4918d5 --- /dev/null +++ b/src/main/java/io/ljunggren/yaml/utils/YamlUtils.java @@ -0,0 +1,34 @@ +package io.ljunggren.yaml.utils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature; + +public class YamlUtils { + + public static String objectToYaml(Object object) throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER)); + return mapper.writeValueAsString(object); + } + + public static T yamlToObject(String yaml, Class clazz) throws JsonMappingException, JsonProcessingException { + return new ObjectMapper(new YAMLFactory()).readValue(yaml, clazz); + } + + public static boolean isValid(String yaml) { + try { + new ObjectMapper(new YAMLFactory()).readTree(yaml); + return true; + } catch (Exception e) { + return false; + } + } + + public static boolean areEqual(String yaml1, String yaml2) throws JsonMappingException, JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); + return mapper.readTree(yaml1).equals(mapper.readTree(yaml2)); + } + +} diff --git a/src/main/java/io/ljunggren/yamlUtils/YamlUtils.java b/src/main/java/io/ljunggren/yamlUtils/YamlUtils.java deleted file mode 100644 index 98461f1..0000000 --- a/src/main/java/io/ljunggren/yamlUtils/YamlUtils.java +++ /dev/null @@ -1,34 +0,0 @@ -package io.ljunggren.yamlUtils; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; -import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature; - -public class YamlUtils { - - public static String objectToYaml(Object object) throws JsonProcessingException { - ObjectMapper mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER)); - return mapper.writeValueAsString(object); - } - - public static T yamlToObject(String yaml, Class clazz) throws JsonMappingException, JsonProcessingException { - return new ObjectMapper(new YAMLFactory()).readValue(yaml, clazz); - } - - public static boolean isValid(String yaml) { - try { - new ObjectMapper(new YAMLFactory()).readTree(yaml); - return true; - } catch (Exception e) { - return false; - } - } - - public static boolean areEqual(String yaml1, String yaml2) throws JsonMappingException, JsonProcessingException { - ObjectMapper mapper = new ObjectMapper(new YAMLFactory()); - return mapper.readTree(yaml1).equals(mapper.readTree(yaml2)); - } - -} diff --git a/src/test/java/io/ljunggren/yaml/utils/YamlUtilsTest.java b/src/test/java/io/ljunggren/yaml/utils/YamlUtilsTest.java new file mode 100644 index 0000000..4e45952 --- /dev/null +++ b/src/test/java/io/ljunggren/yaml/utils/YamlUtilsTest.java @@ -0,0 +1,80 @@ +package io.ljunggren.yaml.utils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +public class YamlUtilsTest { + + @Data + @NoArgsConstructor + @AllArgsConstructor + private static class User { + private String name; + private int age; + private boolean active; + } + + @Test + public void objectToYamlTest() throws JsonProcessingException { + User user = new User("Alex", 40, true); + String expected = "name: \"Alex\"\n" + + "age: 40\n" + + "active: true\n"; + String actual = YamlUtils.objectToYaml(user); + assertEquals(expected, actual); + } + + @Test + public void yamlToObjectTest() throws JsonMappingException, JsonProcessingException { + User user = new User("Alex", 40, true); + String yaml = YamlUtils.objectToYaml(user); + User generatedUser = YamlUtils.yamlToObject(yaml, User.class); + assertEquals(user, generatedUser); + } + + @Test + public void isValidTest() throws JsonProcessingException { + User user = new User("Alex", 40, true); + String yaml = YamlUtils.objectToYaml(user); + assertTrue(YamlUtils.isValid(yaml)); + } + + @Test + public void isValidFalseTest() { + String yaml = "`name"; + assertFalse(YamlUtils.isValid(yaml)); + } + + @Test + public void areEqualTest() throws JsonProcessingException { + User user = new User("Alex", 40, true); + String expected = "# comment\n" + + "name: \"Alex\"\n" + + "age: 40\n" + + "active: true\n"; + String actual = YamlUtils.objectToYaml(user); + assertTrue(YamlUtils.areEqual(expected, actual)); + } + + @Test + public void areEqualFalseTest() throws JsonProcessingException { + User user = new User("Alex", 40, true); + String expected = "# comment\n" + + "name: \"Alexander\"\n" + + "age: 40\n" + + "active: true\n"; + String actual = YamlUtils.objectToYaml(user); + assertFalse(YamlUtils.areEqual(expected, actual)); + } + +} diff --git a/src/test/java/io/ljunggren/yamlUtils/YamlUtilsTest.java b/src/test/java/io/ljunggren/yamlUtils/YamlUtilsTest.java deleted file mode 100644 index b2d3166..0000000 --- a/src/test/java/io/ljunggren/yamlUtils/YamlUtilsTest.java +++ /dev/null @@ -1,80 +0,0 @@ -package io.ljunggren.yamlUtils; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.junit.Test; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonMappingException; - -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; - -public class YamlUtilsTest { - - @Data - @NoArgsConstructor - @AllArgsConstructor - private static class User { - private String name; - private int age; - private boolean active; - } - - @Test - public void objectToYamlTest() throws JsonProcessingException { - User user = new User("Alex", 40, true); - String expected = "name: \"Alex\"\n" - + "age: 40\n" - + "active: true\n"; - String actual = YamlUtils.objectToYaml(user); - assertEquals(expected, actual); - } - - @Test - public void yamlToObjectTest() throws JsonMappingException, JsonProcessingException { - User user = new User("Alex", 40, true); - String yaml = YamlUtils.objectToYaml(user); - User generatedUser = YamlUtils.yamlToObject(yaml, User.class); - assertEquals(user, generatedUser); - } - - @Test - public void isValidTest() throws JsonProcessingException { - User user = new User("Alex", 40, true); - String yaml = YamlUtils.objectToYaml(user); - assertTrue(YamlUtils.isValid(yaml)); - } - - @Test - public void isValidFalseTest() { - String yaml = "`name"; - assertFalse(YamlUtils.isValid(yaml)); - } - - @Test - public void areEqualTest() throws JsonProcessingException { - User user = new User("Alex", 40, true); - String expected = "# comment\n" - + "name: \"Alex\"\n" - + "age: 40\n" - + "active: true\n"; - String actual = YamlUtils.objectToYaml(user); - assertTrue(YamlUtils.areEqual(expected, actual)); - } - - @Test - public void areEqualFalseTest() throws JsonProcessingException { - User user = new User("Alex", 40, true); - String expected = "# comment\n" - + "name: \"Alexander\"\n" - + "age: 40\n" - + "active: true\n"; - String actual = YamlUtils.objectToYaml(user); - assertFalse(YamlUtils.areEqual(expected, actual)); - } - -}