|
| 1 | +package me.chanjar.weixin.cp.bean.intelligentrobot; |
| 2 | + |
| 3 | +import org.testng.annotations.Test; |
| 4 | + |
| 5 | +import static org.testng.Assert.assertEquals; |
| 6 | +import static org.testng.Assert.assertNotNull; |
| 7 | +import static org.testng.Assert.assertNull; |
| 8 | + |
| 9 | +/** |
| 10 | + * 智能机器人回调消息测试. |
| 11 | + */ |
| 12 | +public class WxCpIntelligentRobotMessageTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + public void testFromJsonWithTextMessage() { |
| 16 | + String json = "{" |
| 17 | + + "\"msgid\":\"msg_1\"," |
| 18 | + + "\"aibotid\":\"bot_1\"," |
| 19 | + + "\"chatid\":\"chat_1\"," |
| 20 | + + "\"chattype\":\"group\"," |
| 21 | + + "\"from\":{\"userid\":\"zhangsan\"}," |
| 22 | + + "\"response_url\":\"https://example.com/reply\"," |
| 23 | + + "\"msgtype\":\"text\"," |
| 24 | + + "\"text\":{\"content\":\"@robot hello\"}" |
| 25 | + + "}"; |
| 26 | + |
| 27 | + WxCpIntelligentRobotMessage message = WxCpIntelligentRobotMessage.fromJson(json); |
| 28 | + assertEquals(message.getMsgId(), "msg_1"); |
| 29 | + assertEquals(message.getAiBotId(), "bot_1"); |
| 30 | + assertEquals(message.getChatId(), "chat_1"); |
| 31 | + assertEquals(message.getChatType(), "group"); |
| 32 | + assertNotNull(message.getFrom()); |
| 33 | + assertEquals(message.getFrom().getUserid(), "zhangsan"); |
| 34 | + assertEquals(message.getResponseUrl(), "https://example.com/reply"); |
| 35 | + assertEquals(message.getMsgType(), "text"); |
| 36 | + assertNotNull(message.getText()); |
| 37 | + assertEquals(message.getText().getContent(), "@robot hello"); |
| 38 | + assertNull(message.getMixed()); |
| 39 | + assertNull(message.getStream()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testFromJsonWithMixedAndQuote() { |
| 44 | + String json = "{" |
| 45 | + + "\"msgid\":\"msg_2\"," |
| 46 | + + "\"aibotid\":\"bot_2\"," |
| 47 | + + "\"chattype\":\"single\"," |
| 48 | + + "\"from\":{\"userid\":\"lisi\"}," |
| 49 | + + "\"msgtype\":\"mixed\"," |
| 50 | + + "\"mixed\":{\"msg_item\":[" |
| 51 | + + "{\"msgtype\":\"text\",\"text\":{\"content\":\"hello\"}}," |
| 52 | + + "{\"msgtype\":\"image\",\"image\":{\"url\":\"https://example.com/1.png\"}}" |
| 53 | + + "]}," |
| 54 | + + "\"quote\":{\"msgtype\":\"text\",\"text\":{\"content\":\"quoted\"}}" |
| 55 | + + "}"; |
| 56 | + |
| 57 | + WxCpIntelligentRobotMessage message = WxCpIntelligentRobotMessage.fromJson(json); |
| 58 | + assertEquals(message.getMsgType(), "mixed"); |
| 59 | + assertNotNull(message.getMixed()); |
| 60 | + assertNotNull(message.getMixed().getMsgItem()); |
| 61 | + assertEquals(message.getMixed().getMsgItem().size(), 2); |
| 62 | + assertEquals(message.getMixed().getMsgItem().get(0).getMsgType(), "text"); |
| 63 | + assertEquals(message.getMixed().getMsgItem().get(0).getText().getContent(), "hello"); |
| 64 | + assertEquals(message.getMixed().getMsgItem().get(1).getMsgType(), "image"); |
| 65 | + assertEquals(message.getMixed().getMsgItem().get(1).getImage().getUrl(), "https://example.com/1.png"); |
| 66 | + assertNotNull(message.getQuote()); |
| 67 | + assertEquals(message.getQuote().getMsgType(), "text"); |
| 68 | + assertEquals(message.getQuote().getText().getContent(), "quoted"); |
| 69 | + |
| 70 | + String serialized = message.toJson(); |
| 71 | + WxCpIntelligentRobotMessage deserialized = WxCpIntelligentRobotMessage.fromJson(serialized); |
| 72 | + assertEquals(deserialized.getAiBotId(), "bot_2"); |
| 73 | + assertEquals(deserialized.getFrom().getUserid(), "lisi"); |
| 74 | + assertEquals(deserialized.getMixed().getMsgItem().size(), 2); |
| 75 | + } |
| 76 | +} |
0 commit comments