private Buyer CreateMonitoringBuyer(string buyerName, int numberInStock = 100, int numberToBuy = 10) { var buyer = new Buyer(buyerName, 100, numberToBuy); buyer.Process(StockEvent.Price(200, numberInStock)); return(buyer); }
public void TestStockSellerFunctionHandler() { var testStockPrice = 86; var request = new StockEvent { stockPrice = testStockPrice }; var context = new TestLambdaContext(); var function = new Function(); var response = function.FunctionHandler(request, context); Assert.True(response.id is string); Assert.Equal(testStockPrice.ToString(), response.price); Assert.Equal("Sell", response.type); Assert.True(response.timestamp is string); int quantity; if (int.TryParse(response.qty, out quantity)) { Assert.True(quantity >= 1); Assert.True(quantity <= 10); } else { Assert.True(false, "Quantity was not a valid number."); } }
private Buyer CreateClosedBuyer(int maximumPrice = 5) { var buyer = new Buyer("Buyer", maximumPrice, 1); buyer.Process(StockEvent.Close()); return(buyer); }
public void Close_method_returns_a_close_event() { StockEvent stockEvent = StockEvent.Close(); stockEvent.Type.ShouldEqual(StockEventType.Close); stockEvent.ToString().ShouldEqual("Event: CLOSE;"); }
public SaleOrderItemServices(IRepositoryBase <SaleOrderItem> _db, IRepositoryBase <SaleOrder> _dbOrder, IRepositoryBase <Product> _dbProduct, IRepositoryBase <Inventory> _dbStock, IRepositoryBase <Taxe> _dbTaxe, IRepositoryBase <TaxeOrder> _dbTaxeOrder) { db = _db; dbSaleOrder = new SaleOrderServices(_dbOrder); dbProduct = new ProductServices(_dbProduct, _dbTaxe); dbStock = new StockEvent(_dbStock, _db, _dbProduct); dbTaxeOrder = _dbTaxeOrder; }
public void Does_not_parse_events_with_unknown_types() { string message = "Event: UNKNOWN;"; Action action = () => StockEvent.From(message); action.ShouldThrow <ArgumentException>(); }
public void Does_not_parse_events_with_incorrect_format() { string message = "incorrect message"; Action action = () => StockEvent.From(message); action.ShouldThrow <ArgumentException>(); }
public void Buyer_closes_when_it_buys_enough_items() { Buyer buyer = CreateMonitoringBuyer("Buyer", numberToBuy: 5); StockCommand command = buyer.Process(StockEvent.Purchase("Buyer", 5)); command.ShouldEqual(StockCommand.None()); buyer.Snapshot.State.ShouldEqual(BuyerState.Closed); }
public void Closes_when_item_closes() { var buyer = CreateJoiningBuyer(); StockCommand command = buyer.Process(StockEvent.Close()); command.ShouldEqual(StockCommand.None()); buyer.SnapshotShouldEqual(BuyerState.Closed, 0, 0, 0); }
public void Buyer_does_not_buy_when_price_event_with_too_high_price_arrives() { var buyer = CreateJoiningBuyer(maximumPrice: 10); StockCommand command = buyer.Process(StockEvent.Price(20, 5)); command.ShouldEqual(StockCommand.None()); buyer.SnapshotShouldEqual(BuyerState.Monitoring, 20, 5, 0); }
public void Purchase_method_returns_a_purchase_event() { StockEvent stockEvent = StockEvent.Purchase("some user", 1); stockEvent.Type.ShouldEqual(StockEventType.Purchase); stockEvent.BuyerName.ShouldEqual("some user"); stockEvent.NumberSold.ShouldEqual(1); stockEvent.ToString().ShouldEqual("Event: PURCHASE; BuyerName: some user; NumberSold: 1;"); }
public void Price_method_returns_a_price_event() { StockEvent stockEvent = StockEvent.Price(10, 15); stockEvent.Type.ShouldEqual(StockEventType.Price); stockEvent.CurrentPrice.ShouldEqual(10); stockEvent.NumberInStock.ShouldEqual(15); stockEvent.ToString().ShouldEqual("Event: PRICE; CurrentPrice: 10; NumberInStock: 15;"); }
public void Buyer_buys_when_price_event_with_appropriate_price_arrives() { Buyer buyer = CreateJoiningBuyer(maximumPrice: 50); StockCommand command = buyer.Process(StockEvent.Price(10, 5)); command.ShouldEqual(StockCommand.Buy(10, 1)); buyer.SnapshotShouldEqual(BuyerState.Buying, 10, 5, 0); }
public void Buyer_attempts_to_buy_maximum_amount_available() { Buyer buyer = CreateJoiningBuyer(maximumPrice: 50, numberToBuy: 10); StockCommand command = buyer.Process(StockEvent.Price(10, 5)); command.ShouldEqual(StockCommand.Buy(10, 5)); buyer.SnapshotShouldEqual(BuyerState.Buying, 10, 5, 0); }
public void Closed_buyer_does_not_react_to_further_messages() { Buyer buyer = CreateClosedBuyer(maximumPrice: 10); StockCommand command = buyer.Process(StockEvent.Price(10, 10)); command.ShouldEqual(StockCommand.None()); buyer.Snapshot.State.ShouldEqual(BuyerState.Closed); }
public void Parses_close_event() { string message = "Event: CLOSE;"; StockEvent stockEvent = StockEvent.From(message); string serialized = stockEvent.ToString(); stockEvent.Type.ShouldEqual(StockEventType.Close); serialized.ShouldEqual(message); }
public void Buyer_does_not_react_to_a_purchase_event_related_to_another_buyer() { Buyer buyer = CreateMonitoringBuyer("Buyer"); StockCommand command = buyer.Process(StockEvent.Purchase("Some other buyer", 1)); command.ShouldEqual(StockCommand.None()); buyer.Snapshot.State.ShouldEqual(BuyerState.Monitoring); buyer.Snapshot.BoughtSoFar.ShouldEqual(0); }
/// <summary> /// Changes the stock value and also raising the event of stock value changes /// </summary> public void ChangeStockValue() { var rand = new Random(); CurrentValue += rand.Next(0, MaxChange + 1); NumChanges++; if ((CurrentValue - InitialValue) > Threshold) { StockEvent?.Invoke(this, null); } }
/// <summary> /// Changes the stock value and also raising the event of stock value changes /// </summary> public void ChangeStockValue() { var rand = new Random(); CurrentValue += rand.Next((-1 * MaxChange), MaxChange); NumChanges++; if (Math.Abs((CurrentValue - InitialValue)) > Threshold) { StockEvent?.Invoke(this, new StockNotification(StockName, CurrentValue, NumChanges)); } }
public void Buyer_updates_items_bought_so_far_when_purchase_event_with_the_same_user_name_arrives() { Buyer buyer = CreateMonitoringBuyer("name", numberInStock: 10); StockCommand command = buyer.Process(StockEvent.Purchase("name", 1)); command.ShouldEqual(StockCommand.None()); buyer.Snapshot.State.ShouldEqual(BuyerState.Monitoring); buyer.Snapshot.BoughtSoFar.ShouldEqual(1); buyer.Snapshot.NumberInStock.ShouldEqual(9); }
public void Parses_price_event() { string message = "Event: PRICE; NumberInStock: 12; CurrentPrice: 34;"; StockEvent stockEvent = StockEvent.From(message); string serialized = stockEvent.ToString(); stockEvent.Type.ShouldEqual(StockEventType.Price); stockEvent.NumberInStock.ShouldEqual(12); stockEvent.CurrentPrice.ShouldEqual(34); serialized.ShouldEqual(message); }
public void Parses_purchase_event() { string message = "Event: PURCHASE; BuyerName: Buyer; NumberSold: 1;"; StockEvent stockEvent = StockEvent.From(message); string serialized = stockEvent.ToString(); stockEvent.Type.ShouldEqual(StockEventType.Purchase); stockEvent.BuyerName.ShouldEqual("Buyer"); stockEvent.NumberSold.ShouldEqual(1); serialized.ShouldEqual(message); }
static void addEventRecord(StockContext db, string eventName) { var exist = db.StockEvents.Any(s => s.EventName == eventName); if (!exist) { StockEvent se = new StockEvent(); se.EventName = eventName; se.LastAriseStartDate = DateTime.MinValue; db.StockEvents.Add(se); } }
/// <summary> /// 解碼國際金融封包 /// </summary> /// <param name="socket">作用中的Socket</param> /// <param name="item">StockEvent類別</param> /// <param name="isDecode">是否啟動解碼功能</param> private static void DecodeFinance(Socket socket, StockEvent item, bool isDecode) { switch (item.Header) { case 0x23: //上午時間封包 case 0x24: //下午時間封包 item.Type = 0x54; //時間封包 'T' TimerEvent cTimer = DecodeTime.Decode(item.Source, isDecode); if (cTimer != null) { if (TimerProc != null) { TimerProc(socket, cTimer); } } break; case 0x4d: //Mcp命令封包回應解碼 DecodeReturn.Decode(item, isDecode); break; case 0x41: //國際匯市現價[原編碼] case 0x43: //國際匯市最高價[原編碼] case 0x44: //國際匯市最低價[原編碼] case 0x4f: //國際匯市開盤價[原編碼] case 0x50: //國際匯市昨收價[原編碼] item.Type = 1; //國際匯市 break; case 0x2b: //國際期貨BID價[原編碼] case 0x2d: //國際期貨ASK價[原編碼] case 0x31: //國際期貨現價[原編碼] case 0x35: //國際期貨收盤價[原編碼] case 0x36: //國際期貨昨收價[原編碼] case 0x37: //國際期貨成交量[原編碼] case 0x38: //國際期貨未平倉量[原編碼] case 0x68: //國際期貨最高價[原編碼] case 0x6c: //國際期貨最低價[原編碼] case 0x6f: //國際期貨開盤價[原編碼] item.Type = 0; //國際期貨 break; default: __cFinance.Decode(item, isDecode); break; } if (StockProc != null) { StockProc(socket, item); } }
public async Task <ServiceResponse> Handle(CheckoutOrderCommand request, CancellationToken cancellationToken) { var orderDetails = request.Items; CustomerOrder newOrder = new CustomerOrder(); var service = new ServiceResponse(); if (request != null) { var orderEntity = _mapper.Map <CustomerOrder>(request); orderEntity.OrderNumber = GenerateOrderNumber(); orderEntity.OrderStatus = Status.Created.ToString(); newOrder = await _orderRepository.AddAsync(orderEntity); var newOrderLine = new OrderLines(); if (orderDetails != null) { newOrderLine = await _orderRepository.AddAsyncOrderLine(new OrderLines { OrderID = newOrder.Id.ToString(), Name = orderDetails.Make, ProductID = orderEntity.ProductID }); } var stockMessage = new StockEvent { OrderID = newOrder.Id.ToString(), OrderNumber = orderEntity.OrderNumber, ProductID = newOrderLine.ProductID, OrderStatus = orderEntity.OrderStatus, EmailAddress = orderEntity.EmailAddress, LastName = orderEntity.LastName, FirstName = orderEntity.FirstName, IDNumber = orderEntity.IDNumber, }; service.Message = $"Order {newOrder.Id} is successfully created. with reference number {orderEntity.OrderNumber}"; await _publishEndpoint.Publish <StockEvent>(stockMessage); } _logger.LogInformation($"Order {newOrder.Id} is successfully created."); _logger.LogInformation($"Inventory check for {newOrder.Id} is successfully sent."); await SendMail(newOrder); return(service); }
/// <summary> /// 建構子 /// </summary> /// <param name="Buffer">PacketBuffer 類別</param> public SocketToken(PacketBuffer Buffer) { __cStockEvent = new StockEvent(); __cMcpEvent = new McpPacketEvent(); __cPackage = new PacketBuffer(MAX_BUFFER_SIZE); __cTempBuffer = new PacketBuffer(MAX_BUFFER_SIZE); if (Buffer == null) { __cRecvBuffer = new PacketBuffer(MAX_BUFFER_SIZE); } else { __cRecvBuffer = Buffer; } }
private void StockMessageRecieved(string message) { StockEvent stockEvent = StockEvent.From(message); StockCommand stockCommand = _buyer.Process(stockEvent); if (stockCommand != StockCommand.None()) { _connection.SendMessage(stockCommand.ToString()); } _repository.Save(ItemId, _buyer); Notify(nameof(CurrentPrice)); Notify(nameof(NumberInStock)); Notify(nameof(BoughtSoFar)); Notify(nameof(State)); }
internal void Decode(StockEvent item, bool IsDecode) { item.Type = item.Source.Data[6]; item.Serial = (item.Source.Data[4] << 8) + item.Source.Data[5]; if (IsDecode) { switch (item.Header) { case 0x53: //台灣股票証期權 lock (__oLockObj) { DecodeStock.Decode(item); } break; } } }
// Change the stock value and invoke event to notify stock brokers when the threshold is reach private void ChangeStockValue() { // Generate a random number to within a range that stock can change every time unit and add it to the current stock's value Random rand = new Random(); CurrentValue += rand.Next(-MaxChange, MaxChange); // Increase the number of changes in value by 1 NumOfChanges++; // Check if the threshold is reached if (Math.Abs(CurrentValue - InitialValue) >= Threshold) { // Raise the events Parallel.Invoke(() => StockEvent?.Invoke(StockName, InitialValue, CurrentValue, NumOfChanges, DateTime.Now), () => StockEventData?.Invoke(this, new EventData(StockName, InitialValue, CurrentValue, NumOfChanges, DateTime.Now))); } }
/// <summary> /// Changes the stock value and also raising the event of stock value changes /// </summary> public void ChangeStockValue() { var rand = new Random(); CurrentValue += rand.Next(1, MaxChange + 1); NumChanges++; if ((CurrentValue - InitValue) > NotificationThreshold) { StockNotification args = new StockNotification(); //create event // Create event data args.StockName = Name; args.CurrentValue = CurrentValue; args.NumChanges = NumChanges; StockEvent?.Invoke(this, args); //raises event by invoking delegate StockChangeFile(); } }