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

Commit

Permalink
Add getCommentsByMe feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vergnesOL committed Feb 17, 2012
1 parent e6e2bd4 commit 6d26dec
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ CursoredList<Comment> getComments(long id, int pageSize, int pageNumber,
CursoredList<Comment> getComments(long id, long sinceId, long maxId,
int pageSize, int pageNumber, AuthorFilterType authorFilterType);

CursoredList<Comment> getCommentsByMe();

CursoredList<Comment> getCommentsByMe(int pageSize, int pageNumber);

CursoredList<Comment> getCommentsByMe(int pageSize, int pageNumber,
SourceFilterType sourceFilterType);

CursoredList<Comment> getCommentsByMe(long sinceId, long maxId, int pageSize,
int pageNumber, SourceFilterType sourceFilterType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.social.weibo.api.Comment;
import org.springframework.social.weibo.api.CommentOperations;
import org.springframework.social.weibo.api.CursoredList;
import org.springframework.social.weibo.api.SourceFilterType;
import org.springframework.web.client.RestTemplate;

class CommentTemplate extends AbstractWeiboOperations implements
Expand Down Expand Up @@ -54,16 +55,9 @@ public CursoredList<Comment> getComments(long id, int pageSize,
@Override
public CursoredList<Comment> getComments(long id, long sinceId, long maxId,
int pageSize, int pageNumber, AuthorFilterType authorFilterType) {
return fetchCommentList("comments/show.json", id, sinceId, maxId,
pageSize, pageNumber, authorFilterType);
}

private CursoredList<Comment> fetchCommentList(String url, long id,
long sinceId, long maxId, int pageSize, int pageNumber,
AuthorFilterType authorFilterType) {
requireAuthorization();
JsonNode dataNode = restTemplate.getForObject(
uriBuilder(url)
uriBuilder("comments/show.json")
.queryParam("id", String.valueOf(id))
.queryParam("since_id", String.valueOf(sinceId))
.queryParam("max_id", String.valueOf(maxId))
Expand All @@ -74,4 +68,39 @@ private CursoredList<Comment> fetchCommentList(String url, long id,
.build(), JsonNode.class);
return deserializeCursoredList(dataNode, Comment.class, "comments");
}

@Override
public CursoredList<Comment> getCommentsByMe() {
requireAuthorization();
JsonNode dataNode = restTemplate.getForObject(
buildUri("comments/by_me.json"), JsonNode.class);
return deserializeCursoredList(dataNode, Comment.class, "comments");
}

@Override
public CursoredList<Comment> getCommentsByMe(int pageSize, int pageNumber) {
return getCommentsByMe(pageSize, pageNumber, SourceFilterType.ALL);
}

@Override
public CursoredList<Comment> getCommentsByMe(int pageSize, int pageNumber,
SourceFilterType sourceFilterType) {
return getCommentsByMe(0, 0, pageSize, pageNumber, sourceFilterType);
}

@Override
public CursoredList<Comment> getCommentsByMe(long sinceId, long maxId,
int pageSize, int pageNumber, SourceFilterType sourceFilterType) {
requireAuthorization();
JsonNode dataNode = restTemplate.getForObject(
uriBuilder("comments/by_me.json")
.queryParam("since_id", String.valueOf(sinceId))
.queryParam("max_id", String.valueOf(maxId))
.queryParam("count", String.valueOf(pageSize))
.queryParam("page", String.valueOf(pageNumber))
.queryParam("filter_by_source",
String.valueOf(sourceFilterType.ordinal()))
.build(), JsonNode.class);
return deserializeCursoredList(dataNode, Comment.class, "comments");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public class CommentTemplateTest extends AbstractWeiboOperationsTest {

private CommentTemplate commentTemplate;

@Override
public void setUp() {
commentTemplate = new CommentTemplate(getObjectMapper(),
getRestTemplate(), true);
}

@Test
public void testGetComments() {
mockServer
Expand All @@ -45,12 +51,34 @@ public void testGetComments() {
assertEquals(10, comments.getNextCursor());
}

private void verifyComment(Comment comment) {
assertEquals(12438492184L, comment.getId());
assertEquals(1306860625000L, comment.getCreatedAt().getTime());
assertEquals("我喜欢你做的", comment.getText());
assertNotNull(comment.getUser());
assertNotNull(comment.getStatus());
@Test
public void testGetCommentsByMe() {
mockServer
.expect(requestTo("https://api.weibo.com/2/comments/by_me.json"))
.andExpect(method(GET))
.andRespond(
withResponse(jsonResource("comments"), responseHeaders));
CursoredList<Comment> comments = commentTemplate.getCommentsByMe();
verifyComment(comments.iterator().next());
assertEquals(2, comments.size());
assertEquals(7, comments.getTotalNumber());
assertEquals(0, comments.getPreviousCursor());
assertEquals(10, comments.getNextCursor());
}

@Test
public void testGetCommentsByMePagination() {
mockServer
.expect(requestTo("https://api.weibo.com/2/comments/by_me.json?since_id=0&max_id=0&count=50&page=5&filter_by_source=0"))
.andExpect(method(GET))
.andRespond(
withResponse(jsonResource("comments"), responseHeaders));
CursoredList<Comment> comments = commentTemplate.getCommentsByMe(50, 5);
verifyComment(comments.iterator().next());
assertEquals(2, comments.size());
assertEquals(7, comments.getTotalNumber());
assertEquals(0, comments.getPreviousCursor());
assertEquals(10, comments.getNextCursor());
}

@Test
Expand All @@ -69,10 +97,12 @@ public void testGetCommentsPagination() {
assertEquals(10, comments.getNextCursor());
}

@Override
public void setUp() {
commentTemplate = new CommentTemplate(getObjectMapper(),
getRestTemplate(), true);
private void verifyComment(Comment comment) {
assertEquals(12438492184L, comment.getId());
assertEquals(1306860625000L, comment.getCreatedAt().getTime());
assertEquals("我喜欢你做的", comment.getText());
assertNotNull(comment.getUser());
assertNotNull(comment.getStatus());
}

}

0 comments on commit 6d26dec

Please sign in to comment.