PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/RestFB/library/src/test/java/com/restfb/JsonMapperToJavaTest.java

http://restfb.googlecode.com/
Java | 296 lines | 169 code | 40 blank | 87 comment | 22 complexity | f43f5940ac1f9dd27a8f13d6f45965bf MD5 | raw file
Possible License(s): JSON
  1. /*
  2. * Copyright (c) 2010-2011 Mark Allen.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. package com.restfb;
  23. import static junit.framework.Assert.assertTrue;
  24. import java.util.List;
  25. import junit.framework.Assert;
  26. import org.junit.Test;
  27. import com.restfb.types.NamedFacebookType;
  28. import com.restfb.types.Post;
  29. import com.restfb.types.User;
  30. /**
  31. * Unit tests that exercise {@link JsonMapper} implementations, specifically the
  32. * "convert JSON to Java" functionality.
  33. *
  34. * @author <a href="http://restfb.com">Mark Allen</a>
  35. */
  36. public class JsonMapperToJavaTest extends AbstractJsonMapperTests {
  37. /**
  38. * Can we handle the empty list?
  39. */
  40. @Test
  41. public void emptyList() {
  42. List<Object> objects = createJsonMapper().toJavaList("[]", Object.class);
  43. assertTrue(objects.size() == 0);
  44. }
  45. /**
  46. * Can we handle the empty object?
  47. */
  48. @Test
  49. public void emptyObject() {
  50. Object object = createJsonMapper().toJavaObject("{}", Object.class);
  51. assertTrue(object != null);
  52. }
  53. /**
  54. * Can we handle simple primitive mapping?
  55. */
  56. @Test
  57. public void simplePrimitive() {
  58. String tag = createJsonMapper().toJavaObject(jsonFromClasspath("tag"), String.class);
  59. assertTrue("Good".equals(tag));
  60. }
  61. /**
  62. * Can we handle simple numeric mapping?
  63. */
  64. @Test
  65. public void simplePrimitiveNumber() {
  66. Integer number = createJsonMapper().toJavaObject(jsonFromClasspath("number"), Integer.class);
  67. assertTrue(number.equals(1234));
  68. }
  69. /**
  70. * Can we handle simple primitive list mapping?
  71. */
  72. @Test
  73. public void simplePrimitiveList() {
  74. List<String> tags = createJsonMapper().toJavaList(jsonFromClasspath("tags"), String.class);
  75. assertTrue(tags.size() == 3);
  76. assertTrue("Good".equals(tags.get(0)));
  77. assertTrue("Better".equals(tags.get(1)));
  78. assertTrue("Best".equals(tags.get(2)));
  79. }
  80. /**
  81. * Can we handle simple primitive numeric list mapping?
  82. */
  83. @Test
  84. public void simplePrimitiveNumericList() {
  85. List<Integer> numbers = createJsonMapper().toJavaList(jsonFromClasspath("numbers"), Integer.class);
  86. assertTrue(numbers.size() == 3);
  87. assertTrue(numbers.get(0).equals(1234));
  88. assertTrue(numbers.get(1).equals(5678));
  89. assertTrue(numbers.get(2).equals(9012));
  90. }
  91. /**
  92. * Can we handle simple object mapping?
  93. */
  94. @Test
  95. public void simpleObject() {
  96. BasicUser basicUser = createJsonMapper().toJavaObject(jsonFromClasspath("basic-user"), BasicUser.class);
  97. assertTrue(basicUser.uid.equals(1234L));
  98. assertTrue("Test Person".equals(basicUser.name));
  99. }
  100. /**
  101. * Can we handle simple list mapping?
  102. */
  103. @Test
  104. public void simpleObjectWithList() {
  105. UserWithPhotos userWithPhotos =
  106. createJsonMapper().toJavaObject(jsonFromClasspath("user-with-photos"), UserWithPhotos.class);
  107. assertTrue(userWithPhotos.photos.size() == 2);
  108. assertTrue(userWithPhotos.photos.get(0).photoId.equals(1L));
  109. assertTrue(userWithPhotos.photos.get(1).photoId.equals(2L));
  110. }
  111. /**
  112. * Do we properly find Facebook-annotated fields defined in a superclass?
  113. */
  114. @Test
  115. public void fieldsFromSuperclass() {
  116. UserWithPhotos userWithPhotos =
  117. createJsonMapper().toJavaObject(jsonFromClasspath("user-with-photos"), UserWithPhotos.class);
  118. assertTrue(userWithPhotos.uid.equals(1234L));
  119. assertTrue("Test Person".equals(userWithPhotos.name));
  120. assertTrue(userWithPhotos.photos.size() == 2);
  121. }
  122. /**
  123. * Can we map to Facebook-annotated fields even when they're marked private?
  124. */
  125. @Test
  126. public void privateFields() {
  127. PrivateUser privateUser = createJsonMapper().toJavaObject(jsonFromClasspath("basic-user"), PrivateUser.class);
  128. assertTrue(privateUser.getUid().equals(1234L));
  129. }
  130. /**
  131. * Can we handle slightly-more-complex mapping, including the workaround for
  132. * Facebook's "we gave you an empty object instead of an empty list" bug?
  133. */
  134. @Test
  135. public void usersWithAffiliations() {
  136. List<UserWithAffiliations> usersWithAffiliations =
  137. createJsonMapper().toJavaList(jsonFromClasspath("users-with-affiliations"), UserWithAffiliations.class);
  138. assertTrue(usersWithAffiliations.size() == 3);
  139. Assert.assertTrue("Heather Merlin".equals(usersWithAffiliations.get(0).name));
  140. Assert.assertTrue(("https://secure-profile.facebook.com/profile6/" + "13580/1406/n284asf55_7662.jpg")
  141. .equals(usersWithAffiliations.get(0).bigPictureUrl));
  142. assertTrue(usersWithAffiliations.get(0).affiliations.size() == 1);
  143. assertTrue("Intuit".equals(usersWithAffiliations.get(0).affiliations.get(0).name));
  144. // Make sure the weird Facebook "empty object means empty list" workaround
  145. // works
  146. assertTrue(usersWithAffiliations.get(2).affiliations.size() == 0);
  147. }
  148. /**
  149. * Can we handle nulls nicely?
  150. */
  151. @Test
  152. public void nulls() {
  153. UserWithAffiliations userWithAffiliations =
  154. createJsonMapper().toJavaObject(jsonFromClasspath("nulls"), UserWithAffiliations.class);
  155. assertTrue(userWithAffiliations != null);
  156. assertTrue(userWithAffiliations.uid != null);
  157. assertTrue(userWithAffiliations.name == null);
  158. assertTrue(userWithAffiliations.bigPictureUrl == null);
  159. assertTrue(userWithAffiliations.affiliations == null);
  160. }
  161. /**
  162. * Can we successfully map the results of the auth.createToken call?
  163. */
  164. @Test
  165. public void authCreateToken() {
  166. String token = createJsonMapper().toJavaObject(jsonFromClasspath("api/auth.createToken"), String.class);
  167. assertTrue("3e4a22bb2f5ed75114b0fc9995ea85f1".equals(token));
  168. }
  169. /**
  170. * Can we successfully map the results of the auth.createToken call?
  171. */
  172. @Test
  173. public void usersGetLoggedInUser() {
  174. Long uid = createJsonMapper().toJavaObject(jsonFromClasspath("api/users.getLoggedInUser"), Long.class);
  175. assertTrue(uid.equals(1240077L));
  176. }
  177. /**
  178. * Can we successfully map the results of the friends.get call?
  179. */
  180. @Test
  181. public void friendsGet() {
  182. List<Long> friendUids = createJsonMapper().toJavaList(jsonFromClasspath("api/friends.get"), Long.class);
  183. assertTrue(friendUids.size() == 2);
  184. assertTrue(friendUids.get(0).equals(222333L));
  185. assertTrue(friendUids.get(1).equals(1240079L));
  186. }
  187. /**
  188. * Can we successfully map the case where Facebook sends us an empty array
  189. * instead of an empty string?
  190. */
  191. @Test
  192. public void emptyArray() {
  193. BasicUser user = createJsonMapper().toJavaObject(jsonFromClasspath("empty-array-as-string"), BasicUser.class);
  194. assertTrue("".equals(user.name));
  195. }
  196. /**
  197. * Workaround where Facebook can return the illegal JSON "false" instead of an
  198. * object - just map as null instead of throwing an exception.
  199. */
  200. @Test
  201. public void testFalseInsteadOfObject() {
  202. User user = createJsonMapper().toJavaObject("false", User.class);
  203. assertTrue(user == null);
  204. }
  205. @Test
  206. public void testMultipleFieldsWithSameName() {
  207. JsonMapper jsonMapper = createJsonMapper();
  208. User user1 = jsonMapper.toJavaObject(jsonFromClasspath("user-with-hometown-v1"), User.class);
  209. assertTrue("Beograd".equals(user1.getHometownName()));
  210. assertTrue(user1.getHometown() == null);
  211. User user2 = jsonMapper.toJavaObject(jsonFromClasspath("user-with-hometown-v2"), User.class);
  212. assertTrue("Belgrade, Serbia".equals(user2.getHometown().getName()));
  213. assertTrue("Belgrade, Serbia".equals(user2.getHometownName()));
  214. Post post1 = jsonMapper.toJavaObject(jsonFromClasspath("post-with-likes-v1"), Post.class);
  215. assertTrue(post1.getLikesCount() == 4);
  216. assertTrue(post1.getLikes() == null);
  217. Post post2 = jsonMapper.toJavaObject(jsonFromClasspath("post-with-likes-v2"), Post.class);
  218. assertTrue(post2.getLikes().getCount() == 49);
  219. assertTrue(post2.getLikesCount() == 49);
  220. }
  221. /**
  222. * Makes sure we handle "null" when inside of a list instead of throwing a
  223. * mapping exception.
  224. */
  225. public void testNulls() {
  226. List<NamedFacebookType> types =
  227. createJsonMapper().toJavaList(jsonFromClasspath("nulls-in-list"), NamedFacebookType.class);
  228. assertTrue(types.size() == 3);
  229. }
  230. static class BasicUser {
  231. @Facebook
  232. Long uid;
  233. @Facebook
  234. String name;
  235. }
  236. static class Photo {
  237. @Facebook("id")
  238. Long photoId;
  239. }
  240. static class UserWithPhotos extends BasicUser {
  241. @Facebook
  242. List<Photo> photos;
  243. }
  244. static class Affiliation {
  245. @Facebook
  246. String name;
  247. @Facebook
  248. String type;
  249. }
  250. static class UserWithAffiliations extends BasicUser {
  251. @Facebook("pic_big")
  252. String bigPictureUrl;
  253. @Facebook
  254. List<Affiliation> affiliations;
  255. }
  256. }