From 49dc21c7271ba761fa9a8eea1f89bbb2aa72b02c Mon Sep 17 00:00:00 2001 From: L0ndra Date: Fri, 18 May 2018 15:09:16 +0300 Subject: [PATCH 1/6] Introduce support for XML data format --- .../Models/CustomDataModel.cs | 6 ++- .../Parse/XmlCustomDataModelTest.cs | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs diff --git a/src/SolidifyProject.Engine.Infrastructure/Models/CustomDataModel.cs b/src/SolidifyProject.Engine.Infrastructure/Models/CustomDataModel.cs index a78c619..bb2ed22 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,9 @@ private void ParseJson() private void ParseXml() { - throw new NotImplementedException(); + XDocument doc = XDocument.Parse(ContentRaw); + string jsonText = JsonConvert.SerializeXNode(doc); + CustomData = JObject.Parse(jsonText); } private void ParseYaml() diff --git a/src/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs b/src/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs new file mode 100644 index 0000000..6306b00 --- /dev/null +++ b/src/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs @@ -0,0 +1,43 @@ +using NUnit.Framework; +using SolidifyProject.Engine.Infrastructure.Enums; + +namespace SolidifyProject.Engine.Test.Unit.Infrastructure.Models.CustomDataModel.Parse +{ + [TestFixture] + public class XmlCustomDataModelTest + { + private string _xml = @" + + facebook + https://facebook.com + + + twitter + https://twitter.com + + "; + + [Test] + public void ParseXml() + { + var model = new Engine.Infrastructure.Models.CustomDataModel(); + model.Id = "file.xml"; + model.ContentRaw = _xml; + + 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); + } + } +} \ No newline at end of file From db2ef56d496278b8216c0d623efde4e83fec9702 Mon Sep 17 00:00:00 2001 From: L0ndra Date: Fri, 18 May 2018 15:12:20 +0300 Subject: [PATCH 2/6] add comments about ParseXml method --- .../Models/CustomDataModel.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/SolidifyProject.Engine.Infrastructure/Models/CustomDataModel.cs b/src/SolidifyProject.Engine.Infrastructure/Models/CustomDataModel.cs index bb2ed22..fb4f063 100644 --- a/src/SolidifyProject.Engine.Infrastructure/Models/CustomDataModel.cs +++ b/src/SolidifyProject.Engine.Infrastructure/Models/CustomDataModel.cs @@ -102,6 +102,7 @@ private void ParseJson() private void ParseXml() { + //hack to convert xml document to dynamic object XDocument doc = XDocument.Parse(ContentRaw); string jsonText = JsonConvert.SerializeXNode(doc); CustomData = JObject.Parse(jsonText); From cec84aa0e2e94ab18e08fdb5b3be2bbf838b14b9 Mon Sep 17 00:00:00 2001 From: L0ndra Date: Thu, 7 Jun 2018 23:45:55 +0300 Subject: [PATCH 3/6] move test file to right place add more test --- .../Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{ => Test}/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs (100%) diff --git a/src/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs b/src/Test/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs similarity index 100% rename from src/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs rename to src/Test/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/CustomDataModel/Parse/XmlCustomDataModelTest.cs From c133009040bf86493bd643ff1794f41cf770efe7 Mon Sep 17 00:00:00 2001 From: L0ndra Date: Thu, 7 Jun 2018 23:46:13 +0300 Subject: [PATCH 4/6] add more tests --- .../Parse/XmlCustomDataModelTest.cs | 81 +++++++++++++++---- 1 file changed, 64 insertions(+), 17 deletions(-) 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 index 6306b00..fa69edb 100644 --- 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 @@ -1,4 +1,7 @@ -using NUnit.Framework; +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 @@ -6,23 +9,66 @@ namespace SolidifyProject.Engine.Test.Unit.Infrastructure.Models.CustomDataModel [TestFixture] public class XmlCustomDataModelTest { - private string _xml = @" - - facebook - https://facebook.com - - - twitter - https://twitter.com - - "; + private static IEnumerable<(string, Func, Func)> _xmlSources + { + get + { + yield return ( + @" + + facebook + https://facebook.com + + + twitter + https://twitter.com + + ", + (data, indx) => data.root.links[indx].name, + (data, indx) => data.root.links[indx].url + ); + yield return ( + @" + + + ", + (data, indx) => data.root.links[indx]["@name"], + (data, indx) => data.root.links[indx]["@url"] + ); + yield return ( + @" + + https://facebook.com + + + https://twitter.com + + ", + (data, indx) => data.root.links[indx]["@name"], + (data, indx) => data.root.links[indx].url + ); + yield return ( + @" + https://facebook.com + https://twitter.com + ", + (data, indx) => data.root.links[indx]["@name"], + (data, indx) => data.root.links[indx]["#text"] + ); + + } + + } + + [Test] - public void ParseXml() + [TestCaseSource(nameof(_xmlSources))] + public void ParseXml((string xml, Func nameGetter, Func urlGetter) tuple) { var model = new Engine.Infrastructure.Models.CustomDataModel(); model.Id = "file.xml"; - model.ContentRaw = _xml; + model.ContentRaw = tuple.xml; model.Parse(); @@ -33,11 +79,12 @@ public void ParseXml() 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("facebook", tuple.nameGetter(data, 0)); + Assert.AreEqual("https://facebook.com", tuple.urlGetter(data, 0)); - Assert.AreEqual("twitter", (string)data.root.links[1].name); - Assert.AreEqual("https://twitter.com", (string)data.root.links[1].url); + Assert.AreEqual("twitter", tuple.nameGetter(data, 1)); + Assert.AreEqual("https://twitter.com", tuple.urlGetter(data, 1)); } + } } \ No newline at end of file From 3f19b39775328a3cafba0efc1eab6249ca3088f8 Mon Sep 17 00:00:00 2001 From: L0ndra Date: Fri, 8 Jun 2018 21:44:39 +0300 Subject: [PATCH 5/6] transform tests to easy form --- .../Parse/XmlCustomDataModelTest.cs | 167 +++++++++++------- 1 file changed, 102 insertions(+), 65 deletions(-) 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 index fa69edb..45d8c34 100644 --- 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 @@ -6,15 +6,41 @@ namespace SolidifyProject.Engine.Test.Unit.Infrastructure.Models.CustomDataModel.Parse { - [TestFixture] - public class XmlCustomDataModelTest - { - private static IEnumerable<(string, Func, Func)> _xmlSources - { - get - { - yield return ( - @" + [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", data.root.links[0]["@name"]); + Assert.AreEqual("https://facebook.com", data.root.links[0]["@url"]); + + Assert.AreEqual("twitter", data.root.links[1]["@name"]); + Assert.AreEqual("https://twitter.com", 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 @@ -23,68 +49,79 @@ public class XmlCustomDataModelTest twitter https://twitter.com - ", - (data, indx) => data.root.links[indx].name, - (data, indx) => data.root.links[indx].url - ); - yield return ( - @" - - - ", - (data, indx) => data.root.links[indx]["@name"], - (data, indx) => data.root.links[indx]["@url"] - ); - yield return ( - @" + "; + + 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", data.root.links[0].name); + Assert.AreEqual("https://facebook.com", data.root.links[0].url); + + Assert.AreEqual("twitter", data.root.links[1].name); + Assert.AreEqual("https://twitter.com", 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 - ", - (data, indx) => data.root.links[indx]["@name"], - (data, indx) => data.root.links[indx].url - ); - yield return ( - @" + "; + + 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", data.root.links[0]["@name"]); + Assert.AreEqual("https://facebook.com", data.root.links[0].url); + + Assert.AreEqual("twitter", data.root.links[1]["@name"]); + Assert.AreEqual("https://twitter.com", 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 - ", - (data, indx) => data.root.links[indx]["@name"], - (data, indx) => data.root.links[indx]["#text"] - ); - - } - - } - - - - [Test] - [TestCaseSource(nameof(_xmlSources))] - public void ParseXml((string xml, Func nameGetter, Func urlGetter) tuple) - { - var model = new Engine.Infrastructure.Models.CustomDataModel(); - model.Id = "file.xml"; - model.ContentRaw = tuple.xml; - - 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", tuple.nameGetter(data, 0)); - Assert.AreEqual("https://facebook.com", tuple.urlGetter(data, 0)); - - Assert.AreEqual("twitter", tuple.nameGetter(data, 1)); - Assert.AreEqual("https://twitter.com", tuple.urlGetter(data, 1)); - } - - } + "; + + 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", data.root.links[0]["@name"]); + Assert.AreEqual("https://facebook.com", data.root.links[0]["#text"]); + + Assert.AreEqual("twitter", data.root.links[1]["@name"]); + Assert.AreEqual("https://twitter.com", data.root.links[1]["#text"]); + } + + } } \ No newline at end of file From f3985fc39384094487ea97084d44065927a827d7 Mon Sep 17 00:00:00 2001 From: L0ndra Date: Sat, 9 Jun 2018 13:48:25 +0300 Subject: [PATCH 6/6] add cast to string before check value --- .../Parse/XmlCustomDataModelTest.cs | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) 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 index 45d8c34..677dbb1 100644 --- 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 @@ -28,11 +28,11 @@ public void ParseXmlOnlyWithAttributes() Assert.IsNotNull(data); Assert.AreEqual(2, data.root.links.Count); - Assert.AreEqual("facebook", data.root.links[0]["@name"]); - Assert.AreEqual("https://facebook.com", data.root.links[0]["@url"]); + Assert.AreEqual("facebook", (string)data.root.links[0]["@name"]); + Assert.AreEqual("https://facebook.com", (string)data.root.links[0]["@url"]); - Assert.AreEqual("twitter", data.root.links[1]["@name"]); - Assert.AreEqual("https://twitter.com", data.root.links[1]["@url"]); + Assert.AreEqual("twitter", (string)data.root.links[1]["@name"]); + Assert.AreEqual("https://twitter.com", (string)data.root.links[1]["@url"]); } [Test] @@ -60,11 +60,11 @@ public void ParseXmlOnlyWithFields() Assert.IsNotNull(data); Assert.AreEqual(2, data.root.links.Count); - Assert.AreEqual("facebook", data.root.links[0].name); - Assert.AreEqual("https://facebook.com", data.root.links[0].url); + Assert.AreEqual("facebook", (string)data.root.links[0].name); + Assert.AreEqual("https://facebook.com", (string)data.root.links[0].url); - Assert.AreEqual("twitter", data.root.links[1].name); - Assert.AreEqual("https://twitter.com", data.root.links[1].url); + Assert.AreEqual("twitter", (string)data.root.links[1].name); + Assert.AreEqual("https://twitter.com", (string)data.root.links[1].url); } [Test] @@ -90,11 +90,11 @@ public void ParseXmlWithFieldAndAttribute() Assert.IsNotNull(data); Assert.AreEqual(2, data.root.links.Count); - Assert.AreEqual("facebook", data.root.links[0]["@name"]); - Assert.AreEqual("https://facebook.com", data.root.links[0].url); + Assert.AreEqual("facebook", (string)data.root.links[0]["@name"]); + Assert.AreEqual("https://facebook.com", (string)data.root.links[0].url); - Assert.AreEqual("twitter", data.root.links[1]["@name"]); - Assert.AreEqual("https://twitter.com", data.root.links[1].url); + Assert.AreEqual("twitter", (string)data.root.links[1]["@name"]); + Assert.AreEqual("https://twitter.com", (string)data.root.links[1].url); } [Test] @@ -116,11 +116,11 @@ public void ParseXmlWithAttributeAndValue() Assert.IsNotNull(data); Assert.AreEqual(2, data.root.links.Count); - Assert.AreEqual("facebook", data.root.links[0]["@name"]); - Assert.AreEqual("https://facebook.com", data.root.links[0]["#text"]); + Assert.AreEqual("facebook", (string)data.root.links[0]["@name"]); + Assert.AreEqual("https://facebook.com", (string)data.root.links[0]["#text"]); - Assert.AreEqual("twitter", data.root.links[1]["@name"]); - Assert.AreEqual("https://twitter.com", data.root.links[1]["#text"]); + Assert.AreEqual("twitter", (string)data.root.links[1]["@name"]); + Assert.AreEqual("https://twitter.com", (string)data.root.links[1]["#text"]); } }