Skip to content
This repository has been archived by the owner on Oct 20, 2022. It is now read-only.

Commit

Permalink
Add get active followers feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vergnesOL committed Feb 15, 2012
1 parent 7fb5bd8 commit 606c647
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@
*/
package org.springframework.social.weibo.api;

import java.util.List;

public interface FriendOperations {

List<WeiboProfile> getActiveFollowers(long uid);

List<WeiboProfile> getActiveFollowers(long uid, int pageSize);

CursoredList<WeiboProfile> getBilateralFriends(long uid);

CursoredList<WeiboProfile> getBilateralFriends(long uid, int pageSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.social.weibo.api.impl;

import java.util.List;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.social.weibo.api.CursoredList;
Expand All @@ -41,6 +43,26 @@ private CursoredList<WeiboProfile> fetchUsersList(String url, long uid,
return deserializeCursoredList(dataNode, WeiboProfile.class, "users");
}

@Override
public List<WeiboProfile> getActiveFollowers(long uid) {
requireAuthorization();
JsonNode jsonNode = restTemplate.getForObject(
buildUri("friendships/followers/active.json", "uid", uid),
JsonNode.class);
return deserializeDataList(jsonNode, WeiboProfile.class);
}

@Override
public List<WeiboProfile> getActiveFollowers(long uid, int pageSize) {
requireAuthorization();
JsonNode jsonNode = restTemplate.getForObject(
uriBuilder("friendships/followers/active.json")
.queryParam("uid", String.valueOf(uid))
.queryParam("count", String.valueOf(pageSize)).build(),
JsonNode.class);
return deserializeDataList(jsonNode, WeiboProfile.class);
}

@Override
public CursoredList<WeiboProfile> getBilateralFriends(long uid) {
requireAuthorization();
Expand All @@ -58,61 +80,61 @@ public CursoredList<WeiboProfile> getBilateralFriends(long uid,
}

@Override
public CursoredList<WeiboProfile> getFollowers(long uid) {
public CursoredList<WeiboProfile> getCommonFriends(long user1Uid,
long user2Uid) {
requireAuthorization();
JsonNode dataNode = restTemplate.getForObject(
buildUri("friendships/followers.json", "uid", uid),
uriBuilder("friendships/friends/in_common.json")
.queryParam("uid", String.valueOf(user1Uid))
.queryParam("suid", String.valueOf(user2Uid)).build(),
JsonNode.class);
return deserializeCursoredList(dataNode, WeiboProfile.class, "users");
}

@Override
public CursoredList<WeiboProfile> getFollowers(long uid, int pageSize,
int pageNumber) {
return fetchUsersList("friendships/followers.json", uid, pageSize,
pageNumber);
public CursoredList<WeiboProfile> getCommonFriends(long user1Uid,
long user2Uid, int pageSize, int pageNumber) {
requireAuthorization();
JsonNode dataNode = restTemplate
.getForObject(
uriBuilder("friendships/friends/in_common.json")
.queryParam("uid", String.valueOf(user1Uid))
.queryParam("suid", String.valueOf(user2Uid))
.queryParam("count", String.valueOf(pageSize))
.queryParam("page", String.valueOf(pageNumber))
.build(), JsonNode.class);
return deserializeCursoredList(dataNode, WeiboProfile.class, "users");
}

@Override
public CursoredList<WeiboProfile> getFriends(long uid) {
public CursoredList<WeiboProfile> getFollowers(long uid) {
requireAuthorization();
JsonNode dataNode = restTemplate.getForObject(
buildUri("friendships/friends.json", "uid", uid),
buildUri("friendships/followers.json", "uid", uid),
JsonNode.class);
return deserializeCursoredList(dataNode, WeiboProfile.class, "users");
}

@Override
public CursoredList<WeiboProfile> getFriends(long uid, int pageSize,
public CursoredList<WeiboProfile> getFollowers(long uid, int pageSize,
int pageNumber) {
return fetchUsersList("friendships/friends.json", uid, pageSize,
return fetchUsersList("friendships/followers.json", uid, pageSize,
pageNumber);
}

@Override
public CursoredList<WeiboProfile> getCommonFriends(long user1Uid,
long user2Uid) {
public CursoredList<WeiboProfile> getFriends(long uid) {
requireAuthorization();
JsonNode dataNode = restTemplate.getForObject(
uriBuilder("friendships/friends/in_common.json")
.queryParam("uid", String.valueOf(user1Uid))
.queryParam("suid", String.valueOf(user2Uid)).build(),
buildUri("friendships/friends.json", "uid", uid),
JsonNode.class);
return deserializeCursoredList(dataNode, WeiboProfile.class, "users");
}

@Override
public CursoredList<WeiboProfile> getCommonFriends(long user1Uid,
long user2Uid, int pageSize, int pageNumber) {
requireAuthorization();
JsonNode dataNode = restTemplate
.getForObject(
uriBuilder("friendships/friends/in_common.json")
.queryParam("uid", String.valueOf(user1Uid))
.queryParam("suid", String.valueOf(user2Uid))
.queryParam("count", String.valueOf(pageSize))
.queryParam("page", String.valueOf(pageNumber))
.build(), JsonNode.class);
return deserializeCursoredList(dataNode, WeiboProfile.class, "users");
public CursoredList<WeiboProfile> getFriends(long uid, int pageSize,
int pageNumber) {
return fetchUsersList("friendships/friends.json", uid, pageSize,
pageNumber);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.springframework.social.test.client.RequestMatchers.requestTo;
import static org.springframework.social.test.client.ResponseCreators.withResponse;

import java.util.List;

import org.junit.Test;
import org.springframework.social.weibo.api.CursoredList;
import org.springframework.social.weibo.api.WeiboProfile;
Expand All @@ -37,7 +39,8 @@ public void testGetBilateralFriends() {
+ uid))
.andExpect(method(GET))
.andRespond(
withResponse(jsonResource("users"), responseHeaders));
withResponse(jsonResource("cursoredUsers"),
responseHeaders));
CursoredList<WeiboProfile> users = friendTemplate
.getBilateralFriends(uid);
assertEquals(2, users.size());
Expand All @@ -53,7 +56,8 @@ public void testGetBilateralFriendsPagination() {
.expect(requestTo("https://api.weibo.com/2/friendships/friends/bilateral.json?uid=123&count=20&cursor=5"))
.andExpect(method(GET))
.andRespond(
withResponse(jsonResource("users"), responseHeaders));
withResponse(jsonResource("cursoredUsers"),
responseHeaders));
CursoredList<WeiboProfile> users = friendTemplate.getBilateralFriends(
uid, 20, 5);
assertEquals(2, users.size());
Expand All @@ -70,7 +74,8 @@ public void testGetFriends() {
+ uid))
.andExpect(method(GET))
.andRespond(
withResponse(jsonResource("users"), responseHeaders));
withResponse(jsonResource("cursoredUsers"),
responseHeaders));
CursoredList<WeiboProfile> friends = friendTemplate.getFriends(uid);
assertEquals(2, friends.size());
assertEquals(650, friends.getTotalNumber());
Expand All @@ -85,7 +90,8 @@ public void testGetFriendsPagination() {
.expect(requestTo("https://api.weibo.com/2/friendships/friends.json?uid=123&count=20&cursor=5"))
.andExpect(method(GET))
.andRespond(
withResponse(jsonResource("users"), responseHeaders));
withResponse(jsonResource("cursoredUsers"),
responseHeaders));
CursoredList<WeiboProfile> friends = friendTemplate.getFriends(uid, 20,
5);
assertEquals(2, friends.size());
Expand All @@ -100,6 +106,31 @@ public void setUp() {
getRestTemplate(), true);
}

@Test
public void testGetActiveFollowers() {
long uid = 123L;
mockServer
.expect(requestTo("https://api.weibo.com/2/friendships/followers/active.json?uid="
+ uid))
.andExpect(method(GET))
.andRespond(
withResponse(jsonResource("users"), responseHeaders));
List<WeiboProfile> users = friendTemplate.getActiveFollowers(uid);
assertEquals(2, users.size());
}

@Test
public void testGetActiveFollowersPagination() {
long uid = 123L;
mockServer
.expect(requestTo("https://api.weibo.com/2/friendships/followers/active.json?uid=123&count=20"))
.andExpect(method(GET))
.andRespond(
withResponse(jsonResource("users"), responseHeaders));
List<WeiboProfile> users = friendTemplate.getActiveFollowers(uid, 20);
assertEquals(2, users.size());
}

@Test
public void testGetFollowers() {
long uid = 123L;
Expand All @@ -108,7 +139,8 @@ public void testGetFollowers() {
+ uid))
.andExpect(method(GET))
.andRespond(
withResponse(jsonResource("users"), responseHeaders));
withResponse(jsonResource("cursoredUsers"),
responseHeaders));
CursoredList<WeiboProfile> users = friendTemplate.getFollowers(uid);
assertEquals(2, users.size());
assertEquals(650, users.getTotalNumber());
Expand All @@ -123,7 +155,8 @@ public void testGetFollowersPagination() {
.expect(requestTo("https://api.weibo.com/2/friendships/followers.json?uid=123&count=20&cursor=5"))
.andExpect(method(GET))
.andRespond(
withResponse(jsonResource("users"), responseHeaders));
withResponse(jsonResource("cursoredUsers"),
responseHeaders));
CursoredList<WeiboProfile> users = friendTemplate.getFollowers(uid, 20,
5);
assertEquals(2, users.size());
Expand All @@ -138,7 +171,8 @@ public void testGetCommonFriends() {
.expect(requestTo("https://api.weibo.com/2/friendships/friends/in_common.json?uid=123&suid=456"))
.andExpect(method(GET))
.andRespond(
withResponse(jsonResource("users"), responseHeaders));
withResponse(jsonResource("cursoredUsers"),
responseHeaders));
CursoredList<WeiboProfile> users = friendTemplate.getCommonFriends(
123L, 456L);
assertEquals(2, users.size());
Expand All @@ -153,7 +187,8 @@ public void testGetCommonFriendsPagination() {
.expect(requestTo("https://api.weibo.com/2/friendships/friends/in_common.json?uid=123&suid=456&count=20&page=5"))
.andExpect(method(GET))
.andRespond(
withResponse(jsonResource("users"), responseHeaders));
withResponse(jsonResource("cursoredUsers"),
responseHeaders));
CursoredList<WeiboProfile> users = friendTemplate.getCommonFriends(
123L, 456L, 20, 5);
assertEquals(2, users.size());
Expand Down
102 changes: 102 additions & 0 deletions src/test/resources/json/cursoredUsers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{
"users" : [{
"id" : 1,
"idstr" : "1",
"screen_name" : "Cirrus_Test1",
"name" : "Cirrus_Test1",
"province" : "11",
"city" : "1000",
"location" : "北京三元桥",
"description" : "description",
"url" : "http:https://myFavouriteUrl.com",
"profile_image_url" : "http:https://tp4.sinaimg.cn/2429576463/50/0/1",
"domain" : "domain",
"gender" : "m",
"followers_count" : 1,
"friends_count" : 1,
"statuses_count" : 1,
"favourites_count" : 0,
"created_at" : "Thu Oct 27 00:00:00 +0800 2011",
"following" : false,
"allow_all_act_msg" : false,
"geo_enabled" : true,
"verified" : false,
"verified_type" : -1,
"status" : {
"created_at" : "Thu Oct 27 10:18:29 +0800 2011",
"id" : 3373052761021575,
"mid" : "3373052761021575",
"idstr" : "3373052761021575",
"text" : "Youpie",
"source" : "<a href=\"http:https://weibo.com\" rel=\"\">????????????</a>",
"favorited" : true,
"truncated" : true,
"in_reply_to_status_id" : "",
"in_reply_to_user_id" : "",
"in_reply_to_screen_name" : "",
"geo" : null,
"reposts_count" : 0,
"comments_count" : 0,
"mlevel" : 0
},
"allow_all_comment" : true,
"avatar_large" : "http:https://tp4.sinaimg.cn/2429576463/180/0/1",
"verified_reason" : "verified reason",
"follow_me" : false,
"online_status" : 0,
"bi_followers_count" : 1,
"lang" : "zh-cn"
},
{
"id" : 2,
"idstr" : "2",
"screen_name" : "Cirrus_Test2",
"name" : "Cirrus_Test2",
"province" : "11",
"city" : "1000",
"location" : "北京三元桥",
"description" : "description",
"url" : "http:https://myFavouriteUrl.com",
"profile_image_url" : "http:https://tp4.sinaimg.cn/2429576463/50/0/2",
"domain" : "domain",
"gender" : "m",
"followers_count" : 1,
"friends_count" : 1,
"statuses_count" : 1,
"favourites_count" : 0,
"created_at" : "Thu Oct 27 00:00:00 +0800 2011",
"following" : false,
"allow_all_act_msg" : false,
"geo_enabled" : true,
"verified" : false,
"verified_type" : -1,
"status" : {
"created_at" : "Thu Nov 10 10:18:29 +0800 2011",
"id" : 3373052761021575,
"mid" : "3373052761021575",
"idstr" : "3373052761021575",
"text" : "Youpie",
"source" : "<a href=\"http:https://weibo.com\" rel=\"\">????????????</a>",
"favorited" : true,
"truncated" : true,
"in_reply_to_status_id" : "",
"in_reply_to_user_id" : "",
"in_reply_to_screen_name" : "",
"geo" : null,
"reposts_count" : 0,
"comments_count" : 0,
"mlevel" : 0
},
"allow_all_comment" : true,
"avatar_large" : "http:https://tp4.sinaimg.cn/2429576463/180/0/2",
"verified_reason" : "verified reason",
"follow_me" : false,
"online_status" : 0,
"bi_followers_count" : 1,
"lang" : "zh-cn"
}
],
"next_cursor" : 1,
"previous_cursor" : 0,
"total_number" : 650
}
Loading

0 comments on commit 606c647

Please sign in to comment.