This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/org/springframework/social/weibo/api/TrendOperations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:https://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<UserTrend> getTrends(long userId); | ||
|
||
List<UserTrend> getTrends(long userId, int pageSize, int pageNumber); | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/org/springframework/social/weibo/api/UserTrend.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:https://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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,6 @@ public interface Weibo { | |
|
||
UserOperations userOperations(); | ||
|
||
TrendOperations trendOperations(); | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/org/springframework/social/weibo/api/impl/TrendTemplate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:https://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<UserTrend> 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<UserTrend> 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/org/springframework/social/weibo/api/impl/json/UserTrendMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:https://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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/test/java/org/springframework/social/weibo/api/impl/TrendTemplateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:https://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<UserTrend> 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<UserTrend> 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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[{ | ||
"num" : 225673, | ||
"hotword" : "苹果", | ||
"trend_id" : 1567898 | ||
}, { | ||
"num" : 225674, | ||
"hotword" : "Apple", | ||
"trend_id" : 1567899 | ||
} | ||
] |