QyXmlMessages { public static XmlMessageHeader fromXml(String xml) { try { XmlMessageHeader xmlRequest = XmlObjectMapper.defaultMapper().fromXml(xml, XmlMessageHeader.class); switch (xmlRequest.getMsgType()) { case text: return XmlObjectMapper.defaultMapper().fromXml(xml, QyTextRequest.class); case image: return XmlObjectMapper.defaultMapper().fromXml(xml, QyImageRequest.class); case voice: return XmlObjectMapper.defaultMapper().fromXml(xml, QyVoiceRequest.class); case video: return XmlObjectMapper.defaultMapper().fromXml(xml, QyVideoRequest.class); case shortvideo: return XmlObjectMapper.defaultMapper().fromXml(xml, QyShortVideoRequest.class); case location: return XmlObjectMapper.defaultMapper().fromXml(xml, QyLocationRequest.class); case event: return toEvent(xml); default: logger.warn("xml to bean failed, unknown message type {}.", xmlRequest.getMsgType()); throw new WxRuntimeException(999, "xml to bean failed, unknown message type " + xmlRequest.getMsgType()); } } catch (IOException e) { logger.error("xml to message request failed", e); throw new WxRuntimeException(999, "xml to message request failed," + e.getMessage()); } }  static XmlMessageHeader fromXml(String xml); static String toXml(XmlMessageHeader xmlMessage);  }
@Test public void testXmlToText() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/text.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyTextRequest); Assert.assertEquals("this is a test", ((QyTextRequest) xmlRequest).getContent()); }
@Test public void testXmlToImage() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/image.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyImageRequest); Assert.assertEquals("media_id", ((QyImageRequest) xmlRequest).getMediaId()); Assert.assertEquals(1, ((QyImageRequest) xmlRequest).getAgentId()); }
@Test public void testXmlToVoice() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/voice.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyVoiceRequest); Assert.assertEquals("media_id", ((QyVoiceRequest) xmlRequest).getMediaId()); Assert.assertEquals("Format", ((QyVoiceRequest) xmlRequest).getFormat()); }
@Test public void testXmlToVideo() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/video.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyVideoRequest); Assert.assertEquals("media_id", ((QyVideoRequest) xmlRequest).getMediaId()); Assert.assertEquals("thumb_media_id", ((QyVideoRequest) xmlRequest).getThumbMediaId()); }
@Test public void testXmlToShortVideo() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/shortvideo.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyShortVideoRequest); Assert.assertEquals("media_id", ((QyShortVideoRequest) xmlRequest).getMediaId()); Assert.assertEquals("thumb_media_id", ((QyShortVideoRequest) xmlRequest).getThumbMediaId()); }
@Test public void testXmlToLocation() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/location.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyLocationRequest); Assert.assertEquals("23.134521", ((QyLocationRequest) xmlRequest).getX()); Assert.assertEquals("113.358803", ((QyLocationRequest) xmlRequest).getY()); Assert.assertEquals("20", ((QyLocationRequest) xmlRequest).getScale()); Assert.assertEquals("位置信息", ((QyLocationRequest) xmlRequest).getLabel()); }
@Test public void testXmlToSubscribeEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/subscribe.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QySubscriptionEvent); Assert.assertTrue(((QySubscriptionEvent) xmlRequest).isSub()); }
@Test public void testXmlToUnSubscribeEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/unsubscribe.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QySubscriptionEvent); Assert.assertFalse(((QySubscriptionEvent) xmlRequest).isSub()); }
@Test public void testXmlToLocationReportEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/location-report.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyLocationReportEvent); Assert.assertEquals("23.104105", ((QyLocationReportEvent) xmlRequest).getLatitude()); Assert.assertEquals("113.320107", ((QyLocationReportEvent) xmlRequest).getLongitude()); Assert.assertEquals("65.000000", ((QyLocationReportEvent) xmlRequest).getPrecision()); }
@Test public void testXmlToClickEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/click.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyClickEvent); Assert.assertEquals("EVENTKEY", ((QyClickEvent) xmlRequest).getEventKey()); }
@Test public void testXmlToViewEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/view.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyViewEvent); Assert.assertEquals("www.qq.com", ((QyViewEvent) xmlRequest).getUrl()); }
@Test public void testXmlToScanPushEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/scan-push.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyScanEvent); Assert.assertEquals("qrcode", ((QyScanEvent) xmlRequest).getScanCodeInfo().getScanType()); Assert.assertEquals("1", ((QyScanEvent) xmlRequest).getScanCodeInfo().getScanResult()); }
@Test public void testXmlToScanWaitMsgEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/scan-waitmsg.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyScanEvent); Assert.assertEquals("qrcode", ((QyScanEvent) xmlRequest).getScanCodeInfo().getScanType()); Assert.assertEquals("2", ((QyScanEvent) xmlRequest).getScanCodeInfo().getScanResult()); }
@Test public void testXmlToSystemPhotoEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/sys-photo.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyPhotoEvent); Assert.assertEquals(1, ((QyPhotoEvent) xmlRequest).getSendPicsInfo().getCount()); Assert.assertEquals("1b5f7c23b5bf75682a53e7b6d163e185", ((QyPhotoEvent) xmlRequest).getSendPicsInfo().getItems().get(0).getPicMd5Sum()); }
@Test public void testXmlToSystemPhotoOrAlbumEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/sys-photo-or-album.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyPhotoEvent); Assert.assertEquals(1, ((QyPhotoEvent) xmlRequest).getSendPicsInfo().getCount()); Assert.assertEquals("5a75aaca956d97be686719218f275c6b", ((QyPhotoEvent) xmlRequest).getSendPicsInfo().getItems().get(0).getPicMd5Sum()); }
@Test public void testXmlToAlbumEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/album.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyPhotoEvent); Assert.assertEquals(1, ((QyPhotoEvent) xmlRequest).getSendPicsInfo().getCount()); Assert.assertEquals("5a75aaca956d97be686719218f275c6b", ((QyPhotoEvent) xmlRequest).getSendPicsInfo().getItems().get(0).getPicMd5Sum()); }
@Test public void testXmlToLocationSelectEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/location-select.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof QyLocationSelectEvent); Assert.assertEquals("6", ((QyLocationSelectEvent) xmlRequest).getEventKey()); Assert.assertEquals("23", ((QyLocationSelectEvent) xmlRequest).getSendLocationInfo().getX()); Assert.assertEquals("113", ((QyLocationSelectEvent) xmlRequest).getSendLocationInfo().getY()); Assert.assertEquals("15", ((QyLocationSelectEvent) xmlRequest).getSendLocationInfo().getScale()); Assert.assertEquals(" 广州市海珠区客村艺苑路 106号", ((QyLocationSelectEvent) xmlRequest).getSendLocationInfo().getLabel()); Assert.assertEquals("", ((QyLocationSelectEvent) xmlRequest).getSendLocationInfo().getPoiName()); }
@Test public void testXmlToEnterAgentEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/enter-agent.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof EnterAgentEvent); Assert.assertEquals("enter_agent", ((EnterAgentEvent) xmlRequest).getEventType().name()); }
@Test public void testXmlToBatchJobEvent() throws IOException { String xml = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("message/event/batch-job.xml")); XmlMessageHeader xmlRequest = QyXmlMessages.fromXml(xml); Assert.assertNotNull(xmlRequest); Assert.assertTrue(xmlRequest instanceof BatchJobResultEvent); Assert.assertEquals("S0MrnndvRG5fadSlLwiBqiDDbM143UqTmKP3152FZk4", ((BatchJobResultEvent) xmlRequest).getBatchJob().getJobId()); Assert.assertEquals("sync_user", ((BatchJobResultEvent) xmlRequest).getBatchJob().getJobType().name()); Assert.assertEquals("0", ((BatchJobResultEvent) xmlRequest).getBatchJob().getErrorCode()); Assert.assertEquals("ok", ((BatchJobResultEvent) xmlRequest).getBatchJob().getErrorMessage()); }