public void GetOrderBookTest_ChecksIfOrderBookIsRetreivedProperly_ValidatesReturnedOrderBook()
        {
            // Get the context
            // IApplicationContext applicationContext = ContextRegistry.GetContext();

            // Get the instance through Spring configuration
            MarketController marketController = (MarketController)_applicationContext["MarketController"];
            LimitOrderBook   limitOrderBook   = new LimitOrderBook("XBTUSD");

            Order buyOrder1 = OrderFactory.CreateOrder("1233", "XBTUSD", Constants.ORDER_TYPE_LIMIT,
                                                       Constants.ORDER_SIDE_BUY, 150, 481.34M, new StubbedOrderIdGenerator());
            Order buyOrder2 = OrderFactory.CreateOrder("1234", "XBTUSD", Constants.ORDER_TYPE_LIMIT,
                                                       Constants.ORDER_SIDE_BUY, 50, 482.34M, new StubbedOrderIdGenerator());
            Order buyOrder3 = OrderFactory.CreateOrder("1222", "XBTUSD", Constants.ORDER_TYPE_LIMIT,
                                                       Constants.ORDER_SIDE_BUY, 50, 483.34M, new StubbedOrderIdGenerator());
            Order sellOrder1 = OrderFactory.CreateOrder("1233", "XBTUSD", Constants.ORDER_TYPE_LIMIT,
                                                        Constants.ORDER_SIDE_SELL, 150, 491.34M, new StubbedOrderIdGenerator());
            Order sellOrder2 = OrderFactory.CreateOrder("1234", "XBTUSD", Constants.ORDER_TYPE_LIMIT,
                                                        Constants.ORDER_SIDE_SELL, 50, 492.34M, new StubbedOrderIdGenerator());
            Order sellrder3 = OrderFactory.CreateOrder("1222", "XBTUSD", Constants.ORDER_TYPE_LIMIT,
                                                       Constants.ORDER_SIDE_SELL, 50, 493.34M, new StubbedOrderIdGenerator());

            limitOrderBook.PlaceOrder(buyOrder1);
            limitOrderBook.PlaceOrder(buyOrder2);
            limitOrderBook.PlaceOrder(buyOrder3);
            limitOrderBook.PlaceOrder(sellOrder1);
            limitOrderBook.PlaceOrder(sellOrder2);
            limitOrderBook.PlaceOrder(sellrder3);

            OrderBookMemoryImage orderBookMemoryImage = (OrderBookMemoryImage)_applicationContext["OrderBookMemoryImage"];

            orderBookMemoryImage.OnOrderBookChanged(limitOrderBook);
            IHttpActionResult httpActionResult = marketController.GetOrderBook("XBTUSD");

            OkNegotiatedContentResult <object> okResponseMessage =
                (OkNegotiatedContentResult <object>)httpActionResult;
            OrderBookRepresentation representation = okResponseMessage.Content as OrderBookRepresentation;

            Assert.IsNotNull(okResponseMessage);
            Assert.IsNotNull(representation);
            // Check the Currency Pair for Bid Book
            Assert.AreEqual("XBTUSD", representation.Bids.CurrencyPair);
            // Check the Currency Pair for Ask Book
            Assert.AreEqual("XBTUSD", representation.Asks.CurrencyPair);

            // Count of the number of Bids in the Bid Order Book
            Assert.AreEqual(3, representation.Bids.Count());
            // Count of the number of Asks in the Ask Order Book
            Assert.AreEqual(3, representation.Asks.Count());

            Assert.AreEqual(50, representation.Bids.ToList()[0].Volume);     // Highest Bid Volumein Bid Order Book
            Assert.AreEqual(483.34M, representation.Bids.ToList()[0].Price); // Highest Bid Price in Bid Order Book

            Assert.AreEqual(150, representation.Asks.ToList()[0].Volume);    // Highest Ask Volumein Ask Order Book
            Assert.AreEqual(491.34M, representation.Asks.ToList()[0].Price); // Highest Ask Price in Ask Order Book
        }
        public void ReplayOrderBook_IfScenario2IsExecuted_VerifyTheWholeSystemState()
        {
            _applicationContext = ContextRegistry.GetContext();
            Scenario2();
            MarketController  marketController     = (MarketController)_applicationContext["MarketController"];
            IHttpActionResult marketDataHttpResult = marketController.GetOrderBook("XBTUSD");

            OkNegotiatedContentResult <object> okResponseMessage =
                (OkNegotiatedContentResult <object>)marketDataHttpResult;
            OrderBookRepresentation representation = okResponseMessage.Content as OrderBookRepresentation;
            Tuple <OrderRepresentationList, OrderRepresentationList> orderBooks = new Tuple <OrderRepresentationList, OrderRepresentationList>(representation.Bids, representation.Asks);

            marketDataHttpResult = marketController.GetDepth("XBTUSD");
            OkNegotiatedContentResult <object> okResponseMessageDepth = (OkNegotiatedContentResult <object>)marketDataHttpResult;
            DepthTupleRepresentation           beforeReplayDepth      = okResponseMessageDepth.Content as DepthTupleRepresentation;

            IOrderRepository      orderRepository = (IOrderRepository)_applicationContext["OrderRepository"];
            List <OrderReadModel> before          = orderRepository.GetAllOrderOfTrader("5555");

            before = before.Concat(orderRepository.GetAllOrderOfTrader("4444")).ToList();

            ITradeRepository       tradeRepository    = (ITradeRepository)_applicationContext["TradeRepository"];
            IList <TradeReadModel> beforeReplayTrades = tradeRepository.GetAll();
            IList <object>         beforeReplayEvents = outputEventStore.GetAllEvents();

            marketDataHttpResult = marketController.GetBbo("XBTUSD");
            OkNegotiatedContentResult <BBORepresentation> okResponseMessageBboBefore = (OkNegotiatedContentResult <BBORepresentation>)marketDataHttpResult;

            //down the exchange, make new exchange and reply
            CrashAndInitializeAgainWithSnapshot();

            marketController     = (MarketController)_applicationContext["MarketController"];
            marketDataHttpResult = marketController.GetOrderBook("XBTUSD");

            okResponseMessage =
                (OkNegotiatedContentResult <object>)marketDataHttpResult;
            representation = okResponseMessage.Content as OrderBookRepresentation;
            Tuple <OrderRepresentationList, OrderRepresentationList> orderBook1 = new Tuple <OrderRepresentationList, OrderRepresentationList>(representation.Bids, representation.Asks);

            //verify orderbook state
            VerifyOrderBookStateAfterReplay(orderBooks, orderBook1);

            List <OrderReadModel> after = orderRepository.GetAllOrderOfTrader("5555");

            after = after.Concat(orderRepository.GetAllOrderOfTrader("4444")).ToList();
            //verify order table in database
            VerifyDatabaseStateAfterReplay(before, after);
            IList <TradeReadModel> afterReplayTrades = tradeRepository.GetAll();

            //verify trades table in database
            VerifyDatabaseStateAfterReplay(beforeReplayTrades, afterReplayTrades);
            IList <object> afterReplayEvents = outputEventStore.GetAllEvents();

            //verify event store state
            VerifyEventStoreStateAfterReplay(beforeReplayEvents, afterReplayEvents);

            marketDataHttpResult   = marketController.GetDepth("XBTUSD");
            okResponseMessageDepth = (OkNegotiatedContentResult <object>)marketDataHttpResult;
            DepthTupleRepresentation afterReplayDepth = okResponseMessageDepth.Content as DepthTupleRepresentation;

            VerifyDepthBeforeAndAfterReplay(beforeReplayDepth, afterReplayDepth);

            marketDataHttpResult = marketController.GetBbo("XBTUSD");
            OkNegotiatedContentResult <BBORepresentation> okResponseMessageBboAfter = (OkNegotiatedContentResult <BBORepresentation>)marketDataHttpResult;

            VerifyBboAfterReplay(okResponseMessageBboBefore.Content, okResponseMessageBboAfter.Content);
        }
        public void Scenario3Test_TestsScenario3AndItsOutcome_VerifiesThroughMarketDataOrderAndTradesResults()
        {
            // Get the context
            IApplicationContext applicationContext = ContextRegistry.GetContext();

            // Get the instance through Spring configuration
            OrderController  orderController  = (OrderController)applicationContext["OrderController"];
            TradeController  tradeController  = (TradeController)applicationContext["TradeController"];
            MarketController marketController = (MarketController)applicationContext["MarketController"];

            orderController.Request = new HttpRequestMessage(HttpMethod.Post, "");
            orderController.Request.Headers.Add("Auth", "123456789");
            tradeController.Request = new HttpRequestMessage(HttpMethod.Post, "");
            tradeController.Request.Headers.Add("Auth", "123456789");
            marketController.Request = new HttpRequestMessage(HttpMethod.Post, "");
            marketController.Request.Headers.Add("Auth", "123456789");
            Scenario3OrderCreation(orderController);

            // ------------------------------- Order Book ------------------------------
            IHttpActionResult orderBookResponse = marketController.GetOrderBook("BTCLTC");

            OkNegotiatedContentResult <object> okOrderBookResponse =
                (OkNegotiatedContentResult <object>)orderBookResponse;

            OrderBookRepresentation representation = okOrderBookResponse.Content as OrderBookRepresentation;
            Tuple <OrderRepresentationList, OrderRepresentationList> orderBook = new Tuple <OrderRepresentationList, OrderRepresentationList>(representation.Bids, representation.Asks);

            // Item1 = Bid Book, Item1[i].Item1 = Volume of 'i' Bid, Item1[i].Item2 = Price of 'i' Bid
            Assert.AreEqual(0, orderBook.Item1.Count());
            // Item2 = Bid Book, Item2[i].Item1 = Volume of Ask at index 'i', Item2[i].Item2 = Price of Bid at index 'i'
            Assert.AreEqual(0, orderBook.Item2.Count());
            // ------------------------------------------------------------------------

            // --------------------------------- Depth ---------------------------------
            IHttpActionResult depthResponse = marketController.GetDepth("BTCLTC");

            OkNegotiatedContentResult <object> okDepth
                = (OkNegotiatedContentResult <object>)depthResponse;

            DepthTupleRepresentation depth = okDepth.Content as DepthTupleRepresentation;

            // Item1 = Bid Book, Item1.Item1 = Aggregated Volume, Item1.Item2 = Price, Item1.Item3 = Number of Orders
            Assert.IsNull(depth.BidDepth[0]);
            Assert.IsNull(depth.BidDepth[1]);
            Assert.IsNull(depth.BidDepth[2]);
            Assert.IsNull(depth.BidDepth[3]);
            Assert.IsNull(depth.BidDepth[4]);

            // Item2 = Bid Book, Item2.Item1 = Aggregated Volume, Item2.Item2 = Price, Item2.Item3 = Number of Orders
            Assert.IsNull(depth.AskDepth[0]);
            Assert.IsNull(depth.AskDepth[1]);
            Assert.IsNull(depth.AskDepth[2]);
            Assert.IsNull(depth.AskDepth[3]);
            Assert.IsNull(depth.AskDepth[4]);

            // -----------------------------------------------------------------------

            //------------------------- Open Orders ----------------------------------
            IHttpActionResult openOrdersResponse = GetOpenOrders(orderController);
            OkNegotiatedContentResult <List <OrderReadModel> > okOpenOrdersResponse = (OkNegotiatedContentResult <List <OrderReadModel> >)openOrdersResponse;
            List <OrderReadModel> openOrders = okOpenOrdersResponse.Content;

            Assert.AreEqual(0, openOrders.Count);
            //---------------------------------------------------------------------

            //-------------------------- Closed Orders ----------------------------
            IHttpActionResult closedOrdersResponse = GetClosedOrders(orderController);
            OkNegotiatedContentResult <List <OrderReadModel> > okClosedOrdersResponse = (OkNegotiatedContentResult <List <OrderReadModel> >)closedOrdersResponse;
            List <OrderReadModel> closedOrders = okClosedOrdersResponse.Content;

            // Order List comes in descending order, so asserts are placed that way too
            Assert.AreEqual(252, closedOrders[8].Price);
            Assert.AreEqual(5, closedOrders[8].Volume);
            Assert.AreEqual(0, closedOrders[8].VolumeExecuted);
            Assert.AreEqual(5, closedOrders[8].OpenQuantity);
            Assert.AreEqual("Limit", closedOrders[8].Type);
            Assert.AreEqual("Sell", closedOrders[8].Side);
            Assert.AreEqual("Cancelled", closedOrders[8].Status);

            Assert.AreEqual(245, closedOrders[7].Price);
            Assert.AreEqual(8, closedOrders[7].Volume);
            Assert.AreEqual(0, closedOrders[7].VolumeExecuted);
            Assert.AreEqual(8, closedOrders[7].OpenQuantity);
            Assert.AreEqual("Limit", closedOrders[7].Type);
            Assert.AreEqual("Buy", closedOrders[7].Side);
            Assert.AreEqual("Cancelled", closedOrders[7].Status);

            Assert.AreEqual(250, closedOrders[6].Price);
            Assert.AreEqual(7, closedOrders[6].Volume);
            Assert.AreEqual(7, closedOrders[6].VolumeExecuted);
            Assert.AreEqual(0, closedOrders[6].OpenQuantity);
            Assert.AreEqual("Limit", closedOrders[6].Type);
            Assert.AreEqual("Buy", closedOrders[6].Side);
            Assert.AreEqual("Complete", closedOrders[6].Status);

            Assert.AreEqual(250, closedOrders[5].Price);
            Assert.AreEqual(3, closedOrders[5].Volume);
            Assert.AreEqual(2, closedOrders[5].VolumeExecuted);
            Assert.AreEqual(1, closedOrders[5].OpenQuantity);
            Assert.AreEqual("Limit", closedOrders[5].Type);
            Assert.AreEqual("Buy", closedOrders[5].Side);
            Assert.AreEqual("Cancelled", closedOrders[5].Status);

            Assert.AreEqual(253, closedOrders[4].Price);
            Assert.AreEqual(5, closedOrders[4].Volume);
            Assert.AreEqual(0, closedOrders[4].VolumeExecuted);
            Assert.AreEqual(5, closedOrders[4].OpenQuantity);
            Assert.AreEqual("Limit", closedOrders[4].Type);
            Assert.AreEqual("Sell", closedOrders[4].Side);
            Assert.AreEqual("Cancelled", closedOrders[4].Status);

            Assert.AreEqual(240, closedOrders[3].Price);
            Assert.AreEqual(8, closedOrders[3].Volume);
            Assert.AreEqual(0, closedOrders[3].VolumeExecuted);
            Assert.AreEqual(8, closedOrders[3].OpenQuantity);
            Assert.AreEqual("Limit", closedOrders[3].Type);
            Assert.AreEqual("Buy", closedOrders[3].Side);
            Assert.AreEqual("Cancelled", closedOrders[3].Status);

            Assert.AreEqual(245, closedOrders[2].Price);
            Assert.AreEqual(7, closedOrders[2].Volume);
            Assert.AreEqual(0, closedOrders[2].VolumeExecuted);
            Assert.AreEqual(7, closedOrders[2].OpenQuantity);
            Assert.AreEqual("Limit", closedOrders[2].Type);
            Assert.AreEqual("Buy", closedOrders[2].Side);
            Assert.AreEqual("Cancelled", closedOrders[2].Status);

            Assert.AreEqual(247, closedOrders[1].Price);
            Assert.AreEqual(3, closedOrders[1].Volume);
            Assert.AreEqual(0, closedOrders[1].VolumeExecuted);
            Assert.AreEqual(3, closedOrders[1].OpenQuantity);
            Assert.AreEqual("Limit", closedOrders[1].Type);
            Assert.AreEqual("Buy", closedOrders[1].Side);
            Assert.AreEqual("Cancelled", closedOrders[1].Status);

            Assert.AreEqual(0, closedOrders[0].Price);
            Assert.AreEqual(9, closedOrders[0].Volume);
            Assert.AreEqual(9, closedOrders[0].VolumeExecuted);
            Assert.AreEqual(0, closedOrders[0].OpenQuantity);
            Assert.AreEqual("Market", closedOrders[0].Type);
            Assert.AreEqual("Sell", closedOrders[0].Side);
            Assert.AreEqual("Complete", closedOrders[0].Status);
            //------------------------------------------------------------------------

            //------------------- Trades -------------------------

            IHttpActionResult tradesResponse = GetTrades(tradeController);
            OkNegotiatedContentResult <object> okTradeResponse = (OkNegotiatedContentResult <object>)tradesResponse;
            IList <object>   tradesintermediateList            = (IList <object>)okTradeResponse.Content;
            IList <object[]> trades = new List <object[]>();

            for (int i = 0; i < tradesintermediateList.Count; i++)
            {
                object[] objects = tradesintermediateList[i] as object[];
                trades.Add(objects);
            }

            // These trades execute simultaneously, so when queried from the database and sorted as per the time when
            // they were saved in the database, they can be queried out of the order as we expected because they have the
            // same time. So we check if one trade came before the other and place assertions for it then for the other and
            // vice versa
            if ((decimal)trades[1][3] == 7)
            {
                Assert.AreEqual(250, trades[1][2]);      // Price
                Assert.AreEqual(7, trades[1][3]);        // Volume
                Assert.AreEqual("BTCLTC", trades[1][4]); // CurrencyPair
            }
            else if ((decimal)trades[1][3] == 2)
            {
                Assert.AreEqual(250, trades[1][2]);
                Assert.AreEqual(2, trades[1][3]);
                Assert.AreEqual("BTCLTC", trades[1][4]);
            }
            else
            {
                throw new Exception("No assertions could be made on expected trade");
            }

            if ((decimal)trades[0][3] == 7)
            {
                Assert.AreEqual(250, trades[0][2]);      // Price
                Assert.AreEqual(7, trades[0][3]);        // Volume
                Assert.AreEqual("BTCLTC", trades[0][4]); // CurrencyPair
            }
            else if ((decimal)trades[0][3] == 2)
            {
                Assert.AreEqual(250, trades[0][2]);      // Price
                Assert.AreEqual(2, trades[0][3]);        // Volume
                Assert.AreEqual("BTCLTC", trades[0][4]); // CurrencyPair
            }
            else
            {
                throw new Exception("No assertions could be made on expected trade");
            }
            //-----------------------------------------------------------------------
        }
        public void SubmitsThenCancelsOrders_ChecksOrderBook_AssertsOnExpectedValues()
        {
            // Get the context
            //IApplicationContext applicationContext = ContextRegistry.GetContext();

            // Get the instance through Spring configuration
            OrderController orderController = (OrderController)_applicationContext["OrderController"];

            orderController.Request = new HttpRequestMessage(HttpMethod.Post, "");
            orderController.Request.Headers.Add("Auth", "123456789");

            ManualResetEvent  manualResetEvent = new ManualResetEvent(false);
            IHttpActionResult orderHttpResult  = orderController.CreateOrder(new CreateOrderParam()
            {
                Pair   = "BTCUSD",
                Price  = 491,
                Volume = 100,
                Side   = "buy",
                Type   = "limit"
            });

            OkNegotiatedContentResult <NewOrderRepresentation> order1RepresentationContent = (OkNegotiatedContentResult <NewOrderRepresentation>)orderHttpResult;

            Assert.IsNotNull(order1RepresentationContent.Content);

            manualResetEvent.Reset();
            manualResetEvent.WaitOne(1000);
            orderController.CreateOrder(new CreateOrderParam()
            {
                Pair   = "BTCUSD",
                Price  = 492,
                Volume = 300,
                Side   = "buy",
                Type   = "limit"
            });

            manualResetEvent.Reset();
            manualResetEvent.WaitOne(1000);

            orderController.CreateOrder(new CreateOrderParam()
            {
                Pair   = "BTCUSD",
                Price  = 493,
                Volume = 1000,
                Side   = "buy",
                Type   = "limit"
            });

            orderController.CreateOrder(new CreateOrderParam()
            {
                Pair   = "BTCUSD",
                Price  = 499,
                Volume = 900,
                Side   = "sell",
                Type   = "limit"
            });

            manualResetEvent.Reset();
            manualResetEvent.WaitOne(1000);

            orderController.CreateOrder(new CreateOrderParam()
            {
                Pair   = "BTCUSD",
                Price  = 498,
                Volume = 800,
                Side   = "sell",
                Type   = "limit"
            });

            manualResetEvent.Reset();
            manualResetEvent.WaitOne(1000);

            orderController.CreateOrder(new CreateOrderParam()
            {
                Pair   = "BTCUSD",
                Price  = 497,
                Volume = 700,
                Side   = "sell",
                Type   = "limit"
            });

            manualResetEvent.Reset();
            manualResetEvent.WaitOne(2000);
            MarketController  marketController     = (MarketController)_applicationContext["MarketController"];
            IHttpActionResult marketDataHttpResult = marketController.GetOrderBook("BTCUSD");

            OkNegotiatedContentResult <object> okResponseMessage =
                (OkNegotiatedContentResult <object>)marketDataHttpResult;
            OrderBookRepresentation representation = okResponseMessage.Content as OrderBookRepresentation;

            System.Tuple <OrderRepresentationList, OrderRepresentationList> orderBooks = new System.Tuple <OrderRepresentationList, OrderRepresentationList>(representation.Bids, representation.Asks);
            Assert.AreEqual(3, orderBooks.Item1.Count());               // Count of the orders in the Bid Order book
            Assert.AreEqual(3, orderBooks.Item2.Count());               // Count of the orders in the Ask Order book

            Assert.AreEqual(1000, orderBooks.Item1.ToList()[0].Volume); // Volume @ slot 1 in bid OrderBook
            Assert.AreEqual(493, orderBooks.Item1.ToList()[0].Price);   // Price @ slot 1 in bid OrderBook
            Assert.AreEqual(700, orderBooks.Item2.ToList()[0].Volume);  // Volume @ slot 1 in ask OrderBook
            Assert.AreEqual(497, orderBooks.Item2.ToList()[0].Price);   // Price @ slot 1 in ask OrderBook

            Assert.AreEqual(300, orderBooks.Item1.ToList()[1].Volume);  // Volume @ slot 2 in bid OrderBook
            Assert.AreEqual(492, orderBooks.Item1.ToList()[1].Price);   // Price @ slot 2 in bid OrderBook
            Assert.AreEqual(800, orderBooks.Item2.ToList()[1].Volume);  // Volume @ slot 2 in ask OrderBook
            Assert.AreEqual(498, orderBooks.Item2.ToList()[1].Price);   // Price @ slot 2 in ask OrderBook

            Assert.AreEqual(100, orderBooks.Item1.ToList()[2].Volume);  // Volume @ slot 3 in bid OrderBook
            Assert.AreEqual(491, orderBooks.Item1.ToList()[2].Price);   // Price @ slot 3 in bid OrderBook
            Assert.AreEqual(900, orderBooks.Item2.ToList()[2].Volume);  // Volume @ slot 3 in ask OrderBook
            Assert.AreEqual(499, orderBooks.Item2.ToList()[2].Price);   // Price @ slot 3 in ask OrderBook

            IHttpActionResult httpActionResult = orderController.CancelOrder(order1RepresentationContent.Content.OrderId);

            manualResetEvent.Reset();
            manualResetEvent.WaitOne(3000);

            marketDataHttpResult = marketController.GetOrderBook("BTCUSD");

            okResponseMessage =
                (OkNegotiatedContentResult <object>)marketDataHttpResult;
            representation = okResponseMessage.Content as OrderBookRepresentation;

            orderBooks = new System.Tuple <OrderRepresentationList, OrderRepresentationList>(representation.Bids, representation.Asks);
            Assert.AreEqual(2, orderBooks.Item1.Count());               // Count of the orders in the Bid Order book
            Assert.AreEqual(3, orderBooks.Item2.Count());               // Count of the orders in the Ask Order book

            Assert.AreEqual(1000, orderBooks.Item1.ToList()[0].Volume); // Volume @ slot 1 in bid OrderBook
            Assert.AreEqual(493, orderBooks.Item1.ToList()[0].Price);   // Price @ slot 1 in bid OrderBook
            Assert.AreEqual(700, orderBooks.Item2.ToList()[0].Volume);  // Volume @ slot 1 in ask OrderBook
            Assert.AreEqual(497, orderBooks.Item2.ToList()[0].Price);   // Price @ slot 1 in ask OrderBook

            Assert.AreEqual(300, orderBooks.Item1.ToList()[1].Volume);  // Volume @ slot 2 in bid OrderBook
            Assert.AreEqual(492, orderBooks.Item1.ToList()[1].Price);   // Price @ slot 2 in bid OrderBook
            Assert.AreEqual(800, orderBooks.Item2.ToList()[1].Volume);  // Volume @ slot 2 in ask OrderBook
            Assert.AreEqual(498, orderBooks.Item2.ToList()[1].Price);   // Price @ slot 2 in ask OrderBook

            /*Assert.AreEqual(100, orderBooks.Item1.ToList()[2].Item1); // Volume @ slot 3 in bid OrderBook
             * Assert.AreEqual(491, orderBooks.Item1.ToList()[2].Item2); // Price @ slot 3 in bid OrderBook*/
            Assert.AreEqual(900, orderBooks.Item2.ToList()[2].Volume); // Volume @ slot 3 in ask OrderBook
            Assert.AreEqual(499, orderBooks.Item2.ToList()[2].Price);  // Price @ slot 3 in ask OrderBook
        }
        public void Scenario1Test_TestsScenario1AndItsOutcome_VerifiesThroughMarketDataOrderAndTradesResults()
        {
            // Get the context
            IApplicationContext applicationContext = ContextRegistry.GetContext();

            // Get the instance through Spring configuration
            OrderController  orderController  = (OrderController)applicationContext["OrderController"];
            TradeController  tradeController  = (TradeController)applicationContext["TradeController"];
            MarketController marketController = (MarketController)applicationContext["MarketController"];

            orderController.Request = new HttpRequestMessage(HttpMethod.Post, "");
            orderController.Request.Headers.Add("Auth", "123456789");
            tradeController.Request = new HttpRequestMessage(HttpMethod.Post, "");
            tradeController.Request.Headers.Add("Auth", "123456789");
            marketController.Request = new HttpRequestMessage(HttpMethod.Post, "");
            marketController.Request.Headers.Add("Auth", "123456789");
            Scenario1OrderCreation(orderController);

            // ------------------------------- Order Book ------------------------------
            IHttpActionResult orderBookResponse = marketController.GetOrderBook("BTCLTC");

            OkNegotiatedContentResult <object> okOrderBookResponse =
                (OkNegotiatedContentResult <object>)orderBookResponse;
            OrderBookRepresentation representation = okOrderBookResponse.Content as OrderBookRepresentation;
            Tuple <OrderRepresentationList, OrderRepresentationList> orderBook = new Tuple <OrderRepresentationList, OrderRepresentationList>(representation.Bids, representation.Asks);

            // Item1 = Bid Book, Item1[i].Item1 = Volume of 'i' Bid, Item1[i].Item2 = Price of 'i' Bid
            Assert.AreEqual(5, orderBook.Item1.ToList()[0].Volume);
            Assert.AreEqual(250, orderBook.Item1.ToList()[0].Price);
            Assert.AreEqual(2, orderBook.Item1.ToList()[1].Volume);
            Assert.AreEqual(250, orderBook.Item1.ToList()[1].Price);

            // Item2 = Ask Book, Item2[i].Item1 = Volume of Ask at index 'i', Item2[i].Item2 = Price of Ask at index 'i'
            Assert.AreEqual(0, orderBook.Item2.Count());
            // ------------------------------- Order Book ------------------------------
            IHttpActionResult depthResponse = marketController.GetDepth("BTCLTC");

            OkNegotiatedContentResult <object> okDepth
                = (OkNegotiatedContentResult <object>)depthResponse;

            DepthTupleRepresentation depth = okDepth.Content as DepthTupleRepresentation;

            // Item1 = Bid Book, Item1.Item1 = Aggregated Volume, Item1.Item2 = Price, Item1.Item3 = Number of Orders
            Assert.AreEqual(7, depth.BidDepth[0].Volume);
            Assert.AreEqual(250, depth.BidDepth[0].Price);
            Assert.AreEqual(2, depth.BidDepth[0].OrderCount);
            Assert.IsNull(depth.BidDepth[1]);
            Assert.IsNull(depth.BidDepth[2]);
            Assert.IsNull(depth.BidDepth[3]);
            Assert.IsNull(depth.BidDepth[4]);

            // Item2 = Ask Book, Item2.Item1 = Aggregated Volume, Item2.Item2 = Price, Item2.Item3 = Number of Orders
            Assert.IsNull(depth.AskDepth[0]);
            Assert.IsNull(depth.AskDepth[1]);
            Assert.IsNull(depth.AskDepth[2]);
            Assert.IsNull(depth.AskDepth[3]);
            Assert.IsNull(depth.AskDepth[4]);

            //------------------- Open Orders -------------------------
            IHttpActionResult openOrdersResponse = GetOpenOrders(orderController);
            OkNegotiatedContentResult <List <OrderReadModel> > okOpenOrdersResponse = (OkNegotiatedContentResult <List <OrderReadModel> >)openOrdersResponse;
            List <OrderReadModel> openOrders = okOpenOrdersResponse.Content;

            Assert.AreEqual(2, openOrders.Count);
            // First Open Order
            Assert.AreEqual(250, openOrders[1].Price);
            Assert.AreEqual(10, openOrders[1].Volume);
            Assert.AreEqual(5, openOrders[1].VolumeExecuted);
            Assert.AreEqual(5, openOrders[1].OpenQuantity);
            Assert.AreEqual("Limit", openOrders[1].Type);
            Assert.AreEqual("Buy", openOrders[1].Side);
            Assert.AreEqual("PartiallyFilled", openOrders[1].Status);

            // Second Open Order
            Assert.AreEqual(250, openOrders[0].Price);
            Assert.AreEqual(2, openOrders[0].Volume);
            Assert.AreEqual(0, openOrders[0].VolumeExecuted);
            Assert.AreEqual(2, openOrders[0].OpenQuantity);
            Assert.AreEqual("Limit", openOrders[0].Type);
            Assert.AreEqual("Buy", openOrders[0].Side);
            Assert.AreEqual("Accepted", openOrders[0].Status);
            //------------------- Open Orders -------------------------

            //------------------- Closed Orders -------------------------
            IHttpActionResult closedOrdersResponse = GetClosedOrders(orderController);
            OkNegotiatedContentResult <List <OrderReadModel> > okClosedOrdersResponse = (OkNegotiatedContentResult <List <OrderReadModel> >)closedOrdersResponse;
            List <OrderReadModel> closedOrders = okClosedOrdersResponse.Content;

            Assert.AreEqual(252, closedOrders[3].Price);
            Assert.AreEqual(5, closedOrders[3].Volume);
            Assert.AreEqual(5, closedOrders[3].VolumeExecuted);
            Assert.AreEqual(0, closedOrders[3].OpenQuantity);
            Assert.AreEqual("Limit", closedOrders[3].Type);
            Assert.AreEqual("Sell", closedOrders[3].Side);
            Assert.AreEqual("Complete", closedOrders[3].Status);

            Assert.AreEqual(0, closedOrders[2].Price);
            Assert.AreEqual(3, closedOrders[2].Volume);
            Assert.AreEqual(3, closedOrders[2].VolumeExecuted);
            Assert.AreEqual(0, closedOrders[2].OpenQuantity);
            Assert.AreEqual("Market", closedOrders[2].Type);
            Assert.AreEqual("Buy", closedOrders[2].Side);
            Assert.AreEqual("Complete", closedOrders[2].Status);

            Assert.AreEqual(253, closedOrders[1].Price);
            Assert.AreEqual(2, closedOrders[1].Volume);
            Assert.AreEqual(2, closedOrders[1].VolumeExecuted);
            Assert.AreEqual(0, closedOrders[1].OpenQuantity);
            Assert.AreEqual("Limit", closedOrders[1].Type);
            Assert.AreEqual("Buy", closedOrders[1].Side);
            Assert.AreEqual("Complete", closedOrders[1].Status);

            Assert.AreEqual(0, closedOrders[0].Price);
            Assert.AreEqual(5, closedOrders[0].Volume);
            Assert.AreEqual(5, closedOrders[0].VolumeExecuted);
            Assert.AreEqual(0, closedOrders[0].OpenQuantity);
            Assert.AreEqual("Market", closedOrders[0].Type);
            Assert.AreEqual("Sell", closedOrders[0].Side);
            Assert.AreEqual("Complete", closedOrders[0].Status);
            //------------------- Closed Orders -------------------------

            //------------------- Trades -------------------------

            IHttpActionResult tradesResponse = GetTrades(tradeController);
            OkNegotiatedContentResult <object> okTradeResponse = (OkNegotiatedContentResult <object>)tradesResponse;
            IList <object>   tradesintermediateList            = (IList <object>)okTradeResponse.Content;
            IList <object[]> trades = new List <object[]>();

            for (int i = 0; i < tradesintermediateList.Count; i++)
            {
                object[] objects = tradesintermediateList[i] as object[];
                trades.Add(objects);
            }

            // This call return list of object, so we have to explicitly check values within elements
            Assert.AreEqual(252, trades[2][2]); // Price
            Assert.AreEqual(3, trades[2][3]);   // Volume
            Assert.AreEqual("BTCLTC", trades[2][4]);

            Assert.AreEqual(252, trades[1][2]);
            Assert.AreEqual(2, trades[1][3]);
            Assert.AreEqual("BTCLTC", trades[0][4]);

            Assert.AreEqual(250, trades[0][2]);
            Assert.AreEqual(5, trades[0][3]);
            Assert.AreEqual("BTCLTC", trades[0][4]);
            //------------------- Trades -------------------------
        }