Skip to content

Commit 7187006

Browse files
committed
Replace util compare method with JsonPath
1 parent d329b65 commit 7187006

1 file changed

Lines changed: 64 additions & 48 deletions

File tree

CookieListTest.java

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import static org.junit.Assert.*;
44

5+
import java.util.*;
6+
57
import org.json.*;
68
import org.junit.Test;
79

10+
import com.jayway.jsonpath.*;
11+
812
/**
913
* HTTP cookie specification RFC6265: http://tools.ietf.org/html/rfc6265
1014
* <p>
@@ -60,7 +64,6 @@ public void malFormedCookieListException() {
6064
@Test
6165
public void emptyStringCookieList() {
6266
String cookieStr = "";
63-
String expectedCookieStr = "";
6467
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
6568
assertTrue(jsonObject.length() == 0);
6669
}
@@ -71,10 +74,13 @@ public void emptyStringCookieList() {
7174
@Test
7275
public void simpleCookieList() {
7376
String cookieStr = "SID=31d4d96e407aad42";
74-
String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
7577
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
76-
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
77-
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
78+
Object doc = Configuration.defaultConfiguration().jsonProvider().
79+
parse(jsonObject.toString());
80+
assertTrue("Expected 1 top level item",
81+
((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
82+
assertTrue("expected 31d4d96e407aad42",
83+
"31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
7884
}
7985

8086
/**
@@ -83,10 +89,13 @@ public void simpleCookieList() {
8389
@Test
8490
public void simpleCookieListWithDelimiter() {
8591
String cookieStr = "SID=31d4d96e407aad42;";
86-
String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
8792
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
88-
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
89-
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
93+
Object doc = Configuration.defaultConfiguration().jsonProvider().
94+
parse(jsonObject.toString());
95+
assertTrue("Expected 1 top level item",
96+
((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
97+
assertTrue("expected 31d4d96e407aad42",
98+
"31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
9099
}
91100

92101
/**
@@ -102,18 +111,23 @@ public void multiPartCookieList() {
102111
" name4=myCookieValue4; "+
103112
"name5=myCookieValue5;"+
104113
" name6=myCookieValue6;";
105-
String expectedCookieStr =
106-
"{"+
107-
"\"name1\":\"myCookieValue1\","+
108-
"\"name2\":\"myCookieValue2\","+
109-
"\"name3\":\"myCookieValue3\","+
110-
"\"name4\":\"myCookieValue4\","+
111-
"\"name5\":\"myCookieValue5\","+
112-
"\"name6\":\"myCookieValue6\""+
113-
"}";
114114
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
115-
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
116-
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
115+
Object doc = Configuration.defaultConfiguration().jsonProvider().
116+
parse(jsonObject.toString());
117+
assertTrue("Expected 6 top level items",
118+
((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
119+
assertTrue("expected myCookieValue1",
120+
"myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
121+
assertTrue("expected myCookieValue2",
122+
"myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
123+
assertTrue("expected myCookieValue3",
124+
"myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
125+
assertTrue("expected myCookieValue4",
126+
"myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
127+
assertTrue("expected myCookieValue5",
128+
"myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
129+
assertTrue("expected myCookieValue6",
130+
"myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
117131
}
118132

119133
/**
@@ -139,21 +153,23 @@ public void convertCookieListToString() {
139153
" name4=myCookieValue4; "+
140154
"name5=myCookieValue5;"+
141155
" name6=myCookieValue6;";
142-
String expectedCookieStr =
143-
"{"+
144-
"\"name1\":\"myCookieValue1\","+
145-
"\"name2\":\"myCookieValue2\","+
146-
"\"name3\":\"myCookieValue3\","+
147-
"\"name4\":\"myCookieValue4\","+
148-
"\"name5\":\"myCookieValue5\","+
149-
"\"name6\":\"myCookieValue6\""+
150-
"}";
151156
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
152-
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
153-
String cookieToStr = CookieList.toString(jsonObject);
154-
JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr);
155-
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
156-
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
157+
Object doc = Configuration.defaultConfiguration().jsonProvider().
158+
parse(jsonObject.toString());
159+
assertTrue("Expected 6 top level items",
160+
((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
161+
assertTrue("expected myCookieValue1",
162+
"myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
163+
assertTrue("expected myCookieValue2",
164+
"myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
165+
assertTrue("expected myCookieValue3",
166+
"myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
167+
assertTrue("expected myCookieValue4",
168+
"myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
169+
assertTrue("expected myCookieValue5",
170+
"myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
171+
assertTrue("expected myCookieValue6",
172+
"myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
157173
}
158174

159175
/**
@@ -169,22 +185,22 @@ public void convertEncodedCookieListToString() {
169185
" name4=my%25CookieValue4; "+
170186
"name5=myCookieValue5;"+
171187
" name6=myCookieValue6;";
172-
String expectedCookieStr =
173-
"{"+
174-
"\"name1\":\"myCookieValue1\","+
175-
"\"name2\":\"my Cookie Value 2\","+
176-
"\"name3\":\"my+Cookie&Value;3=\","+
177-
"\"name4\":\"my%CookieValue4\","+
178-
"\"name5\":\"myCookieValue5\","+
179-
"\"name6\":\"myCookieValue6\""+
180-
"}";
181188
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
182-
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
183-
String cookieToStr = CookieList.toString(jsonObject);
184-
JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr);
185-
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
186-
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
189+
Object doc = Configuration.defaultConfiguration().jsonProvider().
190+
parse(jsonObject.toString());
191+
assertTrue("Expected 6 top level items",
192+
((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
193+
assertTrue("expected myCookieValue1",
194+
"myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
195+
assertTrue("expected my Cookie Value 2",
196+
"my Cookie Value 2".equals(JsonPath.read(doc, "$.name2")));
197+
assertTrue("expected my+Cookie&Value;3=",
198+
"my+Cookie&Value;3=".equals(JsonPath.read(doc, "$.name3")));
199+
assertTrue("expected my%CookieValue4",
200+
"my%CookieValue4".equals(JsonPath.read(doc, "$.name4")));
201+
assertTrue("expected my%CookieValue5",
202+
"myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
203+
assertTrue("expected myCookieValue6",
204+
"myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
187205
}
188-
189-
190206
}

0 commit comments

Comments
 (0)