diff options
author | Peter Wu <peter@lekensteyn.nl> | 2014-05-09 23:03:09 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2014-05-09 23:03:09 +0200 |
commit | 34c600891613280e41cfd1ec1ad1bee8f23d043a (patch) | |
tree | 5d08b0673ff939d5bb066b4ac488c7623a86b1cf /test/data | |
parent | 9be617801a8dabe4bcae017c2f671c5c10b3aebb (diff) | |
download | Datafiller-34c600891613280e41cfd1ec1ad1bee8f23d043a.tar.gz |
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.
Diffstat (limited to 'test/data')
-rw-r--r-- | test/data/ValidatingJsonDeserializerTest.java | 52 |
1 files changed, 47 insertions, 5 deletions
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,17 +444,30 @@ 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"); checkImpairedTweet("entities", "hashtags"); @@ -462,8 +489,15 @@ public class ValidatingJsonDeserializerTest { } @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); + } } |