From ad26ed4bfec7d684bb72b8d196444b16fb5276ae Mon Sep 17 00:00:00 2001 From: Richard FAVREAU Date: Sun, 11 Sep 2022 19:34:17 +0200 Subject: [PATCH] Add the header to the build file --- OFX-Tool.Library/Core/Build.cs | 53 +++++++++++++++++++++--------- OFX-Tool.Library/Core/Load.cs | 2 +- OFX-Tool.Library/Entities/Ofx.cs | 9 +++-- OFX-Tool.Test/Core/BuildTests.cs | 16 +++++---- OFX-Tool.Test/Entities/OfxTests.cs | 2 +- 5 files changed, 56 insertions(+), 26 deletions(-) diff --git a/OFX-Tool.Library/Core/Build.cs b/OFX-Tool.Library/Core/Build.cs index 320cd77..3f9666c 100644 --- a/OFX-Tool.Library/Core/Build.cs +++ b/OFX-Tool.Library/Core/Build.cs @@ -1,5 +1,6 @@ using RFD.OFXTool.Library.Core.Elements; using RFD.OFXTool.Library.Entities; +using System.Text; namespace RFD.OFXTool.Library.Core { @@ -13,34 +14,56 @@ internal Build(Ofx ofx, string ofxTargetFile) { OfxFile = ofxTargetFile; Ofx = ofx; - - if (ofx.Header != null) - BuildHeader(ofx.Header); - if (ofx.Response!= null) - BuildResponse(ofx.Response); + + try + { + // Writing data into target file + StreamWriter sw = File.CreateText(OfxFile); + sw.Write(BuildHeader(ofx.Header)); + sw.Write(BuildResponse(ofx.Response)); + sw.Close(); + } catch (Exception e) + { + throw new OFXToolException($"Failed to build the OFX file! [{e.Message}]", e); + } } - private void BuildHeader(HeaderDocument doc) + private StringBuilder BuildHeader(HeaderDocument doc) { - //throw new NotImplementedException(); + var header = new StringBuilder(); + + header.AppendLine($"{Entity.GetHeader(nameof(HeaderDocument.OfxHeader))}:{doc.OfxHeader}"); + header.AppendLine($"{Entity.GetHeader(nameof(HeaderDocument.Data))}:{doc.Data}"); + header.AppendLine($"{Entity.GetHeader(nameof(HeaderDocument.Version))}:{doc.Version}"); + header.AppendLine($"{Entity.GetHeader(nameof(HeaderDocument.Security))}:{doc.Security}"); + header.AppendLine($"{Entity.GetHeader(nameof(HeaderDocument.Encoding))}:{doc.Encoding}"); + header.AppendLine($"{Entity.GetHeader(nameof(HeaderDocument.Charset))}:{doc.Charset}"); + header.AppendLine($"{Entity.GetHeader(nameof(HeaderDocument.Compression))}:{doc.Compression}"); + header.AppendLine($"{Entity.GetHeader(nameof(HeaderDocument.OldFileUid))}:{doc.OldFileUid}"); + header.AppendLine($"{Entity.GetHeader(nameof(HeaderDocument.NewFileUid))}:{doc.NewFileUid}"); + header.AppendLine(""); + + return header; } - private void BuildResponse(ResponseDocument doc) + private StringBuilder BuildResponse(ResponseDocument doc) { - // Writing data into target file - StreamWriter sw = File.CreateText(OfxFile); - sw.WriteLine($"<{Entity.GetElement()}>"); + var response = new StringBuilder(); + + + response.AppendLine($"<{Entity.GetElement()}>"); // if (doc.SignonResponseMessageSetV1 != null) - sw.WriteLine(new SIGNONMSGSRSV1().Build(doc.SignonResponseMessageSetV1)); + response.Append(new SIGNONMSGSRSV1().Build(doc.SignonResponseMessageSetV1)); // if (doc.BankResponseMessageSetV1 != null) - sw.WriteLine(new BANKMSGSRSV1().Build(doc.BankResponseMessageSetV1)); + response.Append(new BANKMSGSRSV1().Build(doc.BankResponseMessageSetV1)); + + response.AppendLine($"()}>"); - sw.WriteLine($"()}>"); - sw.Close(); + return response; } } } diff --git a/OFX-Tool.Library/Core/Load.cs b/OFX-Tool.Library/Core/Load.cs index 51f3cf2..86da7c6 100644 --- a/OFX-Tool.Library/Core/Load.cs +++ b/OFX-Tool.Library/Core/Load.cs @@ -72,9 +72,9 @@ private HeaderDocument LoadHeader() private ResponseDocument LoadResponse() { var response = new ResponseDocument(); + // Translating to XML file and parse the XML var xml = new ExportToXml(OfxFile); - using (var xmlReader = new XmlTextReader(xml.XmlFile)) { diff --git a/OFX-Tool.Library/Entities/Ofx.cs b/OFX-Tool.Library/Entities/Ofx.cs index 04fa625..d1e3502 100644 --- a/OFX-Tool.Library/Entities/Ofx.cs +++ b/OFX-Tool.Library/Entities/Ofx.cs @@ -2,10 +2,15 @@ { public class Ofx { - public HeaderDocument? Header { get; set; } + public HeaderDocument Header { get; set; } - public ResponseDocument? Response { get; set; } + public ResponseDocument Response { get; set; } + public Ofx() + { + Header = new HeaderDocument(); + Response = new ResponseDocument(); + } // Determines whether the specified object is equal to the current object. public override bool Equals(object? obj) diff --git a/OFX-Tool.Test/Core/BuildTests.cs b/OFX-Tool.Test/Core/BuildTests.cs index 9decb6a..e0a5b37 100644 --- a/OFX-Tool.Test/Core/BuildTests.cs +++ b/OFX-Tool.Test/Core/BuildTests.cs @@ -13,12 +13,12 @@ public class BuildTests [TestMethod()] public void BuildTest() { - var myFile = "./build.ofx"; - var doc = new ResponseDocument(); + var myBuildFile = "./buildTest.ofx"; + var ofx = new Ofx(); var status = new Status() { Code = "0", Severity = SeverityEnum.INFO }; var sonrs = new SignonResponse() { Language = LanguageEnum.FRA, ServerDate = "20220802000000", Status = status }; - doc.SignonResponseMessageSetV1 = new SignonResponseMessageSetV1() { SignonResponse = sonrs }; + ofx.Response.SignonResponseMessageSetV1 = new SignonResponseMessageSetV1() { SignonResponse = sonrs }; var bankacctfrom = new BankAccount() { BankId = "12345", BranchId = "6789", AccountId = "00112233445", AccountType = AccountEnum.CHECKING }; var stmttrn = new List(); @@ -30,12 +30,14 @@ public void BuildTest() var stmtrs = new StatementResponse() { Currency = CurrencyEnum.EUR, BankAccountFrom = bankacctfrom, BankTransactionList = banktranslist, LedgerBalance = ledgerbal, AvailableBalance = availbal }; var stmttrns = new List(); stmttrns.Add(new StatementTransactionResponse() { TransactionUniqueId = "20220802000000", Status = status, StatementResponse = stmtrs }); - doc.BankResponseMessageSetV1 = new BankResponseMessageSetV1() { StatementTransactionResponses = stmttrns }; + ofx.Response.BankResponseMessageSetV1 = new BankResponseMessageSetV1() { StatementTransactionResponses = stmttrns }; - new Build(new Ofx() { Response = doc }, myFile); + // Build the OFX File + new Build(ofx, myBuildFile); + // Load the OFX file builed + var newOfx= OfxTool.Get(myBuildFile); - var newDoc= OfxTool.Get(myFile).Response; - Assert.AreEqual(doc, newDoc); + Assert.AreEqual(ofx, newOfx); } } } \ No newline at end of file diff --git a/OFX-Tool.Test/Entities/OfxTests.cs b/OFX-Tool.Test/Entities/OfxTests.cs index bb46c1c..42546db 100644 --- a/OFX-Tool.Test/Entities/OfxTests.cs +++ b/OFX-Tool.Test/Entities/OfxTests.cs @@ -13,7 +13,7 @@ public void EqualsTest() Assert.IsFalse(entity.Equals(null)); Assert.IsFalse(entity.Equals(new Object())); Assert.AreEqual(entity, new Ofx()); - Assert.AreNotEqual(entity, new Ofx() { Header = new HeaderDocument() }); + Assert.AreNotEqual(entity, new Ofx() { Response = new ResponseDocument() { SignonResponseMessageSetV1 = new SignonResponseMessageSetV1() } }); } [TestMethod()]