diff --git a/src/main/java/org/springframework/social/weibo/api/TrendOperations.java b/src/main/java/org/springframework/social/weibo/api/TrendOperations.java new file mode 100644 index 0000000..0fa2ce0 --- /dev/null +++ b/src/main/java/org/springframework/social/weibo/api/TrendOperations.java @@ -0,0 +1,26 @@ +/* + * 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.List; + +public interface TrendOperations { + + List getTrends(long userId); + + List getTrends(long userId, int pageSize, int pageNumber); + +} diff --git a/src/main/java/org/springframework/social/weibo/api/UserTrend.java b/src/main/java/org/springframework/social/weibo/api/UserTrend.java new file mode 100644 index 0000000..c69b586 --- /dev/null +++ b/src/main/java/org/springframework/social/weibo/api/UserTrend.java @@ -0,0 +1,69 @@ +/* + * 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; + +public class UserTrend { + + private long id; + private String num; + private String hotword; + + /** + * @return the id + */ + public long getId() { + return id; + } + + /** + * @param id + * the id to set + */ + public void setId(long id) { + this.id = id; + } + + /** + * @return the num + */ + public String getNum() { + return num; + } + + /** + * @param num + * the num to set + */ + public void setNum(String num) { + this.num = num; + } + + /** + * @return the hotword + */ + public String getHotword() { + return hotword; + } + + /** + * @param hotword + * the hotword to set + */ + public void setHotword(String hotword) { + this.hotword = hotword; + } + +} diff --git a/src/main/java/org/springframework/social/weibo/api/Weibo.java b/src/main/java/org/springframework/social/weibo/api/Weibo.java index 8cc6baf..31f300d 100644 --- a/src/main/java/org/springframework/social/weibo/api/Weibo.java +++ b/src/main/java/org/springframework/social/weibo/api/Weibo.java @@ -29,4 +29,6 @@ public interface Weibo { UserOperations userOperations(); + TrendOperations trendOperations(); + } diff --git a/src/main/java/org/springframework/social/weibo/api/impl/TrendTemplate.java b/src/main/java/org/springframework/social/weibo/api/impl/TrendTemplate.java new file mode 100644 index 0000000..5b820c9 --- /dev/null +++ b/src/main/java/org/springframework/social/weibo/api/impl/TrendTemplate.java @@ -0,0 +1,55 @@ +/* + * 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 java.util.List; + +import org.codehaus.jackson.JsonNode; +import org.codehaus.jackson.map.ObjectMapper; +import org.springframework.social.weibo.api.TrendOperations; +import org.springframework.social.weibo.api.UserTrend; +import org.springframework.web.client.RestTemplate; + +public class TrendTemplate extends AbstractWeiboOperations implements + TrendOperations { + + protected TrendTemplate(ObjectMapper objectMapper, + RestTemplate restTemplate, boolean isAuthorized) { + super(objectMapper, restTemplate, isAuthorized); + } + + @Override + public List getTrends(long userId) { + requireAuthorization(); + JsonNode jsonNode = restTemplate.getForObject( + buildUri("trends.json", "uid", String.valueOf(userId)), + JsonNode.class); + return deserializeDataList(jsonNode, UserTrend.class); + } + + @Override + public List getTrends(long userId, int pageSize, int pageNumber) { + requireAuthorization(); + JsonNode jsonNode = restTemplate + .getForObject( + uriBuilder("trends.json") + .queryParam("uid", String.valueOf(userId)) + .queryParam("count", String.valueOf(pageSize)) + .queryParam("page", String.valueOf(pageNumber)) + .build(), JsonNode.class); + return deserializeDataList(jsonNode, UserTrend.class); + } +} diff --git a/src/main/java/org/springframework/social/weibo/api/impl/WeiboTemplate.java b/src/main/java/org/springframework/social/weibo/api/impl/WeiboTemplate.java index c3784c5..797729b 100644 --- a/src/main/java/org/springframework/social/weibo/api/impl/WeiboTemplate.java +++ b/src/main/java/org/springframework/social/weibo/api/impl/WeiboTemplate.java @@ -35,6 +35,7 @@ import org.springframework.social.weibo.api.FavoriteOperations; import org.springframework.social.weibo.api.FriendOperations; import org.springframework.social.weibo.api.TimelineOperations; +import org.springframework.social.weibo.api.TrendOperations; import org.springframework.social.weibo.api.UserOperations; import org.springframework.social.weibo.api.Weibo; import org.springframework.social.weibo.api.impl.json.WeiboModule; @@ -56,6 +57,8 @@ public class WeiboTemplate extends AbstractOAuth2ApiBinding implements Weibo { private FavoriteTemplate favouriteOperations; + private TrendTemplate trendOperations; + public WeiboTemplate(String accessToken) { super(accessToken); initialize(); @@ -122,6 +125,8 @@ private void initSubApis() { getRestTemplate(), isAuthorized()); this.favouriteOperations = new FavoriteTemplate(objectMapper, getRestTemplate(), isAuthorized()); + this.trendOperations = new TrendTemplate(objectMapper, + getRestTemplate(), isAuthorized()); } @Override @@ -163,4 +168,9 @@ public FavoriteOperations favoriteOperations() { return favouriteOperations; } + @Override + public TrendOperations trendOperations() { + return trendOperations; + } + } diff --git a/src/main/java/org/springframework/social/weibo/api/impl/json/UserTrendMixin.java b/src/main/java/org/springframework/social/weibo/api/impl/json/UserTrendMixin.java new file mode 100644 index 0000000..b7b366d --- /dev/null +++ b/src/main/java/org/springframework/social/weibo/api/impl/json/UserTrendMixin.java @@ -0,0 +1,34 @@ +/* + * 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 org.codehaus.jackson.annotate.JsonIgnoreProperties; +import org.codehaus.jackson.annotate.JsonProperty; + +/** + * Annotated mixin to add Jackson annotations to UserTrend. + * + * @author edva8332 + */ +@JsonIgnoreProperties(ignoreUnknown = true) +abstract class UserTrendMixin { + + String num; + String hotword; + @JsonProperty("trend_id") + long id; + +} diff --git a/src/main/java/org/springframework/social/weibo/api/impl/json/WeiboModule.java b/src/main/java/org/springframework/social/weibo/api/impl/json/WeiboModule.java index 9cd9dda..c93050a 100644 --- a/src/main/java/org/springframework/social/weibo/api/impl/json/WeiboModule.java +++ b/src/main/java/org/springframework/social/weibo/api/impl/json/WeiboModule.java @@ -23,6 +23,7 @@ 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.UserTrend; import org.springframework.social.weibo.api.WeiboProfile; import org.springframework.social.weibo.api.impl.json.FavoriteMixin.TagMixin; @@ -42,6 +43,7 @@ public void setupModule(SetupContext context) { RateLimitStatusMixin.class); context.setMixInAnnotations(Favorite.class, FavoriteMixin.class); context.setMixInAnnotations(Tag.class, TagMixin.class); + context.setMixInAnnotations(UserTrend.class, UserTrendMixin.class); } } diff --git a/src/test/java/org/springframework/social/weibo/api/impl/TrendTemplateTest.java b/src/test/java/org/springframework/social/weibo/api/impl/TrendTemplateTest.java new file mode 100644 index 0000000..c914f10 --- /dev/null +++ b/src/test/java/org/springframework/social/weibo/api/impl/TrendTemplateTest.java @@ -0,0 +1,76 @@ +/* + * 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.UserTrend; + +public class TrendTemplateTest extends AbstractWeiboOperationsTest { + + private TrendTemplate trendTemplate; + + @Test + public void testGetTrendsPagination() { + mockServer + .expect(requestTo("https://api.weibo.com/2/trends.json?uid=1&count=20&page=2")) + .andExpect(method(GET)) + .andExpect(header("Authorization", "OAuth2 accessToken")) + .andRespond( + withResponse(jsonResource("userTrends"), + responseHeaders)); + List trends = trendTemplate.getTrends(1, 20, 2); + assertEquals(2, trends.size()); + UserTrend firstTrend = trends.iterator().next(); + verifyUserTrend(firstTrend); + } + + @Test + public void testGetTrends() { + mockServer + .expect(requestTo("https://api.weibo.com/2/trends.json?uid=1")) + .andExpect(method(GET)) + .andExpect(header("Authorization", "OAuth2 accessToken")) + .andRespond( + withResponse(jsonResource("userTrends"), + responseHeaders)); + List trends = trendTemplate.getTrends(1); + assertEquals(2, trends.size()); + UserTrend firstTrend = trends.iterator().next(); + verifyUserTrend(firstTrend); + } + + private void verifyUserTrend(UserTrend userTrend) { + assertEquals(1567898, userTrend.getId()); + assertEquals("苹果", userTrend.getHotword()); + assertEquals("225673", userTrend.getNum()); + } + + @Override + public void setUp() { + trendTemplate = new TrendTemplate(getObjectMapper(), getRestTemplate(), + true); + } + +} diff --git a/src/test/resources/json/userTrends.json b/src/test/resources/json/userTrends.json new file mode 100644 index 0000000..5bf3dc9 --- /dev/null +++ b/src/test/resources/json/userTrends.json @@ -0,0 +1,10 @@ +[{ + "num" : 225673, + "hotword" : "苹果", + "trend_id" : 1567898 + }, { + "num" : 225674, + "hotword" : "Apple", + "trend_id" : 1567899 + } +]