合并开发分支,发布正式版

This commit is contained in:
Binary Wang
2019-05-18 17:07:45 +08:00
committed by GitHub
194 changed files with 6446 additions and 1140 deletions

View File

@@ -1,11 +1,12 @@
package me.chanjar.weixin.cp.api;
import org.testng.annotations.*;
import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
import org.testng.annotations.*;
import static org.testng.Assert.*;
@@ -14,7 +15,7 @@ import static org.testng.Assert.*;
* @author Daniel Qian
*
*/
@Test(groups = "customMessageAPI")
@Test
@Guice(modules = ApiTestModule.class)
public class WxCpMessageAPITest {
@@ -59,4 +60,51 @@ public class WxCpMessageAPITest {
System.out.println(messageSendResult.getInvalidUserList());
System.out.println(messageSendResult.getInvalidTagList());
}
@Test
public void testSendMessage_markdown() throws WxErrorException {
WxCpMessage message = WxCpMessage
.MARKDOWN()
.toUser(configStorage.getUserId())
.content("您的会议室已经预定,稍后会同步到`邮箱` \n" +
" >**事项详情** \n" +
" >事 项:<font color=\\\"info\\\">开会</font> \n" +
" >组织者:@miglioguan \n" +
" >参与者:@miglioguan、@kunliu、@jamdeezhou、@kanexiong、@kisonwang \n" +
" > \n" +
" >会议室:<font color=\\\"info\\\">广州TIT 1楼 301</font> \n" +
" >日 期:<font color=\\\"warning\\\">2018年5月18日</font> \n" +
" >时 间:<font color=\\\"comment\\\">上午9:00-11:00</font> \n" +
" > \n" +
" >请准时参加会议。 \n" +
" > \n" +
" >如需修改会议信息,请点击:[修改会议信息](https://work.weixin.qq.com)")
.build();
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
assertNotNull(messageSendResult);
System.out.println(messageSendResult);
System.out.println(messageSendResult.getInvalidPartyList());
System.out.println(messageSendResult.getInvalidUserList());
System.out.println(messageSendResult.getInvalidTagList());
}
@Test
public void testSendMessage_textCard() throws WxErrorException {
WxCpMessage message = WxCpMessage
.TEXTCARD()
.toUser(configStorage.getUserId())
.btnTxt("更多")
.description( "<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台领奖码xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>")
.url("URL")
.title("领奖通知")
.build();
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
assertNotNull(messageSendResult);
System.out.println(messageSendResult);
System.out.println(messageSendResult.getInvalidPartyList());
System.out.println(messageSendResult.getInvalidUserList());
System.out.println(messageSendResult.getInvalidTagList());
}
}

View File

@@ -0,0 +1,36 @@
package me.chanjar.weixin.cp.api.impl;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpService;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.*;
/**
* <pre>
* Created by BinaryWang on 2019/3/31.
* </pre>
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Test
@Guice(modules = ApiTestModule.class)
public class BaseWxCpServiceImplTest {
@Inject
protected WxCpService wxService;
@Test
public void testGetAgentJsapiTicket() throws WxErrorException {
assertThat(this.wxService.getAgentJsapiTicket()).isNotEmpty();
assertThat(this.wxService.getAgentJsapiTicket(true)).isNotEmpty();
}
@Test
public void testJsCode2Session() throws WxErrorException {
assertThat(this.wxService.jsCode2Session("111")).isNotNull();
}
}

View File

@@ -2,15 +2,21 @@ package me.chanjar.weixin.cp.api.impl;
import java.util.Arrays;
import me.chanjar.weixin.cp.bean.WxCpChat;
import org.testng.Assert;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import org.testng.*;
import org.testng.annotations.*;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.WxCpConsts.AppChatMsgType;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpAppChatMessage;
import me.chanjar.weixin.cp.bean.WxCpChat;
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
import me.chanjar.weixin.cp.bean.article.NewArticle;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 测试群聊服务
@@ -19,28 +25,134 @@ import me.chanjar.weixin.cp.api.WxCpService;
*/
@Guice(modules = ApiTestModule.class)
public class WxCpChatServiceImplTest {
private String chatId;
private String userId;
@Inject
private WxCpService wxCpService;
@Test
public void create() throws Exception {
wxCpService.getChatService().chatCreate("测试群聊", "gaige_shen", Arrays.asList("gaige_shen", "ZhangXiaoMing"), "mychatid");
private WxCpService cpService;
@BeforeTest
public void init() {
this.chatId = "mychatid";
this.userId = ((ApiTestModule.WxXmlCpInMemoryConfigStorage) this.cpService.getWxCpConfigStorage()).getUserId();
}
@Test
public void get() throws Exception {
WxCpChat chat = wxCpService.getChatService().chatGet("mychatid");
public void testChatCreate() throws Exception {
final String result = cpService.getChatService().chatCreate("测试群聊", userId,
Arrays.asList(userId, userId), chatId);
assertThat(result).isNotEmpty();
assertThat(result).isEqualTo(chatId);
}
@Test
public void testChatGet() throws Exception {
WxCpChat chat = this.cpService.getChatService().chatGet(chatId);
System.out.println(chat);
Assert.assertEquals(chat.getName(), "测试群聊");
}
@Test
public void update() throws Exception {
wxCpService.getChatService().chatUpdate("mychatid", "", "", Arrays.asList("ZhengWuYao"), null);
WxCpChat chat = wxCpService.getChatService().chatGet("mychatid");
public void testChatUpdate() throws Exception {
this.cpService.getChatService().chatUpdate(chatId, "", "", Arrays.asList("ZhengWuYao"), null);
WxCpChat chat = this.cpService.getChatService().chatGet(chatId);
System.out.println(chat);
Assert.assertEquals(chat.getUsers().size(), 3);
}
@DataProvider
public Object[][] messages() {
return new Object[][]{
{WxCpAppChatMessage.builder()
.msgType(AppChatMsgType.TEXT)
.chatId(chatId)
.content("你的快递已到\n请携带工卡前往邮件中心领取")
.build()
},
{WxCpAppChatMessage.builder()
.msgType(AppChatMsgType.IMAGE)
.chatId(chatId)
.mediaId("3_xWGPXZhpOKZrlRISWrjhPrDUZqZ-jIEVzxd56jLuqM")
.build()
},
{WxCpAppChatMessage.builder()
.msgType(AppChatMsgType.VOICE)
.chatId(chatId)
.mediaId("3X5t6HkdN1hUgB7OzrdRnc8v0yI0CqlAxFxnCkS3msTnTLanpYrV4esLv4foZVnlf")
.build()
},
{WxCpAppChatMessage.builder()
.msgType(AppChatMsgType.VIDEO)
.chatId(chatId)
.mediaId("3otWyy_acbID8fyltmCOW5hGVD8oa0_p0za5jhukxKTUDoGT71lqTvtQAWoycXpQf")
.title("aaaa")
.description("ddddd")
.build()
},
{WxCpAppChatMessage.builder()
.msgType(AppChatMsgType.FILE)
.chatId(chatId)
.mediaId("34AyVyDdndVhB4Z2tT-_FYKZ7Xqrr47LPC11GHH4oy7o")
.build()
},
{WxCpAppChatMessage.builder()
.msgType(AppChatMsgType.TEXTCARD)
.chatId(chatId)
.btnTxt("更多")
.title("领奖通知")
.url("https://zhidao.baidu.com/question/2073647112026042748.html")
.description("<div class=\"gray\">2016年9月26日</div> <div class=\"normal\"> 恭喜你抽中iPhone 7一台领奖码:520258</div><div class=\"highlight\">请于2016年10月10日前联系行 政同事领取</div>")
.build()
},
{WxCpAppChatMessage.builder()
.msgType(AppChatMsgType.NEWS)
.chatId(chatId)
.articles(Lists.newArrayList(NewArticle.builder()
.title("领奖通知")
.url("https://zhidao.baidu.com/question/2073647112026042748.html")
.description("今年中秋节公司有豪礼相送")
.picUrl("http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png")
.build()
))
.build()
},
{WxCpAppChatMessage.builder()
.msgType(AppChatMsgType.MPNEWS)
.chatId(chatId)
.mpnewsArticles(Lists.newArrayList(MpnewsArticle.newBuilder()
.title("地球一小时")
.thumbMediaId("3_xWGPXZhpOKZrlRISWrjhPrDUZqZ-jIEVzxd56jLuqM")
.author("Author")
.contentSourceUrl("https://work.weixin.qq.com")
.content("3月24日20:30-21:30 \n办公区将关闭照明一小时请各部门同事相互转告")
.digest("3月24日20:30-21:30 \n办公区将关闭照明一小时")
.build()
))
.build()
},
{WxCpAppChatMessage.builder()
.msgType(AppChatMsgType.MARKDOWN)
.chatId(chatId)
.content("您的会议室已经预定,稍后会同步到`邮箱` \n" +
" >**事项详情** \n" +
" >事 项:<font color=\\\"info\\\">开会</font> \n" +
" >组织者:@miglioguan \n" +
" >参与者:@miglioguan、@kunliu、@jamdeezhou、@kanexiong、@kisonwang \n" +
" > \n" +
" >会议室:<font color=\\\"info\\\">广州TIT 1楼 301</font> \n" +
" >日 期:<font color=\\\"warning\\\">2018年5月18日</font> \n" +
" >时 间:<font color=\\\"comment\\\">上午9:00-11:00</font> \n" +
" > \n" +
" >请准时参加会议。 \n" +
" > \n" +
" >如需修改会议信息,请点击:[修改会议信息](https://work.weixin.qq.com)")
.build()
},
};
}
@Test(dataProvider = "messages")
public void testSendMsg(WxCpAppChatMessage message) throws WxErrorException {
this.cpService.getChatService().sendMsg(message);
}
}

View File

@@ -31,7 +31,7 @@ public class WxCpDepartmentServiceImplTest {
cpDepart.setName("子部门" + System.currentTimeMillis());
cpDepart.setParentId(1L);
cpDepart.setOrder(1L);
Integer departId = this.wxCpService.getDepartmentService().create(cpDepart);
Long departId = this.wxCpService.getDepartmentService().create(cpDepart);
System.out.println(departId);
}

View File

@@ -0,0 +1,49 @@
package me.chanjar.weixin.cp.api.impl;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpCheckinData;
import me.chanjar.weixin.cp.bean.WxCpCheckinOption;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Element
* @date 2019-04-20 13:46
*/
@Guice(modules = ApiTestModule.class)
public class WxCpOAServiceImplTest {
@Inject
protected WxCpService wxService;
@Test
public void testGetCheckinData() throws ParseException, WxErrorException {
Date startTime = DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.parse("2019-04-11");
Date endTime = DateFormatUtils.ISO_8601_EXTENDED_DATE_FORMAT.parse("2019-05-10");
List<WxCpCheckinData> results = wxService.getOAService()
.getCheckinData(1, startTime, endTime, Lists.newArrayList("binary"));
assertThat(results).isNotNull();
}
@Test
public void testGetCheckinOption() throws WxErrorException {
Date now = new Date();
List<WxCpCheckinOption> results = wxService.getOAService()
.getCheckinOption(now, Lists.newArrayList("binary"));
assertThat(results).isNotNull();
}
}

View File

@@ -0,0 +1,65 @@
package me.chanjar.weixin.cp.api.impl;
import com.google.inject.Inject;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
import me.chanjar.weixin.cp.bean.taskcard.TaskCardButton;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;
import java.util.Arrays;
import static org.testng.Assert.assertNotNull;
/**
* 测试任务卡片服务
*
* @author <a href="https://github.com/domainname">Jeff</a>
* @date 2019-05-16
*/
@Guice(modules = ApiTestModule.class)
public class WxCpTaskCardServiceImplTest {
@Inject
private WxCpService wxCpService;
@Test
public void testSendTaskCard() throws WxErrorException {
TaskCardButton btn1 = TaskCardButton.builder()
.key("key1")
.name("同意")
.replaceName("已同意")
.bold(true)
.build();
TaskCardButton btn2 = TaskCardButton.builder()
.key("key2")
.name("拒绝")
.replaceName("已拒绝")
.color("red")
.build();
WxCpMessage message = WxCpMessage.TASKCARD()
.toUser("jeff|mr.t")
.title("有一个待审批的请求")
.description("申请:购买图书\n金额100 元")
.taskId("task_1")
.url("http://www.qq.com")
.buttons(Arrays.asList(btn1, btn2))
.build();
WxCpMessageSendResult messageSendResult = this.wxCpService.messageSend(message);
assertNotNull(messageSendResult);
System.out.println(messageSendResult);
System.out.println(messageSendResult.getInvalidPartyList());
System.out.println(messageSendResult.getInvalidUserList());
System.out.println(messageSendResult.getInvalidTagList());
}
@Test
public void testUpdate() throws Exception {
wxCpService.getTaskCardService().update(Arrays.asList("jeff", "mr.t"), "task_1", "key1");
}
}

View File

@@ -42,7 +42,7 @@ public class WxCpUserServiceImplTest {
WxCpUser user = new WxCpUser();
user.setUserId(userId);
user.setName("Some Woman");
user.setDepartIds(new Integer[]{2});
user.setDepartIds(new Long[]{2L});
user.setEmail("none@none.com");
user.setGender(Gender.FEMALE);
user.setMobile("13560084979");

View File

@@ -1,7 +1,7 @@
package me.chanjar.weixin.cp.bean;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.*;
import org.testng.annotations.*;
/**
* Created by huansinho on 2018/4/13.
@@ -10,7 +10,13 @@ import org.testng.annotations.Test;
public class WxCpAgentTest {
public void testDeserialize() {
String json = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\",\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\",\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}, {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]},\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]},\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0,\"isreportenter\": 0,\"home_url\": \"\"}";
String json = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\"," +
"\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\"," +
"\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}," +
" {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]}," +
"\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]}," +
"\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0," +
"\"isreportenter\": 0,\"home_url\": \"\"}";
WxCpAgent wxCpAgent = WxCpAgent.fromJson(json);
@@ -18,7 +24,8 @@ public class WxCpAgentTest {
Assert.assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray());
Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagIds().toArray());
Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7},
wxCpAgent.getAllowTags().getTagIds().toArray());
}

View File

@@ -2,10 +2,12 @@ package me.chanjar.weixin.cp.bean;
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
import me.chanjar.weixin.cp.bean.article.NewArticle;
import me.chanjar.weixin.cp.bean.taskcard.TaskCardButton;
import org.testng.annotations.Test;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertEquals;
@Test
public class WxCpMessageTest {
@@ -19,12 +21,16 @@ public class WxCpMessageTest {
public void testTextCardBuild() {
WxCpMessage reply = WxCpMessage.TEXTCARD().toUser("OPENID")
.title("领奖通知")
.description("<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台领奖码xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>")
.description("<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台" +
"领奖码xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>")
.url("http://www.qq.com")
.btnTxt("更多")
.build();
assertThat(reply.toJson())
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"textcard\",\"textcard\":{\"title\":\"领奖通知\",\"description\":\"<div class=\\\"gray\\\">2016年9月26日</div> <div class=\\\"normal\\\">恭喜你抽中iPhone 7一台领奖码xxxx</div><div class=\\\"highlight\\\">请于2016年10月10日前联系行政同事领取</div>\",\"url\":\"http://www.qq.com\",\"btntxt\":\"更多\"},\"safe\":\"0\"}");
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"textcard\",\"textcard\":{\"title\":\"领奖通知\"," +
"\"description\":\"<div class=\\\"gray\\\">2016年9月26日</div> <div class=\\\"normal\\\">" +
"恭喜你抽中iPhone 7一台领奖码xxxx</div><div class=\\\"highlight\\\">请于2016年10月10日前联系行政同事领取</div>\"," +
"\"url\":\"http://www.qq.com\",\"btntxt\":\"更多\"},\"safe\":\"0\"}");
}
public void testImageBuild() {
@@ -40,9 +46,11 @@ public class WxCpMessageTest {
}
public void testVideoBuild() {
WxCpMessage reply = WxCpMessage.VIDEO().toUser("OPENID").title("TITLE").mediaId("MEDIA_ID").thumbMediaId("MEDIA_ID").description("DESCRIPTION").build();
WxCpMessage reply = WxCpMessage.VIDEO().toUser("OPENID").title("TITLE").mediaId("MEDIA_ID").thumbMediaId("MEDIA_ID")
.description("DESCRIPTION").build();
assertThat(reply.toJson())
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"safe\":\"0\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"MEDIA_ID\"," +
"\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"},\"safe\":\"0\"}");
}
public void testNewsBuild() {
@@ -61,7 +69,10 @@ public class WxCpMessageTest {
WxCpMessage reply = WxCpMessage.NEWS().toUser("OPENID").addArticle(article1).addArticle(article2).build();
assertThat(reply.toJson())
.isEqualTo( "{\"touser\":\"OPENID\",\"msgtype\":\"news\",\"safe\":\"0\",\"news\":{\"articles\":[{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"},{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"}]}}");
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"news\",\"news\":{\"articles\":" +
"[{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"}," +
"{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"}]}," +
"\"safe\":\"0\"}");
}
public void testMpnewsBuild_with_articles() {
@@ -88,14 +99,45 @@ public class WxCpMessageTest {
WxCpMessage reply = WxCpMessage.MPNEWS().toUser("OPENID").addArticle(article1, article2).build();
assertThat(reply.toJson())
.isEqualTo( "{\"touser\":\"OPENID\",\"msgtype\":\"mpnews\",\"safe\":\"0\",\"mpnews\":{\"articles\":[{\"title\":\"Happy Day\",\"thumb_media_id\":\"thumb\",\"author\":\"aaaaaa\",\"content_source_url\":\"nice url\",\"content\":\"hahaha\",\"digest\":\"digest\",\"show_cover_pic\":\"heihei\"},{\"title\":\"Happy Day\",\"thumb_media_id\":\"thumb\",\"author\":\"aaaaaa\",\"content_source_url\":\"nice url\",\"content\":\"hahaha\",\"digest\":\"digest\",\"show_cover_pic\":\"heihei\"}]}}");
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"mpnews\",\"mpnews\":{\"articles\":" +
"[{\"title\":\"Happy Day\",\"thumb_media_id\":\"thumb\",\"author\":\"aaaaaa\"," +
"\"content_source_url\":\"nice url\",\"content\":\"hahaha\",\"digest\":\"digest\",\"show_cover_pic\":\"heihei\"}" +
",{\"title\":\"Happy Day\",\"thumb_media_id\":\"thumb\",\"author\":\"aaaaaa\"," +
"\"content_source_url\":\"nice url\",\"content\":\"hahaha\",\"digest\":\"digest\",\"show_cover_pic\":\"heihei\"}]}," +
"\"safe\":\"0\"}");
}
public void testMpnewsBuild_with_media_id() {
WxCpMessage reply = WxCpMessage.MPNEWS().toUser("OPENID").mediaId("mmm").build();
assertThat(reply.toJson())
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"mpnews\",\"safe\":\"0\",\"mpnews\":{\"media_id\":\"mmm\"}}");
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"mpnews\",\"mpnews\":{\"media_id\":\"mmm\"},\"safe\":\"0\"}");
}
public void testTaskCardBuilder() {
TaskCardButton button1 = TaskCardButton.builder()
.key("yes")
.name("批准")
.replaceName("已批准")
.color("blue")
.bold(true)
.build();
TaskCardButton button2 = TaskCardButton.builder()
.key("yes")
.name("拒绝")
.replaceName("已拒绝")
.color("red")
.bold(false)
.build();
WxCpMessage reply = WxCpMessage.TASKCARD().toUser("OPENID")
.title("任务卡片")
.description("有一条待处理任务")
.url("http://www.qq.com")
.taskId("task_123")
.buttons(Arrays.asList(button1, button2))
.build();
assertThat(reply.toJson())
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"taskcard\",\"taskcard\":{\"title\":\"任务卡片\",\"description\":\"有一条待处理任务\",\"url\":\"http://www.qq.com\",\"task_id\":\"task_123\",\"btn\":[{\"key\":\"yes\",\"name\":\"批准\",\"replace_name\":\"已批准\",\"color\":\"blue\",\"is_bold\":true},{\"key\":\"yes\",\"name\":\"拒绝\",\"replace_name\":\"已拒绝\",\"color\":\"red\",\"is_bold\":false}]}}");
}
}

View File

@@ -1,9 +1,11 @@
package me.chanjar.weixin.cp.bean;
import me.chanjar.weixin.common.api.WxConsts;
import org.testng.annotations.*;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
import static me.chanjar.weixin.cp.WxCpConsts.EventType.TASKCARD_CLICK;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
@Test
public class WxCpXmlMessageTest {
@@ -117,4 +119,58 @@ public class WxCpXmlMessageTest {
assertEquals(wxMessage.getSendPicsInfo().getPicList().get(0).getPicMd5Sum(), "aef52ae501537e552725c5d7f99c1741");
assertEquals(wxMessage.getSendPicsInfo().getPicList().get(1).getPicMd5Sum(), "c4564632a4fab91378c39bea6aad6f9e");
}
public void testExtAttr() {
String xml = "<xml>" +
" <ToUserName><![CDATA[w56c9fe3d50ad1ea2]]></ToUserName>" +
" <FromUserName><![CDATA[sys]]></FromUserName>" +
" <CreateTime>1557241961</CreateTime>" +
" <MsgType><![CDATA[event]]></MsgType>" +
" <Event><![CDATA[change_contact]]></Event>" +
" <ChangeType><![CDATA[update_user]]></ChangeType>" +
" <UserID><![CDATA[zhangsan]]></UserID>" +
" <ExtAttr>" +
" <Item><Name><![CDATA[爱好]]></Name><Value><![CDATA[111]]></Value><Text><Value><![CDATA[111]]></Value></Text></Item>" +
" <Item><Name><![CDATA[入职时间]]></Name><Value><![CDATA[11111]]></Value><Text><Value><![CDATA[11111]]></Value></Text></Item>" +
" <Item><Name><![CDATA[城市]]></Name><Value><![CDATA[11111]]></Value><Text><Value><![CDATA[11111]]></Value></Text></Item>" +
" </ExtAttr>" +
" <Address><![CDATA[11111]]></Address>" +
"</xml>";
WxCpXmlMessage wxMessage = WxCpXmlMessage.fromXml(xml);
assertEquals(wxMessage.getToUserName(), "w56c9fe3d50ad1ea2");
assertEquals(wxMessage.getFromUserName(), "sys");
assertEquals(wxMessage.getCreateTime(), new Long(1557241961));
assertEquals(wxMessage.getMsgType(), WxConsts.XmlMsgType.EVENT);
assertEquals(wxMessage.getEvent(), "change_contact");
assertEquals(wxMessage.getChangeType(), "update_user");
assertEquals(wxMessage.getUserId(), "zhangsan");
assertNotNull(wxMessage.getExtAttrs());
assertNotNull(wxMessage.getExtAttrs().getItems());
assertEquals(wxMessage.getExtAttrs().getItems().size(), 3);
assertEquals(wxMessage.getExtAttrs().getItems().get(0).getName(), "爱好");
}
public void testTaskCardEvent() {
String xml = "<xml>" +
"<ToUserName><![CDATA[toUser]]></ToUserName>" +
"<FromUserName><![CDATA[FromUser]]></FromUserName>" +
"<CreateTime>123456789</CreateTime>" +
"<MsgType><![CDATA[event]]></MsgType>" +
"<Event><![CDATA[taskcard_click]]></Event>" +
"<EventKey><![CDATA[key111]]></EventKey>" +
"<TaskId><![CDATA[taskid111]]></TaskId >" +
"<AgentID>1</AgentID>" +
"</xml>";
WxCpXmlMessage wxMessage = WxCpXmlMessage.fromXml(xml);
assertEquals(wxMessage.getToUserName(), "toUser");
assertEquals(wxMessage.getFromUserName(), "FromUser");
assertEquals(wxMessage.getCreateTime(), Long.valueOf(123456789L));
assertEquals(wxMessage.getMsgType(), WxConsts.XmlMsgType.EVENT);
assertEquals(wxMessage.getAgentId(), Integer.valueOf(1));
assertEquals(wxMessage.getEvent(), TASKCARD_CLICK);
assertEquals(wxMessage.getEventKey(), "key111");
assertEquals(wxMessage.getTaskId(), "taskid111");
}
}

View File

@@ -1,28 +1,23 @@
package me.chanjar.weixin.cp.demo;
import java.io.InputStream;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.ToString;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import me.chanjar.weixin.cp.config.WxCpInMemoryConfigStorage;
import java.io.InputStream;
/**
* @author Daniel Qian
*/
@XStreamAlias("xml")
class WxCpDemoInMemoryConfigStorage extends WxCpInMemoryConfigStorage {
@ToString
public class WxCpDemoInMemoryConfigStorage extends WxCpInMemoryConfigStorage {
public static WxCpDemoInMemoryConfigStorage fromXml(InputStream is) {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxCpDemoInMemoryConfigStorage.class);
return (WxCpDemoInMemoryConfigStorage) xstream.fromXML(is);
}
@Override
public String toString() {
return "SimpleWxConfigProvider [appidOrCorpid=" + this.corpId + ", corpSecret=" + this.corpSecret + ", accessToken=" + this.accessToken
+ ", expiresTime=" + this.expiresTime + ", token=" + this.token + ", aesKey=" + this.aesKey + "]";
}
}

View File

@@ -76,6 +76,13 @@ public class WxCpUserGsonAdapterTest {
final WxCpUser user = WxCpUser.fromJson(userJson);
assertThat(user).isNotNull();
assertThat(user.getOrders()).isNotEmpty();
assertThat(user.getOrders().length).isEqualTo(2);
assertThat(user.getOrders()[0]).isEqualTo(1);
assertThat(user.getOrders()[1]).isEqualTo(2);
assertThat(user.getExternalAttrs()).isNotEmpty();
final WxCpUser.ExternalAttribute externalAttr1 = user.getExternalAttrs().get(0);
@@ -100,6 +107,7 @@ public class WxCpUserGsonAdapterTest {
@Test
public void testSerialize() {
WxCpUser user = new WxCpUser();
user.setOrders(new Integer[]{1, 2});
user.addExternalAttr(WxCpUser.ExternalAttribute.builder()
.type(0)
.name("文本名称")
@@ -119,6 +127,10 @@ public class WxCpUserGsonAdapterTest {
.title("my miniprogram")
.build());
assertThat(user.toJson()).isEqualTo("{\"external_profile\":{\"external_attr\":[{\"type\":0,\"name\":\"文本名称\",\"text\":{\"value\":\"文本\"}},{\"type\":1,\"name\":\"网页名称\",\"web\":{\"url\":\"http://www.test.com\",\"title\":\"标题\"}},{\"type\":2,\"name\":\"测试app\",\"miniprogram\":{\"appid\":\"wx8bd80126147df384\",\"pagepath\":\"/index\",\"title\":\"my miniprogram\"}}]}}");
assertThat(user.toJson()).isEqualTo("{\"order\":[1,2],\"external_profile\":{\"external_attr\":" +
"[{\"type\":0,\"name\":\"文本名称\",\"text\":{\"value\":\"文本\"}}," +
"{\"type\":1,\"name\":\"网页名称\",\"web\":{\"url\":\"http://www.test.com\",\"title\":\"标题\"}}," +
"{\"type\":2,\"name\":\"测试app\"," +
"\"miniprogram\":{\"appid\":\"wx8bd80126147df384\",\"pagepath\":\"/index\",\"title\":\"my miniprogram\"}}]}}");
}
}