1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Replace util compare method with JsonPath

This commit is contained in:
stleary 2015-12-27 17:01:42 -06:00
parent d329b6514c
commit 7187006bae

View file

@ -2,9 +2,13 @@ package org.json.junit;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.*;
import org.json.*; import org.json.*;
import org.junit.Test; import org.junit.Test;
import com.jayway.jsonpath.*;
/** /**
* HTTP cookie specification RFC6265: http://tools.ietf.org/html/rfc6265 * HTTP cookie specification RFC6265: http://tools.ietf.org/html/rfc6265
* <p> * <p>
@ -60,7 +64,6 @@ public class CookieListTest {
@Test @Test
public void emptyStringCookieList() { public void emptyStringCookieList() {
String cookieStr = ""; String cookieStr = "";
String expectedCookieStr = "";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
assertTrue(jsonObject.length() == 0); assertTrue(jsonObject.length() == 0);
} }
@ -71,10 +74,13 @@ public class CookieListTest {
@Test @Test
public void simpleCookieList() { public void simpleCookieList() {
String cookieStr = "SID=31d4d96e407aad42"; String cookieStr = "SID=31d4d96e407aad42";
String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); Object doc = Configuration.defaultConfiguration().jsonProvider().
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); parse(jsonObject.toString());
assertTrue("Expected 1 top level item",
((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
assertTrue("expected 31d4d96e407aad42",
"31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
} }
/** /**
@ -83,10 +89,13 @@ public class CookieListTest {
@Test @Test
public void simpleCookieListWithDelimiter() { public void simpleCookieListWithDelimiter() {
String cookieStr = "SID=31d4d96e407aad42;"; String cookieStr = "SID=31d4d96e407aad42;";
String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); Object doc = Configuration.defaultConfiguration().jsonProvider().
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); parse(jsonObject.toString());
assertTrue("Expected 1 top level item",
((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
assertTrue("expected 31d4d96e407aad42",
"31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
} }
/** /**
@ -102,18 +111,23 @@ public class CookieListTest {
" name4=myCookieValue4; "+ " name4=myCookieValue4; "+
"name5=myCookieValue5;"+ "name5=myCookieValue5;"+
" name6=myCookieValue6;"; " name6=myCookieValue6;";
String expectedCookieStr =
"{"+
"\"name1\":\"myCookieValue1\","+
"\"name2\":\"myCookieValue2\","+
"\"name3\":\"myCookieValue3\","+
"\"name4\":\"myCookieValue4\","+
"\"name5\":\"myCookieValue5\","+
"\"name6\":\"myCookieValue6\""+
"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); Object doc = Configuration.defaultConfiguration().jsonProvider().
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); parse(jsonObject.toString());
assertTrue("Expected 6 top level items",
((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
assertTrue("expected myCookieValue1",
"myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
assertTrue("expected myCookieValue2",
"myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
assertTrue("expected myCookieValue3",
"myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
assertTrue("expected myCookieValue4",
"myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
assertTrue("expected myCookieValue5",
"myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
assertTrue("expected myCookieValue6",
"myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
} }
/** /**
@ -139,21 +153,23 @@ public class CookieListTest {
" name4=myCookieValue4; "+ " name4=myCookieValue4; "+
"name5=myCookieValue5;"+ "name5=myCookieValue5;"+
" name6=myCookieValue6;"; " name6=myCookieValue6;";
String expectedCookieStr =
"{"+
"\"name1\":\"myCookieValue1\","+
"\"name2\":\"myCookieValue2\","+
"\"name3\":\"myCookieValue3\","+
"\"name4\":\"myCookieValue4\","+
"\"name5\":\"myCookieValue5\","+
"\"name6\":\"myCookieValue6\""+
"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); Object doc = Configuration.defaultConfiguration().jsonProvider().
String cookieToStr = CookieList.toString(jsonObject); parse(jsonObject.toString());
JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr); assertTrue("Expected 6 top level items",
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); assertTrue("expected myCookieValue1",
"myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
assertTrue("expected myCookieValue2",
"myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
assertTrue("expected myCookieValue3",
"myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
assertTrue("expected myCookieValue4",
"myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
assertTrue("expected myCookieValue5",
"myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
assertTrue("expected myCookieValue6",
"myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
} }
/** /**
@ -169,22 +185,22 @@ public class CookieListTest {
" name4=my%25CookieValue4; "+ " name4=my%25CookieValue4; "+
"name5=myCookieValue5;"+ "name5=myCookieValue5;"+
" name6=myCookieValue6;"; " name6=myCookieValue6;";
String expectedCookieStr =
"{"+
"\"name1\":\"myCookieValue1\","+
"\"name2\":\"my Cookie Value 2\","+
"\"name3\":\"my+Cookie&Value;3=\","+
"\"name4\":\"my%CookieValue4\","+
"\"name5\":\"myCookieValue5\","+
"\"name6\":\"myCookieValue6\""+
"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); Object doc = Configuration.defaultConfiguration().jsonProvider().
String cookieToStr = CookieList.toString(jsonObject); parse(jsonObject.toString());
JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr); assertTrue("Expected 6 top level items",
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); assertTrue("expected myCookieValue1",
"myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
assertTrue("expected my Cookie Value 2",
"my Cookie Value 2".equals(JsonPath.read(doc, "$.name2")));
assertTrue("expected my+Cookie&Value;3=",
"my+Cookie&Value;3=".equals(JsonPath.read(doc, "$.name3")));
assertTrue("expected my%CookieValue4",
"my%CookieValue4".equals(JsonPath.read(doc, "$.name4")));
assertTrue("expected my%CookieValue5",
"myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
assertTrue("expected myCookieValue6",
"myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
} }
} }