public void TestInterchangeRead() { // ARRANGE const string sample = "EdiWeave.UnitTests.Edifact.Edi.Edifact_INVOIC_D00A_MultipleInterchange.txt"; var ediStream = CommonHelper.LoadStream(sample); var ediItems = new List <object>(); // ACT using (var ediReader = new EdifactReader(ediStream, "EdiWeave.Rules.EdifactD00A")) { while (ediReader.Read()) { ediItems.Add(ediReader.Item); if (!(ediReader.Item is UNZ)) { continue; } // ASSERT Assert.IsNotNull(ediItems.OfType <UNB>().SingleOrDefault()); Assert.IsNull(ediItems.OfType <UNG>().SingleOrDefault()); Assert.IsNotNull(ediItems.OfType <TSINVOIC>().SingleOrDefault()); Assert.IsNull(ediItems.OfType <UNE>().SingleOrDefault()); Assert.IsNotNull(ediItems.OfType <UNZ>().SingleOrDefault()); Assert.IsNull(ediItems.OfType <ErrorContext>().SingleOrDefault()); ediItems.Clear(); } } }
/// <summary> /// Reads purchase orders and invoices batched up in the same interchange. /// </summary> public static void ReadMixedTransactions() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); // 1. Load to a stream Stream ediStream = File.OpenRead(Directory.GetCurrentDirectory() + @"\..\..\..\Files.Edifact\MixedTransactions.txt"); // 2. Read multiple transactions batched up in the same interchange using (var ediReader = new EdifactReader(ediStream, TemplateFactory.FullTemplateFactory)) { while (ediReader.Read()) { // Process purchase orders if no parsing errors var po = ediReader.Item as TSORDERS; if (po != null && !po.HasErrors) { ProcessPurchaseOrder(ediReader.CurrentInterchangeHeader, ediReader.CurrentGroupHeader, po); } // Process invoices if no parsing errors var invoice = ediReader.Item as TSINVOIC; if (invoice != null && !invoice.HasErrors) { ProcessInvoice(ediReader.CurrentInterchangeHeader, ediReader.CurrentGroupHeader, invoice); } } } }
public void EdifactReader_UNOA_ReadLine() { const string value = "ABC@@@"; var bytes = Utf8.GetBytes(value); var output = new byte[3]; var sb = new StringBuilder(); using (var sr = new EdifactReader(new MemoryStream(bytes), Utf8, EdifactCharacterSet.UNOA, '.')) { int c; while ((c = sr.Read()) != -1) { sb.Append((char)c); } } var resultRead = sb.ToString(); var sr2 = new EdifactReader(new MemoryStream(bytes), Utf8, EdifactCharacterSet.UNOA, '.'); var resultReadLine = sr2.ReadLine(); Assert.AreEqual <string>("ABC...", resultRead); Assert.AreEqual <string>("ABC...", resultReadLine); }
public void TestMultipleInterchange() { // ARRANGE const string sample = "EdiWeave.UnitTests.Edifact.Edi.Edifact_INVOIC_D00A_MultipleInterchange.txt"; var ediStream = CommonHelper.LoadStream(sample); var expected = CommonHelper.LoadString(sample, Encoding.UTF8); var ediItems = new List <EdiItem>(); // ACT var actual = ""; using (var ediReader = new EdifactReader(ediStream, "EdiWeave.Rules.EdifactD00A")) { while (ediReader.Read()) { if (ediReader.Item is UNB && ediItems.Any()) { actual = actual + EdifactHelper.Generate(ediItems, null, Environment.NewLine); ediItems.Clear(); } ediItems.Add(ediReader.Item); } actual = actual + EdifactHelper.Generate(ediItems, ediReader.Separators, Environment.NewLine); } // ASSERT Assert.AreEqual(expected, actual); }
public void EdifactReader_UNOC_Replacement() { //only in UTF8 so not in UNOC //Ђ CYRILLIC CAPITAL LETTER DJE (U+0402) d082 const string value = "ABCЂ"; var bytes = Utf8.GetBytes(value); var output = new byte[3]; var sb = new StringBuilder(); using (var sr = new EdifactReader(new MemoryStream(bytes), Utf8, EdifactCharacterSet.UNOB, '.', true)) { int c; while ((c = sr.Read()) != -1) { sb.Append((char)c); } } var result = sb.ToString(); Assert.AreEqual <string>("ABC.", result); }
/// <summary> /// Detect duplicate groups. /// </summary> public static void GenerateContrlWithDuplicateGroup() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); var edi = File.OpenRead(Directory.GetCurrentDirectory() + @"\..\..\..\Files.Edifact\DuplicateGroup.txt"); var settings = new AckSettings { AckHandler = (s, a) => { var contrl = a.Message as TSCONTRL; if (contrl != null && a.AckType == AckType.Technical) { // a.Message is technical acknowledgment } if (contrl != null && a.AckType == AckType.Functional) { var ack = AckBuilders.BuildAck(a.InterchangeHeader, a.GroupHeader, contrl); Debug.Write(ack); } }, MessageHandler = (s, a) => { if (!a.ErrorContext.HasErrors) { if (a.InDuplicateGroup) { Debug.WriteLine(string.Format("Interchange with control number {0}", a.InterchangeHeader.InterchangeControlReference_5)); Debug.WriteLine(string.Format("Group with control number {0}", a.GroupHeader.GroupReferenceNumber_5)); Debug.WriteLine("Message {0} with control number {1}", a.Message.Name, a.Message.GetTransactionContext().HeaderControlNumber); Debug.WriteLine("Is in duplicate group: {0}", a.InDuplicateGroup); // reject message } } }, // Turn on UCM for valid messages GenerateForValidMessages = true, GroupDuplicates = true }; using (var ackMan = new Plugins.Acknowledgments.Edifact.AckMan(settings)) { using (var ediReader = new EdifactReader(edi, TemplateFactory.FullTemplateFactory)) { while (ediReader.Read()) { ackMan.Publish(ediReader.Item); } } } }
/// <summary> /// Auto increment custom control numbers. /// </summary> public static void GenerateContrlWithControlNumbers() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); var edi = File.OpenRead(Directory.GetCurrentDirectory() + @"\..\..\..\Files.Edifact\PurchaseOrder.txt"); int unbControlNumber = 28; int ungControlNumber = 35; Debug.WriteLine("Start interchange control number: {0}", unbControlNumber); Debug.WriteLine("Start group control number: {0}", ungControlNumber); var settings = new AckSettings { AckHandler = (s, a) => { var contrl = a.Message as TSCONTRL; if (contrl != null && a.AckType == AckType.Technical) { // a.Message is technical acknowledgment } if (contrl != null && a.AckType == AckType.Functional) { var ack = AckBuilders.BuildAck(a.InterchangeHeader, a.GroupHeader, contrl, ++unbControlNumber, ++ungControlNumber); Debug.Write(ack); } }, MessageHandler = (s, a) => { if (!a.ErrorContext.HasErrors) { // do something with the message a.Message Debug.WriteLine("Message {0} with control number {1} is valid.", a.ErrorContext.Name, a.ErrorContext.ControlNumber); } }, }; using (var ackMan = new Plugins.Acknowledgments.Edifact.AckMan(settings)) { using (var ediReader = new EdifactReader(edi, TemplateFactory.FullTemplateFactory)) { while (ediReader.Read()) { ackMan.Publish(ediReader.Item); } } } Debug.WriteLine("Last interchange control number: {0}", unbControlNumber); Debug.WriteLine("Last group control number: {0}", ungControlNumber); }
/// <summary> /// Detects duplicate groups. /// </summary> public static void GenerateContrlWithDuplicateGroup() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); var edi = Assembly.GetExecutingAssembly().GetManifestResourceStream("EdiFabric.Sdk.Edifact.Edi.DetectDuplicateGroup.txt"); var settings = new AckSettings { AckHandler = (s, a) => { if (a.Message.Name == "CONTRL" && a.AckType == AckType.Technical) { // a.AckInterchange is technical acknowledgment } if (a.Message.Name == "CONTRL" && a.AckType == AckType.Functional) { var ack = BuildAck(a.InterchangeHeader, a.GroupHeader, a.Message); Debug.Write(ack); } }, MessageHandler = (s, a) => { if (!a.ErrorContext.HasErrors) { if (a.InDuplicateGroup) { Debug.WriteLine(string.Format("Interchange with control number {0}", a.InterchangeHeader.InterchangeControlReference_5)); Debug.WriteLine(string.Format("Group with control number {0}", a.GroupHeader.GroupReferenceNumber_5)); Debug.WriteLine("Message {0} with control number {1}", a.Message.Name, a.Message.GetTransactionContext().HeaderControlNumber); Debug.WriteLine("Is in duplicate group: {0}", a.InDuplicateGroup); // reject message } } }, // Turn on UCM for valid messages GenerateForValidMessages = true, GroupDuplicates = true }; var ackMan = new Plugins.Acknowledgments.Edifact.AckMan(settings); using (var ediReader = new EdifactReader(edi, "EdiFabric.Sdk.Edifact")) { while (ediReader.Read()) { ackMan.Publish(ediReader.Item); } } ackMan.Complete(); }
/// <summary> /// Auto increments non default control numbers. /// </summary> public static void GenerateContrlWithControlNumbers() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); var edi = Assembly.GetExecutingAssembly().GetManifestResourceStream("EdiFabric.Sdk.Edifact.Edi.PurchaseOrder.txt"); int unbControlNumber = 28; int ungControlNumber = 35; Debug.WriteLine("Start interchange control number: {0}", unbControlNumber); Debug.WriteLine("Start group control number: {0}", ungControlNumber); var settings = new AckSettings { AckHandler = (s, a) => { if (a.Message.Name == "CONTRL" && a.AckType == AckType.Technical) { // a.AckInterchange is technical acknowledgment } if (a.Message.Name == "CONTRL" && a.AckType == AckType.Functional) { var ack = BuildAck(a.InterchangeHeader, a.GroupHeader, a.Message, ++unbControlNumber, ++ungControlNumber); Debug.Write(ack); } }, MessageHandler = (s, a) => { if (!a.ErrorContext.HasErrors) { // do something with the message a.Message Debug.WriteLine("Message {0} with control number {1} is valid.", a.ErrorContext.Name, a.ErrorContext.ControlNumber); } }, }; var ackMan = new Plugins.Acknowledgments.Edifact.AckMan(settings); using (var ediReader = new EdifactReader(edi, "EdiFabric.Sdk.Edifact")) { while (ediReader.Read()) { ackMan.Publish(ediReader.Item); } } ackMan.Complete(); Debug.WriteLine("Last interchange control number: {0}", unbControlNumber); Debug.WriteLine("Last group control number: {0}", ungControlNumber); }
/// <summary> /// Generate CONTRL for a valid message. /// </summary> public static void GenerateContrlForValid() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); var edi = File.OpenRead(Directory.GetCurrentDirectory() + @"\..\..\..\Files.Edifact\PurchaseOrder.txt"); var settings = new AckSettings { AckHandler = (s, a) => { var contrl = a.Message as TSCONTRL; if (contrl != null && a.AckType == AckType.Technical) { // a.Message is technical acknowledgment } if (contrl != null && a.AckType == AckType.Functional) { // Inspect the acknowledgment var ackCode = contrl.UCI.Actioncoded_04; var ack = AckBuilders.BuildAck(a.InterchangeHeader, a.GroupHeader, contrl); Debug.Write(ack); } }, MessageHandler = (s, a) => { if (!a.ErrorContext.HasErrors) { // do something with the message a.Message Debug.WriteLine("Message {0} with control number {1} is valid.", a.ErrorContext.Name, a.ErrorContext.ControlNumber); } }, // Turn on UCM for valid messages GenerateForValidMessages = true }; using (var ackMan = new Plugins.Acknowledgments.Edifact.AckMan(settings)) { using (var ediReader = new EdifactReader(edi, TemplateFactory.FullTemplateFactory)) { while (ediReader.Read()) { ackMan.Publish(ediReader.Item); } } } }
/// <summary> /// Detect duplicate messages. /// </summary> public static void Run() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); var edi = File.OpenRead(Directory.GetCurrentDirectory() + @"\..\..\..\Files\Edifact\DuplicateMessage.txt"); var settings = new AckSettings { AckHandler = (s, a) => { var contrl = a.Message as TSCONTRL; if (contrl != null && a.AckType == AckType.Technical) { // a.Message is technical acknowledgment } if (contrl != null && a.AckType == AckType.Functional) { var ack = AckBuilders.BuildAck(a.InterchangeHeader, a.GroupHeader, contrl); Debug.Write(ack); } }, MessageHandler = (s, a) => { if (a.ErrorContext.HasErrors && a.ErrorContext.Codes.Any(c => c == Core.ErrorCodes.MessageErrorCode.DuplicateMessageControlNumber)) { // do something with the message a.Message Debug.WriteLine("Message {0} with control number {1} is duplicate.", a.ErrorContext.Name, a.ErrorContext.ControlNumber); } }, // Turn on UCM for valid messages GenerateForValidMessages = true, TransactionSetDuplicates = true, TechnicalAck = TechnicalAck.Enforce }; using (var ackMan = new Plugins.Acknowledgments.Edifact.AckMan(settings)) { using (var ediReader = new EdifactReader(edi, "EdiFabric.Examples.EDIFACT.Templates.D96A")) { while (ediReader.Read()) { ackMan.Publish(ediReader.Item); } } } }
/// <summary> /// Detects duplicate messages. /// </summary> public static void GenerateContrlWithDuplicateMessage() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); var edi = Assembly.GetExecutingAssembly().GetManifestResourceStream("EdiFabric.Sdk.Edifact.Edi.DuplicateMessage.txt"); var settings = new AckSettings { AckHandler = (s, a) => { if (a.Message.Name == "CONTRL" && a.AckType == AckType.Technical) { // a.AckInterchange is technical acknowledgment } if (a.Message.Name == "CONTRL" && a.AckType == AckType.Functional) { var ack = BuildAck(a.InterchangeHeader, a.GroupHeader, a.Message); Debug.Write(ack); } }, MessageHandler = (s, a) => { if (a.ErrorContext.HasErrors && a.ErrorContext.Codes.Any(c => c == Core.ErrorCodes.MessageErrorCode.DuplicateMessageControlNumber)) { // do something with the message a.Message Debug.WriteLine("Message {0} with control number {1} is duplicate.", a.ErrorContext.Name, a.ErrorContext.ControlNumber); } }, // Turn on UCM for valid messages GenerateForValidMessages = true, TransactionSetDuplicates = true }; var ackMan = new Plugins.Acknowledgments.Edifact.AckMan(settings); using (var ediReader = new EdifactReader(edi, "EdiFabric.Sdk.Edifact")) { while (ediReader.Read()) { ackMan.Publish(ediReader.Item); } } ackMan.Complete(); }
/// <summary> /// Reads one item at a time from the EDI stream /// The interchange contains two purchase orders: /// The first is valid /// The second is invalid /// </summary> static void ReadPurchaseOrdersOneAtATime() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); // 1. Load the sample purchase order to a stream Stream purchaseOrderStream = Assembly.GetExecutingAssembly() .GetManifestResourceStream("EdiFabric.Sdk.Edifact.Edi.PurchaseOrders.txt"); // 2. Read item by item, that is each call to Read() brings back either a control segment (UNB, UNG, UNE or UNZ) or a transaction using (var ediReader = new EdifactReader(purchaseOrderStream, "EdiFabric.Sdk.Edifact")) { // Call Read() instead of ReadToEnd() while (ediReader.Read()) { var po = ediReader.Item as TSORDERS; if (po != null) { // 3. Validate it MessageErrorContext errorContext; if (po.IsValid(out errorContext)) { // The purchase order is valid, process it downstream } else { // The purchase order is invalid // Report it back to the sender, log, etc. // Inspect MessageErrorContext for the validation errors var errors = errorContext.Flatten(); // List all error messages Debug.WriteLine("Message {0} with control number {1} is invalid with errors:", errorContext.Name, errorContext.ControlNumber); foreach (var error in errors) { Debug.WriteLine(error); } } } } } }
/// <summary> /// Reads one item at a time from the EDI stream. /// Use for interchanges containing multiple transactions. /// The sample file contains two purchase orders - a valid one and an invalid one. /// </summary> public static void ReadPurchaseOrdersOneAtATime() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); // 1. Load to a stream Stream ediStream = File.OpenRead(Directory.GetCurrentDirectory() + @"\..\..\..\Files.Edifact\PurchaseOrders.txt"); // 2. Read item by item, that is each call to Read() // brings back either a control segment (UNB, UNG, UNE or UNZ) or a transaction using (var ediReader = new EdifactReader(ediStream, TemplateFactory.FullTemplateFactory)) { while (ediReader.Read()) { // 3. Check if current item is purchase order var po = ediReader.Item as TSORDERS; if (po != null) { // 4. Validate it MessageErrorContext errorContext; if (po.IsValid(out errorContext)) { // The purchase order is valid, process it downstream } else { // The purchase order is invalid Debug.WriteLine("Message {0} with control number {1} is invalid with errors:", errorContext.Name, errorContext.ControlNumber); // List all error messages var errors = errorContext.Flatten(); foreach (var error in errors) { Debug.WriteLine(error); } } } } } }
public void EdifactReader_DontRemoveControlChars() { const string value = "A\aB\bCDEF\fGHIJKLMON\nPQR\rST\tUV\vWXYZ"; var bytes = Utf8.GetBytes(value); var output = new byte[3]; var sb = new StringBuilder(); using (var sr = new EdifactReader(new MemoryStream(bytes), Utf8, EdifactCharacterSet.UNOB, '.', true, false)) { int c; while ((c = sr.Read()) != -1) { sb.Append((char)c); } } var result = sb.ToString(); Assert.AreEqual <string>("A\aB\bCDEF\fGHIJKLMON\nPQR\rST\tUV\vWXYZ", result); }
public void EdifactReader_UNOB_Default() { const string value = "ABCabc"; var bytes = Utf8.GetBytes(value); var output = new byte[3]; var sb = new StringBuilder(); using (var sr = new EdifactReader(new MemoryStream(bytes), Utf8, EdifactCharacterSet.UNOB, '.', true)) { int c; while ((c = sr.Read()) != -1) { sb.Append((char)c); } } var result = sb.ToString(); Assert.AreEqual <string>("ABCabc", result); }
/// <summary> /// Reads one item at a time from the EDI stream. /// Use for interchanges containing multiple transactions. /// The sample file contains two purchase orders - a valid one and an invalid one. /// </summary> public static void Run() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); // 1. Load to a stream Stream ediStream = File.OpenRead(Directory.GetCurrentDirectory() + @"\..\..\..\Files\Edifact\PurchaseOrders.txt"); // 2. Read item by item, that is each call to Read() // brings back either a control segment (UNB, UNG, UNE or UNZ) or a transaction using (var ediReader = new EdifactReader(ediStream, "EdiFabric.Templates.Edifact")) { while (ediReader.Read()) { // 3. Check if current item is purchase order var po = ediReader.Item as TSORDERS; if (po != null) { ProcessPurchaseOrder(ediReader.CurrentInterchangeHeader, ediReader.CurrentGroupHeader, po); } } } }
/// <summary> /// Validate control segments. /// </summary> public static void ValidateControlSegments() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); Stream ediStream = File.OpenRead(Directory.GetCurrentDirectory() + @"\..\..\..\Files.Edifact\Invoice.txt"); using (var ediReader = new EdifactReader(ediStream, TemplateFactory.FullTemplateFactory)) { while (ediReader.Read()) { var unb = ediReader.Item as UNB; if (unb != null) { // Validate var unbErrors = unb.Validate(); // Pull the sender id from UNB var senderId = unb.INTERCHANGESENDER_2.InterchangeSenderIdentification_1; Debug.WriteLine("Sender ID:"); Debug.WriteLine(senderId); } var ung = ediReader.Item as UNG; if (ung != null) { // Validate var ungErrors = ung.Validate(); // Pull the sender id from UNG var senderId = ung.APPLICATIONSENDERIDENTIFICATION_2.ApplicationSenderIdentification_1; Debug.WriteLine("Sender ID:"); Debug.WriteLine(senderId); } } } }