PageRenderTime 34ms CodeModel.GetById 14ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/V4/StockTrader RI/Desktop/StockTraderRI.Modules.Position.Tests/Orders/OrderDetailsPresenterFixture.cs

#
C# | 329 lines | 242 code | 53 blank | 34 comment | 2 complexity | c8c90ad2368e7756f1c66a7cbf919eff MD5 | raw file
  1//===================================================================================
  2// Microsoft patterns & practices
  3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4//===================================================================================
  5// Copyright (c) Microsoft Corporation.  All rights reserved.
  6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9// FITNESS FOR A PARTICULAR PURPOSE.
 10//===================================================================================
 11// The example companies, organizations, products, domain names,
 12// e-mail addresses, logos, people, places, and events depicted
 13// herein are fictitious.  No association with any real company,
 14// organization, product, domain name, email address, logo, person,
 15// places, or events is intended or should be inferred.
 16//===================================================================================
 17using System;
 18using System.ComponentModel;
 19using Microsoft.VisualStudio.TestTools.UnitTesting;
 20using StockTraderRI.Infrastructure;
 21using StockTraderRI.Infrastructure.Interfaces;
 22using StockTraderRI.Infrastructure.Models;
 23using StockTraderRI.Modules.Position.Interfaces;
 24using StockTraderRI.Modules.Position.Models;
 25using StockTraderRI.Modules.Position.Orders;
 26using StockTraderRI.Modules.Position.Tests.Mocks;
 27
 28namespace StockTraderRI.Modules.Position.Tests.Orders
 29{
 30    [TestClass]
 31    public class OrderDetailsPresenterFixture
 32    {
 33        [TestMethod]
 34        public void PresenterCreatesPublicSubmitCommand()
 35        {
 36            var presenter = CreatePresentationModel(null);
 37
 38            Assert.IsNotNull(presenter.SubmitCommand);
 39        }
 40
 41        [TestMethod]
 42        public void CanExecuteChangedIsRaisedForSubmitCommandWhenModelBecomesValid()
 43        {
 44            bool canExecuteChanged = false;
 45            var presenter = CreatePresentationModel(null);
 46            presenter.SubmitCommand.CanExecuteChanged += delegate { canExecuteChanged = true; };
 47            presenter.Shares = 2;
 48            canExecuteChanged = false;
 49
 50            presenter.StopLimitPrice = 2;
 51
 52            Assert.IsTrue(canExecuteChanged);
 53        }
 54
 55        [TestMethod]
 56        [ExpectedException(typeof(InputValidationException))]
 57        public void NonPositiveSharesThrows()
 58        {
 59            var presenter = CreatePresentationModel(null);
 60
 61            presenter.Shares = 0;
 62        }
 63
 64        [TestMethod]
 65        public void CannotSubmitWhenSharesIsNotPositive()
 66        {
 67            var presenter = CreatePresentationModel(null);
 68
 69            presenter.Shares = 2;
 70            presenter.StopLimitPrice = 2;
 71            Assert.IsTrue(presenter.SubmitCommand.CanExecute(null));
 72
 73            try
 74            {
 75                presenter.Shares = 0;
 76            }
 77            catch (InputValidationException) { }
 78
 79            Assert.IsFalse(presenter.SubmitCommand.CanExecute(null));
 80        }
 81
 82        [TestMethod]
 83        [ExpectedException(typeof(InvalidOperationException))]
 84        public void SubmitThrowsIfCanExecuteIsFalse()
 85        {
 86            var presenter = CreatePresentationModel(new MockAccountPositionService());
 87            try
 88            {
 89                presenter.Shares = 0;
 90            }
 91            catch (InputValidationException) { }
 92
 93            Assert.IsFalse(presenter.SubmitCommand.CanExecute(null));
 94
 95            presenter.SubmitCommand.Execute(null);
 96        }
 97
 98        [TestMethod]
 99        public void CancelRaisesCloseViewEvent()
100        {
101            bool closeViewRaised = false;
102
103            var presenter = CreatePresentationModel(null);
104            presenter.CloseViewRequested += delegate
105            {
106                closeViewRaised = true;
107            };
108
109            presenter.CancelCommand.Execute(null);
110
111            Assert.IsTrue(closeViewRaised);
112        }
113
114        [TestMethod]
115        public void SubmitRaisesCloseViewEvent()
116        {
117            bool closeViewRaised = false;
118
119            var presenter = CreatePresentationModel(new MockAccountPositionService());
120            presenter.CloseViewRequested += delegate
121            {
122                closeViewRaised = true;
123            };
124
125            presenter.TransactionType = TransactionType.Buy;
126            presenter.Shares = 1;
127            presenter.StopLimitPrice = 1;
128            Assert.IsTrue(presenter.SubmitCommand.CanExecute(null));
129            presenter.SubmitCommand.Execute(null);
130
131            Assert.IsTrue(closeViewRaised);
132        }
133
134        [TestMethod]
135        public void CannotSubmitOnSellWhenSharesIsHigherThanCurrentPosition()
136        {
137            var accountPositionService = new MockAccountPositionService();
138            accountPositionService.AddPosition(new AccountPosition("TESTFUND", 10m, 15));
139            var presenter = CreatePresentationModel(accountPositionService);
140
141            presenter.TickerSymbol = "TESTFUND";
142            presenter.TransactionType = TransactionType.Sell;
143            presenter.Shares = 5;
144            presenter.StopLimitPrice = 1;
145            Assert.IsTrue(presenter.SubmitCommand.CanExecute(null));
146
147            try
148            {
149                presenter.Shares = 16;
150            }
151            catch (InputValidationException) { }
152            Assert.IsFalse(presenter.SubmitCommand.CanExecute(null));
153        }
154
155        [TestMethod]
156        public void SharesIsHigherThanCurrentPositionOnSellThrows()
157        {
158            var accountPositionService = new MockAccountPositionService();
159            accountPositionService.AddPosition(new AccountPosition("TESTFUND", 10m, 15));
160            var presenter = CreatePresentationModel(accountPositionService);
161
162            presenter.TickerSymbol = "TESTFUND";
163            presenter.TransactionType = TransactionType.Sell;
164
165            try
166            {
167                presenter.Shares = 16;
168            }
169            catch (InputValidationException)
170            {
171                return;
172            }
173            Assert.Fail("Exception not thrown.");
174        }
175
176        [TestMethod]
177        public void PresenterCreatesCallSetOrderTypes()
178        {
179            var presenter = new OrderDetailsViewModel(null, null);
180
181            Assert.IsNotNull(presenter.AvailableOrderTypes);
182            Assert.IsTrue(presenter.AvailableOrderTypes.Count > 0);
183            Assert.AreEqual(GetEnumCount(typeof(OrderType)), presenter.AvailableOrderTypes.Count);
184        }
185
186        [TestMethod]
187        public void PresenterCreatesCallSetTimeInForce()
188        {
189            var presenter = new OrderDetailsViewModel(null, null);
190            Assert.IsNotNull(presenter.AvailableTimesInForce);
191            Assert.IsTrue(presenter.AvailableTimesInForce.Count > 0);
192            Assert.AreEqual(GetEnumCount(typeof(TimeInForce)), presenter.AvailableTimesInForce.Count);
193        }
194
195        [TestMethod]
196        public void SetTransactionInfoShouldUpdateTheModel()
197        {
198            var presenter = new OrderDetailsViewModel(new MockAccountPositionService(), null);
199            presenter.TransactionInfo = new TransactionInfo { TickerSymbol = "T000", TransactionType = TransactionType.Sell };
200
201            Assert.AreEqual("T000", presenter.TickerSymbol);
202            Assert.AreEqual(TransactionType.Sell, presenter.TransactionType);
203        }
204
205        [TestMethod]
206        public void SubmitCallsServiceWithCorrectOrder()
207        {
208            var ordersService = new MockOrdersService();
209            var presenter = new OrderDetailsViewModel(new MockAccountPositionService(), ordersService);
210            presenter.Shares = 2;
211            presenter.TickerSymbol = "AAAA";
212            presenter.TransactionType = TransactionType.Buy;
213            presenter.TimeInForce = TimeInForce.EndOfDay;
214            presenter.OrderType = OrderType.Limit;
215            presenter.StopLimitPrice = 15;
216
217            Assert.IsNull(ordersService.SubmitArgumentOrder);
218            presenter.SubmitCommand.Execute(null);
219
220            var submittedOrder = ordersService.SubmitArgumentOrder;
221            Assert.IsNotNull(submittedOrder);
222            Assert.AreEqual("AAAA", submittedOrder.TickerSymbol);
223            Assert.AreEqual(TransactionType.Buy, submittedOrder.TransactionType);
224            Assert.AreEqual(TimeInForce.EndOfDay, submittedOrder.TimeInForce);
225            Assert.AreEqual(OrderType.Limit, submittedOrder.OrderType);
226            Assert.AreEqual(15, submittedOrder.StopLimitPrice);
227        }
228
229        [TestMethod]
230        public void VerifyTransactionInfoModificationsInOrderDetails()
231        {
232            var orderDetailsPresenter = new OrderDetailsViewModel(new MockAccountPositionService(), null);
233            var transactionInfo = new TransactionInfo { TickerSymbol = "Fund0", TransactionType = TransactionType.Buy };
234            orderDetailsPresenter.TransactionInfo = transactionInfo;
235            orderDetailsPresenter.TransactionType = TransactionType.Sell;
236            Assert.AreEqual(TransactionType.Sell, transactionInfo.TransactionType);
237
238            orderDetailsPresenter.TickerSymbol = "Fund1";
239            Assert.AreEqual("Fund1", transactionInfo.TickerSymbol);
240        }
241
242        [TestMethod]
243        public void CannotSubmitIfStopLimitZero()
244        {
245            var accountPositionService = new MockAccountPositionService();
246            accountPositionService.AddPosition(new AccountPosition("TESTFUND", 10m, 15));
247            var presenter = CreatePresentationModel(accountPositionService);
248
249            presenter.TickerSymbol = "TESTFUND";
250            presenter.TransactionType = TransactionType.Sell;
251            presenter.Shares = 5;
252            presenter.StopLimitPrice = 1;
253            Assert.IsTrue(presenter.SubmitCommand.CanExecute(null));
254
255            try
256            {
257                presenter.StopLimitPrice = 0;
258            }
259            catch (InputValidationException) { }
260
261            Assert.IsFalse(presenter.SubmitCommand.CanExecute(null));
262        }
263
264        //[TestMethod]
265        //public void ShouldSetStopLimitPriceInModel()
266        //{
267        //    var accountPositionService = new MockAccountPositionService();
268        //    accountPositionService.AddPosition(new AccountPosition("TESTFUND", 10m, 15));
269        //    var presenter = CreatePresentationModel(new MockOrderDetailsView(), accountPositionService);
270
271        //    presenter.TickerSymbol = "TESTFUND";
272        //    presenter.TransactionType = TransactionType.Sell;
273        //    presenter.Shares = 5;
274        //    presenter.StopLimitPrice = 0;
275
276        //    Assert.AreEqual<string>("The stop limit price must be greater than 0", presenter["StopLimitPrice"]);
277        //}
278
279
280        //[TestMethod]
281        //public void DisposeUnregistersLocalCommandsFromGlobalCommands()
282        //{
283        //    var presenter = new TestableOrderDetailsPresentationModel(new MockOrderDetailsView(), null);
284        //    Assert.IsTrue(StockTraderRICommands.SubmitOrderCommand.
285        //}
286        [TestMethod]
287        public void PropertyChangedIsRaisedWhenSharesIsChanged()
288        {
289            var presenter = new OrderDetailsViewModel(null, null);
290            presenter.Shares = 5;
291
292            bool sharesPropertyChangedRaised = false;
293            presenter.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
294            {
295                if (e.PropertyName == "Shares")
296                    sharesPropertyChangedRaised = true;
297            };
298            presenter.Shares = 1;
299            Assert.IsTrue(sharesPropertyChangedRaised);
300        }
301
302        private static int GetEnumCount(Type enumType)
303        {
304            Array availableOrderTypes;
305#if SILVERLIGHT
306            availableOrderTypes = enumType.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
307#else
308            availableOrderTypes = Enum.GetValues(enumType);
309#endif
310            return availableOrderTypes.Length;
311        }
312
313        private static OrderDetailsViewModel CreatePresentationModel(IAccountPositionService accountPositionService)
314        {
315            return new OrderDetailsViewModel(accountPositionService, new MockOrdersService());
316        }
317    }
318
319    internal class MockOrdersService : IOrdersService
320    {
321        public Order SubmitArgumentOrder;
322
323        public void Submit(Order order)
324        {
325            SubmitArgumentOrder = order;
326        }
327    }
328
329}