Skip to content

Commit

Permalink
Add getFavorites feature
Browse files Browse the repository at this point in the history
  • Loading branch information
vergnesOL committed Mar 16, 2012
1 parent 1720886 commit 927c666
Show file tree
Hide file tree
Showing 9 changed files with 602 additions and 87 deletions.
123 changes: 123 additions & 0 deletions src/main/java/org/springframework/social/weibo/api/Favorite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright 2011 France Telecom R&D Beijing Co., Ltd 北京法国电信研发中心有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.weibo.api;

import java.util.Date;
import java.util.List;

public class Favorite {

public static class Tag {
private long id;
private String value;
private int count;

/**
* @return the id
*/
public long getId() {
return id;
}

/**
* @param id
* the id to set
*/
public void setId(long id) {
this.id = id;
}

/**
* @return the value
*/
public String getValue() {
return value;
}

/**
* @param value
* the value to set
*/
public void setValue(String value) {
this.value = value;
}

/**
* @return the count
*/
public int getCount() {
return count;
}

/**
* @param count
* the count to set
*/
public void setCount(int count) {
this.count = count;
}
}

private Status status;
private List<Tag> tags;
private Date favoritedTime;

/**
* @return the status
*/
public Status getStatus() {
return status;
}

/**
* @param status
* the status to set
*/
public void setStatus(Status status) {
this.status = status;
}

/**
* @return the tags
*/
public List<Tag> getTags() {
return tags;
}

/**
* @param tags
* the tags to set
*/
public void setTags(List<Tag> tags) {
this.tags = tags;
}

/**
* @return the favoritedTime
*/
public Date getFavoritedTime() {
return favoritedTime;
}

/**
* @param favoritedTime
* the favoritedTime to set
*/
public void setFavoritedTime(Date favoritedTime) {
this.favoritedTime = favoritedTime;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2011 France Telecom R&D Beijing Co., Ltd 北京法国电信研发中心有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.weibo.api;

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

public interface FavoriteOperations {

Favorite getFavorite(long id);

CursoredList<Favorite> getFavorites();

CursoredList<Favorite> getFavorites(int pageSize, int pageNumber);

CursoredList<Tag> getTags();

CursoredList<Tag> getTags(int pageSize, int pageNumber);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2011 France Telecom R&D Beijing Co., Ltd 北京法国电信研发中心有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.weibo.api.impl;

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.web.client.RestTemplate;

public class FavoriteTemplate extends AbstractWeiboOperations implements
FavoriteOperations {

protected FavoriteTemplate(ObjectMapper objectMapper,
RestTemplate restTemplate, boolean isAuthorized) {
super(objectMapper, restTemplate, isAuthorized);
}

@Override
public CursoredList<Favorite> getFavorites() {
requireAuthorization();
JsonNode jsonNode = restTemplate.getForObject(
buildUri("favorites.json"), JsonNode.class);
return deserializeCursoredList(jsonNode, Favorite.class, "favorites");
}

@Override
public CursoredList<Favorite> getFavorites(int pageSize, int pageNumber) {
requireAuthorization();
JsonNode jsonNode = restTemplate
.getForObject(
uriBuilder("favorites.json")
.queryParam("count", String.valueOf(pageSize))
.queryParam("page", String.valueOf(pageNumber))
.build(), JsonNode.class);
return deserializeCursoredList(jsonNode, Favorite.class, "favorites");
}

@Override
public Favorite getFavorite(long id) {
// TODO Auto-generated method stub
return null;
}

@Override
public CursoredList<Tag> getTags() {
// TODO Auto-generated method stub
return null;
}

@Override
public CursoredList<Tag> getTags(int pageSize, int pageNumber) {
// TODO Auto-generated method stub
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2011 France Telecom R&D Beijing Co., Ltd 北京法国电信研发中心有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.weibo.api.impl.json;

import java.util.Date;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.springframework.social.weibo.api.Status;

/**
* Annotated mixin to add Jackson annotations to Favorite.
*
* @author edva8332
*/
@JsonIgnoreProperties(ignoreUnknown = true)
abstract class FavoriteMixin {

@JsonIgnoreProperties(ignoreUnknown = true)
abstract static class TagMixin {
long id;
@JsonProperty("tag")
String value;
int count;
}

@JsonProperty("status")
Status status;
@JsonProperty("favorited_time")
@JsonDeserialize(using = TimelineDateDeserializer.class)
Date favoritedTime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import org.codehaus.jackson.map.module.SimpleModule;
import org.springframework.social.weibo.api.ApiRateLimit;
import org.springframework.social.weibo.api.Comment;
import org.springframework.social.weibo.api.Favorite;
import org.springframework.social.weibo.api.Favorite.Tag;
import org.springframework.social.weibo.api.RateLimitStatus;
import org.springframework.social.weibo.api.Status;
import org.springframework.social.weibo.api.WeiboProfile;
import org.springframework.social.weibo.api.impl.json.FavoriteMixin.TagMixin;

public class WeiboModule extends SimpleModule {

Expand All @@ -37,6 +40,8 @@ public void setupModule(SetupContext context) {
context.setMixInAnnotations(ApiRateLimit.class, ApiRateLimitMixin.class);
context.setMixInAnnotations(RateLimitStatus.class,
RateLimitStatusMixin.class);
context.setMixInAnnotations(Favorite.class, FavoriteMixin.class);
context.setMixInAnnotations(Tag.class, TagMixin.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2011 France Telecom R&D Beijing Co., Ltd 北京法国电信研发中心有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.social.weibo.api.impl;

import static org.junit.Assert.assertEquals;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.social.test.client.RequestMatchers.header;
import static org.springframework.social.test.client.RequestMatchers.method;
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.Favorite;
import org.springframework.social.weibo.api.Favorite.Tag;
import org.springframework.social.weibo.matcher.StatusMatcher;

public class FavoriteTemplateTest extends AbstractWeiboOperationsTest {

private FavoriteTemplate favoriteTemplate;

@Test
public void testGetFavorites() {
mockServer
.expect(requestTo("https://api.weibo.com/2/favorites.json"))
.andExpect(method(GET))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(
withResponse(jsonResource("cursoredFavorites"),
responseHeaders));
CursoredList<Favorite> cursoredList = favoriteTemplate.getFavorites();
assertEquals(16, cursoredList.getTotalNumber());
assertEquals(2, cursoredList.size());
Favorite firstFavorite = cursoredList.iterator().next();
List<Tag> tags = firstFavorite.getTags();
assertEquals(2, tags.size());
Tag firstTag = tags.iterator().next();
verifyTag(firstTag);
StatusMatcher.verifyStatus(firstFavorite.getStatus());
}

@Test
public void testGetFavoritesIntInt() {
mockServer
.expect(requestTo("https://api.weibo.com/2/favorites.json?count=20&page=5"))
.andExpect(method(GET))
.andExpect(header("Authorization", "OAuth2 accessToken"))
.andRespond(
withResponse(jsonResource("cursoredFavorites"),
responseHeaders));
CursoredList<Favorite> cursoredList = favoriteTemplate.getFavorites(20,
5);
assertEquals(16, cursoredList.getTotalNumber());
assertEquals(2, cursoredList.size());
Favorite firstFavorite = cursoredList.iterator().next();
List<Tag> tags = firstFavorite.getTags();
assertEquals(2, tags.size());
Tag firstTag = tags.iterator().next();
verifyTag(firstTag);
StatusMatcher.verifyStatus(firstFavorite.getStatus());
}

private void verifyTag(Tag firstTag) {
assertEquals(23, firstTag.getId());
assertEquals("80后", firstTag.getValue());
assertEquals(25369, firstTag.getCount());
}

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

}
Loading

0 comments on commit 927c666

Please sign in to comment.