WxCustomMessageTest.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package me.chanjar.weixin.enterprise.bean;
  2. import me.chanjar.weixin.enterprise.bean.WxCustomMessage;
  3. import org.testng.Assert;
  4. import org.testng.annotations.Test;
  5. import me.chanjar.weixin.enterprise.api.WxConsts;
  6. import me.chanjar.weixin.enterprise.bean.WxCustomMessage.WxArticle;
  7. @Test
  8. public class WxCustomMessageTest {
  9. public void testTextReply() {
  10. WxCustomMessage reply = new WxCustomMessage();
  11. reply.setToUser("OPENID");
  12. reply.setMsgType(WxConsts.CUSTOM_MSG_TEXT);
  13. reply.setContent("sfsfdsdf");
  14. Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
  15. }
  16. public void testTextBuild() {
  17. WxCustomMessage reply = WxCustomMessage.TEXT().toUser("OPENID").content("sfsfdsdf").build();
  18. Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"text\",\"text\":{\"content\":\"sfsfdsdf\"}}");
  19. }
  20. public void testImageReply() {
  21. WxCustomMessage reply = new WxCustomMessage();
  22. reply.setToUser("OPENID");
  23. reply.setMsgType(WxConsts.CUSTOM_MSG_IMAGE);
  24. reply.setMediaId("MEDIA_ID");
  25. Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
  26. }
  27. public void testImageBuild() {
  28. WxCustomMessage reply = WxCustomMessage.IMAGE().toUser("OPENID").mediaId("MEDIA_ID").build();
  29. Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"MEDIA_ID\"}}");
  30. }
  31. public void testVoiceReply() {
  32. WxCustomMessage reply = new WxCustomMessage();
  33. reply.setToUser("OPENID");
  34. reply.setMsgType(WxConsts.CUSTOM_MSG_VOICE);
  35. reply.setMediaId("MEDIA_ID");
  36. Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
  37. }
  38. public void testVoiceBuild() {
  39. WxCustomMessage reply = WxCustomMessage.VOICE().toUser("OPENID").mediaId("MEDIA_ID").build();
  40. Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"MEDIA_ID\"}}");
  41. }
  42. public void testVideoReply() {
  43. WxCustomMessage reply = new WxCustomMessage();
  44. reply.setToUser("OPENID");
  45. reply.setMsgType(WxConsts.CUSTOM_MSG_VIDEO);
  46. reply.setMediaId("MEDIA_ID");
  47. reply.setThumbMediaId("MEDIA_ID");
  48. reply.setTitle("TITLE");
  49. reply.setDescription("DESCRIPTION");
  50. Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
  51. }
  52. public void testVideoBuild() {
  53. WxCustomMessage reply = WxCustomMessage.VIDEO().toUser("OPENID").title("TITLE").mediaId("MEDIA_ID").thumbMediaId("MEDIA_ID").description("DESCRIPTION").build();
  54. Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
  55. }
  56. public void testMusicReply() {
  57. WxCustomMessage reply = new WxCustomMessage();
  58. reply.setToUser("OPENID");
  59. reply.setMsgType(WxConsts.CUSTOM_MSG_MUSIC);
  60. reply.setThumbMediaId("MEDIA_ID");
  61. reply.setDescription("DESCRIPTION");
  62. reply.setTitle("TITLE");
  63. reply.setMusicUrl("MUSIC_URL");
  64. reply.setHqMusicUrl("HQ_MUSIC_URL");
  65. Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"music\",\"music\":{\"title\":\"TITLE\",\"description\":\"DESCRIPTION\",\"thumb_media_id\":\"MEDIA_ID\",\"musicurl\":\"MUSIC_URL\",\"hqmusicurl\":\"HQ_MUSIC_URL\"}}");
  66. }
  67. public void testMusicBuild() {
  68. WxCustomMessage reply = WxCustomMessage.MUSIC()
  69. .toUser("OPENID")
  70. .title("TITLE")
  71. .thumbMediaId("MEDIA_ID")
  72. .description("DESCRIPTION")
  73. .musicUrl("MUSIC_URL")
  74. .hqMusicUrl("HQ_MUSIC_URL")
  75. .build();
  76. Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"music\",\"music\":{\"title\":\"TITLE\",\"description\":\"DESCRIPTION\",\"thumb_media_id\":\"MEDIA_ID\",\"musicurl\":\"MUSIC_URL\",\"hqmusicurl\":\"HQ_MUSIC_URL\"}}");
  77. }
  78. public void testNewsReply() {
  79. WxCustomMessage reply = new WxCustomMessage();
  80. reply.setToUser("OPENID");
  81. reply.setMsgType(WxConsts.CUSTOM_MSG_NEWS);
  82. WxArticle article1 = new WxArticle();
  83. article1.setUrl("URL");
  84. article1.setPicUrl("PIC_URL");
  85. article1.setDescription("Is Really A Happy Day");
  86. article1.setTitle("Happy Day");
  87. reply.getArticles().add(article1);
  88. WxArticle article2 = new WxArticle();
  89. article2.setUrl("URL");
  90. article2.setPicUrl("PIC_URL");
  91. article2.setDescription("Is Really A Happy Day");
  92. article2.setTitle("Happy Day");
  93. reply.getArticles().add(article2);
  94. Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"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\"}]}");
  95. }
  96. public void testNewsBuild() {
  97. WxArticle article1 = new WxArticle();
  98. article1.setUrl("URL");
  99. article1.setPicUrl("PIC_URL");
  100. article1.setDescription("Is Really A Happy Day");
  101. article1.setTitle("Happy Day");
  102. WxArticle article2 = new WxArticle();
  103. article2.setUrl("URL");
  104. article2.setPicUrl("PIC_URL");
  105. article2.setDescription("Is Really A Happy Day");
  106. article2.setTitle("Happy Day");
  107. WxCustomMessage reply = WxCustomMessage.NEWS().toUser("OPENID").addArticle(article1).addArticle(article2).build();
  108. Assert.assertEquals(reply.toJson(), "{\"touser\":\"OPENID\",\"msgtype\":\"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\"}]}");
  109. }
  110. }