新增标签API单元测试

This commit is contained in:
jinyu 2016-05-02 20:54:50 +08:00
parent f2f117bd3e
commit 205d9c4ee3
5 changed files with 225 additions and 20 deletions

View File

@ -1720,6 +1720,24 @@ public class WeixinProxy {
return tagApi.getTagFollowingOpenIds(tagId, nextOpenId);
}
/**
* 获取标签下粉丝列表 <font corlor="red">请慎重使用</font>
*
* @param tagId
* 标签ID
* @param nextOpenId
* 第一个拉取的OPENID不填默认从头开始拉取
* @return 被打标签者信息 <font color="red">包含用户的详细信息</font>
* @throws WeixinException
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @see <a
* href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">获取标签下粉丝列表</a>
*/
public Following getTagFollowing(int tagId, String nextOpenId)
throws WeixinException {
return tagApi.getTagFollowing(tagId, nextOpenId);
}
/**
* 获取标签下全部的粉丝列表 <font corlor="red">请慎重使用</font>
*
@ -1737,6 +1755,22 @@ public class WeixinProxy {
return tagApi.getAllTagFollowingOpenIds(tagId);
}
/**
* 获取标签下全部的粉丝列表 <font corlor="red">请慎重使用</font>
*
* @param tagId
* 标签ID
* @return 被打标签者信息 <font color="red">包含用户的详细信息</font>
* @throws WeixinException
* @see com.foxinmy.weixin4j.mp.api.TagApi
* @see #getTagFollowing(int,String)
* @see <a
* href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">获取标签下粉丝列表</a>
*/
public List<User> getAllTagFollowing(int tagId) throws WeixinException {
return tagApi.getAllTagFollowing(tagId);
}
/**
* 获取用户身上的标签列表
*

View File

@ -10,6 +10,7 @@ import com.foxinmy.weixin4j.http.weixin.JsonResult;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.mp.model.Following;
import com.foxinmy.weixin4j.mp.model.Tag;
import com.foxinmy.weixin4j.mp.model.User;
import com.foxinmy.weixin4j.token.TokenHolder;
/**
@ -23,9 +24,11 @@ import com.foxinmy.weixin4j.token.TokenHolder;
*/
public class TagApi extends MpApi {
private final TokenHolder tokenHolder;
private final UserApi userApi;
public TagApi(TokenHolder tokenHolder) {
this.tokenHolder = tokenHolder;
this.userApi = new UserApi(tokenHolder);
}
/**
@ -45,7 +48,8 @@ public class TagApi extends MpApi {
String.format(tag_create_uri, tokenHolder.getAccessToken()),
String.format("{\"tag\":{\"name\":\"%s\"}}", name));
return response.getAsJson().getObject("tag", Tag.class);
return JSON.parseObject(response.getAsJson().getString("tag"),
Tag.class);
}
/**
@ -79,9 +83,11 @@ public class TagApi extends MpApi {
*/
public JsonResult updateTag(Tag tag) throws WeixinException {
String tag_update_uri = getRequestUri("tag_update_uri");
JSONObject obj = new JSONObject();
obj.put("tag", tag);
WeixinResponse response = weixinExecutor.post(
String.format(tag_update_uri, tokenHolder.getAccessToken()),
JSON.toJSONString(tag));
obj.toJSONString());
return response.getAsJsonResult();
}
@ -181,6 +187,35 @@ public class TagApi extends MpApi {
return following;
}
/**
* 获取标签下粉丝列表 <font corlor="red">请慎重使用</font>
*
* @param tagId
* 标签ID
* @param nextOpenId
* 第一个拉取的OPENID不填默认从头开始拉取
* @return 被打标签者信息 <font color="red">包含用户的详细信息</font>
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">获取标签下粉丝列表</a>
*/
public Following getTagFollowing(int tagId, String nextOpenId)
throws WeixinException {
Following following = getTagFollowingOpenIds(tagId, nextOpenId);
if (following.getCount() > 0) {
List<User> users = new ArrayList<User>(following.getCount());
for (int i = 1; i <= (int) Math.ceil(following.getCount() / 100d); i++) {
users.addAll(userApi.getUsers(following
.getOpenIds()
.subList((i - 1) * 100,
Math.min(i * 100, following.getCount()))
.toArray(new String[] {})));
}
following.setUserList(users);
}
return following;
}
/**
* 获取标签下全部的粉丝列表 <font corlor="red">请慎重使用</font>
*
@ -208,6 +243,32 @@ public class TagApi extends MpApi {
return openIds;
}
/**
* 获取标签下全部的粉丝列表 <font corlor="red">请慎重使用</font>
*
* @param tagId
* 标签ID
* @return 被打标签者信息 <font color="red">包含用户的详细信息</font>
* @throws WeixinException
* @see #getTagFollowing(int,String)
* @see <a
* href="http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140837&token=&lang=zh_CN">获取标签下粉丝列表</a>
*/
public List<User> getAllTagFollowing(int tagId) throws WeixinException {
List<User> userList = new ArrayList<User>();
String nextOpenId = null;
Following f = null;
for (;;) {
f = getTagFollowing(tagId, nextOpenId);
if (f.getCount() == 0) {
break;
}
userList.addAll(f.getUserList());
nextOpenId = f.getNextOpenId();
}
return userList;
}
/**
* 获取用户身上的标签列表
*

View File

@ -67,8 +67,8 @@ public class UserApi extends MpApi {
public User getUser(String openId, Lang lang) throws WeixinException {
String user_info_uri = getRequestUri("api_user_info_uri");
Token token = tokenHolder.getToken();
WeixinResponse response = weixinExecutor
.get(String.format(user_info_uri, token.getAccessToken(), openId, lang.name()));
WeixinResponse response = weixinExecutor.get(String.format(
user_info_uri, token.getAccessToken(), openId, lang.name()));
return response.getAsObject(new TypeReference<User>() {
});
@ -106,21 +106,25 @@ public class UserApi extends MpApi {
* @see com.foxinmy.weixin4j.mp.model.User
* @throws WeixinException
*/
public List<User> getUsers(Lang lang, String... openIds) throws WeixinException {
public List<User> getUsers(Lang lang, String... openIds)
throws WeixinException {
String api_users_info_uri = getRequestUri("api_users_info_uri");
StringBuilder parameter = new StringBuilder();
parameter.append("{\"user_list\": [");
for (String openId : openIds) {
parameter.append("{\"openid\": \"").append(openId).append("\"");
parameter.append(",\"lang\": \"").append(lang.name()).append("\"").append("},");
parameter.append(",\"lang\": \"").append(lang.name()).append("\"")
.append("},");
}
parameter.delete(parameter.length() - 1, parameter.length());
parameter.append("]}");
Token token = tokenHolder.getToken();
WeixinResponse response = weixinExecutor.post(String.format(api_users_info_uri, token.getAccessToken()),
WeixinResponse response = weixinExecutor.post(
String.format(api_users_info_uri, token.getAccessToken()),
parameter.toString());
return JSON.parseArray(response.getAsJson().getString("user_info_list"), User.class);
return JSON.parseArray(
response.getAsJson().getString("user_info_list"), User.class);
}
/**
@ -144,8 +148,11 @@ public class UserApi extends MpApi {
if (following.getCount() > 0) {
List<User> users = new ArrayList<User>(following.getCount());
for (int i = 1; i <= (int) Math.ceil(following.getCount() / 100d); i++) {
users.addAll(getUsers(following.getOpenIds()
.subList((i - 1) * 100, Math.min(i * 100, following.getCount())).toArray(new String[] {})));
users.addAll(getUsers(following
.getOpenIds()
.subList((i - 1) * 100,
Math.min(i * 100, following.getCount()))
.toArray(new String[] {})));
}
following.setUserList(users);
}
@ -164,17 +171,20 @@ public class UserApi extends MpApi {
* 获取关注者列表</a>
* @see com.foxinmy.weixin4j.mp.model.Following
*/
public Following getFollowingOpenIds(String nextOpenId) throws WeixinException {
public Following getFollowingOpenIds(String nextOpenId)
throws WeixinException {
String following_uri = getRequestUri("following_uri");
Token token = tokenHolder.getToken();
WeixinResponse response = weixinExecutor
.get(String.format(following_uri, token.getAccessToken(), nextOpenId == null ? "" : nextOpenId));
WeixinResponse response = weixinExecutor.get(String.format(
following_uri, token.getAccessToken(), nextOpenId == null ? ""
: nextOpenId));
JSONObject result = response.getAsJson();
Following following = JSON.toJavaObject(result, Following.class);
if (following.getCount() > 0) {
following.setOpenIds(JSON.parseArray(result.getJSONObject("data").getString("openid"), String.class));
following.setOpenIds(JSON.parseArray(result.getJSONObject("data")
.getString("openid"), String.class));
}
return following;
}
@ -204,10 +214,10 @@ public class UserApi extends MpApi {
Following f = null;
for (;;) {
f = getFollowing(nextOpenId);
if (f.getCount() == 0) {
userList.addAll(f.getUserList());
if (f.getCount() == f.getTotal() || f.getCount() == 0) {
break;
}
userList.addAll(f.getUserList());
nextOpenId = f.getNextOpenId();
}
return userList;
@ -233,10 +243,10 @@ public class UserApi extends MpApi {
Following f = null;
for (;;) {
f = getFollowingOpenIds(nextOpenId);
if (f.getCount() == 0) {
openIds.addAll(f.getOpenIds());
if (f.getCount() == f.getTotal() || f.getCount() == 0) {
break;
}
openIds.addAll(f.getOpenIds());
nextOpenId = f.getNextOpenId();
}
return openIds;
@ -254,13 +264,15 @@ public class UserApi extends MpApi {
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140838&token=&lang=zh_CN">
* 设置用户备注名</a>
*/
public JsonResult remarkUserName(String openId, String remark) throws WeixinException {
public JsonResult remarkUserName(String openId, String remark)
throws WeixinException {
String username_remark_uri = getRequestUri("username_remark_uri");
Token token = tokenHolder.getToken();
JSONObject obj = new JSONObject();
obj.put("openid", openId);
obj.put("remark", remark);
WeixinResponse response = weixinExecutor.post(String.format(username_remark_uri, token.getAccessToken()),
WeixinResponse response = weixinExecutor.post(
String.format(username_remark_uri, token.getAccessToken()),
obj.toJSONString());
return response.getAsJsonResult();

View File

@ -31,6 +31,12 @@ public class Tag implements Serializable {
*/
private int count;
@JSONCreator
public Tag(@JSONField(name = "id") int id,
@JSONField(name = "name") String name) {
this(id, name, 0);
}
@JSONCreator
public Tag(@JSONField(name = "id") int id,
@JSONField(name = "name") String name,

View File

@ -0,0 +1,92 @@
package com.foxinmy.weixin4j.mp.test;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.JsonResult;
import com.foxinmy.weixin4j.mp.api.TagApi;
import com.foxinmy.weixin4j.mp.model.Tag;
import com.foxinmy.weixin4j.mp.model.User;
/**
* 标签单元测试
*
* @className TagTest
* @author jy
* @date 2016年5月2日
* @since JDK 1.6
* @see
*/
public class TagTest extends TokenTest {
private TagApi tagApi;
@Before
public void init() {
tagApi = new TagApi(tokenHolder);
}
@Test
public void create() throws WeixinException {
Tag tag = tagApi.createTag("测试三");
Assert.assertNotNull(tag);
System.out.println(tag);
}
@Test
public void list() throws WeixinException {
List<Tag> tags = tagApi.listTags();
Assert.assertFalse(tags.isEmpty());
}
@Test
public void update() throws WeixinException {
JsonResult result = tagApi.updateTag(new Tag(120, "测试12"));
System.err.println(result);
}
@Test
public void remove() throws WeixinException {
JsonResult result = tagApi.deleteTag(134);
System.err.print(result);
}
@Test
public void batchtagging() throws WeixinException {
JsonResult result = tagApi.taggingUsers(120,
"owGBft-GyGJuKXBzpkzrfl-RG8TI", "owGBfty5TYNwh-3iUTGtxAHcD310",
"owGBftzXEfBml_bYvbrYxE5lE5U8");
System.err.println(result);
}
@Test
public void batchuntagging() throws WeixinException {
JsonResult result = tagApi.taggingUsers(120,
"owGBftwS5Yr6xKH_Hb9mGv1nbd3o");
System.err.println(result);
}
@Test
public void getidlist() throws WeixinException {
Integer[] tagIds = tagApi.getUserTags("owGBft-GyGJuKXBzpkzrfl-RG8TI");
Assert.assertNotNull(tagIds);
System.out.println(tagIds[0]);
}
@Test
public void getAllTagFollowing() throws WeixinException {
List<User> users = tagApi.getAllTagFollowing(120);
Assert.assertNotNull(users);
System.out.println(users);
}
@Test
public void getAllTagFollowingOpenIds() throws WeixinException {
List<String> tags = tagApi.getAllTagFollowingOpenIds(120);
Assert.assertNotNull(tags);
System.out.println(tags);
}
}