Skip to content

Commit cfec741

Browse files
committed
Merge pull request #31 from stleary/json-path-for-validation
Replace util compare method with JsonPath
2 parents c578216 + abe421e commit cfec741

File tree

5 files changed

+571
-541
lines changed

5 files changed

+571
-541
lines changed

CookieListTest.java

Lines changed: 39 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,11 @@ 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+
// validate JSON content
79+
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
80+
assertTrue("Expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
81+
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
7882
}
7983

8084
/**
@@ -83,10 +87,11 @@ public void simpleCookieList() {
8387
@Test
8488
public void simpleCookieListWithDelimiter() {
8589
String cookieStr = "SID=31d4d96e407aad42;";
86-
String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
8790
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
88-
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
89-
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
91+
// validate JSON content
92+
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
93+
assertTrue("Expected 1 top level item", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 1);
94+
assertTrue("expected 31d4d96e407aad42", "31d4d96e407aad42".equals(JsonPath.read(doc, "$.SID")));
9095
}
9196

9297
/**
@@ -102,18 +107,16 @@ public void multiPartCookieList() {
102107
" name4=myCookieValue4; "+
103108
"name5=myCookieValue5;"+
104109
" 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-
"}";
114110
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
115-
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
116-
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
111+
// validate JSON content
112+
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
113+
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
114+
assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
115+
assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
116+
assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
117+
assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
118+
assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
119+
assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
117120
}
118121

119122
/**
@@ -139,21 +142,16 @@ public void convertCookieListToString() {
139142
" name4=myCookieValue4; "+
140143
"name5=myCookieValue5;"+
141144
" 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-
"}";
151145
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);
146+
// validate JSON content
147+
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
148+
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
149+
assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
150+
assertTrue("expected myCookieValue2", "myCookieValue2".equals(JsonPath.read(doc, "$.name2")));
151+
assertTrue("expected myCookieValue3", "myCookieValue3".equals(JsonPath.read(doc, "$.name3")));
152+
assertTrue("expected myCookieValue4", "myCookieValue4".equals(JsonPath.read(doc, "$.name4")));
153+
assertTrue("expected myCookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
154+
assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
157155
}
158156

159157
/**
@@ -169,22 +167,15 @@ public void convertEncodedCookieListToString() {
169167
" name4=my%25CookieValue4; "+
170168
"name5=myCookieValue5;"+
171169
" 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-
"}";
181170
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);
171+
// validate JSON content
172+
Object doc = Configuration.defaultConfiguration().jsonProvider().parse(jsonObject.toString());
173+
assertTrue("Expected 6 top level items", ((Map<?,?>)(JsonPath.read(doc, "$"))).size() == 6);
174+
assertTrue("expected myCookieValue1", "myCookieValue1".equals(JsonPath.read(doc, "$.name1")));
175+
assertTrue("expected my Cookie Value 2", "my Cookie Value 2".equals(JsonPath.read(doc, "$.name2")));
176+
assertTrue("expected my+Cookie&Value;3=", "my+Cookie&Value;3=".equals(JsonPath.read(doc, "$.name3")));
177+
assertTrue("expected my%CookieValue4", "my%CookieValue4".equals(JsonPath.read(doc, "$.name4")));
178+
assertTrue("expected my%CookieValue5", "myCookieValue5".equals(JsonPath.read(doc, "$.name5")));
179+
assertTrue("expected myCookieValue6", "myCookieValue6".equals(JsonPath.read(doc, "$.name6")));
187180
}
188-
189-
190181
}

0 commit comments

Comments
 (0)