예제 #1
0
        public void ShouldAddTransactiont()
        {
            //Arrange
            var transactionTableRepository = Substitute.For <ITableRepository <TransactionHistory> >();
            TransactionHistoryService transactionService = new TransactionHistoryService(transactionTableRepository);
            TransactionInfo           transactInfo       = new TransactionInfo
            {
                CustomerOrderId = 1,
                SalerOrderId    = 1,
                TrDateTime      = new DateTime(2019, 08, 21)
            };

            //Act
            transactionService.AddTransactionInfo(transactInfo);
            //Assert
            transactionTableRepository.Received(1).Add(Arg.Is <TransactionHistory>(
                                                           w => w.CustomerOrderID == 1 &&
                                                           w.SalerOrderID == 1 &&
                                                           w.TransactionDateTime == new DateTime(2019, 08, 21)

                                                           ));
        }
예제 #2
0
        public void RunTraiding()
        {
            int loopcount = 10;

            for (int i = 0; i < loopcount; i++)
            {
                int amountInLotForSale = 10;


                //Select random saler
                Client saler = GetRandomClient();
                //Select random stock for saler
                ClientStock clstock = GetRandomClientStock(saler.ClientID);
                if (clstock == null || clstock.Quantity < 10)
                {
                    continue;
                }

                //determine amount for sale
                int    lotsAmount    = clstock.Quantity / amountInLotForSale;
                Random random        = new Random();
                int    amountForSale = random.Next(1, lotsAmount) * amountInLotForSale;

                orderService.AddOrder(new OrderInfo()
                {
                    ClientId  = clstock.ClientID,
                    StockId   = clstock.StockID,
                    Quantity  = amountForSale,
                    OrderType = OrderInfo.OrdType.Sale
                });
                this.logger.Info($"Order for sale stock {clstock.StockID} for client {clstock.ClientID} has been added to DB");

                Order salerOrder = orderService.LastOrder();

                Client customer;
                do
                {
                    customer = GetRandomClient();
                }while (customer.ClientID == saler.ClientID);

                orderService.AddOrder(new OrderInfo()
                {
                    ClientId  = customer.ClientID,
                    StockId   = clstock.StockID,
                    Quantity  = amountForSale,
                    OrderType = OrderInfo.OrdType.Purchase
                });
                this.logger.Info($"Order for purchasing stock {clstock.StockID} for client {customer.ClientID} has been added to DB");

                Order customerOrder = orderService.LastOrder();

                DateTime dealDateTime = DateTime.Now;
                decimal  dealPrice    = priceHistoryService.GetStockPriceByDateTime(new PriceArguments()
                {
                    StockId        = clstock.StockID,
                    DateTimeLookUp = dealDateTime
                });

                clientService.EditClientBalance(saler.ClientID, (dealPrice * amountForSale));
                this.logger.Info($"Client {saler.ClientID} balance has been increased by {(dealPrice * amountForSale)}");
                clientService.EditClientBalance(customer.ClientID, (-1 * dealPrice * amountForSale));
                this.logger.Info($"Client {customer.ClientID} balance has been reduced by {(dealPrice * amountForSale)}");
                clientStockService.EditClientStocksAmount(saler.ClientID, clstock.StockID, -amountForSale);
                this.logger.Info($"Client {saler.ClientID} stock {salerOrder.StockID} amount has been reduced on {amountForSale}");
                clientStockService.EditClientStocksAmount(customer.ClientID, clstock.StockID, amountForSale);
                this.logger.Info($"Client {customer.ClientID} stock {salerOrder.StockID} amount has been increased on {amountForSale}");


                transactionHistoryService.AddTransactionInfo(new TransactionInfo()
                {
                    CustomerOrderId = customerOrder.OrderID,
                    SalerOrderId    = salerOrder.OrderID,
                    TrDateTime      = dealDateTime
                });
                this.logger.Info($"Transaction has been added to DB");

                orderService.SetIsExecuted(salerOrder);
                this.logger.Info($"Saler's order {salerOrder.OrderID} status has been set as 'IsExecuted'");
                orderService.SetIsExecuted(customerOrder);
                this.logger.Info($"Customer's order {customerOrder.OrderID} status has been set as 'IsExecuted'");
                this.logger.Info($"Deal is finished");

                this.SimulatePriceChange(salerOrder.StockID, dealPrice, dealDateTime);
                this.logger.Info($"Stock {salerOrder.StockID} price has been changed'");


                Thread.Sleep(10000);
            }
        }