/// <summary> /// /// </summary> /// <param name="log"></param> /// <param name="testId"></param> /// <param name="pathA"></param> /// <param name="pathB"></param> /// <param name="errorCounter"></param> public static void CompareXml(ITestLog log, string testId, string pathA, string pathB, Counter errorCounter) { log.LogInfo($" CompareXml: pathA={pathA}", testId); log.LogInfo($" CompareXml: pathB={pathB}", testId); try { // load original into xml doc var docA = new XmlDocument(); using (var fs = new FileStream(pathA, FileMode.Open, FileAccess.Read)) using (var sr = new StreamReader(fs, Encoding.UTF8)) { docA.Load(sr); } // load emitted stream into xml doc var docB = new XmlDocument(); using (var stream = new FileStream(pathB, FileMode.Open, FileAccess.Read)) { docB.Load(stream); } // compare XmlCompare.CompareXmlDocs(docA, docB); } catch (Exception excp) { errorCounter.Inc(); log.LogException(excp, testId, false); } }
private T Roundtrip_OutThenIn <T>(T input, bool compare, bool validate) { string originalPath = Path.GetFullPath(@"..\..\step0original.xml"); string internalPath = Path.GetFullPath(@"..\..\step1external.xml"); string externalPath = Path.GetFullPath(@"..\..\step2internal.xml"); IXmlTransformer transformIncoming = new CustomXmlTransformer(FpMLViewHelpers.GetIncomingConversionMap()); IXmlTransformer transformOutgoing = new CustomXmlTransformer(FpMLViewHelpers.GetOutgoingConversionMap()); // emit original internal var xs = new XmlSerializer(typeof(T)); using (var fs = new FileStream(originalPath, FileMode.Create, FileAccess.Write)) { xs.Serialize(fs, input); } // transform to external format transformOutgoing.Transform(originalPath, externalPath); // transform to internal format transformIncoming.Transform(externalPath, internalPath); if (compare) { // compare // load original into xml doc var docA = new XmlDocument(); using (var fs = new FileStream(originalPath, FileMode.Open, FileAccess.Read)) //using (var sr = new StreamReader(fs, Encoding.UTF8)) { docA.Load(fs); } // load emitted stream into xml doc var docB = new XmlDocument(); using (var fs = new FileStream(internalPath, FileMode.Open, FileAccess.Read)) { docB.Load(fs); } // compare XmlCompare.CompareXmlDocs(docA, docB); } // validate external if (validate) { const string schemaPath = @"..\..\..\..\..\Metadata\FpML.V5r3\FpML.V5r3\" + fpmlViewName + ".xsd"; string schemaFullPath = Path.GetFullPath(schemaPath); Assert.IsTrue(File.Exists(schemaFullPath)); // validate external xml int validationEvents = 0; var externalDoc = new XmlDocument(); externalDoc.Schemas.Add(null, "file://" + schemaFullPath); externalDoc.Load(externalPath); externalDoc.Validate((o, e) => { validationEvents++; System.Diagnostics.Debug.Print("Validation event: {0}", e.Message); }); Assert.AreEqual(0, validationEvents); } // deserialise using (TextReader tr = new StreamReader(internalPath)) { return((T)xs.Deserialize(tr)); } }
public void TestReportingRoundtripDateTimes() { const string schemaPath = @"..\..\..\..\..\Metadata\FpML.V5r10\FpML.V5r10.Reporting\MergedReportingSchemas\Reporting.xsd"; string schemaFullPath = Path.GetFullPath(schemaPath); Assert.IsTrue(File.Exists(schemaFullPath)); string originalFullPath = Path.GetFullPath(@"..\..\testOriginalFragment.xml"); string internalFullPath = Path.GetFullPath(@"..\..\testInternalFragment.xml"); string outgoingFullPath = Path.GetFullPath(@"..\..\testOutgoingFragment.xml"); string externalFullPath = Path.GetFullPath(@"..\..\testExternalFragment.xml"); IXmlTransformer transformIncoming = new CustomXmlTransformer(FpMLViewHelpers.GetIncomingConversionMap(), OutputDateTimeKind.UnspecifiedOrUniversal, null); IXmlTransformer transformOutgoing = new CustomXmlTransformer(FpMLViewHelpers.GetOutgoingConversionMap(), OutputDateTimeKind.UnspecifiedOrCustom, TimeSpan.FromHours(-5)); XmlSerializer xs = new XmlSerializer(typeof(ExposureReport)); // original FpML.org sample string originalFpMLText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<exposureReport" + " xmlns=\"http://www.fpml.org/FpML-5/reporting\"" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + " fpmlVersion=\"5-10\" >" + " <asOfDate>2010-09-27</asOfDate>" + " <asOfTime>20:08:10-05:00</asOfTime>" + "</exposureReport>"; using (var sw = new StreamWriter(originalFullPath)) { sw.WriteLine(originalFpMLText); } DateTime expectedAsOfDate = new DateTime(2010, 9, 27, 0, 0, 0); DateTime expectedAsOfTime = new DateTime(1, 1, 1, 1, 8, 10);//11 became 12 { // original FpML.org sample // --- does not parse reliably due to variably local time zone offset of parser --- // asOfDate has an unspecified time zone // asOfTime has specific time zone (New York) ExposureReport orig; using (StringReader sr = new StringReader(originalFpMLText)) { orig = (ExposureReport)xs.Deserialize(sr); } Assert.AreEqual(DateTimeKind.Unspecified, orig.asOfDate.Value.Kind); Assert.IsTrue(orig.asOfTimeSpecified); Assert.AreEqual(DateTimeKind.Local, orig.asOfTime.Kind); // --- this test does not pass reliably due to variable local time zone offset of parser --- DateTime asOfDate = new DateTime(2010, 9, 27); Assert.AreEqual(asOfDate, orig.asOfDate.Value); DateTime asOfTime = new DateTime(1, 1, 2, 11, 8, 10);//TODO the time changed due to daylight saving??Why? Assert.AreEqual(asOfTime, orig.asOfTime); } { // modified FpML.org sample // --- this test does not pass reliably due to variable local time zone offset of parser --- // - asOfDate time zone modified to match asOfTime string externalXmlText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<exposureReport" + " xmlns=\"http://www.fpml.org/FpML-5/reporting\"" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + " fpmlVersion=\"5-10\" >" + " <asOfDate>2010-09-27-5:00</asOfDate>" + " <asOfTime>20:08:10-05:00</asOfTime>" + "</exposureReport>"; ExposureReport orig; using (StringReader sr = new StringReader(externalXmlText)) { orig = (ExposureReport)xs.Deserialize(sr); } Assert.AreEqual(DateTimeKind.Local, orig.asOfDate.Value.Kind); Assert.IsTrue(orig.asOfTimeSpecified); Assert.AreEqual(DateTimeKind.Local, orig.asOfTime.Kind); // --- this test does not pass reliably due to variable local time zone offset of parser --- DateTime asOfDate = new DateTime(2010, 9, 27, 15, 0, 0); // Australia EST is +10:00 Assert.AreEqual(asOfDate, orig.asOfDate.Value); DateTime asOfTime = new DateTime(1, 1, 2, 11, 8, 10); //11 became 12 Assert.AreEqual(asOfTime, orig.asOfTime); } { // modified FpML.org sample // - asOfDate and asOfTime set to UTC string externalXmlText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<exposureReport" + " xmlns=\"http://www.fpml.org/FpML-5/reporting\"" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + " fpmlVersion=\"5-10\" >" + " <asOfDate>2010-09-27Z</asOfDate>" + " <asOfTime>20:08:10Z</asOfTime>" + "</exposureReport>"; ExposureReport orig; using (StringReader sr = new StringReader(externalXmlText)) { orig = (ExposureReport)xs.Deserialize(sr); } Assert.AreEqual(DateTimeKind.Local, orig.asOfDate.Value.Kind); Assert.IsTrue(orig.asOfTimeSpecified); Assert.AreEqual(DateTimeKind.Utc, orig.asOfTime.Kind); //Local // --- this test does not pass reliably due to variable local time zone offset of parser --- DateTime asOfDate = new DateTime(2010, 9, 27, 10, 0, 0); // Australia EST is +10:00 Assert.AreEqual(asOfDate, orig.asOfDate.Value); DateTime asOfTime = new DateTime(1, 1, 1, 20, 8, 10); //6 became 7 Assert.AreEqual(asOfTime, orig.asOfTime); } { // modified FpML.org sample // --- passes reliably because local time zone offset of parser is ignored --- // - asOfDate and asOfTime set to unspecified time zone string externalXmlText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<exposureReport" + " xmlns=\"http://www.fpml.org/FpML-5/reporting\"" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + " fpmlVersion=\"5-10\" >" + " <asOfDate>2010-09-27</asOfDate>" + " <asOfTime>20:08:10</asOfTime>" + "</exposureReport>"; ExposureReport orig; using (StringReader sr = new StringReader(externalXmlText)) { orig = (ExposureReport)xs.Deserialize(sr); } Assert.AreEqual(DateTimeKind.Unspecified, orig.asOfDate.Value.Kind); Assert.IsTrue(orig.asOfTimeSpecified); Assert.AreEqual(DateTimeKind.Unspecified, orig.asOfTime.Kind); Assert.AreEqual(new DateTime(2010, 9, 27, 0, 0, 0), orig.asOfDate.Value); Assert.AreEqual(new DateTime(1, 1, 1, 20, 8, 10), orig.asOfTime); } // now that we have a reliably formatted external fpml fragment we can test the roundtrip // transform to internal format transformIncoming.Transform(originalFullPath, internalFullPath); // deserialise ExposureReport fpml; using (var sr = new FileStream(internalFullPath, FileMode.Open, FileAccess.Read)) { fpml = (ExposureReport)xs.Deserialize(sr); } Assert.AreEqual(DateTimeKind.Unspecified, fpml.asOfDate.Value.Kind); Assert.IsTrue(fpml.asOfTimeSpecified); Assert.AreEqual(DateTimeKind.Utc, fpml.asOfTime.Kind);//Local Assert.AreEqual(expectedAsOfDate, fpml.asOfDate.Value); Assert.AreEqual(expectedAsOfTime, fpml.asOfTime); // emit test fragment using (var fs = new FileStream(outgoingFullPath, FileMode.Create, FileAccess.Write)) { xs.Serialize(fs, fpml); } // transform to external format transformOutgoing.Transform(outgoingFullPath, externalFullPath); // compare external xml using (var originalStream = new FileStream(originalFullPath, FileMode.Open, FileAccess.Read)) using (var externalStream = new FileStream(externalFullPath, FileMode.Open, FileAccess.Read)) { XmlDocument originalDoc = new XmlDocument(); originalDoc.Load(originalStream); XmlDocument emittedDoc = new XmlDocument(); emittedDoc.Load(externalStream); // compare XmlCompare.CompareXmlDocs(originalDoc, emittedDoc); } }