Exemplo n.º 1
0
        //string json3 = @"{'Type': 'M',
        //    'Name': 'M_940',
        //    'Content': [{
        //        'Type': 'S',
        //        'Name': 'W05',
        //        'Content': [ { 'E': 'N' }, { 'E': '538686' }, { 'E': '' }, { 'E': '001001' }, { 'E': '538686' }]
        //    }]
        //}";
        //{'Type': 'M','Name': 'M_940','Content': [{'Type': 'S','Name': 'W05','Content': [ { 'E': 'N' }, { 'E': '538686' }, { 'E': '' }, { 'E': '001001' }, { 'E': '538686' }]}]}
        public string GetEDIDocFromJSON(string jsonEDIdata)
        {
            dynamic json = JsonConvert.DeserializeObject(jsonEDIdata, typeof(object));

            //TODO: convert logic to determine map to separate method

            MapLoop       map     = Factory.GetMap((string)json.Name); //TODO: add exception handling for the string cast operation
            JsonMapReader jReader = new JsonMapReader(map);
            EdiTrans      ediTxn  = jReader.ReadToEnd(jsonEDIdata);

            var eGroup = new EdiGroup("OW");

            eGroup.Transactions.Add(ediTxn);

            var eInterchange = new EdiInterchange();

            eInterchange.Groups.Add(eGroup);

            EdiBatch eBatch = new EdiBatch();

            eBatch.Interchanges.Add(eInterchange);

            //Add all service segments
            EdiDataWriterSettings settings = new EdiDataWriterSettings(
                new SegmentDefinitions.ISA(), new SegmentDefinitions.IEA(),
                new SegmentDefinitions.GS(), new SegmentDefinitions.GE(),
                new SegmentDefinitions.ST(), new SegmentDefinitions.SE(),
                "ZZ", "SENDER", "ZZ", "RECEIVER", "GSSENDER", "GSRECEIVER", "00401", "004010", "T", 100, 200, "\r\n", "*");

            EdiDataWriter ediWriter = new EdiDataWriter(settings);

            return(ediWriter.WriteToString(eBatch));
        }
Exemplo n.º 2
0
        public void JsonReadWrite_DeserializeJsonWithValidationErrors()
        {
            string json = TestUtils.ReadResourceStream("EdiEngine.Tests.TestData.940.ERR.json");

            M_940         map = new M_940();
            JsonMapReader r   = new JsonMapReader(map);

            EdiTrans t = r.ReadToEnd(json);

            Assert.AreEqual(2, t.ValidationErrors.Count);
        }
Exemplo n.º 3
0
        public void JsonReadWrite_DeserializeJsonHlLoopOk()
        {
            string json = TestUtils.ReadResourceStream("EdiEngine.Tests.TestData.856.Crossdock.OK.json");

            M_856         map = new M_856();
            JsonMapReader r   = new JsonMapReader(map);

            EdiTrans t = r.ReadToEnd(json);

            Assert.AreEqual(0, t.ValidationErrors.Count);

            //string edi = TestUtils.WriteEdiEnvelope(t, "SH");
        }
Exemplo n.º 4
0
        public void JsonReadWrite_DeserializeComposite()
        {
            string json = TestUtils.ReadResourceStream("EdiEngine.Tests.TestData.001.Fake.Composite.json");

            M_001         map = new M_001();
            JsonMapReader r   = new JsonMapReader(map);

            EdiTrans t = r.ReadToEnd(json);

            Assert.AreEqual(0, t.ValidationErrors.Count);

            var sln = (EdiSegment)t.Content.First();

            Assert.IsTrue(sln.Content[4] is EdiCompositeDataElement);
            Assert.AreEqual(6, ((EdiCompositeDataElement)sln.Content[4]).Content.Count);
        }
Exemplo n.º 5
0
        public void EdiWriter_FromJson()
        {
            //get sample json
            string jsonTrans;

            using (
                Stream s =
                    GetType().Assembly.GetManifestResourceStream("EdiEngine.Tests.TestData.940.OK.json"))
            {
                if (s == null)
                {
                    throw new InvalidDataException("stream is null");
                }

                using (StreamReader sr = new StreamReader(s))
                {
                    jsonTrans = sr.ReadToEnd();
                }
            }

            //Read json and convert it to trans
            M_940         map = new M_940();
            JsonMapReader r   = new JsonMapReader(map);
            EdiTrans      t   = r.ReadToEnd(jsonTrans);

            //write EDI
            string data = TestUtils.WriteEdiEnvelope(t, "OW");

            //Read produced results and check for errors and correct parsing
            EdiDataReader reader = new EdiDataReader();
            EdiBatch      batch  = reader.FromString(data);

            Assert.AreEqual(1, batch.Interchanges.Count);
            Assert.AreEqual(0, batch.Interchanges[0].ValidationErrors.Count);

            Assert.AreEqual(1, batch.Interchanges[0].Groups.Count);
            Assert.AreEqual(0, batch.Interchanges[0].Groups[0].ValidationErrors.Count);

            EdiTrans trans = batch.Interchanges[0].Groups[0].Transactions[0];

            Assert.AreEqual(0, trans.ValidationErrors.Count);

            int w05Count = trans.Content.Count(l => l.Definition.GetType() == typeof(SegmentDefinitions.W05));
            int n1Count  = trans.Content.Count(l => l.Definition.GetType() == typeof(M_940.L_N1));
            int n1FirstIterationCount =
                ((EdiLoop)trans.Content.First(l => l.Definition.GetType() == typeof(M_940.L_N1))).Content.Count;
            int n1SecondIterationCount =
                ((EdiLoop)trans.Content.Where(l => l.Definition.GetType() == typeof(M_940.L_N1)).Skip(1).First())
                .Content.Count;
            int n1ThirdIterationCount =
                ((EdiLoop)trans.Content.Where(l => l.Definition.GetType() == typeof(M_940.L_N1)).Skip(2).First())
                .Content.Count;
            int n9Count  = trans.Content.Count(l => l.Definition.GetType() == typeof(SegmentDefinitions.N9));
            int g62Count = trans.Content.Count(l => l.Definition.GetType() == typeof(SegmentDefinitions.G62));
            int nteCount = trans.Content.Count(l => l.Definition.GetType() == typeof(SegmentDefinitions.NTE));
            int w66Count = trans.Content.Count(l => l.Definition.GetType() == typeof(SegmentDefinitions.W66));
            int lxCount  = trans.Content.Count(l => l.Definition.GetType() == typeof(M_940.L_LX));
            int lxFirstIterationCount =
                ((EdiLoop)trans.Content.First(l => l.Definition.GetType() == typeof(M_940.L_LX))).Content.Count;
            int lxFirstIterationW01Count =
                ((EdiLoop)((EdiLoop)trans.Content.First(l => l.Definition.GetType() == typeof(M_940.L_LX)))
                 .Content.Skip(1).First()).Content.Count;
            int w76Count = trans.Content.Count(l => l.Definition.GetType() == typeof(SegmentDefinitions.W76));

            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);
        }