#region Apache License Version 2.0 /*---------------------------------------------------------------- Copyright 2023 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md ----------------------------------------------------------------*/ #endregion Apache License Version 2.0 using System; using System.Linq; using System.Xml.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace Senparc.Weixin.MP.Test { using Senparc.NeuChar; using Senparc.NeuChar.Entities; using Senparc.Weixin.MP.Entities; using Senparc.NeuChar.Helpers; [TestClass] public class EntityHelperTest { string xml = @" 1357986928 5832509444155992350 "; [TestMethod] public void FillEntityWithXmlTest() { var doc = XDocument.Parse(xml); var entity = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultMpMessageContext(), doc); EntityHelper.FillEntityWithXml(entity as RequestMessageBase, doc); Assert.AreEqual("gh_a96a4a619366", entity.ToUserName); Assert.AreEqual(RequestMsgType.Text, entity.MsgType); } #region 可为空对象测试 class NullableClass : ResponseMessageBase, IResponseMessageBase { public int Id { get; set; } public string Name { get; set; } public int? AgentId { get; set; } public int? MchId { get; set; } public string ToUserName { get; set; } public string FromUserName { get; set; } public DateTimeOffset CreateTime { get; set; } public ResponseMsgType MsgType { get; } } [TestMethod] public void ConvertEntityToXml_NullableTest() { var nullableTestXml = @" 123 "; var doc = XDocument.Parse(nullableTestXml); var entity = new NullableClass(); EntityHelper.FillEntityWithXml(entity as ResponseMessageBase, doc); Assert.AreEqual(10, entity.Id); Assert.AreEqual("Jeffrey Su", entity.Name); Assert.AreEqual(null, entity.AgentId); Assert.AreEqual(123, entity.MchId); } #endregion #region 多层嵌套Entity测试 private string embedXml = @" 1481013459 1000001625 0 0 0 0 2 1 0 2 1 1 1 1 2 0 2 1 1 1 1 2 2 1 2 "; /// /// 测试多层复杂结构XML /// [TestMethod] public void FillEntityWithEmbedXmlTest() { var doc = XDocument.Parse(embedXml); var entity = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultMpMessageContext(), doc); EntityHelper.FillEntityWithXml(entity as RequestMessageBase, doc); Assert.AreEqual("gh_4d00ed8d6399", entity.ToUserName); Assert.AreEqual(RequestMsgType.Event, entity.MsgType); var strongEntity = entity as RequestMessageEvent_MassSendJobFinish; Assert.IsNotNull(strongEntity); Assert.AreEqual(2, strongEntity.CopyrightCheckResult.Count); Assert.AreEqual("Url_1", strongEntity.CopyrightCheckResult.ResultList[0].item.OriginalArticleUrl); Assert.AreEqual("Url_2", strongEntity.CopyrightCheckResult.ResultList[1].item.OriginalArticleUrl); Assert.AreEqual(2, strongEntity.ArticleUrlResult.Count); Assert.AreEqual(2, strongEntity.ArticleUrlResult.ResultList[1].item.ArticleIdx); Assert.AreEqual("Url_1", strongEntity.ArticleUrlResult.ResultList[0].item.ArticleUrl); Assert.AreEqual("Url_2", strongEntity.ArticleUrlResult.ResultList[1].item.ArticleUrl); } #endregion [TestMethod] public void ConvertEntityToXmlTest() { var doc = XDocument.Parse(xml); var requestEntity = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultMpMessageContext(), doc); { //Text var responseText = ResponseMessageBase.CreateFromRequestMessage(requestEntity); Assert.IsNotNull(responseText); responseText.Content = "新内容"; var responseDoc = EntityHelper.ConvertEntityToXml(responseText); Console.WriteLine(responseDoc.ToString()); Assert.AreEqual("新内容", responseDoc.Root.Element("Content").Value); } { //News var responseNews = ResponseMessageBase.CreateFromRequestMessage(requestEntity); Assert.IsNotNull(responseNews); responseNews.Articles.Add(new Article() { Description = "测试说明", Title = "测试标题", Url = "http://www.senparc.com", PicUrl = "http://img.senparc.com/images/v2/logo.jpg'" }); Assert.AreEqual(1, responseNews.ArticleCount); var responseDoc = EntityHelper.ConvertEntityToXml(responseNews); Console.WriteLine(responseDoc.ToString()); } } [TestMethod] public void ConvertEntityToXml_ImageTest() { { var imageRequestXML = @" 1357996976 5832552599987382826 "; var doc = XDocument.Parse(imageRequestXML); var requestEntity = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultMpMessageContext(), doc) as RequestMessageImage; Assert.IsNotNull(requestEntity); //var responseNews = // ResponseMessageBase.CreateFromRequestMessage(requestEntity, ResponseMsgType.News) as // ResponseMessageNews; //Assert.IsNotNull(responseNews); //responseNews.Articles.Add(new Article() //{ // Description = "测试说明", // Title = "测试标题", // Url = "http://www.senparc.com", // PicUrl = requestEntity.PicUrl //}); //Assert.AreEqual(1, responseNews.ArticleCount); //var responseDoc = EntityHelper.ConvertEntityToXml(responseNews); //Console.WriteLine(responseDoc.ToString()); //Assert.AreEqual(requestEntity.PicUrl, responseDoc.Root.Element("Articles").Elements("item").First().Element("PicUrl").Value); //返回图片信息 var responseMessage = ResponseMessageBase.CreateFromRequestMessage(requestEntity); responseMessage.Image.MediaId = requestEntity.MediaId; var responseDoc = EntityHelper.ConvertEntityToXml(responseMessage); Assert.IsNotNull(responseDoc); } // { // var imageResponseXML = @" // // // //1392354263 // // //5980116024231210973 // //"; // } } [TestMethod] public void ConvertEntityToXml_MusicTest() { var voiceTest = @" 1361430302 5847298622973403529 "; var doc = XDocument.Parse(voiceTest); var requestEntity = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultMpMessageContext(), doc) as RequestMessageVoice; Assert.IsNotNull(requestEntity); var responseMusic = ResponseMessageBase.CreateFromRequestMessage(requestEntity); Assert.IsNotNull(responseMusic); responseMusic.Music.Title = "测试Music"; responseMusic.Music.Description = "测试Music的说明"; responseMusic.Music.MusicUrl = "https://sdk.weixin.senparc.com/Content/music1.mp3"; responseMusic.Music.HQMusicUrl = "https://sdk.weixin.senparc.com/Content/music2.mp3"; var responseDoc = EntityHelper.ConvertEntityToXml(responseMusic); Console.WriteLine(responseDoc.ToString()); Assert.AreEqual(responseMusic.Music.Title, responseDoc.Root.Element("Music").Element("Title").Value); Assert.AreEqual(responseMusic.Music.Description, responseDoc.Root.Element("Music").Element("Description").Value); Assert.AreEqual(responseMusic.Music.MusicUrl, responseDoc.Root.Element("Music").Element("MusicUrl").Value); Assert.AreEqual(responseMusic.Music.HQMusicUrl, responseDoc.Root.Element("Music").Element("HQMusicUrl").Value); } [TestMethod] public void ConvertEntityToXml_subscribe_msg_popup_eventTest() { var xml = @" 1610969440 2 2 "; var doc = XDocument.Parse(xml); var requestEntity = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultMpMessageContext(), doc) as RequestMessageEvent_Subscribe_Msg_Popup; Assert.IsNotNull(requestEntity); Assert.IsTrue(requestEntity.Event.ToString() == "subscribe_msg_popup_event"); Assert.IsTrue(requestEntity.FromUserName == "otFpruAK8D-E6EfStSYonYSBZ8_4"); Assert.IsTrue(requestEntity.SubscribeMsgPopupEvent.Count == 2); Assert.IsTrue(requestEntity.SubscribeMsgPopupEvent[1].TemplateId == "9nLIlbOQZC5Y89AZteFEux3WCXRRRG5Wfzkpssu4bLI"); Assert.IsTrue(requestEntity.SubscribeMsgPopupEvent[1].SubscribeStatusString == "reject"); Assert.IsTrue(requestEntity.SubscribeMsgPopupEvent[1].PopupScene == 2); } [TestMethod] public void ConvertEntityToXml_subscribe_msg_change_eventTest() { var xml = @" 1610969440 "; var doc = XDocument.Parse(xml); var requestEntity = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultMpMessageContext(), doc) as RequestMessageEvent_Subscribe_Msg_Change; Assert.IsNotNull(requestEntity); Assert.IsTrue(requestEntity.Event.ToString() == "subscribe_msg_change_event"); Assert.IsTrue(requestEntity.FromUserName == "otFpruAK8D-E6EfStSYonYSBZ8_4"); Assert.IsTrue(requestEntity.SubscribeMsgChangeEvent.Count == 1); Assert.IsTrue(requestEntity.SubscribeMsgChangeEvent[0].TemplateId == "VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc"); Assert.IsTrue(requestEntity.SubscribeMsgChangeEvent[0].SubscribeStatusString == "reject"); } [TestMethod] public void ConvertEntityToXml_subscribe_msg_sent_eventTest() { var xml = @" 1610969468 1700827132819554304 0 "; var doc = XDocument.Parse(xml); var requestEntity = RequestMessageFactory.GetRequestEntity(new MessageContexts.DefaultMpMessageContext(), doc) as RequestMessageEvent_Subscribe_Msg_Sent; Assert.IsNotNull(requestEntity); Assert.IsTrue(requestEntity.Event.ToString() == "subscribe_msg_sent_event"); Assert.IsTrue(requestEntity.FromUserName == "otFpruAK8D-E6EfStSYonYSBZ8_4"); Assert.IsTrue(requestEntity.SubscribeMsgSentEvent.Count == 1); Assert.IsTrue(requestEntity.SubscribeMsgSentEvent[0].TemplateId == "VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc"); Assert.IsTrue(requestEntity.SubscribeMsgSentEvent[0].MsgID == "1700827132819554304"); Assert.IsTrue(requestEntity.SubscribeMsgSentEvent[0].ErrorCode == 0); Assert.IsTrue(requestEntity.SubscribeMsgSentEvent[0].ErrorStatus == "success"); } } }