示例#1
0
        public IList <TransactionItem> Read(Stream stream)
        {
            var          result    = new List <TransactionItem>();
            var          errorList = new List <string>();
            Transactions transaction;

            try
            {
                var serializer = new XmlSerializer(typeof(Transactions));
                transaction = (Transactions)serializer.Deserialize(XmlReader.Create(stream));
            }
            catch (InvalidOperationException)
            {
                throw new UnKnowFormatException();
            }

            if (transaction.Transaction == null)
            {
                throw new TransactionValidateErrorException("Transaction is empty");
            }

            foreach (var t in transaction.Transaction)
            {
                if (_validator.Validate(t, out string error))
                {
                    result.Add(new TransactionItem
                    {
                        TransactionId   = t.id,
                        CurrencyCode    = t.PaymentDetails.CurrencyCode,
                        Amount          = decimal.Parse(t.PaymentDetails.Amount),
                        TransactionDate = DateTime.ParseExact(t.TransactionDate, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture),
                        Status          = StaticValue.XmlStatusList[t.Status]
                    });
                }
                else
                {
                    errorList.Add(error);
                }
            }

            if (errorList.Any())
            {
                var errorMessage = string.Join(Environment.NewLine, errorList);
                throw new TransactionValidateErrorException(errorMessage);
            }

            return(result);
        }
        public void Validate_Should_ReturnTrueWithoutErrorMessage_When_AllFieldsAreValid()
        {
            var trans = new TransactionsTransaction
            {
                id             = "Inv00001",
                PaymentDetails = new TransactionsTransactionPaymentDetails
                {
                    Amount       = "200.00",
                    CurrencyCode = "USD"
                },
                TransactionDate = "2019-01-23T13:45:10",
                Status          = "Rejected"
            };

            var result = _validator.Validate(trans, out string error);

            Assert.IsTrue(result);
            Assert.AreEqual(string.Empty, error);
        }