From 6d26deca7391ea263839ef25a716aa0fc9ddf416 Mon Sep 17 00:00:00 2001 From: Denis Vergnes Date: Fri, 17 Feb 2012 15:43:46 +0800 Subject: [PATCH] Add getCommentsByMe feature --- .../social/weibo/api/CommentOperations.java | 10 ++++ .../weibo/api/impl/CommentTemplate.java | 45 ++++++++++++++--- .../weibo/api/impl/CommentTemplateTest.java | 50 +++++++++++++++---- 3 files changed, 87 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/springframework/social/weibo/api/CommentOperations.java b/src/main/java/org/springframework/social/weibo/api/CommentOperations.java index 942d5ff..6ba5a7d 100644 --- a/src/main/java/org/springframework/social/weibo/api/CommentOperations.java +++ b/src/main/java/org/springframework/social/weibo/api/CommentOperations.java @@ -27,4 +27,14 @@ CursoredList getComments(long id, int pageSize, int pageNumber, CursoredList getComments(long id, long sinceId, long maxId, int pageSize, int pageNumber, AuthorFilterType authorFilterType); + CursoredList getCommentsByMe(); + + CursoredList getCommentsByMe(int pageSize, int pageNumber); + + CursoredList getCommentsByMe(int pageSize, int pageNumber, + SourceFilterType sourceFilterType); + + CursoredList getCommentsByMe(long sinceId, long maxId, int pageSize, + int pageNumber, SourceFilterType sourceFilterType); + } diff --git a/src/main/java/org/springframework/social/weibo/api/impl/CommentTemplate.java b/src/main/java/org/springframework/social/weibo/api/impl/CommentTemplate.java index f23a8c6..ceb51eb 100644 --- a/src/main/java/org/springframework/social/weibo/api/impl/CommentTemplate.java +++ b/src/main/java/org/springframework/social/weibo/api/impl/CommentTemplate.java @@ -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 @@ -54,16 +55,9 @@ public CursoredList getComments(long id, int pageSize, @Override public CursoredList 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 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)) @@ -74,4 +68,39 @@ private CursoredList fetchCommentList(String url, long id, .build(), JsonNode.class); return deserializeCursoredList(dataNode, Comment.class, "comments"); } + + @Override + public CursoredList getCommentsByMe() { + requireAuthorization(); + JsonNode dataNode = restTemplate.getForObject( + buildUri("comments/by_me.json"), JsonNode.class); + return deserializeCursoredList(dataNode, Comment.class, "comments"); + } + + @Override + public CursoredList getCommentsByMe(int pageSize, int pageNumber) { + return getCommentsByMe(pageSize, pageNumber, SourceFilterType.ALL); + } + + @Override + public CursoredList getCommentsByMe(int pageSize, int pageNumber, + SourceFilterType sourceFilterType) { + return getCommentsByMe(0, 0, pageSize, pageNumber, sourceFilterType); + } + + @Override + public CursoredList 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"); + } } diff --git a/src/test/java/org/springframework/social/weibo/api/impl/CommentTemplateTest.java b/src/test/java/org/springframework/social/weibo/api/impl/CommentTemplateTest.java index 62d6d0b..3f96427 100644 --- a/src/test/java/org/springframework/social/weibo/api/impl/CommentTemplateTest.java +++ b/src/test/java/org/springframework/social/weibo/api/impl/CommentTemplateTest.java @@ -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 @@ -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 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 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 @@ -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()); } }