Esempio n. 1
0
        public static void Validate_NullDeliveryDate_SuccessIsFalse(
            Order order,
            CreateOrderItemModel model,
            OrderItemValidator orderItemValidator)
        {
            model.ServiceRecipients.Should().NotBeNullOrEmpty();
            order.CommencementDate.Should().NotBeNull();

            var serviceRecipients = model.ServiceRecipients.Select(_ =>
                                                                   new OrderItemRecipientModel {
                DeliveryDate = null
            }).ToList();

            var result = orderItemValidator.Validate(order, new CreateOrderItemModel {
                ServiceRecipients = serviceRecipients
            }, CatalogueItemType.AdditionalService);

            result.FailedValidations.Should().NotBeEmpty();

            foreach ((_, ValidationResult validationResult) in result.FailedValidations)
            {
                var errorDetails = validationResult.Errors.SingleOrDefault();
                errorDetails.Should().NotBeNull();
                errorDetails.Field.Should().Be("DeliveryDate");
                errorDetails.Id.Should().Be("DeliveryDateRequired");
            }

            result.Success.Should().BeFalse();
        }
        public OrderItemDTO AddOrderItem(OrderItemDTO orderItemDto)
        {
            var orderItem     = OrderItemMapper.DtoToDomain(orderItemDto);
            var errorMessages = new List <string>();

            if (!OrderItemValidator.Validate(orderItem, unitOfWork, ref errorMessages))
            {
                throw new InvalidObjectException("Order", orderItem, errorMessages);
            }

            try
            {
                var item = unitOfWork.ItemRepository.GetById(orderItemDto.ItemId);

                orderItem.ItemPrice         = item.Price;
                orderItem.ItemHeight        = item.Height;
                orderItem.ItemWidth         = item.Width;
                orderItem.ItemUnit          = item.Unit;
                orderItem.ItemWeightInGrams = item.WeightInGrams;

                unitOfWork.OrderItemRepository.Add(orderItem);
                unitOfWork.Commit();

                return(OrderItemMapper.DomainToDto(orderItem));
            }
            catch (Exception exception)
            {
                throw new Exception("An exception occured adding customer: ", exception);
            }
        }
Esempio n. 3
0
        public static void Validate_DeliveryDateWithinMaxDeliveryDateOffsetInDays_SuccessIsTrue(
            Order order,
            CreateOrderItemModel model,
            CatalogueItemType itemType,
            ValidationSettings validationSettings)
        {
            model.ServiceRecipients.Should().NotBeNullOrEmpty();

            itemType.Should().NotBe(CatalogueItemType.AssociatedService);

            var serviceRecipients = model.ServiceRecipients.Select(_ =>
                                                                   new OrderItemRecipientModel {
                DeliveryDate = order.CommencementDate.Value.AddDays(validationSettings.MaxDeliveryDateOffsetInDays - 1)
            }).ToList();

            OrderItemValidator orderItemValidator = new OrderItemValidator(validationSettings);

            order.CommencementDate.Should().NotBeNull();

            var result = orderItemValidator.Validate(order, new CreateOrderItemModel {
                ServiceRecipients = serviceRecipients
            }, itemType);

            result.FailedValidations.Should().BeEmpty();
            result.Success.Should().BeTrue();
        }
Esempio n. 4
0
        public static void Validate_DeliveryDateAfterMaxDeliveryDateOffsetInDays_SuccessIsFalse(
            Order order,
            CreateOrderItemModel model,
            CatalogueItemType itemType,
            ValidationSettings validationSettings)
        {
            model.ServiceRecipients.Should().NotBeNullOrEmpty();

            OrderItemValidator orderItemValidator = new OrderItemValidator(validationSettings);

            var serviceRecipients = model.ServiceRecipients.Select(_ =>
                                                                   new OrderItemRecipientModel {
                DeliveryDate = order.CommencementDate.Value.AddDays(validationSettings.MaxDeliveryDateOffsetInDays + 1)
            }).ToList();

            var result = orderItemValidator.Validate(order, new CreateOrderItemModel {
                ServiceRecipients = serviceRecipients
            }, itemType);

            result.FailedValidations.Should().NotBeEmpty();

            foreach ((_, ValidationResult validationResult) in result.FailedValidations)
            {
                var errorDetails = validationResult.Errors.SingleOrDefault();
                errorDetails.Should().NotBeNull();
                errorDetails.Field.Should().Be("DeliveryDate");
                errorDetails.Id.Should().Be("DeliveryDateOutsideDeliveryWindow");
            }

            result.Success.Should().BeFalse();
        }
Esempio n. 5
0
        public void Validate(Notification note)
        {
            var validator = new OrderItemValidator();

            validator.Validate(note, this);

            this.IsValid = !note.HasErrors;
        }
Esempio n. 6
0
        public static void Validate_NullCommencementDate_ThrowsArgumentNullException(
            Order order,
            CreateOrderItemModel model,
            CatalogueItemType itemType,
            OrderItemValidator orderItemValidator)
        {
            order.CommencementDate = null;

            Assert.Throws <ArgumentNullException>(() => _ = orderItemValidator.Validate(order, model, itemType));
        }
Esempio n. 7
0
        public static void Validate_AssociatedService_SuccessIsTrue(
            Order order,
            CreateOrderItemModel model,
            OrderItemValidator orderItemValidator)
        {
            model.ServiceRecipients.Should().NotBeNullOrEmpty();
            order.CommencementDate.Should().NotBeNull();

            var result = orderItemValidator.Validate(order, model, CatalogueItemType.AssociatedService);

            result.FailedValidations.Should().BeEmpty();
            result.Success.Should().BeTrue();
        }