Ejemplo n.º 1
0
        public void TestSerializationOfMessageErrorContext()
        {
            var segContext1 = new SegmentErrorContext("Name", 66, ErrorCodes.ComponentDataElementsTooMany);

            segContext1.Add(ErrorCodes.DataElementsTooMany);
            segContext1.Add("Name1", 66, ErrorCodes.ComponentDataElementsTooMany, 32, 96, "876");
            segContext1.Add("Name2", 88, ErrorCodes.ControlNumberNotMatching, 16, 48, "438");

            var segContext2 = new SegmentErrorContext("Name2", 32, ErrorCodes.ComponentDataElementsTooMany);

            segContext2.Add(ErrorCodes.DataElementsTooMany);
            segContext2.Add("Name1", 66, ErrorCodes.ComponentDataElementsTooMany, 16, 48, "476");
            segContext2.Add("Name2", 88, ErrorCodes.ControlNumberNotMatching, 5, 5, "200");

            var msgContext = new MessageErrorContext("MsgError", "111111");

            msgContext.Add(ErrorCodes.DataElementsTooMany);
            msgContext.Add(segContext1);
            msgContext.Add(segContext2);


            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream    = new MemoryStream();

            formatter.Serialize(stream, msgContext);
            stream.Seek(0, SeekOrigin.Begin);
            var copy = (MessageErrorContext)formatter.Deserialize(stream);

            AssertTwoMessageErrorsAreEqual(msgContext, copy);
        }
Ejemplo n.º 2
0
        private static SegmentErrorContext BuildCompositeDataElementContext(string failedElement, XElement originalParent, ErrorCodes errorCode)
        {
            if (failedElement == null || originalParent == null || originalParent.Document == null)
            {
                return(null);
            }

            var segments = originalParent.Document.Descendants()
                           .Where(
                d =>
                d.Name.LocalName.StartsWith("S_", StringComparison.Ordinal))
                           .ToList();

            int dataElementPosition = 0;
            int repetitionPosition  = 0;
            var lastElement         = originalParent.Elements().LastOrDefault(e => e.Name.LocalName == failedElement);
            var firstElement        = originalParent.Elements().FirstOrDefault(e => e.Name.LocalName == failedElement);

            if (lastElement != null && firstElement != null)
            {
                dataElementPosition = originalParent.Elements().ToList().IndexOf(firstElement) + 1;
                repetitionPosition  = lastElement.ElementsBeforeSelf().Count(e => e.Name.LocalName == failedElement) + 1;
            }

            var newErroCode = dataElementPosition == 0 ? ErrorCodes.RequiredMissing : errorCode;
            var context     = new SegmentErrorContext(ExtractName(originalParent.Name.LocalName), segments.IndexOf(originalParent) + 1);

            context.Add(ExtractName(failedElement), dataElementPosition, newErroCode, 0, repetitionPosition, null);

            return(context);
        }
Ejemplo n.º 3
0
        private SegmentErrorContext ValidateDataElement(IList list, InstanceContext instanceContext,
                                                        int segmentIndex, int inSegmentIndex, int inCompositeIndex)
        {
            if (instanceContext.Parent == null)
            {
                throw new Exception(
                          string.Format("Parent of data element {0} must be either a segment or a composite.",
                                        instanceContext.Property.Name));
            }

            var errorCode = list.Count > MaxCount
                ? DataElementErrorCode.TooManyRepetitions
                : DataElementErrorCode.TooFewRepetitions;

            var repIndex = list.Count > MaxCount
                ? MaxCount + 1
                : MinCount - list.Count + 1;

            var value = list.Count > MaxCount
                ? list[MaxCount] as string
                : null;

            var segmentName = instanceContext.Parent.IsPropertyOfType <SegmentAttribute>()
                ? instanceContext.Parent.GetId()
                : instanceContext.Parent.GetDeclaringTypeId();

            var segmentType = instanceContext.Parent.IsPropertyOfType <SegmentAttribute>()
                ? instanceContext.Parent.GetStandardType()
                : instanceContext.Parent.Property.GetStandardDeclaringType();

            if (string.IsNullOrEmpty(segmentName) && instanceContext.Parent.Instance != null)
            {
                var type         = instanceContext.Parent.Instance.GetStandardType();
                var ediAttribute = type.GetCustomAttribute <EdiAttribute>();
                if (ediAttribute == null)
                {
                    throw new Exception(string.Format("Can't find segment name for {0}", GetType().Name));
                }

                segmentName = ediAttribute.Id;
                segmentType = type;
            }

            var dataElementAttr = instanceContext.Property.GetCustomAttribute <DataElementAttribute>();
            var name            = dataElementAttr == null ? "" : dataElementAttr.Code;

            var result       = new SegmentErrorContext(segmentName, segmentIndex, segmentType);
            var errorContext = new DataElementErrorContext(name, inSegmentIndex, errorCode, inCompositeIndex, repIndex, value);

            result.Add(errorContext);
            return(result);
        }
Ejemplo n.º 4
0
        private SegmentErrorContext ValidateComposite(InstanceContext instanceContext,
                                                      int segmentIndex, int inSegmentIndex)
        {
            if (instanceContext.Parent == null || !instanceContext.Parent.IsPropertyOfType <SegmentAttribute>())
            {
                throw new Exception(string.Format("Parent of composite {0} must be a segment.",
                                                  instanceContext.Property.Name));
            }

            var result       = new SegmentErrorContext(instanceContext.Parent.GetId(), segmentIndex, instanceContext.Parent.GetStandardType());
            var errorContext = new DataElementErrorContext(instanceContext.GetId(), inSegmentIndex,
                                                           DataElementErrorCode.RequiredDataElementMissing, 0, 0, null);

            result.Add(errorContext);

            return(result);
        }
Ejemplo n.º 5
0
        private SegmentErrorContext ValidateDataElement(string value, InstanceContext instanceContext,
                                                        int segmentIndex, int inSegmentIndex, int inCompositeIndex, int repetitionIndex)
        {
            if (instanceContext.Parent == null)
            {
                throw new Exception(
                          string.Format("Parent of data element {0} must be either a segment or a composite.",
                                        instanceContext.Property.Name));
            }

            var errorCode = value.Length < MinLen
                ? DataElementErrorCode.DataElementTooShort
                : DataElementErrorCode.DataElementTooLong;

            var segmentName = instanceContext.Parent.IsPropertyOfType <SegmentAttribute>()
                ? instanceContext.Parent.GetId()
                : instanceContext.Parent.GetDeclaringTypeId();

            var segmentType = instanceContext.Parent.IsPropertyOfType <SegmentAttribute>()
                ? instanceContext.Parent.GetStandardType()
                : instanceContext.Parent.Property.GetStandardDeclaringType();

            if (string.IsNullOrEmpty(segmentName) && instanceContext.Parent.Instance != null)
            {
                var type         = instanceContext.Parent.Instance.GetStandardType();
                var ediAttribute = type.GetCustomAttribute <EdiAttribute>();
                if (ediAttribute == null)
                {
                    throw new Exception(string.Format("Can't find segment name for {0}", GetType().Name));
                }

                segmentName = ediAttribute.Id;
                segmentType = type;
            }

            var dataElementAttr = instanceContext.Property.GetCustomAttribute <DataElementAttribute>();
            var name            = dataElementAttr == null ? "" : dataElementAttr.Code;

            var result       = new SegmentErrorContext(segmentName, segmentIndex, segmentType);
            var errorContext = new DataElementErrorContext(name, inSegmentIndex, errorCode, inCompositeIndex,
                                                           repetitionIndex, value);

            result.Add(errorContext);
            return(result);
        }
Ejemplo n.º 6
0
        public void TestSerializationOfSegmentErrorContext()
        {
            var context = new SegmentErrorContext("Name", 66, ErrorCodes.ComponentDataElementsTooMany);

            context.Add(ErrorCodes.DataElementsTooMany);
            context.Add("Name1", 66, ErrorCodes.ComponentDataElementsTooMany, 32, 96, "876");
            context.Add("Name2", 88, ErrorCodes.ControlNumberNotMatching, 16, 48, "438");


            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream    = new MemoryStream();

            formatter.Serialize(stream, context);
            stream.Seek(0, SeekOrigin.Begin);
            var copy = (SegmentErrorContext)formatter.Deserialize(stream);

            AssertTwoSegmentErrorsAreEqual(context, copy);
        }
Ejemplo n.º 7
0
        public void TestSerializationOfParsingException()
        {
            var segContext1 = new SegmentErrorContext("Name", 66, ErrorCodes.ComponentDataElementsTooMany);

            segContext1.Add(ErrorCodes.DataElementsTooMany);
            segContext1.Add("Name1", 66, ErrorCodes.ComponentDataElementsTooMany, 32, 96, "876");
            segContext1.Add("Name2", 88, ErrorCodes.ControlNumberNotMatching, 16, 48, "438");

            var segContext2 = new SegmentErrorContext("Name2", 32, ErrorCodes.ComponentDataElementsTooMany);

            segContext2.Add(ErrorCodes.DataElementsTooMany);
            segContext2.Add("Name1", 66, ErrorCodes.ComponentDataElementsTooMany, 16, 48, "476");
            segContext2.Add("Name2", 88, ErrorCodes.ControlNumberNotMatching, 5, 5, "200");

            var msgContext = new MessageErrorContext("MsgError", "111111");

            msgContext.Add(ErrorCodes.DataElementsTooMany);
            msgContext.Add(segContext1);
            msgContext.Add(segContext2);

            ParsingException ex;

            try
            {
                throw new ParsingException(ErrorCodes.SegmentWithErrors, "message", "HL*1*0*20*1~", msgContext);
            }
            catch (ParsingException thrown)
            {
                ex = thrown;
            }

            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream    = new MemoryStream();

            formatter.Serialize(stream, ex);
            stream.Seek(0, SeekOrigin.Begin);
            var copy = (ParsingException)formatter.Deserialize(stream);

            //var copy = new ParsingException(ErrorCodes.ControlNumberNotMatching, "message2");

            AssertTwoParsingExceptionsAreEqual(ex, copy);
        }
Ejemplo n.º 8
0
        void AssertTwoSegmentErrorsAreEqual(SegmentErrorContext context, SegmentErrorContext other)
        {
            Assert.AreEqual(context.Name, other.Name, "Name");
            Assert.AreEqual(context.Position, other.Position, "Position");
            Assert.AreEqual(context.Codes.Count, other.Codes.Count, "Error Code Count");

            var contextCodesArray = other.Codes.ToArray();
            var copyCodesArray    = context.Codes.ToArray();

            for (int i = 0; i < context.Codes.Count; i++)
            {
                Assert.AreEqual(copyCodesArray[i], contextCodesArray[i]);
            }

            Assert.AreEqual(context.Errors.Count, other.Errors.Count);
            var copyErrorsArray    = context.Errors.ToArray();
            var contextErrorsArray = other.Errors.ToArray();

            for (int i = 0; i < context.Errors.Count; i++)
            {
                AssertTwoDataElementErrorsAreEqual(contextErrorsArray[i], copyErrorsArray[i]);
            }
        }
Ejemplo n.º 9
0
        private SegmentErrorContext ValidateComposite(IList list, InstanceContext instanceContext,
                                                      int segmentIndex, int inSegmentIndex)
        {
            if (instanceContext.Parent == null || !instanceContext.Parent.IsPropertyOfType <SegmentAttribute>())
            {
                throw new Exception(string.Format("Parent of composite {0} must be a segment.",
                                                  instanceContext.Property.Name));
            }

            var errorCode = list.Count > MaxCount
                ? DataElementErrorCode.TooManyRepetitions
                : DataElementErrorCode.TooFewRepetitions;

            var repIndex = list.Count > MaxCount
                ? MaxCount + 1
                : MinCount - list.Count + 1;

            var result       = new SegmentErrorContext(instanceContext.Parent.GetId(), segmentIndex, instanceContext.Parent.GetStandardType());
            var errorContext = new DataElementErrorContext(instanceContext.GetId(), inSegmentIndex, errorCode, 0,
                                                           repIndex, null);

            result.Add(errorContext);
            return(result);
        }
Ejemplo n.º 10
0
        public MessageErrorContext Analyze(IEnumerable <SegmentContext> segments, MessageContext messageContext,
                                           Separators separators, int partsIndex, int segmentIndex)
        {
            var errorContext = new MessageErrorContext(messageContext.Name, messageContext.ControlNumber,
                                                       messageContext.Version, partsIndex,
                                                       "Message was parsed with errors.");

            var currSeg = Children.First() as Segment;
            var index   = segmentIndex;

            foreach (var segment in segments)
            {
                Segment tempSeg;
                index++;
                if (segment.IsJump)
                {
                    tempSeg =
                        this.Descendants <Segment>()
                        .LastOrDefault(
                            d => d.EdiName == "HL" && d.Children.Count > 1 && d.Children.ElementAt(1).Value == segment.SecondValue);

                    if (tempSeg != null)
                    {
                        currSeg = tempSeg;
                    }
                }

                tempSeg = currSeg.TraverseDepthFirst().FirstOrDefault(n => n.Match(segment));
                if (tempSeg == null)
                {
                    var      errorCode       = SegmentErrorCode.SegmentNotInProperSequence;
                    TypeInfo type            = null;
                    var      notFoundSegment = this.Descendants <Segment>().FirstOrDefault(d => d.EdiName == segment.Name);
                    if (notFoundSegment == null)
                    {
                        errorCode = SegmentErrorCode.UnrecognizedSegment;
                    }
                    else
                    {
                        type = notFoundSegment.TypeInfo;
                    }

                    errorContext.Add(new SegmentErrorContext(segment.Name, index, type,
                                                             segment.Value, errorCode));

                    if (messageContext.PartialAllowed)
                    {
                        // ReSharper disable once RedundantAssignment
                        tempSeg = currSeg;
                        continue;
                    }

                    return(errorContext);
                }

                currSeg = tempSeg;

                if (currSeg.IsParsed)
                {
                    currSeg = (Segment)currSeg.InsertRepetition();
                }

                try
                {
                    if (separators == null)
                    {
                        currSeg.Parse(segment.Value, messageContext.PartialAllowed);
                    }
                    else
                    {
                        currSeg.Parse(segment.Value, separators, messageContext.PartialAllowed);
                    }
                }
                catch (ParserSegmentException ex)
                {
                    var segmentContext = new SegmentErrorContext(segment.Name, index, currSeg.TypeInfo, segment.Value);
                    segmentContext.Add(ex.ErrorContext);
                    errorContext.Add(segmentContext);

                    if (messageContext.PartialAllowed)
                    {
                        continue;
                    }

                    return(errorContext);
                }
            }

            return(errorContext.HasErrors ? errorContext : null);
        }