diff --git a/src/SolidifyProject.Engine.Infrastructure/Models/CustomDataModel.cs b/src/SolidifyProject.Engine.Infrastructure/Models/CustomDataModel.cs index a78c619..fb4f063 100644 --- a/src/SolidifyProject.Engine.Infrastructure/Models/CustomDataModel.cs +++ b/src/SolidifyProject.Engine.Infrastructure/Models/CustomDataModel.cs @@ -2,7 +2,9 @@ using System.Collections.Generic; using System.Dynamic; using System.IO; +using System.Xml.Linq; using CsvHelper; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SolidifyProject.Engine.Infrastructure.Enums; using SolidifyProject.Engine.Infrastructure.Models.Base; @@ -100,7 +102,10 @@ private void ParseJson() private void ParseXml() { - throw new NotImplementedException(); + //hack to convert xml document to dynamic object + XDocument doc = XDocument.Parse(ContentRaw); + string jsonText = JsonConvert.SerializeXNode(doc); + CustomData = JObject.Parse(jsonText); } private void ParseYaml() diff --git a/src/Test/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs b/src/Test/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs new file mode 100644 index 0000000..677dbb1 --- /dev/null +++ b/src/Test/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs @@ -0,0 +1,127 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using NUnit.Framework; +using SolidifyProject.Engine.Infrastructure.Enums; + +namespace SolidifyProject.Engine.Test.Unit.Infrastructure.Models.CustomDataModel.Parse +{ + [TestFixture] + public class XmlCustomDataModelTest + { + [Test] + public void ParseXmlOnlyWithAttributes() + { + var model = new Engine.Infrastructure.Models.CustomDataModel(); + model.Id = "file.xml"; + model.ContentRaw = @" + + + "; + + model.Parse(); + + Assert.NotNull(model.DataType); + Assert.AreEqual(CustomDataType.Xml.ToString(), model.DataType.ToString()); + + var data = (dynamic) model.CustomData; + Assert.IsNotNull(data); + Assert.AreEqual(2, data.root.links.Count); + + Assert.AreEqual("facebook", (string)data.root.links[0]["@name"]); + Assert.AreEqual("https://facebook.com", (string)data.root.links[0]["@url"]); + + Assert.AreEqual("twitter", (string)data.root.links[1]["@name"]); + Assert.AreEqual("https://twitter.com", (string)data.root.links[1]["@url"]); + } + + [Test] + public void ParseXmlOnlyWithFields() + { + var model = new Engine.Infrastructure.Models.CustomDataModel(); + model.Id = "file.xml"; + model.ContentRaw = @" + + facebook + https://facebook.com + + + twitter + https://twitter.com + + "; + + model.Parse(); + + Assert.NotNull(model.DataType); + Assert.AreEqual(CustomDataType.Xml.ToString(), model.DataType.ToString()); + + var data = (dynamic) model.CustomData; + Assert.IsNotNull(data); + Assert.AreEqual(2, data.root.links.Count); + + Assert.AreEqual("facebook", (string)data.root.links[0].name); + Assert.AreEqual("https://facebook.com", (string)data.root.links[0].url); + + Assert.AreEqual("twitter", (string)data.root.links[1].name); + Assert.AreEqual("https://twitter.com", (string)data.root.links[1].url); + } + + [Test] + public void ParseXmlWithFieldAndAttribute() + { + var model = new Engine.Infrastructure.Models.CustomDataModel(); + model.Id = "file.xml"; + model.ContentRaw = @" + + https://facebook.com + + + https://twitter.com + + "; + + model.Parse(); + + Assert.NotNull(model.DataType); + Assert.AreEqual(CustomDataType.Xml.ToString(), model.DataType.ToString()); + + var data = (dynamic) model.CustomData; + Assert.IsNotNull(data); + Assert.AreEqual(2, data.root.links.Count); + + Assert.AreEqual("facebook", (string)data.root.links[0]["@name"]); + Assert.AreEqual("https://facebook.com", (string)data.root.links[0].url); + + Assert.AreEqual("twitter", (string)data.root.links[1]["@name"]); + Assert.AreEqual("https://twitter.com", (string)data.root.links[1].url); + } + + [Test] + public void ParseXmlWithAttributeAndValue() + { + var model = new Engine.Infrastructure.Models.CustomDataModel(); + model.Id = "file.xml"; + model.ContentRaw = @" + https://facebook.com + https://twitter.com + "; + + model.Parse(); + + Assert.NotNull(model.DataType); + Assert.AreEqual(CustomDataType.Xml.ToString(), model.DataType.ToString()); + + var data = (dynamic) model.CustomData; + Assert.IsNotNull(data); + Assert.AreEqual(2, data.root.links.Count); + + Assert.AreEqual("facebook", (string)data.root.links[0]["@name"]); + Assert.AreEqual("https://facebook.com", (string)data.root.links[0]["#text"]); + + Assert.AreEqual("twitter", (string)data.root.links[1]["@name"]); + Assert.AreEqual("https://twitter.com", (string)data.root.links[1]["#text"]); + } + + } +} \ No newline at end of file