Skip to content

Commit f6da9df

Browse files
author
Robert Ramsay
committed
Update JsonCreateRequestBodyTest so that the order of keys is ignored
1 parent 0305e45 commit f6da9df

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/test/java/com/telesign/JsonCreateRequestBodyTest.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,46 @@
11
package com.telesign;
22

3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonObject;
7+
import com.google.gson.reflect.TypeToken;
38
import junit.framework.TestCase;
49
import okhttp3.RequestBody;
510
import okio.Buffer;
611

712
import java.io.IOException;
813
import java.util.HashMap;
14+
import java.util.Map;
915

1016
public class JsonCreateRequestBodyTest extends TestCase {
1117

12-
private void makeRequestBodyAndAssert(HashMap<String, ? extends Object> params, String expectedJson) {
18+
private void makeRequestBodyAndAssert(HashMap<String, ? extends Object> params) {
1319
PhoneIdClient phoneIdClient = new PhoneIdClient("", "");
1420
Buffer actualJsonBuffer = new Buffer();
21+
Gson gson = new Gson();
22+
1523
try {
1624
RequestBody rb = phoneIdClient.createRequestBody(params, RestClient.JSON_CONTENT_TYPE);
1725
rb.writeTo(actualJsonBuffer);
1826
} catch (IOException exc) {
1927
fail(exc.getMessage());
2028
}
21-
assertEquals(expectedJson, actualJsonBuffer.readUtf8());
29+
HashMap<String, Object> unserializedJson = gson.fromJson(actualJsonBuffer.readUtf8(),
30+
new TypeToken<HashMap<String, Object>>() {}.getType());
31+
assertEquals(params, unserializedJson);
2232
}
2333

2434
public void testEmptyParams() {
25-
makeRequestBodyAndAssert(new HashMap<String, Object>(), "{}");
35+
makeRequestBodyAndAssert(new HashMap<String, Object>());
2636
}
2737

2838
public void testNoAddonsParams() {
2939
HashMap<String, String> params = new HashMap<String, String>() {{
3040
put("originating_ip", "127.0.0.1");
3141
put("bogus", "param");
3242
}};
33-
makeRequestBodyAndAssert(params, "{\"bogus\":\"param\",\"originating_ip\":\"127.0.0.1\"}");
43+
makeRequestBodyAndAssert(params);
3444
}
3545

3646
public void testAddonParamOnly() {
@@ -56,7 +66,7 @@ public void testAddonParamOnly() {
5666
put("subscriber_status", new HashMap<>());
5767
}});
5868
}};
59-
makeRequestBodyAndAssert(params, "{\"addons\":{\"contact_plus\":{\"billing_postal_code\":\"90210\"},\"device_info\":{},\"current_location_plus\":{},\"contact\":{},\"number_deactivation\":{},\"subscriber_status\":{},\"contact_match\":{\"country\":\"USA\",\"address\":\"12345 Some St\",\"city\":\"Los Angeles\",\"last_name\":\"Smith\",\"state\":\"CA\",\"postal_code\":\"90210\",\"first_name\":\"Bob\"},\"current_location\":{}}}");
69+
makeRequestBodyAndAssert(params);
6070
}
6171
public void testAddonAndRegularParams() {
6272
HashMap<String, Object> params = new HashMap<String, Object>() {{
@@ -83,6 +93,6 @@ public void testAddonAndRegularParams() {
8393
put("subscriber_status", new HashMap<>());
8494
}});
8595
}};
86-
makeRequestBodyAndAssert(params, "{\"addons\":{\"contact_plus\":{\"billing_postal_code\":\"90210\"},\"device_info\":{},\"current_location_plus\":{},\"contact\":{},\"number_deactivation\":{},\"subscriber_status\":{},\"contact_match\":{\"country\":\"USA\",\"address\":\"12345 Some St\",\"city\":\"Los Angeles\",\"last_name\":\"Smith\",\"state\":\"CA\",\"postal_code\":\"90210\",\"first_name\":\"Bob\"},\"current_location\":{}},\"originating_ip\":\"127.0.0.1\",\"account_lifecycle_event\":\"create\"}");
96+
makeRequestBodyAndAssert(params);
8797
}
8898
}

0 commit comments

Comments
 (0)