public void PutTransactionTest()
        {
            var transaction = new TransactionEntity
            {
                AccountId = 3,
                Amount = 100,
                Details = "Test Details",
                IsDeposit = true,
                Message = "Test Transaction"
            };
            var accountServiceMock = new Mock<IAccountService>();
            accountServiceMock.Setup(ser => ser.ExecuteTransaction(transaction)).Returns(transaction);

            var controller = new TransactionController(accountServiceMock.Object)
            {
                Configuration = new HttpConfiguration(),
                Request = new HttpRequestMessage
                {
                    Method = HttpMethod.Post,
                    RequestUri = new Uri($"http://{Localhost}/api/account")
                }
            };
            controller.Configuration.MapHttpAttributeRoutes();
            controller.Configuration.EnsureInitialized();
            controller.RequestContext.RouteData = new HttpRouteData(new HttpRoute(), new HttpRouteValueDictionary
            {
                {"controller", "Transaction"}
            });

            var httpResponse = controller.PutTransaction(3, transaction);
            var trans = httpResponse.Content.ReadAsAsync<TransactionEntity>().Result;
            Assert.IsNotNull(trans, "Transaction was NULL");
            Assert.AreEqual(trans.AccountId, 3, "Account ID was different");
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Execute a transaction
        /// </summary>
        /// <param name="transaction">Transaction object</param>
        public TransactionEntity ExecuteTransaction(TransactionEntity transaction)
        {
            try
            {
                var account =
                    _instance.DataContext.Account.FirstOrDefault(a => a.Account_Id.Equals(transaction.AccountId));
                if (account == null)
                {
                    throw WebExceptionFactory.GetNotFoundError("Account not found");
                }
                if (transaction.IsDeposit)
                {
                    account.Account_Balance += transaction.Amount;
                }
                else
                {
                    if (account.Account_Balance < transaction.Amount)
                    {
                        throw WebExceptionFactory.GetBadRequestError(
                            "Low availble funds cannot execute the transaction.");
                    }
                    account.Account_Balance -= transaction.Amount;
                }

                // Create a transaction 
                var trans = new Transaction
                {
                    Account_Id = transaction.AccountId,
                    Amount = transaction.Amount,
                    Transaction_Type = transaction.IsDeposit ? 1 : 2,
                    Message = transaction.Message,
                    Details = transaction.Details,
                    Timestamp = DateTime.Now.ToString("yyyyMMddHHmmssffff")
                };
                var newTransaction = _instance.DataContext.Transaction.Add(trans);
                Log.Info($"new transaction has been created with id {newTransaction.Transaction_Id}");
                Commit();
                Mapper.CreateMap<Transaction, TransactionEntity>();
                return Mapper.Map<TransactionEntity>(newTransaction);
            }
            catch (ArgumentNullException ex)
            {
                const string msg = "Error occured while executing a transaction in the account";
                throw WebExceptionFactory.GetBadRequestError(msg, ex);
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Log.Error(
                        $"Entity of type \"{eve.Entry.Entity.GetType().Name}\" in state \"{eve.Entry.State}\" has the following validation errors:");
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Log.Error($"- Property: \"{ve.PropertyName}\", Error: \"{ve.ErrorMessage}\"");
                    }
                }
                throw WebExceptionFactory.GetBadRequestError("Database error", e);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw WebExceptionFactory.GetServerError(ex.Message);
            }
        }