public async Task ShouldUpdateProductAsExpired() { // given Product product = new Filler <Product>().Create(); var productExpirationEvent = new ProductExpirationEvent { ProductId = product.Id, ExpirationDate = product.ExpirationDate }; CancellationToken cancellationToken = new CancellationToken(); Message message = new Message { Body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(productExpirationEvent)), ScheduledEnqueueTimeUtc = product.ExpirationDate.ToUniversalTime() }; this.storageBroker.Setup(broker => broker.GetProductByIdAsync(product.Id)) .ReturnsAsync(product); // when var productService = new ProductEventService(this.eventsBroker.Object, this.storageBroker.Object); await productService.ExpireProductAsync(message, cancellationToken); // then this.storageBroker.Verify(broker => broker.UpdateProductAsync(It.Is <Product>( storageProduct => storageProduct.Id == product.Id && storageProduct.Condition == ProductCondition.Expired && storageProduct.ExpirationDate == product.ExpirationDate)), Times.Once); }
public async Task ShouldAddNewProduct() { // given Product product = new Filler <Product>().Create(); var productExpirationEvent = new ProductExpirationEvent { ProductId = product.Id, ExpirationDate = product.ExpirationDate }; string serializedProductExpirationEvent = JsonConvert.SerializeObject(productExpirationEvent); var productExpirationEventMessage = new Message(Encoding.UTF8.GetBytes(serializedProductExpirationEvent)) { ScheduledEnqueueTimeUtc = product.ExpirationDate.ToUniversalTime() }; this.eventsBroker.Setup(broker => broker.SendEventMessageAsync(It.Is <Message>(message => Encoding.UTF8.GetString(message.Body) == Encoding.UTF8.GetString(productExpirationEventMessage.Body) && message.ScheduledEnqueueTimeUtc == productExpirationEventMessage.ScheduledEnqueueTimeUtc))); // when var productService = new ProductEventService(this.eventsBroker.Object, this.storageBroker.Object); await productService.CreateProductEventAsync(product); // then this.eventsBroker.Verify(broker => broker.SendEventMessageAsync(It.Is <Message>(message => Encoding.UTF8.GetString(message.Body) == Encoding.UTF8.GetString(productExpirationEventMessage.Body) && message.ScheduledEnqueueTimeUtc == productExpirationEventMessage.ScheduledEnqueueTimeUtc)), Times.Once); }
public async Task ExpireProductAsync(Message productMessage, CancellationToken cancellationToken) { string serializedProductExpirationEvent = Encoding.UTF8.GetString(productMessage.Body); ProductExpirationEvent productExpirationEvent = JsonConvert.DeserializeObject <ProductExpirationEvent>(serializedProductExpirationEvent); Product product = await this.storageBroker.GetProductByIdAsync(productExpirationEvent.ProductId); if (product != null && product.ExpirationDate == productExpirationEvent.ExpirationDate) { product.Condition = ProductCondition.Expired; await this.storageBroker.UpdateProductAsync(product); } }
private Message CreateProductAsMessage(Product product) { var productExpirationEvent = new ProductExpirationEvent { ProductId = product.Id, ExpirationDate = product.ExpirationDate }; string serializedProductExpirationEvent = JsonConvert.SerializeObject(productExpirationEvent); byte[] productBinary = Encoding.UTF8.GetBytes(serializedProductExpirationEvent); return(new Message(productBinary) { ScheduledEnqueueTimeUtc = product.ExpirationDate.ToUniversalTime() }); }