public async Task OnActionExecutionAsync_WhenOrderItems_ShouldReturnOnlyValidItems()
        {
            // Arrange
            var validOrderItems = new List <RequestOrderItem>
            {
                new RequestOrderItem {
                    EquipmentName = "Valid-Name-1", RentalDays = 1
                },
                new RequestOrderItem {
                    EquipmentName = "Valid-Name-2", RentalDays = 1
                }
            };

            var invalidOrderItems = new List <RequestOrderItem>
            {
                new RequestOrderItem {
                    EquipmentName = "Invalid-Name-1", RentalDays = 1
                },
                new RequestOrderItem {
                    EquipmentName = "Invalid-Name-2", RentalDays = 1
                }
            };

            IEnumerable <RequestOrderItem> allOrderItems = validOrderItems.Concat(invalidOrderItems);

            IEquipmentRepository equipmentRepository = Substitute.For <IEquipmentRepository>();

            equipmentRepository
            .DoesEquipmentExist(Arg.Is <string>(value => value.Contains("Valid-Name")))
            .Returns(true);

            equipmentRepository
            .DoesEquipmentExist(Arg.Is <string>(value => value.Contains("Invalid-Name")))
            .Returns(false);

            EquipmentFilter equipmentFilter = new EquipmentFilter(equipmentRepository);

            // Act
            var result = await ExecuteFilterForOrderItems(equipmentFilter, allOrderItems);

            // Assert
            result.executingContext.ActionArguments
            .Values
            .Cast <InvoiceRequestModel>()
            .First().OrderItems
            .Should()
            .BeEquivalentTo(validOrderItems);
        }
示例#2
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            List <InvoiceRequestModel> models = context.ActionArguments.Values.OfType <InvoiceRequestModel>().ToList();

            if (models.Count > 1)
            {
                throw new ArgumentException($"Controller action can't have more than one {nameof(InvoiceRequestModel)} parameter.");
            }

            InvoiceRequestModel model = models.FirstOrDefault();

            if (model == null)
            {
                await next();

                return;
            }

            List <RequestOrderItem> orderItems = model.OrderItems.ToList();

            foreach (RequestOrderItem requestOrderItem in model.OrderItems)
            {
                // For demonstration purposes we just remove invalid equipment items, but
                // it's better to send a response with invalid items.
                if (!_equipmentRepository.DoesEquipmentExist(requestOrderItem.EquipmentName))
                {
                    Log.Warn($"Invalid equipment name {requestOrderItem.EquipmentName}.");
                    orderItems.Remove(requestOrderItem);
                }
            }

            model.OrderItems = orderItems;

            await next();
        }