diff --git a/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlParserTests.cs b/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlParserTests.cs index 64035e30..55e78fba 100644 --- a/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlParserTests.cs +++ b/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlParserTests.cs @@ -6,17 +6,84 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Moq; +using SafeExamBrowser.Configuration.DataFormats; +using SafeExamBrowser.Contracts.Configuration; +using SafeExamBrowser.Contracts.Configuration.DataFormats; +using SafeExamBrowser.Contracts.Logging; namespace SafeExamBrowser.Configuration.UnitTests.DataFormats { [TestClass] public class XmlParserTests { + private Mock logger; + private XmlParser sut; + [TestInitialize] public void Initialize() { + logger = new Mock(); + sut = new XmlParser(logger.Object); + } + + [TestMethod] + public void MustOnlyParseXmlData() + { + Assert.IsFalse(sut.CanParse(null)); + Assert.IsFalse(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("someKey1")))); + Assert.IsFalse(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("")))); + Assert.IsTrue(sut.CanParse(new MemoryStream(Encoding.UTF8.GetBytes("")))); + } + + [TestMethod] + public void MustCorrectlyParseXml() + { + var data = LoadTestData(); + var result = sut.TryParse(data); + + Assert.AreEqual(LoadStatus.Success, result.Status); + Assert.AreEqual(FormatType.Xml, result.Format); + Assert.IsNull(result.Encryption); + Assert.IsNotNull(result.RawData); + + Assert.AreEqual("test123", result.RawData["someString"]); + Assert.AreEqual(9876, result.RawData["someInteger"]); + Assert.IsTrue(Convert.FromBase64String("/5vlF76sb+5vgkhjiNTOn7l1SN3Ho2UAMJD3TtLo49M=").SequenceEqual(result.RawData["someData"] as IEnumerable)); + Assert.AreEqual(true, result.RawData["someBoolean"]); + Assert.AreEqual(12.34, result.RawData["someReal"]); + Assert.AreEqual(new DateTime(2019, 02, 20, 12, 30, 00, 123), result.RawData["someDate"]); + + var array = result.RawData["anArray"] as List; + var dictOne = array[0] as Dictionary; + var dictTwo = array[1] as Dictionary; + var dictThree = array[2] as Dictionary; + + Assert.AreEqual(3, dictOne["dictOneKeyOne"]); + Assert.AreEqual(4, dictOne["dictOneKeyTwo"]); + Assert.AreEqual(5, dictTwo["dictTwoKeyOne"]); + Assert.AreEqual(6, dictTwo["dictTwoKeyTwo"]); + Assert.AreEqual(7, dictThree["dictThreeKeyOne"]); + Assert.AreEqual(1, (dictThree["dictThreeKeyTwo"] as List)[0]); + Assert.AreEqual(2, (dictThree["dictThreeKeyTwo"] as List)[1]); + Assert.AreEqual(3, (dictThree["dictThreeKeyTwo"] as List)[2]); + Assert.AreEqual(4, (dictThree["dictThreeKeyTwo"] as List)[3]); + } + + private Stream LoadTestData() + { + var path = $"{nameof(SafeExamBrowser)}.{nameof(Configuration)}.{nameof(UnitTests)}.{nameof(DataFormats)}.XmlTestData.xml"; + var data = Assembly.GetAssembly(GetType()).GetManifestResourceStream(path); + + return data; } } } diff --git a/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlTestData.xml b/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlTestData.xml new file mode 100644 index 00000000..97098a4d --- /dev/null +++ b/SafeExamBrowser.Configuration.UnitTests/DataFormats/XmlTestData.xml @@ -0,0 +1,43 @@ + + + + someString + test123 + someInteger + 9876 + someData + /5vlF76sb+5vgkhjiNTOn7l1SN3Ho2UAMJD3TtLo49M= + anArray + + + dictOneKeyOne + 3 + dictOneKeyTwo + 4 + + + dictTwoKeyOne + 5 + dictTwoKeyTwo + 6 + + + dictThreeKeyOne + 7 + dictThreeKeyTwo + + 1 + 2 + 3 + 4 + + + + someBoolean + + someReal + 12.34 + someDate + 2019-02-20T14:30:00.1230000+02:00 + + \ No newline at end of file diff --git a/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj b/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj index 735d5689..39778e07 100644 --- a/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj +++ b/SafeExamBrowser.Configuration.UnitTests/SafeExamBrowser.Configuration.UnitTests.csproj @@ -110,6 +110,9 @@ SafeExamBrowser.Contracts + + + diff --git a/SafeExamBrowser.Configuration/DataFormats/XmlParser.cs b/SafeExamBrowser.Configuration/DataFormats/XmlParser.cs index 434c96c5..96873661 100644 --- a/SafeExamBrowser.Configuration/DataFormats/XmlParser.cs +++ b/SafeExamBrowser.Configuration/DataFormats/XmlParser.cs @@ -54,7 +54,7 @@ namespace SafeExamBrowser.Configuration.DataFormats } catch (Exception e) { - logger.Error($"Failed to determine whether '{data}' with {data.Length / 1000.0} KB data matches the {FormatType.Xml} format!", e); + logger.Error($"Failed to determine whether '{data}' with {data?.Length / 1000.0} KB data matches the {FormatType.Xml} format!", e); } return false;