public void JsonReadWrite_JsonSerializationTest() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.940.OK.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); //Write Json using newtonsoft //check no exception JsonConvert.SerializeObject(b); JsonConvert.SerializeObject(b.Interchanges[0].Groups[0].Transactions[0]); //or use writer to write to string or stream JsonDataWriter w = new JsonDataWriter(); string str = w.WriteToString(b); Stream stream = w.WriteToStream(b); Assert.IsNotNull(str); Assert.IsNotNull(stream); Assert.AreEqual(0, stream.Position); Assert.IsTrue(stream.CanRead); Assert.AreEqual(str.Length, stream.Length); } }
public void EdiReader_ParseNestedLoopsEdi940() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.940.2.OK.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); EdiTrans t = b.Interchanges[0].Groups[0].Transactions[0]; int w05Count = t.Content.Count(l => l.Definition.GetType() == typeof(W05)); int w66Count = t.Content.Count(l => l.Definition.GetType() == typeof(W05)); int llxCount = t.Content.Count(l => l.Definition.GetType() == typeof(M_940.L_LX)); int slxCount = ((EdiLoop)t.Content.First(l => l.Definition.GetType() == typeof(M_940.L_LX))) .Content.Count(c => c.Definition.GetType() == typeof(LX)); int lw01Count = ((EdiLoop)t.Content.First(l => l.Definition.GetType() == typeof(M_940.L_LX))) .Content.Count(l => l.Definition.GetType() == typeof(M_940.L_W01)); int lw01N9Count = ((EdiLoop)((EdiLoop)t.Content.First(l => l.Definition.GetType() == typeof(M_940.L_LX))) .Content.First(l => l.Definition.GetType() == typeof(M_940.L_W01))) .Content.Count(l => l.Definition.GetType() == typeof(N9)); Assert.AreEqual(0, t.ValidationErrors.Count); Assert.AreEqual(1, w05Count); Assert.AreEqual(1, w66Count); Assert.AreEqual(1, llxCount); Assert.AreEqual(1, slxCount); Assert.AreEqual(1, lw01Count); Assert.AreEqual(3, lw01N9Count); } }
public void EdiReader_ParseNonEDI() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.NonEdi.edi")) { EdiDataReader r = new EdiDataReader(); r.FromStream(s); } }
public void EdiReader_WrongGroupsAndTranCount() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.WrongGroupsAndTranCount.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); Assert.AreEqual("Expected 2 groups. Found 1. Interchange # 3438.", b.Interchanges[0].ValidationErrors.Last().Message); Assert.AreEqual("Expected 2 transactions. Found 1. Group # 3314.", b.Interchanges[0].Groups[0].ValidationErrors.Last().Message); } }
public void JsonReadWrite_SerializeComposite() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.850.Composite.SLN.OK.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); JsonDataWriter jsonWriter = new JsonDataWriter(); jsonWriter.WriteToString(b); } }
public void JsonReadWrite_JsonSerializationHlLoopTest() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.856.Crossdock.OK.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); JsonDataWriter jsonWriter = new JsonDataWriter(); jsonWriter.WriteToString(b); } }
public void EdiReader_ParseFailedSegCount() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.940_Failed_SE01.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); EdiTrans t = b.Interchanges[0].Groups[0].Transactions[0]; Assert.AreEqual("Expected 12 segments. Found 9. Trans # 20066.", t.ValidationErrors.Last().Message); } }
public void EdiReader_ParseGenericEdi940() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.940.OK.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); Assert.AreEqual(1, b.Interchanges.Count); Assert.AreEqual(1, b.Interchanges[0].Groups.Count); Assert.AreEqual(1, b.Interchanges[0].Groups[0].Transactions.Count); EdiTrans t = b.Interchanges[0].Groups[0].Transactions[0]; int w05Count = t.Content.Count(l => l.Definition.GetType() == typeof(W05)); int n1Count = t.Content.Count(l => l.Definition.GetType() == typeof(M_940.L_N1)); int n1FirstIterationCount = ((EdiLoop)t.Content.First(l => l.Definition.GetType() == typeof(M_940.L_N1))).Content.Count; int n1SecondIterationCount = ((EdiLoop)t.Content.Where(l => l.Definition.GetType() == typeof(M_940.L_N1)).Skip(1).First()) .Content.Count; int n1ThirdIterationCount = ((EdiLoop)t.Content.Where(l => l.Definition.GetType() == typeof(M_940.L_N1)).Skip(2).First()) .Content.Count; int n9Count = t.Content.Count(l => l.Definition.GetType() == typeof(N9)); int g62Count = t.Content.Count(l => l.Definition.GetType() == typeof(G62)); int nteCount = t.Content.Count(l => l.Definition.GetType() == typeof(NTE)); int w66Count = t.Content.Count(l => l.Definition.GetType() == typeof(W66)); int lxCount = t.Content.Count(l => l.Definition.GetType() == typeof(M_940.L_LX)); int lxFirstIterationCount = ((EdiLoop)t.Content.First(l => l.Definition.GetType() == typeof(M_940.L_LX))).Content.Count; int lxFirstIterationW01Count = ((EdiLoop)((EdiLoop)t.Content.First(l => l.Definition.GetType() == typeof(M_940.L_LX))) .Content.Skip(1).First()).Content.Count; int w76Count = t.Content.Count(l => l.Definition.GetType() == typeof(W76)); Assert.AreEqual(0, t.ValidationErrors.Count); Assert.AreEqual(1, w05Count); Assert.AreEqual(3, n1Count); Assert.AreEqual(3, n1FirstIterationCount); Assert.AreEqual(3, n1SecondIterationCount); Assert.AreEqual(2, n1ThirdIterationCount); Assert.AreEqual(1, n9Count); Assert.AreEqual(2, g62Count); Assert.AreEqual(1, nteCount); Assert.AreEqual(1, w66Count); Assert.AreEqual(3, lxCount); Assert.AreEqual(2, lxFirstIterationCount); Assert.AreEqual(3, lxFirstIterationW01Count); Assert.AreEqual(1, w76Count); } }
public void EdiReader_ControlNumbersMismatcht() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.ControlNumbersMismatch.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); Assert.AreEqual("Control numbers do not match. ISA 000003438. IEA 000003439.", b.Interchanges[0].ValidationErrors.Last().Message); Assert.AreEqual("Control numbers do not match. GS 3314. GE 3315.", b.Interchanges[0].Groups[0].ValidationErrors.Last().Message); Assert.AreEqual("Control numbers do not match. ST 0001. SE 0002.", b.Interchanges[0].Groups[0].Transactions[0].ValidationErrors.Last().Message); } }
public void EdiReader_ParseGenericEdi940WithExternalMapAssembly() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.940.W05.Only.edi")) { EdiDataReader r = new EdiDataReader("EdiEngine.Tests"); EdiBatch b = r.FromStream(s); EdiTrans t = b.Interchanges[0].Groups[0].Transactions[0]; Assert.AreEqual(0, t.ValidationErrors.Count); } }
public void AckBuilder_AcceptAll_WithAk2() { EdiBatch b; using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.AckTest.edi")) { EdiDataReader r = new EdiDataReader(); b = r.FromStream(s); } AckBuilderSettings ackSettings = new AckBuilderSettings(AckValidationErrorBehavour.AcceptAll, true, 100, 200); var ack = new AckBuilder(ackSettings); EdiBatch ackBatch = ack.GetnerateAcknowledgment(b); //string data = ack.WriteToString(b); EdiTrans ack1 = ackBatch.Interchanges[0].Groups[0].Transactions[0]; EdiTrans ack2 = ackBatch.Interchanges[0].Groups[1].Transactions[0]; EdiSegment ak1 = (EdiSegment)ack1.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK1)); EdiLoop loopAk2_1 = (EdiLoop)ack1.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(M_997.L_AK2)); EdiLoop loopAk2_2 = (EdiLoop)ack1.Content.LastOrDefault(l => l.Definition.GetType() == typeof(M_997.L_AK2)); EdiSegment ak5_1 = (EdiSegment)loopAk2_1.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK5)); EdiSegment ak5_2 = (EdiSegment)loopAk2_2.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK5)); EdiSegment ak9 = (EdiSegment)ack1.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK9)); Assert.IsNotNull(ak1); Assert.IsNotNull(loopAk2_1); Assert.AreEqual("A", ak5_1.Content[0].Val); Assert.AreEqual("A", ak5_2.Content[0].Val); Assert.IsNotNull(loopAk2_2); Assert.IsNotNull(ak9); Assert.AreEqual("A", ak9.Content[0].Val); Assert.AreEqual("2", ak9.Content[1].Val); Assert.AreEqual("2", ak9.Content[2].Val); Assert.AreEqual("2", ak9.Content[3].Val); ak1 = (EdiSegment)ack2.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK1)); loopAk2_1 = (EdiLoop)ack1.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(M_997.L_AK2)); ak5_1 = (EdiSegment)loopAk2_1.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK5)); ak9 = (EdiSegment)ack2.Content.FirstOrDefault(l => l.Definition.GetType() == typeof(AK9)); Assert.IsNotNull(ak1); Assert.IsNotNull(loopAk2_1); Assert.AreEqual("A", ak5_1.Content[0].Val); Assert.IsNotNull(ak9); Assert.AreEqual("A", ak9.Content[0].Val); Assert.AreEqual("1", ak9.Content[1].Val); Assert.AreEqual("1", ak9.Content[2].Val); Assert.AreEqual("1", ak9.Content[3].Val); }
public void EdiReaderHL_ParseCrossDock856() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.856.Crossdock.OK.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); Assert.AreEqual(1, b.Interchanges.Count); Assert.AreEqual(1, b.Interchanges[0].Groups.Count); Assert.AreEqual(1, b.Interchanges[0].Groups[0].Transactions.Count); EdiTrans t = b.Interchanges[0].Groups[0].Transactions[0]; Assert.AreEqual(0, t.ValidationErrors.Count); } }
public void XmlReadWrite_XmlSerializationHlLoopTest() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.856.Crossdock.OK.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); XmlDataWriter w = new XmlDataWriter(); string data = w.WriteToString(b); XmlDocument xdoc = ValidateBySchema(data); //check parsed seg count int?segCount = xdoc.SelectNodes("//EdiSegment")?.Count; Assert.AreEqual(61, segCount); } }
public void EdiReader_ParseMultipleInterchangesAndGroups() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.MultipleInterchangesAndGroups.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); Assert.AreEqual(2, b.Interchanges.Count); Assert.AreEqual(2, b.Interchanges[0].Groups.Count); Assert.AreEqual(2, b.Interchanges[0].Groups[0].Transactions.Count); Assert.AreEqual(1, b.Interchanges[0].Groups[1].Transactions.Count); Assert.AreEqual(2, b.Interchanges[1].Groups[0].Transactions.Count); Assert.AreEqual(1, b.Interchanges[1].Groups[1].Transactions.Count); } }
public void SyntaxNote_Edi850Fail() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.850.SyntaxNotes.ERR.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); EdiTrans t = b.Interchanges[0].Groups[0].Transactions[0]; Assert.AreEqual(3, t.ValidationErrors.Count); Assert.AreEqual(13, t.ValidationErrors[0].SegmentPos); Assert.IsTrue(t.ValidationErrors[0].Message.Contains("P0607")); Assert.AreEqual(30, t.ValidationErrors[1].SegmentPos); Assert.IsTrue(t.ValidationErrors[1].Message.Contains("C0506")); Assert.AreEqual(30, t.ValidationErrors[2].SegmentPos); Assert.IsTrue(t.ValidationErrors[2].Message.Contains("P0607")); } }
public void XmlReadWrite_XmlSerializationTest() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.940.OK.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); XmlDataWriter w = new XmlDataWriter(); string data = w.WriteToString(b); Stream stream = w.WriteToStream(b); Assert.IsNotNull(stream); Assert.AreEqual(0, stream.Position); Assert.IsTrue(stream.CanRead); XmlDocument xdoc = ValidateBySchema(data); //check parsed seg count int?segCount = xdoc.SelectNodes("//EdiSegment")?.Count; Assert.AreEqual(33, segCount); } }
public void EdiReader_ParseGenericEdi850() { using (Stream s = GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.850.OK.edi")) { EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(s); Assert.AreEqual(1, b.Interchanges.Count); Assert.AreEqual(1, b.Interchanges[0].Groups.Count); Assert.AreEqual(1, b.Interchanges[0].Groups[0].Transactions.Count); Assert.AreEqual(1, b.Interchanges.Count); Assert.AreEqual(1, b.Interchanges[0].Groups.Count); Assert.AreEqual(1, b.Interchanges[0].Groups[0].Transactions.Count); EdiTrans t = b.Interchanges[0].Groups[0].Transactions[0]; int rootSegCount = t.Content.Count(seg => seg is EdiSegment); int rootLoopCount = t.Content.Count(l => l is EdiLoop); int n1LoopCount = t.Content.Count(l => l.Definition.GetType() == typeof(M_850.L_N1)); int n1ContentCount = ((EdiLoop)t.Content.First(l => l.Definition.GetType() == typeof(M_850.L_N1))).Content.Count(); int po1LoopCount = t.Content.Count(l => l.Definition.GetType() == typeof(M_850.L_PO1)); int pidLoopCount = t.Content.Where(l => l.Definition.GetType() == typeof(M_850.L_PO1)) .Select(l2 => l2.Definition.GetType() == typeof(M_850.L_PID)).Count(); int po1Count = 0; int pidCount = 0; int po4Count = 0; var lst = t.Content.Where(l => l.Definition.GetType() == typeof(M_850.L_PO1)).Select(l => l).ToList(); lst.ForEach(l => { var po1Loop = (EdiLoop)l; if (po1Loop.Content[0].Definition.GetType() == typeof(PO1)) { po1Count++; } if (po1Loop.Content[2].Definition.GetType() == typeof(PO4)) { po4Count++; } EdiLoop pidLoop = (EdiLoop)po1Loop.Content.First(l2 => l2.Definition.GetType() == typeof(M_850.L_PID)); if (pidLoop.Content[0].Definition.GetType() == typeof(PID)) { pidCount++; } }); int cttLoopCount = t.Content.Count(l => l.Definition.GetType() == typeof(M_850.L_CTT)); int cttCount = ((EdiLoop)t.Content.First(l => l.Definition.GetType() == typeof(M_850.L_CTT))) .Content.Count(s2 => s2.Definition.GetType() == typeof(CTT)); Assert.AreEqual(0, t.ValidationErrors.Count); Assert.AreEqual(8, rootSegCount); Assert.AreEqual(8, rootLoopCount); Assert.AreEqual(1, n1LoopCount); Assert.AreEqual(3, n1ContentCount); Assert.AreEqual(6, po1LoopCount); Assert.AreEqual(6, po1Count); Assert.AreEqual(6, pidLoopCount); Assert.AreEqual(6, pidCount); Assert.AreEqual(6, po4Count); Assert.AreEqual(1, cttLoopCount); Assert.AreEqual(1, cttCount); } }
public static async Task <String> SplitEDIFile(string BLOBFileName, string splitHalf, ILogger log) { log.LogInformation("SplitEDIFile function processing a request."); //Incoming JSON Payload: //{ "ediFileName":"916386MS210_202001190125_000000001.raw","splitHalf":"1"} // Parse the connection string and get a reference to the blob. var storageCredentials = new StorageCredentials("jbupsbtspaassagenv2", "Pi5FpNwJgcwJiAj7fxyCc4ncM4llKu3184a0OrTE1Jn0VxSYLGPIOZAO1j36DjT1Plmw8udLR3NXUBVNUWnucA=="); var cloudStorageAccount = new CloudStorageAccount(storageCredentials, true); var cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient(); //Define BlobIN Stream CloudBlobClient blobINClient = cloudStorageAccount.CreateCloudBlobClient(); CloudBlobContainer blobINContainer = blobINClient.GetContainerReference("edi-process-split"); CloudBlockBlob blobIN = blobINContainer.GetBlockBlobReference(BLOBFileName); Stream blobINStream = await blobIN.OpenReadAsync(); //Define BlobOUT FileName - for FIRST OR SECOND half of transactions string blobOUTPreFix = ""; if (splitHalf == "1") { blobOUTPreFix = "A_"; } else { blobOUTPreFix = "B_"; } CloudBlobClient blobOUTClient = cloudStorageAccount.CreateCloudBlobClient(); CloudBlobContainer blobOUTContainer = blobOUTClient.GetContainerReference("edi-process-split"); CloudBlockBlob blobOUT = blobOUTContainer.GetBlockBlobReference(blobOUTPreFix + BLOBFileName); //Read blobIN EdiDataReader r = new EdiDataReader(); EdiBatch b = r.FromStream(blobINStream); int totEDITransCtr = 0; totEDITransCtr = b.Interchanges[0].Groups[0].Transactions.Count(); // ********************************************************** // ** SAVE Original EDI Interchange: Data & Transactions // ********************************************************** EdiInterchange OrigInter = new EdiInterchange(); OrigInter = b.Interchanges[0]; List <EdiTrans> OrigTrans = new List <EdiTrans>(); OrigTrans = b.Interchanges[0].Groups[0].Transactions; // ********************************************************** // ** Calculate SPLIT // ********************************************************** //1st Half int EDI_Trans_Ctr1 = totEDITransCtr / 2; //2nd Half int EDI_Trans_Ctr2 = totEDITransCtr - (totEDITransCtr / 2); // ********************************************************** // ** Write-Out 1st Half to file // ********************************************************** List <EdiTrans> HalfTrans = new List <EdiTrans>(); //Define LOOP Parameters - for FIRST OR SECOND half of transactions int loopStart = 0; int loopEnd = 0; if (splitHalf == "1") { loopStart = 0; loopEnd = EDI_Trans_Ctr1; } else { loopStart = EDI_Trans_Ctr1 + 1; loopEnd = totEDITransCtr; } //Process TRANSACTIONS for (int i = loopStart; i < loopEnd; i++) { EdiTrans ediItem = new EdiTrans(); ediItem = b.Interchanges[0].Groups[0].Transactions[i]; HalfTrans.Add(ediItem); } EdiBatch b2 = new EdiBatch(); //Handle InterChange EdiInterchange ediInterChgItem = new EdiInterchange(); ediInterChgItem = b.Interchanges[0]; //Remove existing EdiGroup jbEdiGroupItem = new EdiGroup(""); jbEdiGroupItem = b.Interchanges[0].Groups[0]; ediInterChgItem.Groups.RemoveRange(0, 1); //Add Interchange b2.Interchanges.Add(ediInterChgItem); //Handle Group jbEdiGroupItem.Transactions.RemoveRange(0, totEDITransCtr); ediInterChgItem.Groups.Add(jbEdiGroupItem); //Add Transactions for (int i = 0; i < HalfTrans.Count(); i++) //Hardcoded to 91 { EdiTrans ediItem = new EdiTrans(); ediItem = HalfTrans[i]; b2.Interchanges[0].Groups[0].Transactions.Add(ediItem); } EdiDataWriterSettings settings = new EdiDataWriterSettings( new SegmentDefinitions.ISA(), new SegmentDefinitions.IEA(), new SegmentDefinitions.GS(), new SegmentDefinitions.GE(), new SegmentDefinitions.ST(), new SegmentDefinitions.SE(), OrigInter.ISA.Content[4].ToString(), //isaSenderQual OrigInter.ISA.Content[5].ToString(), //isaSenderId OrigInter.ISA.Content[6].ToString(), //isaReceiverQual // OrigInter.ISA.Content[7].ToString(), //isaReceiverId "9163863M210", //isaReceiverId OrigInter.ISA.Content[8].ToString(), //gsSenderId OrigInter.ISA.Content[9].ToString(), //gsReceiverId // OrigInter.ISA.Content[10].ToString(), //JB Test OrigInter.ISA.Content[11].ToString(), //isaEdiVersion OrigInter.ISA.Content[12].ToString(), //gsEdiVersion // "00403", //gsEdiVersion OrigInter.ISA.Content[14].ToString(), //P //isaUsageIndicator //[ 000, //isaFirstControlNumber 001, //gsFirstControlNumber OrigInter.SegmentSeparator.ToString(), //segmentSeparator OrigInter.ElementSeparator.ToString()); //elementSeparator EdiDataWriter w = new EdiDataWriter(settings); string JBData = w.WriteToString(b2); log.LogInformation("trigger function - WRITING SPLIT FILE : " + blobOUT.Name.ToString()); await blobOUT.UploadTextAsync(JBData); blobINStream.Close(); return(" "); }