From 7c0151b4217c56ded02d99484018656842384e76 Mon Sep 17 00:00:00 2001 From: Shafiullah Rahman <52661483+shafi-1@users.noreply.github.com> Date: Thu, 15 Aug 2019 17:46:35 +0100 Subject: [PATCH] Add unit tests for cn.escheduler.common.utils.JSONUtils These tests were written using Diffblue Cover. --- .../common/utils/JSONUtilsTest.java | 68 ++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/escheduler-common/src/test/java/cn/escheduler/common/utils/JSONUtilsTest.java b/escheduler-common/src/test/java/cn/escheduler/common/utils/JSONUtilsTest.java index 4db6bcf6be..78631ff702 100644 --- a/escheduler-common/src/test/java/cn/escheduler/common/utils/JSONUtilsTest.java +++ b/escheduler-common/src/test/java/cn/escheduler/common/utils/JSONUtilsTest.java @@ -20,10 +20,13 @@ import cn.escheduler.common.enums.DataType; import cn.escheduler.common.enums.Direct; import cn.escheduler.common.process.Property; import com.alibaba.fastjson.JSONObject; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; import org.junit.Assert; import org.junit.Test; import java.util.ArrayList; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -86,7 +89,70 @@ public class JSONUtilsTest { return resultJson; } + @Test + public void testToJson() { + Map map = new HashMap<>(); + map.put("foo","bar"); + + Assert.assertEquals("{\"foo\":\"bar\"}", JSONUtils.toJson(map)); + Assert.assertEquals( + String.valueOf((Object) null), JSONUtils.toJson(null)); + } + + @Test + public void testParseObject() { + Assert.assertEquals("{\"foo\":\"bar\"}", JSONUtils.parseObject( + "{\n" + "\"foo\": \"bar\",\n" + "}", String.class)); + + Assert.assertNull(JSONUtils.parseObject("", null)); + Assert.assertNull(JSONUtils.parseObject("foo", String.class)); + } + + @Test + public void testToList() { + Assert.assertEquals(new ArrayList(), + JSONUtils.toList("A1B2C3", null)); + Assert.assertEquals(new ArrayList(), + JSONUtils.toList("", null)); + } + + @Test + public void testCheckJsonVaild() { + Assert.assertTrue(JSONUtils.checkJsonVaild("3")); + Assert.assertFalse(JSONUtils.checkJsonVaild("")); + } + + @Test + public void testFindValue() { + Assert.assertNull(JSONUtils.findValue( + new ArrayNode(new JsonNodeFactory(true)), null)); + } + @Test + public void testToMap() { + Map map = new HashMap<>(); + map.put("foo","bar"); + Assert.assertTrue(map.equals(JSONUtils.toMap( + "{\n" + "\"foo\": \"bar\",\n" + "}"))); -} \ No newline at end of file + Assert.assertFalse(map.equals(JSONUtils.toMap( + "{\n" + "\"bar\": \"foo\",\n" + "}"))); + + Assert.assertNull(JSONUtils.toMap("3")); + Assert.assertNull(JSONUtils.toMap(null)); + Assert.assertNull(JSONUtils.toMap("3", null, null)); + Assert.assertNull(JSONUtils.toMap(null, null, null)); + } + + @Test + public void testToJsonString() { + Map map = new HashMap<>(); + map.put("foo", "bar"); + + Assert.assertEquals("{\"foo\":\"bar\"}", + JSONUtils.toJsonString(map)); + Assert.assertEquals(String.valueOf((Object) null), + JSONUtils.toJsonString(null)); + } +}