From 343ed7a70845c07c4e6844c18c0b105ad34ae8f7 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sun, 27 Jan 2019 19:12:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/cp/bean/WxCpAppChatMessage.java | 165 +++++++++--------- .../chanjar/weixin/cp/bean/WxCpMessage.java | 144 +++++++++++++-- .../weixin/cp/util/json/WxCpGsonBuilder.java | 2 - .../cp/util/json/WxCpMessageGsonAdapter.java | 139 --------------- .../chanjar/weixin/cp/bean/WxCpAgentTest.java | 15 +- .../weixin/cp/bean/WxCpMessageTest.java | 32 +++- 6 files changed, 254 insertions(+), 243 deletions(-) delete mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpMessageGsonAdapter.java diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAppChatMessage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAppChatMessage.java index 65a13bad..a3f9a62d 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAppChatMessage.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAppChatMessage.java @@ -9,8 +9,7 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import me.chanjar.weixin.common.api.WxConsts; -import me.chanjar.weixin.cp.WxCpConsts; +import me.chanjar.weixin.cp.WxCpConsts.AppChatMsgType; import me.chanjar.weixin.cp.bean.article.MpnewsArticle; import me.chanjar.weixin.cp.bean.article.NewArticle; @@ -46,7 +45,7 @@ public class WxCpAppChatMessage implements Serializable { */ public static WxCpAppChatMessage buildTextMsg(String chatId, String content, boolean safe) { final WxCpAppChatMessage message = new WxCpAppChatMessage(); - message.setMsgType(WxCpConsts.AppChatMsgType.TEXT); + message.setMsgType(AppChatMsgType.TEXT); message.setContent(content); message.setChatId(chatId); message.setSafe(safe); @@ -61,94 +60,104 @@ public class WxCpAppChatMessage implements Serializable { messageJson.addProperty("msgtype", this.getMsgType()); messageJson.addProperty("chatid", this.getChatId()); - if (WxConsts.KefuMsgType.TEXT.equals(this.getMsgType())) { - JsonObject text = new JsonObject(); - text.addProperty("content", this.getContent()); - messageJson.add("text", text); - } - - if (WxConsts.KefuMsgType.MARKDOWN.equals(this.getMsgType())) { - JsonObject text = new JsonObject(); - text.addProperty("content", this.getContent()); - messageJson.add("markdown", text); - } - - if (WxConsts.KefuMsgType.TEXTCARD.equals(this.getMsgType())) { - JsonObject text = new JsonObject(); - text.addProperty("title", this.getTitle()); - text.addProperty("description", this.getDescription()); - text.addProperty("url", this.getUrl()); - text.addProperty("btntxt", this.getBtnTxt()); - messageJson.add("textcard", text); - } - - if (WxConsts.KefuMsgType.IMAGE.equals(this.getMsgType())) { - JsonObject image = new JsonObject(); - image.addProperty("media_id", this.getMediaId()); - messageJson.add("image", image); - } - - if (WxConsts.KefuMsgType.FILE.equals(this.getMsgType())) { - JsonObject image = new JsonObject(); - image.addProperty("media_id", this.getMediaId()); - messageJson.add("file", image); - } - - if (WxConsts.KefuMsgType.VOICE.equals(this.getMsgType())) { - JsonObject voice = new JsonObject(); - voice.addProperty("media_id", this.getMediaId()); - messageJson.add("voice", voice); - } - if (this.getSafe() != null && this.getSafe()) { messageJson.addProperty("safe", 1); } - if (WxConsts.KefuMsgType.VIDEO.equals(this.getMsgType())) { - JsonObject video = new JsonObject(); - video.addProperty("media_id", this.getMediaId()); - video.addProperty("title", this.getTitle()); - video.addProperty("description", this.getDescription()); - messageJson.add("video", video); - } + this.handleMsgType(messageJson); - if (WxConsts.KefuMsgType.NEWS.equals(this.getMsgType())) { - JsonObject newsJsonObject = new JsonObject(); - JsonArray articleJsonArray = new JsonArray(); - for (NewArticle article : this.getArticles()) { - JsonObject articleJson = new JsonObject(); - articleJson.addProperty("title", article.getTitle()); - articleJson.addProperty("description", article.getDescription()); - articleJson.addProperty("url", article.getUrl()); - articleJson.addProperty("picurl", article.getPicUrl()); - articleJsonArray.add(articleJson); - } - newsJsonObject.add("articles", articleJsonArray); - messageJson.add("news", newsJsonObject); - } + return messageJson.toString(); + } - if (WxConsts.KefuMsgType.MPNEWS.equals(this.getMsgType())) { - JsonObject newsJsonObject = new JsonObject(); - if (this.getMediaId() != null) { - newsJsonObject.addProperty("media_id", this.getMediaId()); - } else { + private void handleMsgType(JsonObject messageJson) { + switch (this.getMsgType()) { + case AppChatMsgType.TEXT: { + JsonObject text = new JsonObject(); + text.addProperty("content", this.getContent()); + messageJson.add("text", text); + break; + } + case AppChatMsgType.MARKDOWN: { + JsonObject text = new JsonObject(); + text.addProperty("content", this.getContent()); + messageJson.add("markdown", text); + break; + } + case AppChatMsgType.TEXTCARD: { + JsonObject text = new JsonObject(); + text.addProperty("title", this.getTitle()); + text.addProperty("description", this.getDescription()); + text.addProperty("url", this.getUrl()); + text.addProperty("btntxt", this.getBtnTxt()); + messageJson.add("textcard", text); + break; + } + case AppChatMsgType.IMAGE: { + JsonObject image = new JsonObject(); + image.addProperty("media_id", this.getMediaId()); + messageJson.add("image", image); + break; + } + case AppChatMsgType.FILE: { + JsonObject image = new JsonObject(); + image.addProperty("media_id", this.getMediaId()); + messageJson.add("file", image); + break; + } + case AppChatMsgType.VOICE: { + JsonObject voice = new JsonObject(); + voice.addProperty("media_id", this.getMediaId()); + messageJson.add("voice", voice); + break; + } + case AppChatMsgType.VIDEO: { + JsonObject video = new JsonObject(); + video.addProperty("media_id", this.getMediaId()); + video.addProperty("title", this.getTitle()); + video.addProperty("description", this.getDescription()); + messageJson.add("video", video); + break; + } + case AppChatMsgType.NEWS: { + JsonObject newsJsonObject = new JsonObject(); JsonArray articleJsonArray = new JsonArray(); - for (MpnewsArticle article : this.getMpnewsArticles()) { + for (NewArticle article : this.getArticles()) { JsonObject articleJson = new JsonObject(); articleJson.addProperty("title", article.getTitle()); - articleJson.addProperty("thumb_media_id", article.getThumbMediaId()); - articleJson.addProperty("author", article.getAuthor()); - articleJson.addProperty("content_source_url", article.getContentSourceUrl()); - articleJson.addProperty("content", article.getContent()); - articleJson.addProperty("digest", article.getDigest()); + articleJson.addProperty("description", article.getDescription()); + articleJson.addProperty("url", article.getUrl()); + articleJson.addProperty("picurl", article.getPicUrl()); articleJsonArray.add(articleJson); } - newsJsonObject.add("articles", articleJsonArray); + messageJson.add("news", newsJsonObject); + break; + } + case AppChatMsgType.MPNEWS: { + JsonObject newsJsonObject = new JsonObject(); + if (this.getMediaId() != null) { + newsJsonObject.addProperty("media_id", this.getMediaId()); + } else { + JsonArray articleJsonArray = new JsonArray(); + for (MpnewsArticle article : this.getMpnewsArticles()) { + JsonObject articleJson = new JsonObject(); + articleJson.addProperty("title", article.getTitle()); + articleJson.addProperty("thumb_media_id", article.getThumbMediaId()); + articleJson.addProperty("author", article.getAuthor()); + articleJson.addProperty("content_source_url", article.getContentSourceUrl()); + articleJson.addProperty("content", article.getContent()); + articleJson.addProperty("digest", article.getDigest()); + articleJsonArray.add(articleJson); + } + + newsJsonObject.add("articles", articleJsonArray); + } + messageJson.add("mpnews", newsJsonObject); + break; + } + default: { + //do nothing } - messageJson.add("mpnews", newsJsonObject); } - - return messageJson.toString(); } } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java index cd455d88..df90775b 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java @@ -4,8 +4,12 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import org.apache.commons.lang3.StringUtils; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import lombok.Data; -import me.chanjar.weixin.common.api.WxConsts; +import me.chanjar.weixin.common.api.WxConsts.KefuMsgType; import me.chanjar.weixin.cp.bean.article.MpnewsArticle; import me.chanjar.weixin.cp.bean.article.NewArticle; import me.chanjar.weixin.cp.bean.messagebuilder.FileBuilder; @@ -17,7 +21,6 @@ import me.chanjar.weixin.cp.bean.messagebuilder.TextBuilder; import me.chanjar.weixin.cp.bean.messagebuilder.TextCardBuilder; import me.chanjar.weixin.cp.bean.messagebuilder.VideoBuilder; import me.chanjar.weixin.cp.bean.messagebuilder.VoiceBuilder; -import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; /** * 消息. @@ -113,14 +116,14 @@ public class WxCpMessage implements Serializable { /** *
    * 请使用
-   * {@link WxConsts.KefuMsgType#TEXT}
-   * {@link WxConsts.KefuMsgType#IMAGE}
-   * {@link WxConsts.KefuMsgType#VOICE}
-   * {@link WxConsts.KefuMsgType#MUSIC}
-   * {@link WxConsts.KefuMsgType#VIDEO}
-   * {@link WxConsts.KefuMsgType#NEWS}
-   * {@link WxConsts.KefuMsgType#MPNEWS}
-   * {@link WxConsts.KefuMsgType#MARKDOWN}
+   * {@link KefuMsgType#TEXT}
+   * {@link KefuMsgType#IMAGE}
+   * {@link KefuMsgType#VOICE}
+   * {@link KefuMsgType#MUSIC}
+   * {@link KefuMsgType#VIDEO}
+   * {@link KefuMsgType#NEWS}
+   * {@link KefuMsgType#MPNEWS}
+   * {@link KefuMsgType#MARKDOWN}
    * 
* * @param msgType 消息类型 @@ -130,7 +133,126 @@ public class WxCpMessage implements Serializable { } public String toJson() { - return WxCpGsonBuilder.create().toJson(this); + JsonObject messageJson = new JsonObject(); + if (this.getAgentId() != null) { + messageJson.addProperty("agentid", this.getAgentId()); + } + + if (StringUtils.isNotBlank(this.getToUser())) { + messageJson.addProperty("touser", this.getToUser()); + } + + messageJson.addProperty("msgtype", this.getMsgType()); + + if (StringUtils.isNotBlank(this.getToParty())) { + messageJson.addProperty("toparty", this.getToParty()); + } + + if (StringUtils.isNotBlank(this.getToTag())) { + messageJson.addProperty("totag", this.getToTag()); + } + + this.handleMsgType(messageJson); + + if (StringUtils.isNotBlank(this.getSafe())) { + messageJson.addProperty("safe", this.getSafe()); + } + + return messageJson.toString(); + } + + private void handleMsgType(JsonObject messageJson) { + switch (this.getMsgType()) { + case KefuMsgType.TEXT: { + JsonObject text = new JsonObject(); + text.addProperty("content", this.getContent()); + messageJson.add("text", text); + break; + } + case KefuMsgType.MARKDOWN: { + JsonObject text = new JsonObject(); + text.addProperty("content", this.getContent()); + messageJson.add("markdown", text); + break; + } + case KefuMsgType.TEXTCARD: { + JsonObject text = new JsonObject(); + text.addProperty("title", this.getTitle()); + text.addProperty("description", this.getDescription()); + text.addProperty("url", this.getUrl()); + text.addProperty("btntxt", this.getBtnTxt()); + messageJson.add("textcard", text); + break; + } + case KefuMsgType.IMAGE: { + JsonObject image = new JsonObject(); + image.addProperty("media_id", this.getMediaId()); + messageJson.add("image", image); + break; + } + case KefuMsgType.FILE: { + JsonObject image = new JsonObject(); + image.addProperty("media_id", this.getMediaId()); + messageJson.add("file", image); + break; + } + case KefuMsgType.VOICE: { + JsonObject voice = new JsonObject(); + voice.addProperty("media_id", this.getMediaId()); + messageJson.add("voice", voice); + break; + } + case KefuMsgType.VIDEO: { + JsonObject video = new JsonObject(); + video.addProperty("media_id", this.getMediaId()); + video.addProperty("thumb_media_id", this.getThumbMediaId()); + video.addProperty("title", this.getTitle()); + video.addProperty("description", this.getDescription()); + messageJson.add("video", video); + break; + } + case KefuMsgType.NEWS: { + JsonObject newsJsonObject = new JsonObject(); + JsonArray articleJsonArray = new JsonArray(); + for (NewArticle article : this.getArticles()) { + JsonObject articleJson = new JsonObject(); + articleJson.addProperty("title", article.getTitle()); + articleJson.addProperty("description", article.getDescription()); + articleJson.addProperty("url", article.getUrl()); + articleJson.addProperty("picurl", article.getPicUrl()); + articleJsonArray.add(articleJson); + } + newsJsonObject.add("articles", articleJsonArray); + messageJson.add("news", newsJsonObject); + break; + } + case KefuMsgType.MPNEWS: { + JsonObject newsJsonObject = new JsonObject(); + if (this.getMediaId() != null) { + newsJsonObject.addProperty("media_id", this.getMediaId()); + } else { + JsonArray articleJsonArray = new JsonArray(); + for (MpnewsArticle article : this.getMpnewsArticles()) { + JsonObject articleJson = new JsonObject(); + articleJson.addProperty("title", article.getTitle()); + articleJson.addProperty("thumb_media_id", article.getThumbMediaId()); + articleJson.addProperty("author", article.getAuthor()); + articleJson.addProperty("content_source_url", article.getContentSourceUrl()); + articleJson.addProperty("content", article.getContent()); + articleJson.addProperty("digest", article.getDigest()); + articleJson.addProperty("show_cover_pic", article.getShowCoverPic()); + articleJsonArray.add(articleJson); + } + + newsJsonObject.add("articles", articleJsonArray); + } + messageJson.add("mpnews", newsJsonObject); + break; + } + default: { + // do nothing + } + } } } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpGsonBuilder.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpGsonBuilder.java index 654d5511..16f0108e 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpGsonBuilder.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpGsonBuilder.java @@ -7,7 +7,6 @@ import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.util.json.WxErrorAdapter; import me.chanjar.weixin.cp.bean.WxCpChat; import me.chanjar.weixin.cp.bean.WxCpDepart; -import me.chanjar.weixin.cp.bean.WxCpMessage; import me.chanjar.weixin.cp.bean.WxCpTag; import me.chanjar.weixin.cp.bean.WxCpUser; @@ -20,7 +19,6 @@ public class WxCpGsonBuilder { static { INSTANCE.disableHtmlEscaping(); - INSTANCE.registerTypeAdapter(WxCpMessage.class, new WxCpMessageGsonAdapter()); INSTANCE.registerTypeAdapter(WxCpChat.class, new WxCpChatGsonAdapter()); INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter()); INSTANCE.registerTypeAdapter(WxCpUser.class, new WxCpUserGsonAdapter()); diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpMessageGsonAdapter.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpMessageGsonAdapter.java deleted file mode 100644 index fbf70bb5..00000000 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpMessageGsonAdapter.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved. - * - * This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended - * only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction - * arose from modification of the original source, or other redistribution of this source - * is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD. - */ -package me.chanjar.weixin.cp.util.json; - -import java.lang.reflect.Type; - -import org.apache.commons.lang3.StringUtils; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; -import me.chanjar.weixin.common.api.WxConsts; -import me.chanjar.weixin.cp.bean.WxCpMessage; -import me.chanjar.weixin.cp.bean.article.MpnewsArticle; -import me.chanjar.weixin.cp.bean.article.NewArticle; - -/** - * @author Daniel Qian - */ -public class WxCpMessageGsonAdapter implements JsonSerializer { - - @Override - public JsonElement serialize(WxCpMessage message, Type typeOfSrc, JsonSerializationContext context) { - JsonObject messageJson = new JsonObject(); - messageJson.addProperty("agentid", message.getAgentId()); - if (StringUtils.isNotBlank(message.getToUser())) { - messageJson.addProperty("touser", message.getToUser()); - } - messageJson.addProperty("msgtype", message.getMsgType()); - - if (StringUtils.isNotBlank(message.getToParty())) { - messageJson.addProperty("toparty", message.getToParty()); - } - if (StringUtils.isNotBlank(message.getToTag())) { - messageJson.addProperty("totag", message.getToTag()); - } - - if (WxConsts.KefuMsgType.TEXT.equals(message.getMsgType())) { - JsonObject text = new JsonObject(); - text.addProperty("content", message.getContent()); - messageJson.add("text", text); - } - - if (WxConsts.KefuMsgType.MARKDOWN.equals(message.getMsgType())) { - JsonObject text = new JsonObject(); - text.addProperty("content", message.getContent()); - messageJson.add("markdown", text); - } - - if (WxConsts.KefuMsgType.TEXTCARD.equals(message.getMsgType())) { - JsonObject text = new JsonObject(); - text.addProperty("title", message.getTitle()); - text.addProperty("description", message.getDescription()); - text.addProperty("url", message.getUrl()); - text.addProperty("btntxt", message.getBtnTxt()); - messageJson.add("textcard", text); - } - - if (WxConsts.KefuMsgType.IMAGE.equals(message.getMsgType())) { - JsonObject image = new JsonObject(); - image.addProperty("media_id", message.getMediaId()); - messageJson.add("image", image); - } - - if (WxConsts.KefuMsgType.FILE.equals(message.getMsgType())) { - JsonObject image = new JsonObject(); - image.addProperty("media_id", message.getMediaId()); - messageJson.add("file", image); - } - - if (WxConsts.KefuMsgType.VOICE.equals(message.getMsgType())) { - JsonObject voice = new JsonObject(); - voice.addProperty("media_id", message.getMediaId()); - messageJson.add("voice", voice); - } - - if (StringUtils.isNotBlank(message.getSafe())) { - messageJson.addProperty("safe", message.getSafe()); - } - - if (WxConsts.KefuMsgType.VIDEO.equals(message.getMsgType())) { - JsonObject video = new JsonObject(); - video.addProperty("media_id", message.getMediaId()); - video.addProperty("thumb_media_id", message.getThumbMediaId()); - video.addProperty("title", message.getTitle()); - video.addProperty("description", message.getDescription()); - messageJson.add("video", video); - } - - if (WxConsts.KefuMsgType.NEWS.equals(message.getMsgType())) { - JsonObject newsJsonObject = new JsonObject(); - JsonArray articleJsonArray = new JsonArray(); - for (NewArticle article : message.getArticles()) { - JsonObject articleJson = new JsonObject(); - articleJson.addProperty("title", article.getTitle()); - articleJson.addProperty("description", article.getDescription()); - articleJson.addProperty("url", article.getUrl()); - articleJson.addProperty("picurl", article.getPicUrl()); - articleJsonArray.add(articleJson); - } - newsJsonObject.add("articles", articleJsonArray); - messageJson.add("news", newsJsonObject); - } - - if (WxConsts.KefuMsgType.MPNEWS.equals(message.getMsgType())) { - JsonObject newsJsonObject = new JsonObject(); - if (message.getMediaId() != null) { - newsJsonObject.addProperty("media_id", message.getMediaId()); - } else { - JsonArray articleJsonArray = new JsonArray(); - for (MpnewsArticle article : message.getMpnewsArticles()) { - JsonObject articleJson = new JsonObject(); - articleJson.addProperty("title", article.getTitle()); - articleJson.addProperty("thumb_media_id", article.getThumbMediaId()); - articleJson.addProperty("author", article.getAuthor()); - articleJson.addProperty("content_source_url", article.getContentSourceUrl()); - articleJson.addProperty("content", article.getContent()); - articleJson.addProperty("digest", article.getDigest()); - articleJson.addProperty("show_cover_pic", article.getShowCoverPic()); - articleJsonArray.add(articleJson); - } - - newsJsonObject.add("articles", articleJsonArray); - } - messageJson.add("mpnews", newsJsonObject); - } - - return messageJson; - } - -} diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/WxCpAgentTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/WxCpAgentTest.java index 74b8269d..9f187e43 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/WxCpAgentTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/WxCpAgentTest.java @@ -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()); } diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/WxCpMessageTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/WxCpMessageTest.java index 0af121a8..344d1e82 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/WxCpMessageTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/WxCpMessageTest.java @@ -1,11 +1,11 @@ package me.chanjar.weixin.cp.bean; +import org.testng.annotations.*; + import me.chanjar.weixin.cp.bean.article.MpnewsArticle; import me.chanjar.weixin.cp.bean.article.NewArticle; -import org.testng.annotations.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.testng.Assert.assertEquals; @Test public class WxCpMessageTest { @@ -19,12 +19,16 @@ public class WxCpMessageTest { public void testTextCardBuild() { WxCpMessage reply = WxCpMessage.TEXTCARD().toUser("OPENID") .title("领奖通知") - .description("
2016年9月26日
恭喜你抽中iPhone 7一台,领奖码:xxxx
请于2016年10月10日前联系行政同事领取
") + .description("
2016年9月26日
恭喜你抽中iPhone 7一台," + + "领奖码:xxxx
请于2016年10月10日前联系行政同事领取
") .url("http://www.qq.com") .btnTxt("更多") .build(); assertThat(reply.toJson()) - .isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"textcard\",\"textcard\":{\"title\":\"领奖通知\",\"description\":\"
2016年9月26日
恭喜你抽中iPhone 7一台,领奖码:xxxx
请于2016年10月10日前联系行政同事领取
\",\"url\":\"http://www.qq.com\",\"btntxt\":\"更多\"},\"safe\":\"0\"}"); + .isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"textcard\",\"textcard\":{\"title\":\"领奖通知\"," + + "\"description\":\"
2016年9月26日
" + + "恭喜你抽中iPhone 7一台,领奖码:xxxx
请于2016年10月10日前联系行政同事领取
\"," + + "\"url\":\"http://www.qq.com\",\"btntxt\":\"更多\"},\"safe\":\"0\"}"); } public void testImageBuild() { @@ -40,9 +44,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 +67,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 +97,19 @@ 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\"}"); } }