public static async Task UpdateAsync_UpdatesSupplierDetails(
            [Frozen] Mock <IContactDetailsService> contactDetailsService,
            [Frozen] Mock <ISupplierSectionService> supplierSectionService,
            [Frozen] CallOffId callOffId,
            Order order,
            SupplierModel model,
            SupplierSectionController controller)
        {
            order.Supplier.Should().NotBeEquivalentTo(model);
            supplierSectionService.Setup(o => o.GetOrder(callOffId)).ReturnsAsync(order);
            supplierSectionService.Setup(o => o.SetSupplierSection(order, It.IsAny <Supplier>(), It.IsAny <Contact>())).Callback(() =>
            {
                order.Supplier = new Supplier {
                    Name = model.Name
                };
            });

            contactDetailsService
            .Setup(s => s.AddOrUpdateAddress(It.IsNotNull <Address>(), It.IsNotNull <AddressModel>()))
            .Returns(order.Supplier.Address);

            await controller.UpdateAsync(callOffId, model);

            order.Supplier.Should().BeEquivalentTo(model, o => o.Including(s => s.Name));
        }
        public static async Task UpdateAsync_InvokesAddOrUpdateAddress(
            [Frozen] Mock <IContactDetailsService> contactDetailsService,
            [Frozen] Mock <ISupplierSectionService> supplierSectionService,
            [Frozen] CallOffId callOffId,
            Order order,
            SupplierModel model,
            SupplierSectionController controller)
        {
            var originalAddress = order.Supplier.Address;

            Expression <Func <IContactDetailsService, Address> > addOrUpdateAddress = s => s.AddOrUpdateAddress(
                It.Is <Address>(a => a == originalAddress),
                It.Is <AddressModel>(a => a == model.Address));

            supplierSectionService.Setup(o => o.GetOrder(callOffId)).ReturnsAsync(order);
            supplierSectionService.Setup(o => o.SetSupplierSection(order, It.IsAny <Supplier>(), It.IsAny <Contact>())).Callback(() =>
            {
                order.Supplier.Address = originalAddress;
            });
            contactDetailsService.Setup(addOrUpdateAddress).Returns(originalAddress);

            await controller.UpdateAsync(callOffId, model);

            contactDetailsService.Verify(addOrUpdateAddress);

            order.Supplier.Address.Should().BeEquivalentTo(originalAddress);
        }
Esempio n. 3
0
        public static async Task CreateOrderItemsAsync_ValidationFailure_ReturnsExpectedResponse(
            ErrorDetails errorDetails,
            AggregateValidationResult aggregateValidationResult,
            [Frozen] Mock <ICreateOrderItemService> createOrderItemService,
            [Frozen] Mock <IOrderItemService> orderItemService,
            [Frozen] CallOffId callOffId,
            CatalogueItemId catalogueItemId,
            Order order,
            CreateOrderItemModel model,
            OrderItemsController controller)
        {
            controller.ProblemDetailsFactory = new TestProblemDetailsFactory();
            aggregateValidationResult.AddValidationResult(new ValidationResult(errorDetails), 0);

            Expression <Func <ICreateOrderItemService, Task <AggregateValidationResult> > > createAsync = s => s.CreateAsync(
                It.Is <Order>(o => o.Equals(order)),
                It.Is <CatalogueItemId>(i => i == catalogueItemId),
                It.Is <CreateOrderItemModel>(m => m == model));

            orderItemService.Setup(o => o.GetOrder(callOffId)).ReturnsAsync(order);
            createOrderItemService.Setup(createAsync).ReturnsAsync(aggregateValidationResult);

            var response = await controller.CreateOrderItemAsync(callOffId, catalogueItemId, model);

            response.Should().BeOfType <BadRequestObjectResult>();
            response.As <BadRequestObjectResult>().Value.Should().BeOfType <ValidationProblemDetails>();
        }
 public async Task <Order> GetOrder(CallOffId callOffId, CatalogueItemId catalogueItemId)
 {
     return(await context.Order
            .Where(o => o.Id == callOffId.Id)
            .Include(o => o.DefaultDeliveryDates.Where(d => d.CatalogueItemId == catalogueItemId))
            .SingleOrDefaultAsync());
 }
Esempio n. 5
0
        public static async Task CreateOrderItemAsync_OrderExists_ReturnsCreatedAtActionResult(
            [Frozen] Mock <ICreateOrderItemService> createOrderItemService,
            [Frozen] Mock <IOrderItemService> orderItemService,
            [Frozen] CallOffId callOffId,
            CatalogueItemId catalogueItemId,
            Order order,
            CreateOrderItemModel model,
            OrderItemsController controller)
        {
            Expression <Func <ICreateOrderItemService, Task <AggregateValidationResult> > > createAsync = s => s.CreateAsync(
                It.Is <Order>(o => o.Equals(order)),
                It.Is <CatalogueItemId>(i => i == catalogueItemId),
                It.Is <CreateOrderItemModel>(m => m == model));

            orderItemService.Setup(o => o.GetOrder(callOffId)).ReturnsAsync(order);
            createOrderItemService.Setup(createAsync).ReturnsAsync(new AggregateValidationResult());

            var result = await controller.CreateOrderItemAsync(callOffId, catalogueItemId, model);

            result.Should().BeOfType <CreatedAtActionResult>();
            result.As <CreatedAtActionResult>().Should().BeEquivalentTo(new
            {
                ActionName  = "Get",
                RouteValues = new RouteValueDictionary
                {
                    { nameof(callOffId), callOffId.ToString() },
                    { nameof(catalogueItemId), catalogueItemId.ToString() },
                },
            });
        }
Esempio n. 6
0
        public static async Task CreateOrderItemAsync_ValidationFailure_AddsModelErrors(
            [Frozen] IReadOnlyList <ErrorDetails> errorDetails,
            AggregateValidationResult aggregateValidationResult,
            [Frozen] Mock <ICreateOrderItemService> createOrderItemService,
            [Frozen] Mock <IOrderItemService> orderItemService,
            [Frozen] CallOffId callOffId,
            CatalogueItemId catalogueItemId,
            Order order,
            CreateOrderItemModel model,
            OrderItemsController controller)
        {
            controller.ProblemDetailsFactory = new TestProblemDetailsFactory();
            aggregateValidationResult.AddValidationResult(new ValidationResult(errorDetails), 0);

            Expression <Func <ICreateOrderItemService, Task <AggregateValidationResult> > > createAsync = s => s.CreateAsync(
                It.Is <Order>(o => o.Equals(order)),
                It.Is <CatalogueItemId>(i => i == catalogueItemId),
                It.Is <CreateOrderItemModel>(m => m == model));

            orderItemService.Setup(o => o.GetOrder(callOffId)).ReturnsAsync(order);
            createOrderItemService.Setup(createAsync).ReturnsAsync(aggregateValidationResult);

            await controller.CreateOrderItemAsync(callOffId, catalogueItemId, model);

            var modelState = controller.ModelState;

            modelState.ErrorCount.Should().Be(errorDetails.Count);
            modelState.Keys.Should().BeEquivalentTo(errorDetails.Select(e => e.ParentName + "[0]." + e.Field));

            var modelStateErrors = modelState.Values.Select(v => v.Errors[0].ErrorMessage);

            modelStateErrors.Should().BeEquivalentTo(errorDetails.Select(e => e.Id));
        }
Esempio n. 7
0
        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());
        }
Esempio n. 8
0
        public static async Task ListAsync_WithFilter_ReturnsExpectedResult(
            [Frozen] Mock <IOrderItemService> service,
            [Frozen] CallOffId callOffId,
            List <OrderItem> orderItems,
            Order order,
            OrderItemsController controller)
        {
            foreach (var orderItem in orderItems)
            {
                order.AddOrUpdateOrderItem(orderItem);
            }

            const CatalogueItemType catalogueItemType = CatalogueItemType.AdditionalService;

            service.Setup(o => o.GetOrder(callOffId)).ReturnsAsync(order);
            service.Setup(o => o.GetOrderItems(callOffId, catalogueItemType)).ReturnsAsync(
                orderItems.Where(i => i.CatalogueItem.CatalogueItemType == catalogueItemType).Select(i => i).ToList());

            var expectedResult = orderItems
                                 .Where(i => i.CatalogueItem.CatalogueItemType == catalogueItemType)
                                 .Select(i => new GetOrderItemModel(i));

            var response = await controller.ListAsync(callOffId, catalogueItemType);

            response.Value.Should().BeEquivalentTo(expectedResult);
        }
 public async Task <string> GetOrderDescription(CallOffId callOffId)
 {
     return(await context.Order
            .Where(o => o.Id == callOffId.Id)
            .Select(o => o.Description)
            .SingleOrDefaultAsync());
 }
Esempio n. 10
0
        public async Task <IActionResult> UpdateAsync(CallOffId callOffId, SupplierModel model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var order = await supplierSectionService.GetOrder(callOffId);

            if (order is null)
            {
                return(NotFound());
            }

            var supplierModel = new Supplier
            {
                Id      = model.SupplierId,
                Name    = model.Name,
                Address = contactDetailsService.AddOrUpdateAddress(order.Supplier?.Address, model.Address),
            };

            var contact = contactDetailsService.AddOrUpdatePrimaryContact(
                order.SupplierContact,
                model.PrimaryContact);

            await supplierSectionService.SetSupplierSection(order, supplierModel, contact);

            return(NoContent());
        }
        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 void AddOrUpdateAsync_NullDefaultDeliveryDateModel_ThrowsArgumentNullException(
     CallOffId callOffId,
     CatalogueItemId catalogueItemId,
     DefaultDeliveryDateController controller)
 {
     Assert.ThrowsAsync <ArgumentNullException>(() => controller.AddOrUpdateAsync(callOffId, catalogueItemId, null));
 }
        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() },
                },
            });
        }
Esempio n. 14
0
        public static void Parse_ValidId_ReturnsExpectedResult()
        {
            (bool success, CallOffId id) = CallOffId.Parse("C999999-99");

            success.Should().BeTrue();
            id.Should().Be(new CallOffId(999999, 99));
        }
 public async Task <bool?> GetFundingSource(CallOffId callOffId)
 {
     return(await context.Order
            .Where(o => o.Id == callOffId.Id)
            .Select(o => o.FundingSourceOnlyGms)
            .SingleOrDefaultAsync());
 }
Esempio n. 16
0
        public static async Task UpdateAsync_InvokesAddOrUpdatePrimaryContact(
            [Frozen] Mock <IContactDetailsService> contactDetailsService,
            [Frozen] Mock <IOrderingPartyService> service,
            [Frozen] CallOffId callOffId,
            Order order,
            OrderingPartyModel model,
            OrderingPartyController controller)
        {
            var originalContact = order.OrderingPartyContact;

            service.Setup(o => o.GetOrder(callOffId)).ReturnsAsync(order);
            service.Setup(o => o.SetOrderingParty(order, It.IsAny <OrderingParty>(), It.IsAny <Contact>())).Callback(() =>
            {
                order.OrderingParty.Name    = model.Name;
                order.OrderingParty.OdsCode = model.OdsCode;
            });
            contactDetailsService.Setup(o => o.AddOrUpdatePrimaryContact(
                                            It.Is <Contact>(c => c == originalContact),
                                            It.Is <PrimaryContactModel>(c => c == model.PrimaryContact)))
            .Returns(order.OrderingPartyContact);

            await controller.UpdateAsync(callOffId, model);

            contactDetailsService.Verify(s => s.AddOrUpdatePrimaryContact(
                                             It.Is <Contact>(c => c == originalContact),
                                             It.Is <PrimaryContactModel>(c => c == model.PrimaryContact)));

            order.OrderingPartyContact.Should().BeEquivalentTo(originalContact);
        }
Esempio n. 17
0
        public static async Task OnAuthorizationAsync_UserHasSamePrimaryOrganisationId_ReturnsExpectedValue(
            [Frozen] ApplicationDbContext dbContext,
            [Frozen] CallOffId callOffId,
            Order order,
            OrderLookupOrganisationAuthorizationFilter filter)
        {
            dbContext.Order.Add(order);
            await dbContext.SaveChangesAsync();

            const string parameterName = OrderLookupOrganisationAuthorizationFilter.DefaultParameterName;

            var user = ClaimsPrincipalBuilder.Create()
                       .WithClaim(ApplicationClaimTypes.Ordering)
                       .WithClaim(UserClaimTypes.PrimaryOrganisationId, order.OrderingParty.Id.ToString())
                       .Build();

            var actionDescriptor = new ActionDescriptor
            {
                EndpointMetadata = new object[] { new AuthorizeOrganisationAttribute() },
                Parameters       = new[] { new ParameterDescriptor {
                                               Name = parameterName
                                           } },
            };

            var context = AuthorizationFilterContextBuilder.Create()
                          .WithActionDescription(actionDescriptor)
                          .WithRouteValue(parameterName, callOffId.ToString())
                          .WithUser(user)
                          .Build();

            await filter.OnAuthorizationAsync(context);

            context.Result.Should().BeNull();
        }
Esempio n. 18
0
 public async Task <Order> GetOrder(CallOffId callOffId)
 {
     return(await context.Order
            .Where(o => o.Id == callOffId.Id)
            .Include(o => o.Progress)
            .SingleOrDefaultAsync());
 }
Esempio n. 19
0
        public async Task <IActionResult> CreateOrderItemAsync(
            CallOffId callOffId,
            CatalogueItemId catalogueItemId,
            CreateOrderItemModel model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var order = await orderItemService.GetOrder(callOffId);

            if (order is null)
            {
                return(NotFound());
            }

            var validationResult = await createOrderItemService.CreateAsync(order, catalogueItemId, model);

            if (validationResult.Success)
            {
                return(CreatedAtAction(
                           nameof(GetAsync).TrimAsync(),
                           new { callOffId = callOffId.ToString(), catalogueItemId = catalogueItemId.ToString() },
                           null));
            }

            foreach ((string key, string errorMessage) in validationResult.ToModelErrors())
            {
                ModelState.AddModelError(key, errorMessage);
            }

            return(ValidationProblem(ModelState));
        }
        public static async Task GetAllAsync_HasServiceRecipients_ReturnsExpectedResult(
            [Frozen] Mock <IServiceRecipientService> service,
            [Frozen] CallOffId callOffId,
            Order order,
            List <OrderItem> orderItems,
            ServiceRecipientsSectionController controller)
        {
            foreach (var orderItem in orderItems)
            {
                order.AddOrUpdateOrderItem(orderItem);
            }

            var serviceRecipients = order.OrderItems
                                    .Where(o => o.CatalogueItem.CatalogueItemType == CatalogueItemType.Solution)
                                    .SelectMany(o => o.OrderItemRecipients)
                                    .Select(r => new ServiceRecipient(r.Recipient.OdsCode, r.Recipient.Name))
                                    .ToList();

            service.Setup(s => s.GetAllOrderItemRecipients(callOffId)).ReturnsAsync(
                serviceRecipients);

            var expected = new ServiceRecipientsModel
            {
                ServiceRecipients = serviceRecipients.Select(r =>
                                                             new ServiceRecipientModel {
                    Name = r.Name, OdsCode = r.OdsCode
                })
                                    .ToList(),
            };

            var response = await controller.GetAllAsync(callOffId);

            response.Value.Should().BeEquivalentTo(expected);
        }
 public async Task <DateTime?> GetCommencementDate(CallOffId callOffId)
 {
     return(await context.Order
            .Where(o => o.Id == callOffId.Id)
            .Select(o => o.CommencementDate)
            .SingleOrDefaultAsync());
 }
Esempio n. 22
0
        public async Task <ActionResult> UpdateAsync(CallOffId callOffId, OrderingPartyModel model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var order = await orderingPartyService.GetOrder(callOffId);

            if (order is null)
            {
                return(NotFound());
            }

            var orderingParty = new OrderingParty
            {
                Name    = model.Name,
                OdsCode = model.OdsCode,
                Address = contactDetailsService.AddOrUpdateAddress(order.OrderingParty.Address, model.Address),
            };

            Contact contact = contactDetailsService.AddOrUpdatePrimaryContact(
                order.OrderingPartyContact,
                model.PrimaryContact);

            await orderingPartyService.SetOrderingParty(order, orderingParty, contact);

            return(NoContent());
        }
        public static async Task GetOrderDescription_ReturnsNull(
            CallOffId callOffId,
            OrderDescriptionService service)
        {
            var result = await service.GetOrderDescription(callOffId);

            result.Should().BeNull();
        }
Esempio n. 24
0
 public async Task <Order> GetOrder(CallOffId callOffId)
 {
     return(await context.Order
            .Where(o => o.Id == callOffId.Id)
            .Include(o => o.Supplier).ThenInclude(s => s.Address)
            .Include(o => o.SupplierContact)
            .SingleOrDefaultAsync());
 }
        public void ThenTheOrderIsSaved()
        {
            Test.Pages.OrderForm.TaskListDisplayed().Should().BeTrue();
            var(success, callOffId) = CallOffId.Parse(Test.Pages.OrderForm.GetCallOffId());
            var order = DbContext.Order.Single(o => o.Id == callOffId.Id);

            Context.TryAdd(ContextKeys.CreatedOrder, order);
        }
Esempio n. 26
0
        public static async Task GetOrderingParty_ReturnsNull(
            CallOffId callOffId,
            OrderingPartyService service)
        {
            var result = await service.GetOrder(callOffId);

            result.Should().BeNull();
        }
Esempio n. 27
0
        public static async Task GetAsync_OrderIdDoesNotExist_ReturnsNull(
            CallOffId callOffId,
            OrderDescriptionController controller)
        {
            var result = await controller.GetAsync(callOffId);

            result.Result.Should().BeNull();
        }
Esempio n. 28
0
        public static async Task GetFundingSource_ReturnsNull(
            CallOffId callOffId,
            FundingSourceService service)
        {
            var result = await service.GetFundingSource(callOffId);

            result.Should().BeNull();
        }
Esempio n. 29
0
        public static async Task GetOrder_ReturnsNull(
            CallOffId callOffId,
            SupplierSectionService service)
        {
            var result = await service.GetOrder(callOffId);

            result.Should().BeNull();
        }
Esempio n. 30
0
        public static async Task GetCommencementDate_ReturnsNull(
            CallOffId callOffId,
            CommencementDateService service)
        {
            var result = await service.GetCommencementDate(callOffId);

            result.Should().BeNull();
        }