Skip to content

Commit

Permalink
Add the header to the build file
Browse files Browse the repository at this point in the history
  • Loading branch information
rfavreau committed Sep 11, 2022
1 parent 0d44cb5 commit ad26ed4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
53 changes: 38 additions & 15 deletions OFX-Tool.Library/Core/Build.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using RFD.OFXTool.Library.Core.Elements;
using RFD.OFXTool.Library.Entities;
using System.Text;

namespace RFD.OFXTool.Library.Core
{
Expand All @@ -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<ResponseDocument>()}>");
var response = new StringBuilder();


response.AppendLine($"<{Entity.GetElement<ResponseDocument>()}>");

//
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($"</{Entity.GetElement<ResponseDocument>()}>");

sw.WriteLine($"</{Entity.GetElement<ResponseDocument>()}>");
sw.Close();
return response;
}
}
}
2 changes: 1 addition & 1 deletion OFX-Tool.Library/Core/Load.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{

Expand Down
9 changes: 7 additions & 2 deletions OFX-Tool.Library/Entities/Ofx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 9 additions & 7 deletions OFX-Tool.Test/Core/BuildTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StatementTransaction>();
Expand All @@ -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<StatementTransactionResponse>();
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<ResponseDocument>(doc, newDoc);
Assert.AreEqual<Ofx>(ofx, newOfx);
}
}
}
2 changes: 1 addition & 1 deletion OFX-Tool.Test/Entities/OfxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down

0 comments on commit ad26ed4

Please sign in to comment.