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

Commit

Permalink
Add delete favorites in batch mode feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vergnesOL committed Mar 16, 2012
1 parent e73c7c0 commit 1926d35
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.social.weibo.api;

import java.util.List;

import org.springframework.social.weibo.api.Favorite.Tag;

public interface FavoriteOperations {
Expand All @@ -23,6 +25,8 @@ public interface FavoriteOperations {

Favorite deleteFavorite(long statusId);

boolean deleteFavorites(List<Long> statusIds);

Favorite getFavorite(long id);

CursoredList<Favorite> getFavorites();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
*/
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;
import org.springframework.social.weibo.api.Favorite;
import org.springframework.social.weibo.api.Favorite.Tag;
import org.springframework.social.weibo.api.FavoriteOperations;
import org.springframework.social.weibo.util.StringUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
Expand All @@ -33,6 +36,38 @@ protected FavoriteTemplate(ObjectMapper objectMapper,
super(objectMapper, restTemplate, isAuthorized);
}

@Override
public Favorite createFavorite(long statusId) {
requireAuthorization();
MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>(
1);
request.add("id", String.valueOf(statusId));
return restTemplate.postForObject(buildUri("favorites/create.json"),
request, Favorite.class);
}

@Override
public Favorite deleteFavorite(long statusId) {
requireAuthorization();
MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>(
1);
request.add("id", String.valueOf(statusId));
return restTemplate.postForObject(buildUri("favorites/destroy.json"),
request, Favorite.class);
}

@Override
public boolean deleteFavorites(List<Long> statusId) {
requireAuthorization();
MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>(
1);
request.add("ids", StringUtils.join(statusId));
JsonNode jsonNode = restTemplate.postForObject(
buildUri("favorites/destroy_batch.json"), request,
JsonNode.class);
return jsonNode.get("result").asBoolean();
}

@Override
public Favorite getFavorite(long id) {
requireAuthorization();
Expand Down Expand Up @@ -103,24 +138,4 @@ public CursoredList<Tag> getTags(int pageSize, int pageNumber) {
return deserializeCursoredList(jsonNode, Tag.class, "tags");
}

@Override
public Favorite createFavorite(long statusId) {
requireAuthorization();
MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>(
1);
request.add("id", String.valueOf(statusId));
return restTemplate.postForObject(buildUri("favorites/create.json"),
request, Favorite.class);
}

@Override
public Favorite deleteFavorite(long statusId) {
requireAuthorization();
MultiValueMap<String, String> request = new LinkedMultiValueMap<String, String>(
1);
request.add("id", String.valueOf(statusId));
return restTemplate.postForObject(buildUri("favorites/destroy.json"),
request, Favorite.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.social.weibo.api.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.social.test.client.RequestMatchers.body;
Expand All @@ -24,6 +25,7 @@
import static org.springframework.social.test.client.RequestMatchers.requestTo;
import static org.springframework.social.test.client.ResponseCreators.withResponse;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
Expand All @@ -42,6 +44,40 @@ public void setUp() {
getRestTemplate(), true);
}

@Test
public void testCreateFavorite() {
mockServer
.expect(requestTo("https://api.weibo.com/2/favorites/create.json"))
.andExpect(method(POST))
.andExpect(body("id=1"))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(
withResponse(jsonResource("favorite"), responseHeaders));
verifyFavorite(favoriteTemplate.createFavorite(1));
}

@Test
public void testDeleteFavorite() {
mockServer
.expect(requestTo("https://api.weibo.com/2/favorites/destroy.json"))
.andExpect(method(POST))
.andExpect(body("id=1"))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(
withResponse(jsonResource("favorite"), responseHeaders));
verifyFavorite(favoriteTemplate.deleteFavorite(1));
}

@Test
public void testDeleteFavorites() {
mockServer
.expect(requestTo("https://api.weibo.com/2/favorites/destroy_batch.json"))
.andExpect(method(POST)).andExpect(body("ids=1%2C2%2C3"))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(withResponse("{\"result\":true}", responseHeaders));
assertTrue(favoriteTemplate.deleteFavorites(Arrays.asList(1L, 2L, 3L)));
}

@Test
public void testGetFavorite() {
mockServer
Expand Down Expand Up @@ -69,6 +105,40 @@ public void testGetFavorites() {
verifyFavorite(firstFavorite);
}

@Test
public void testGetFavoritesByTag() {
mockServer
.expect(requestTo("https://api.weibo.com/2/favorites/by_tags.json?tid=1"))
.andExpect(method(GET))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(
withResponse(jsonResource("cursoredFavorites"),
responseHeaders));
CursoredList<Favorite> cursoredList = favoriteTemplate
.getFavoritesByTag(1);
assertEquals(16, cursoredList.getTotalNumber());
assertEquals(2, cursoredList.size());
Favorite firstFavorite = cursoredList.iterator().next();
verifyFavorite(firstFavorite);
}

@Test
public void testGetFavoritesByTagPagination() {
mockServer
.expect(requestTo("https://api.weibo.com/2/favorites/by_tags.json?tid=1&count=20&page=5"))
.andExpect(method(GET))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(
withResponse(jsonResource("cursoredFavorites"),
responseHeaders));
CursoredList<Favorite> cursoredList = favoriteTemplate
.getFavoritesByTag(1, 20, 5);
assertEquals(16, cursoredList.getTotalNumber());
assertEquals(2, cursoredList.size());
Favorite firstFavorite = cursoredList.iterator().next();
verifyFavorite(firstFavorite);
}

@Test
public void testGetFavoritesPagination() {
mockServer
Expand Down Expand Up @@ -130,64 +200,4 @@ private void verifyTag(Tag firstTag) {
assertEquals("80后", firstTag.getValue());
assertEquals(25369, firstTag.getCount());
}

@Test
public void testGetFavoritesByTag() {
mockServer
.expect(requestTo("https://api.weibo.com/2/favorites/by_tags.json?tid=1"))
.andExpect(method(GET))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(
withResponse(jsonResource("cursoredFavorites"),
responseHeaders));
CursoredList<Favorite> cursoredList = favoriteTemplate
.getFavoritesByTag(1);
assertEquals(16, cursoredList.getTotalNumber());
assertEquals(2, cursoredList.size());
Favorite firstFavorite = cursoredList.iterator().next();
verifyFavorite(firstFavorite);
}

@Test
public void testGetFavoritesByTagPagination() {
mockServer
.expect(requestTo("https://api.weibo.com/2/favorites/by_tags.json?tid=1&count=20&page=5"))
.andExpect(method(GET))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(
withResponse(jsonResource("cursoredFavorites"),
responseHeaders));
CursoredList<Favorite> cursoredList = favoriteTemplate
.getFavoritesByTag(1, 20, 5);
assertEquals(16, cursoredList.getTotalNumber());
assertEquals(2, cursoredList.size());
Favorite firstFavorite = cursoredList.iterator().next();
verifyFavorite(firstFavorite);
}

@Test
public void testCreateFavorite() {
mockServer
.expect(requestTo("https://api.weibo.com/2/favorites/create.json"))
.andExpect(method(POST))
.andExpect(body("id=1"))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(
withResponse(jsonResource("favorite"), responseHeaders));
verifyFavorite(favoriteTemplate.createFavorite(1));
}

@Test
public void testDeleteFavorite() {
mockServer
.expect(requestTo("https://api.weibo.com/2/favorites/destroy.json"))
.andExpect(method(POST))
.andExpect(body("id=1"))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(
withResponse(jsonResource("favorite"), responseHeaders));
verifyFavorite(favoriteTemplate.deleteFavorite(1));

}

}

0 comments on commit 1926d35

Please sign in to comment.