From 34c600891613280e41cfd1ec1ad1bee8f23d043a Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Fri, 9 May 2014 23:03:09 +0200 Subject: Entities can be missing, user.place is not a string * User: place is not a string but a Place object. * User: entities is nullable. * Tweet: in_reply_to_user_id, coordinates is nullable. * ValidatingJsonDeserializer: Treat null values as missing fields. * ValidatingJsonDeserializerTest: Test for null values. --- test/data/ValidatingJsonDeserializerTest.java | 52 ++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 5 deletions(-) (limited to 'test/data') diff --git a/test/data/ValidatingJsonDeserializerTest.java b/test/data/ValidatingJsonDeserializerTest.java index 84dde5c..e258be2 100644 --- a/test/data/ValidatingJsonDeserializerTest.java +++ b/test/data/ValidatingJsonDeserializerTest.java @@ -3,6 +3,7 @@ package data; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonNull; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; @@ -359,7 +360,7 @@ public class ValidatingJsonDeserializerTest { tweet.addProperty("in_reply_to_user_id", 4); tweet.addProperty("created_at", "X"); tweet.addProperty("favorite_count", 4); - tweet.addProperty("place", "X"); + tweet.add("place", JsonNull.INSTANCE); tweet.addProperty("coordinates", "X"); tweet.addProperty("text", "X"); tweet.add("retweeted_status", JsonNull.INSTANCE); // Tweet object @@ -385,6 +386,19 @@ public class ValidatingJsonDeserializerTest { obj.remove(prop); } + private void addProperty(JsonObject obj, JsonElement val, String... names) { + String prop; + for (int i = 0; i < names.length - 1; i++) { + prop = names[i]; + if (!obj.has(prop)) { + obj.add(prop, new JsonObject()); + } + obj = obj.getAsJsonObject(prop); + } + prop = names[names.length - 1]; + obj.add(prop, val); + } + private void checkImpairedTweet(String... names) { JsonObject tweet = buildMinimalTweet(buildMinimalUser()); removeProperty(tweet, names); @@ -430,16 +444,29 @@ public class ValidatingJsonDeserializerTest { public void testTweet() { checkImpairedTweet("id"); checkImpairedTweet("lang"); - checkImpairedTweet("in_reply_to_user_id"); checkImpairedTweet("created_at"); checkImpairedTweet("favorite_count"); - checkImpairedTweet("place"); - checkImpairedTweet("coordinates"); checkImpairedTweet("text"); checkImpairedTweet("retweet_count"); checkImpairedTweet("user"); } + @Test + public void testTweetNulls() { + // place can be null + JsonObject tweet = buildMinimalTweet(buildMinimalUser()); + removeProperty(tweet, "place"); + checkTweetPass(tweet); + + tweet = buildMinimalTweet(buildMinimalUser()); + removeProperty(tweet, "in_reply_to_user_id"); + checkTweetPass(tweet); + + tweet = buildMinimalTweet(buildMinimalUser()); + removeProperty(tweet, "coordinates"); + checkTweetPass(tweet); + } + @Test public void testTweetEntities() { checkImpairedTweet("entities"); @@ -461,9 +488,16 @@ public class ValidatingJsonDeserializerTest { checkImpairedUser("lang"); } + @Test + public void testNullUserEntities() { + // entities can be null + JsonObject tweet = buildMinimalTweet(buildMinimalUser()); + removeProperty(tweet, "user", "entities"); + checkTweetPass(tweet); + } + @Test public void testUserEntities() { - checkImpairedTweet("user", "entities"); checkImpairedTweet("user", "entities", "url"); checkImpairedTweet("user", "entities", "url", "urls"); } @@ -489,4 +523,12 @@ public class ValidatingJsonDeserializerTest { tweet.add("retweeted_status", retweet); checkTweetFail(tweet, "Missing field: retweeted_status.text"); } + + @Test + public void testNullPlace() { + JsonObject tweet = buildMinimalTweet(buildMinimalUser()); + removeProperty(tweet, "place"); + tweet.add("place", null); + checkTweetPass(tweet); + } } -- cgit v1.2.1