public async Task <IActionResult> AddOrUpdateAsync( CallOffId callOffId, CatalogueItemId catalogueItemId, DefaultDeliveryDateModel model) { if (model is null) { throw new ArgumentNullException(nameof(model)); } Order order = await defaultDeliveryDateService.GetOrder(callOffId, catalogueItemId); if (order is null) { return(NotFound()); } (bool isValid, ErrorsModel errors) = validator.Validate(model, order.CommencementDate); if (!isValid) { return(BadRequest(errors)); } // ReSharper disable once PossibleInvalidOperationException (covered by model validation) DeliveryDateResult addedOrUpdated = await defaultDeliveryDateService.SetDefaultDeliveryDate(callOffId, catalogueItemId, model.DeliveryDate.Value); return(addedOrUpdated == DeliveryDateResult.Added ? CreatedAtAction(nameof(GetAsync).TrimAsync(), new { callOffId = callOffId.ToString(), catalogueItemId = catalogueItemId.ToString() }, null) : Ok()); }
public static async Task AddOrUpdateAsync_AddsDefaultDeliveryDate( [Frozen] Mock <IDefaultDeliveryDateService> service, [Frozen] CallOffId callOffId, [Frozen] CatalogueItemId catalogueItemId, Order order, DefaultDeliveryDateModel defaultDeliveryDate, [Frozen] Mock <IDefaultDeliveryDateValidator> validator, DefaultDeliveryDateController controller) { service.Setup(o => o.GetOrder(callOffId, catalogueItemId)).ReturnsAsync(order); service.Setup(o => o.SetDefaultDeliveryDate(callOffId, catalogueItemId, defaultDeliveryDate.DeliveryDate.Value)).Callback(() => { order.SetDefaultDeliveryDate(catalogueItemId, defaultDeliveryDate.DeliveryDate !.Value); }); validator.Setup(v => v.Validate(defaultDeliveryDate, order.CommencementDate)).Returns((true, null)); order.DefaultDeliveryDates.Should().BeEmpty(); await controller.AddOrUpdateAsync(callOffId, catalogueItemId, defaultDeliveryDate); service.Verify(o => o.GetOrder(callOffId, catalogueItemId)); service.Verify(o => o.SetDefaultDeliveryDate(callOffId, catalogueItemId, defaultDeliveryDate.DeliveryDate.Value)); var expectedDeliveryDate = new DefaultDeliveryDate { OrderId = callOffId.Id, CatalogueItemId = catalogueItemId, DeliveryDate = defaultDeliveryDate.DeliveryDate.GetValueOrDefault(), }; order.DefaultDeliveryDates.Should().HaveCount(1); order.DefaultDeliveryDates.Should().Contain(expectedDeliveryDate); }
public static async Task AddOrUpdateAsync_Add_ReturnsExpectedStatusCode( [Frozen] Mock <IDefaultDeliveryDateService> service, [Frozen] CallOffId callOffId, [Frozen] CatalogueItemId catalogueItemId, Order order, DefaultDeliveryDateModel defaultDeliveryDate, [Frozen] Mock <IDefaultDeliveryDateValidator> validator, DefaultDeliveryDateController controller) { service.Setup(o => o.GetOrder(callOffId, catalogueItemId)).ReturnsAsync(order); service.Setup(o => o.SetDefaultDeliveryDate(callOffId, catalogueItemId, defaultDeliveryDate.DeliveryDate.Value)).Callback(() => { order.SetDefaultDeliveryDate(catalogueItemId, defaultDeliveryDate.DeliveryDate !.Value); }); validator.Setup(v => v.Validate(defaultDeliveryDate, order.CommencementDate)).Returns((true, null)); var response = await controller.AddOrUpdateAsync(callOffId, catalogueItemId, defaultDeliveryDate); service.Verify(o => o.GetOrder(callOffId, catalogueItemId)); service.Verify(o => o.SetDefaultDeliveryDate(callOffId, catalogueItemId, defaultDeliveryDate.DeliveryDate.Value)); response.Should().BeOfType <CreatedAtActionResult>(); response.As <CreatedAtActionResult>().Should().BeEquivalentTo(new { ActionName = "Get", RouteValues = new RouteValueDictionary { { nameof(callOffId), callOffId.ToString() }, { nameof(catalogueItemId), catalogueItemId.ToString() }, }, }); }
public static async Task AddOrUpdateAsync_BadCatalogueItemId_ReturnsNotFound( [Frozen] CallOffId callOffId, CatalogueItemId catalogueItemId, DefaultDeliveryDateModel model, DefaultDeliveryDateController controller) { var response = await controller.AddOrUpdateAsync(callOffId, catalogueItemId, model); response.Should().BeOfType <NotFoundResult>(); }
public static void Validate_NullCommencementDate_ReturnsExpectedResult( DefaultDeliveryDateModel model, DefaultDeliveryDateValidator validator) { (bool isValid, ErrorsModel errors) = validator.Validate(model, null); isValid.Should().BeFalse(); errors.Should().NotBeNull(); errors.Errors.Should().NotBeNull(); errors.Errors.Should().HaveCount(1); errors.Errors.First().Should().BeEquivalentTo(DefaultDeliveryDateValidator.CommencementDateRequired); }
public static void Validate_DateAtLimit_ReturnsExpectedResult( DateTime commencementDate, [Frozen] ValidationSettings validationSettings, DefaultDeliveryDateModel model, DefaultDeliveryDateValidator validator) { model.DeliveryDate = commencementDate.AddDays(validationSettings.MaxDeliveryDateOffsetInDays); (bool isValid, ErrorsModel errors) = validator.Validate(model, commencementDate); isValid.Should().BeTrue(); errors.Should().NotBeNull(); errors.Errors.Should().NotBeNull(); errors.Errors.Should().HaveCount(0); }
public static void Validate_DateBeforeCommencementDate_ReturnsExpectedResult( DateTime commencementDate, DefaultDeliveryDateModel model, DefaultDeliveryDateValidator validator) { model.DeliveryDate = commencementDate.AddDays(-1); (bool isValid, ErrorsModel errors) = validator.Validate(model, commencementDate); isValid.Should().BeFalse(); errors.Should().NotBeNull(); errors.Errors.Should().NotBeNull(); errors.Errors.Should().HaveCount(1); errors.Errors.First().Should().BeEquivalentTo(DefaultDeliveryDateValidator.OutsideWindow); }
public static void Validate_DateOutsideWindow_ReturnsExpectedResult( DateTime commencementDate, [Frozen] ValidationSettings validationSettings, DefaultDeliveryDateModel model, DefaultDeliveryDateValidator validator) { model.DeliveryDate = commencementDate.AddDays(validationSettings.MaxDeliveryDateOffsetInDays + 1); (bool isValid, ErrorsModel errors) = validator.Validate(model, commencementDate); isValid.Should().BeFalse(); errors.Should().NotBeNull(); errors.Errors.Should().NotBeNull(); errors.Errors.Should().HaveCount(1); errors.Errors.First().Should().BeEquivalentTo(DefaultDeliveryDateValidator.OutsideWindow); }
public static async Task AddOrUpdateAsync_NotValid_ReturnsExpectedResponse( [Frozen] Mock <IDefaultDeliveryDateService> service, [Frozen] CallOffId callOffId, CatalogueItemId catalogueItemId, Order order, DefaultDeliveryDateModel model, [Frozen] Mock <IDefaultDeliveryDateValidator> validator, ErrorsModel errors, DefaultDeliveryDateController controller) { service.Setup(o => o.GetOrder(callOffId, catalogueItemId)).ReturnsAsync(order); validator.Setup(v => v.Validate(model, order.CommencementDate)).Returns((false, errors)); var response = await controller.AddOrUpdateAsync(callOffId, catalogueItemId, model); service.Verify(o => o.GetOrder(callOffId, catalogueItemId)); response.Should().BeOfType <BadRequestObjectResult>(); response.As <BadRequestObjectResult>().Value.Should().Be(errors); }
public static async Task AddOrUpdateAsync_Update_ReturnsExpectedStatusCode( [Frozen] Mock <IDefaultDeliveryDateService> service, [Frozen] CallOffId callOffId, [Frozen] CatalogueItemId catalogueItemId, Order order, DefaultDeliveryDateModel defaultDeliveryDate, [Frozen] Mock <IDefaultDeliveryDateValidator> validator, DefaultDeliveryDateController controller) { service.Setup(o => o.GetOrder(callOffId, catalogueItemId)).ReturnsAsync(order); service.Setup(o => o.SetDefaultDeliveryDate(callOffId, catalogueItemId, defaultDeliveryDate.DeliveryDate.Value)).ReturnsAsync(DeliveryDateResult.Updated); validator.Setup(v => v.Validate(defaultDeliveryDate, order.CommencementDate)).Returns((true, null)); var response = await controller.AddOrUpdateAsync(callOffId, catalogueItemId, defaultDeliveryDate); service.Verify(o => o.GetOrder(callOffId, catalogueItemId)); service.Verify(o => o.SetDefaultDeliveryDate(callOffId, catalogueItemId, defaultDeliveryDate.DeliveryDate.Value)); response.Should().BeOfType <OkResult>(); }