PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/twitter4j-core/src/test/java/twitter4j/FriendsFollowersResourcesTest.java

http://github.com/yusuke/twitter4j
Java | 300 lines | 227 code | 36 blank | 37 comment | 7 complexity | b136a10b87f108f5cacdf7bd19275249 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2007 Yusuke Yamamoto
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package twitter4j;
  17. import org.junit.jupiter.api.Test;
  18. import twitter4j.conf.PropertyConfiguration;
  19. import static org.junit.jupiter.api.Assertions.*;
  20. /**
  21. * @author Yusuke Yamamoto - yusuke at mac.com
  22. * @since Twitter4J 2.2.4
  23. */
  24. class FriendsFollowersResourcesTest extends TwitterTestBase {
  25. @Test
  26. void testSocialGraphMethods() throws Exception {
  27. IDs ids;
  28. ids = twitter1.getFriendsIDs(-1);
  29. assertNotNull(TwitterObjectFactory.getRawJSON(ids));
  30. int yusukey = 4933401;
  31. assertIDExsits("twit4j is following yusukey", ids, yusukey);
  32. IDs obamaFollowers;
  33. obamaFollowers = twitter1.getFollowersIDs("barackobama", -1);
  34. assertNotNull(TwitterObjectFactory.getRawJSON(obamaFollowers));
  35. assertEquals(obamaFollowers, TwitterObjectFactory.createIDs(TwitterObjectFactory.getRawJSON(obamaFollowers)));
  36. assertTrue(obamaFollowers.hasNext());
  37. assertFalse(obamaFollowers.hasPrevious());
  38. assertFalse(obamaFollowers.hasPrevious());
  39. obamaFollowers = twitter1.getFollowersIDs("barackobama", obamaFollowers.getNextCursor());
  40. assertEquals(obamaFollowers, TwitterObjectFactory.createIDs(TwitterObjectFactory.getRawJSON(obamaFollowers)));
  41. assertNotNull(TwitterObjectFactory.getRawJSON(obamaFollowers));
  42. assertTrue(obamaFollowers.hasNext());
  43. assertTrue(obamaFollowers.hasPrevious());
  44. obamaFollowers = twitter1.getFollowersIDs("barackobama", -1, 100);
  45. assertNotNull(TwitterObjectFactory.getRawJSON(obamaFollowers));
  46. assertEquals(obamaFollowers, TwitterObjectFactory.createIDs(TwitterObjectFactory.getRawJSON(obamaFollowers)));
  47. assertTrue(obamaFollowers.hasNext());
  48. assertFalse(obamaFollowers.hasPrevious());
  49. obamaFollowers = twitter1.getFollowersIDs(813286, -1, 100);
  50. assertNotNull(TwitterObjectFactory.getRawJSON(obamaFollowers));
  51. assertEquals(obamaFollowers, TwitterObjectFactory.createIDs(TwitterObjectFactory.getRawJSON(obamaFollowers)));
  52. assertTrue(obamaFollowers.hasNext());
  53. assertFalse(obamaFollowers.hasPrevious());
  54. IDs obamaFriends;
  55. obamaFriends = twitter1.getFriendsIDs("barackobama", -1, 100);
  56. assertNull(TwitterObjectFactory.getRawJSON(obamaFollowers));
  57. assertNotNull(TwitterObjectFactory.getRawJSON(obamaFriends));
  58. assertEquals(obamaFriends, TwitterObjectFactory.createIDs(TwitterObjectFactory.getRawJSON(obamaFriends)));
  59. assertTrue(obamaFriends.hasNext());
  60. assertFalse(obamaFriends.hasPrevious());
  61. }
  62. private void assertIDExsits(String assertion, IDs ids, long idToFind) {
  63. boolean found = false;
  64. for (long id : ids.getIDs()) {
  65. if (id == idToFind) {
  66. found = true;
  67. break;
  68. }
  69. }
  70. assertTrue(found, assertion);
  71. }
  72. @Test
  73. void testCreateDestroyFriend() throws Exception {
  74. User user;
  75. try {
  76. user = twitter2.destroyFriendship(id1.screenName);
  77. assertNotNull(TwitterObjectFactory.getRawJSON(user));
  78. assertEquals(user, TwitterObjectFactory.createUser(TwitterObjectFactory.getRawJSON(user)));
  79. } catch (TwitterException te) {
  80. //ensure destroy id1 before the actual test
  81. }
  82. try {
  83. user = twitter2.destroyFriendship(id1.screenName);
  84. assertNotNull(TwitterObjectFactory.getRawJSON(user));
  85. assertEquals(user, TwitterObjectFactory.createUser(TwitterObjectFactory.getRawJSON(user)));
  86. } catch (TwitterException te) {
  87. assertEquals(403, te.getStatusCode());
  88. }
  89. user = twitter2.createFriendship(id1.screenName, true);
  90. assertNotNull(TwitterObjectFactory.getRawJSON(user));
  91. assertEquals(user, TwitterObjectFactory.createUser(TwitterObjectFactory.getRawJSON(user)));
  92. assertEquals(id1.screenName, user.getScreenName());
  93. try {
  94. user = twitter2.destroyFriendship("@" + id1.screenName);
  95. assertNotNull(TwitterObjectFactory.getRawJSON(user));
  96. assertEquals(user, TwitterObjectFactory.createUser(TwitterObjectFactory.getRawJSON(user)));
  97. } catch (TwitterException te) {
  98. //ensure destroy id1 before the actual test
  99. }
  100. try {
  101. user = twitter2.destroyFriendship("@" + id1.screenName);
  102. assertNotNull(TwitterObjectFactory.getRawJSON(user));
  103. assertEquals(user, TwitterObjectFactory.createUser(TwitterObjectFactory.getRawJSON(user)));
  104. } catch (TwitterException te) {
  105. assertEquals(403, te.getStatusCode());
  106. }
  107. user = twitter2.createFriendship("@" + id1.screenName, true);
  108. assertNotNull(TwitterObjectFactory.getRawJSON(user));
  109. assertEquals(user, TwitterObjectFactory.createUser(TwitterObjectFactory.getRawJSON(user)));
  110. assertEquals(id1.screenName, user.getScreenName());
  111. // the Twitter API is not returning appropriate notifications value
  112. // http://code.google.com/p/twitter-api/issues/detail?id=474
  113. // User detail = twitterAPI2.showUser(id1);
  114. // assertTrue(detail.isNotificationEnabled());
  115. try {
  116. user = twitter2.createFriendship(id2.screenName);
  117. fail("shouldn't be able to befrinend yourself");
  118. } catch (TwitterException te) {
  119. assertEquals(403, te.getStatusCode());
  120. }
  121. try {
  122. user = twitter2.createFriendship("doesnotexist--");
  123. fail("non-existing user");
  124. } catch (TwitterException te) {
  125. //now befriending with non-existing user returns 404
  126. //http://groups.google.com/group/twitter-development-talk/browse_thread/thread/bd2a912b181bc39f
  127. // assertEquals(404, te.getStatusCode());
  128. // and now it returns 403 again
  129. assertEquals(403, te.getStatusCode());
  130. }
  131. }
  132. @Test
  133. void testRelationship() throws Exception {
  134. Twitter followsOneWay = new TwitterFactory(new PropertyConfiguration(p, "/followsOneWay")).getInstance();
  135. followsOneWay.createFriendship(id1.screenName);
  136. TestUserInfo followsOneWayInfo = new TestUserInfo("followsOneWay");
  137. // TESTING PRECONDITIONS:
  138. // 1) id1 is followed by "followsOneWay", but not following "followsOneWay"
  139. Relationship rel1 = twitter1.showFriendship(id1.screenName, followsOneWayInfo.screenName);
  140. assertNotNull(TwitterObjectFactory.getRawJSON(rel1));
  141. assertEquals(rel1, TwitterObjectFactory.createRelationship(TwitterObjectFactory.getRawJSON(rel1)));
  142. // test second precondition
  143. assertNotNull(rel1);
  144. assertTrue(rel1.isSourceFollowedByTarget());
  145. assertFalse(rel1.isSourceFollowingTarget());
  146. assertTrue(rel1.isTargetFollowingSource());
  147. assertFalse(rel1.isTargetFollowedBySource());
  148. assertTrue(rel1.canSourceDm());
  149. rel1 = twitter1.showFriendship("@" + id1.screenName, followsOneWayInfo.screenName);
  150. assertNotNull(TwitterObjectFactory.getRawJSON(rel1));
  151. assertEquals(rel1, TwitterObjectFactory.createRelationship(TwitterObjectFactory.getRawJSON(rel1)));
  152. // test second precondition
  153. assertNotNull(rel1);
  154. assertTrue(rel1.isSourceFollowedByTarget());
  155. assertFalse(rel1.isSourceFollowingTarget());
  156. assertTrue(rel1.isTargetFollowingSource());
  157. assertFalse(rel1.isTargetFollowedBySource());
  158. assertTrue(rel1.canSourceDm());
  159. // reverse precondition
  160. Relationship rel1r = twitter1.showFriendship(followsOneWayInfo.screenName, id1.screenName);
  161. assertNotNull(rel1r);
  162. assertFalse(rel1r.isSourceFollowedByTarget());
  163. assertTrue(rel1r.isSourceFollowingTarget());
  164. assertFalse(rel1r.isTargetFollowingSource());
  165. assertTrue(rel1r.isTargetFollowedBySource());
  166. assertFalse(rel1r.canSourceDm());
  167. rel1r = twitter1.showFriendship(followsOneWayInfo.screenName, "@" + id1.screenName);
  168. assertNotNull(rel1r);
  169. assertFalse(rel1r.isSourceFollowedByTarget());
  170. assertTrue(rel1r.isSourceFollowingTarget());
  171. assertFalse(rel1r.isTargetFollowingSource());
  172. assertTrue(rel1r.isTargetFollowedBySource());
  173. assertFalse(rel1r.canSourceDm());
  174. ResponseList<Friendship> friendshipList = twitter1.lookupFriendships("barakobama", followsOneWayInfo.screenName);
  175. assertEquals(2, friendshipList.size());
  176. assertEquals("barakobama", friendshipList.get(0).getScreenName());
  177. assertFalse(friendshipList.get(0).isFollowing());
  178. assertFalse(friendshipList.get(0).isFollowedBy());
  179. assertEquals(followsOneWay.getScreenName(), friendshipList.get(1).getScreenName());
  180. String twitterapi = "TwitterAPI";
  181. long twitterapiID = 6253282L;
  182. try {
  183. if (!twitter1.showFriendship(id1.id, twitterapiID).isSourceFollowingTarget()) {
  184. twitter1.createFriendship(twitterapi);
  185. }
  186. } catch (TwitterException ignore) {
  187. }
  188. String twitterdev = "TwitterDev";
  189. long twitterDevID = 2244994945L;
  190. try {
  191. if (!twitter1.showFriendship(id1.id, twitterDevID).isSourceFollowingTarget()) {
  192. // following an account enables retweet notification forcibly. follow him only when not following
  193. twitter1.createFriendship(twitterdev);
  194. }
  195. } catch (TwitterException ignore) {
  196. }
  197. Relationship updatedRelationship = twitter1.updateFriendship(twitterapi, true, true);
  198. assertEquals(twitterapi, updatedRelationship.getTargetUserScreenName());
  199. assertTrue(updatedRelationship.isSourceNotificationsEnabled());
  200. assertTrue(updatedRelationship.isSourceWantRetweets());
  201. }
  202. @Test
  203. void testLookupFriendships() throws Exception {
  204. // https://dev.twitter.com/discussions/13658
  205. //http://jira.twitter4j.org/browse/TFJ-746
  206. readonly.lookupUsers(255794088L, 441705930L, 100581062L, 49255734L, 996328051L, 559170296L, 119261332L, 373027695L, 18515199L, 83777877L, 28973601L, 228325562L, 3474001L, 297909405L, 79159050L, 94355673L, 882514368L, 97212832L, 6459802L, 124127525L, 9121432L, 57142748L, 15797140L, 985973888L, 216250008L, 121663344L, 14832427L, 54904048L, 47539662L, 22330739L, 41032024L, 24160034L, 70614432L, 489294299L, 165725102L, 203209274L, 93831273L, 49661959L, 15670331L, 30597967L, 194643708L, 70630881L, 105973663L, 114333005L, 76407522L, 5898822L, 43796154L, 5929112L, 14118563L, 64378588L, 60287587L, 28745562L, 77718059L, 106944925L, 15744975L, 81170234L, 98512820L, 20766533L, 91238103L, 15965719L, 351559615L, 5371072L, 582489524L, 367671660L, 4037021L, 52773639L, 440662114L, 14288985L, 3868621L, 13585812L, 3811061L, 14724896L, 5491152L, 4862261L, 106114624L, 459133564L, 6609032L, 4175171L, 565734536L, 174637883L, 834785976L, 338234111L, 278590903L, 205621126L, 7362472L, 329830408L, 116640150L, 52106559L, 831322291L, 53856691L, 313143098L, 563798434L, 219589985L, 5420012L, 15142903L, 107381955L, 117652722L, 188738873L, 167628541L);
  207. }
  208. @Test
  209. void testIncomingOutgoingFriendships() throws Exception {
  210. IDs incomingFriendships = twitter1.getIncomingFriendships(-1L);
  211. assertNotNull(TwitterObjectFactory.getRawJSON(incomingFriendships));
  212. assertEquals(incomingFriendships, TwitterObjectFactory.createIDs(TwitterObjectFactory.getRawJSON(incomingFriendships)));
  213. IDs outgoingFriendships = twitter1.getOutgoingFriendships(-1L);
  214. assertNotNull(TwitterObjectFactory.getRawJSON(outgoingFriendships));
  215. assertEquals(outgoingFriendships, TwitterObjectFactory.createIDs(TwitterObjectFactory.getRawJSON(outgoingFriendships)));
  216. }
  217. @Test
  218. void testFriendsFolowers() throws TwitterException {
  219. twitter1.getFollowersIDs(id1.id, -1L).getIDs();
  220. twitter1.getFriendsIDs(id1.id, -1L).getIDs();
  221. }
  222. @Test
  223. void testFriendsFollowersList() throws Exception {
  224. PagableResponseList<User> t4jfriends = twitter1.getFriendsList("t4j_news", -1L);
  225. PagableResponseList<User> t4jfriends2 = twitter1.getFriendsList(72297675L, -1L);
  226. assertEquals(t4jfriends, t4jfriends2);
  227. assertTrue(t4jfriends.size() > 0);
  228. PagableResponseList<User> t4jfriends1 = twitter1.getFriendsList("@t4j_news", -1L, 3);
  229. PagableResponseList<User> t4jfriends12 = twitter1.getFriendsList(72297675L, -1L, 3);
  230. assertEquals(t4jfriends1, t4jfriends12);
  231. assertTrue(t4jfriends1.size() > 0);
  232. assertTrue(t4jfriends1.size() <= 3);
  233. PagableResponseList<User> t4jfollowers = twitter1.getFollowersList("t4j_news", -1L);
  234. PagableResponseList<User> t4jfollowers2 = twitter1.getFollowersList(72297675L, -1L);
  235. assertEquals(t4jfollowers, t4jfollowers2);
  236. assertTrue(t4jfollowers.size() > 0);
  237. PagableResponseList<User> t4jfollowers1 = twitter1.getFollowersList("@t4j_news", -1L, 3);
  238. PagableResponseList<User> t4jfollowers12 = twitter1.getFollowersList(72297675L, -1L, 3);
  239. assertEquals(t4jfollowers1, t4jfollowers12);
  240. assertTrue(t4jfollowers1.size() > 0);
  241. assertTrue(t4jfollowers1.size() <= 3);
  242. }
  243. @Test
  244. void testFriendsFollowersListWithSkipStatusIncludeUserEntities() throws Exception {
  245. PagableResponseList<User> t4jfriends = twitter1.getFriendsList("t4j_news", -1L, 3, true, false);
  246. PagableResponseList<User> t4jfriends2 = twitter1.getFriendsList(72297675L, -1L, 3, true, false);
  247. assertEquals(t4jfriends, t4jfriends2);
  248. assertTrue(t4jfriends.size() > 0);
  249. assertTrue(t4jfriends.size() <= 3);
  250. for (User user : t4jfriends) {
  251. assertNull(user.getStatus());
  252. assertEquals(0, user.getDescriptionURLEntities().length);
  253. }
  254. PagableResponseList<User> t4jfollowers = twitter1.getFollowersList("t4j_news", -1L, 3, true, false);
  255. PagableResponseList<User> t4jfollowers2 = twitter1.getFollowersList(72297675L, -1L, 3, true, false);
  256. assertEquals(t4jfollowers, t4jfollowers2);
  257. assertTrue(t4jfollowers.size() > 0);
  258. assertTrue(t4jfollowers.size() <= 3);
  259. for (User user : t4jfollowers) {
  260. assertNull(user.getStatus());
  261. assertEquals(0, user.getDescriptionURLEntities().length);
  262. }
  263. }
  264. }