Beispiel #1
0
        public void BasketsController_RemoveBasketItem()
        {
            //prepare dataset: a product, a basket and a basket item
            var product = new PhotoProduct {
                Name = "Alpha", Id = "42"
            };
            var basket = new Basket {
                Id = "Aruba", Created = DateTime.Now, Modified = DateTime.Now
            };
            var basketItem = basket.Add(product, 1337);

            //prepare the ceremonies: the repo's, services and the controller
            var productRepo = new PhotoProductMemoryRepository();

            productRepo.Add(product);
            var productService = new PhotoProductService(productRepo);

            var repo = new BasketMemoryRepository();

            repo.Add(basket);

            var service = new BasketService(repo, productService);

            var controller = new BasketsController(service, productService);

            //actual test
            var result = controller.RemoveBasketItem("Aruba", basket.Contents[0].Id);

            Assert.IsNotNull(result, "There should be a result.");
            Assert.IsInstanceOfType(result, typeof(OkResult));

            //check result in original basket
            Assert.AreEqual(0, basket.Contents.Count, "There should be no basket items anymore.");
        }
Beispiel #2
0
        public void BasketsController_GetBasket()
        {
            var productRepo    = new PhotoProductMemoryRepository();
            var productService = new PhotoProductService(productRepo);
            var repo           = new BasketMemoryRepository();

            repo.Add(new Basket {
                Id = "Aruba", Created = DateTime.Now, Modified = DateTime.Now
            });
            var service = new BasketService(repo, productService);

            var controller = new BasketsController(service, productService);

            var result = controller.Get("Aruba");

            Assert.IsNotNull(result, "There should be a result.");
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));

            var okResult = result as OkObjectResult;

            Assert.IsNotNull(okResult.Value, "Value should not be null.");
            Assert.IsInstanceOfType(okResult.Value, typeof(BasketModel));

            var basket = okResult.Value as BasketModel;

            Assert.IsNotNull(basket, "The retrieved basket should not be null.");
            Assert.AreEqual("Aruba", basket.Id);
        }
Beispiel #3
0
        public void BasketsController_AddBasketItem()
        {
            var productRepo = new PhotoProductMemoryRepository();

            productRepo.Add(new PhotoProduct {
                Name = "Alpha", Id = "42"
            });
            var productService = new PhotoProductService(productRepo);

            var repo = new BasketMemoryRepository();

            repo.Add(new Basket {
                Id = "Aruba", Created = DateTime.Now, Modified = DateTime.Now
            });
            var service = new BasketService(repo, productService);

            var controller = new BasketsController(service, productService);

            var result = controller.AddBasketItem("Aruba", "42", 1337);

            Assert.IsNotNull(result, "There should be a result.");
            Assert.IsInstanceOfType(result, typeof(OkObjectResult));

            var okResult = result as OkObjectResult;

            Assert.IsNotNull(okResult.Value, "Value should not be null.");
            Assert.IsInstanceOfType(okResult.Value, typeof(BasketItemModel));

            var item = okResult.Value as BasketItemModel;

            Assert.IsNotNull(item, "New basket item should be present");
            Assert.IsNotNull(item.Content, "New basket item content should be present.");
            Assert.AreEqual("42", item.Content.Id, "Content identifier should be 42.");
            Assert.AreEqual(1337u, item.Amount, "The amount should be 1337.");
        }
Beispiel #4
0
        public void BasketsController_Update_Basket_Order_Line_Return_Correct_Updated_Order_Line()
        {
            var baskets  = new InMemoryDataStore <Basket>();
            var products = new InMemoryDataStore <Product>();

            products.Seed();
            baskets.Seed(products);

            var bc = new BasketsController(products, baskets, _mapper);

            var actionResult = bc.PatchOrderLine(_testBasketId, _testProductId, new JsonPatchDocument <OrderLineUpdateDto>().Add(p => p.Quantity, 12));

            OkObjectResult okresult = actionResult as OkObjectResult;

            Assert.IsNotNull(okresult);

            OrderLineDto resultOrderLine = okresult.Value as OrderLineDto;

            Assert.IsNotNull(resultOrderLine);

            Assert.IsTrue(resultOrderLine.Quantity == 12);

            OrderLine orderline = null;

            baskets.Get(_testBasketId)?.OrderLines.TryGetValue(resultOrderLine.ProductId, out orderline);

            Assert.IsNotNull(orderline);

            Assert.IsTrue(orderline.Quantity == 12);
        }
Beispiel #5
0
        public void BasketsController_Create_Basket_Order_Line_Return_Correct_Order_Line()
        {
            var baskets  = new InMemoryDataStore <Basket>();
            var products = new InMemoryDataStore <Product>();

            products.Seed();
            baskets.Seed(products);

            var orderLine = new OrderLineCreateDto()
            {
                ProductId = new Guid("3aee1758-77b9-4e86-9c57-77adbbc0956f"), //Watermelon
                Quantity  = 5
            };

            var bc           = new BasketsController(products, baskets, _mapper);
            var actionResult = bc.PostAddOrderLineToBasket(_testBasketId, orderLine);

            CreatedAtRouteResult createdresult = actionResult as CreatedAtRouteResult;

            Assert.IsNotNull(createdresult);

            OrderLineDto resultOrderLine = createdresult.Value as OrderLineDto;

            Assert.IsNotNull(resultOrderLine);

            Assert.AreEqual(resultOrderLine.ProductId, orderLine.ProductId);
        }
Beispiel #6
0
        public async Task Should_Return_NotFound_If_Basket_Doesnt_Exists()
        {
            using (var context = await MockDbContext.GetDbContext())
            {
                controller = new BasketsController(context);
                var actionResult = await controller.DeleteItemAsync(15);

                Assert.Equal((int)HttpStatusCode.NotFound, (actionResult as NotFoundResult).StatusCode);
            }
        }
Beispiel #7
0
        public async Task Should_Delete_Basket_And_Related_Bookings()
        {
            using (var context = await MockDbContext.GetDbContext())
            {
                controller = new BasketsController(context);
                var actionResult = await controller.DeleteItemAsync(2);

                var basketBookings = context.Set <Booking>().Where(x => x.BasketID == 2 && x.DeletedAt == null).ToList();
                Assert.Equal((int)HttpStatusCode.NoContent, (actionResult as NoContentResult).StatusCode);
                Assert.Empty(basketBookings);
            }
        }
Beispiel #8
0
        public async Task Should_Update_Basket()
        {
            using (var context = await MockDbContext.GetDbContext())
            {
                controller = new BasketsController(context);
                var basketMock = context.Find <Basket.API.Models.Basket>(1);
                basketMock.State = BasketStates.SENT;
                var actionResult = await controller.PutItemAsync(basketMock, 1) as NoContentResult;

                Assert.Equal((int)HttpStatusCode.NoContent, actionResult.StatusCode);
            }
        }
        public void Setup()
        {
            var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new MappingProfile());
            });

            IMapper mapper = mappingConfig.CreateMapper();

            _storage    = new Storage();
            _controller = new BasketsController(_storage, mapper);
        }
Beispiel #10
0
        public void BasketsController_RemoveBasketItem_InvalidBasket_BadRequest()
        {
            var productService = new PhotoProductService(new PhotoProductMemoryRepository());
            var repo           = new BasketMemoryRepository();
            var service        = new BasketService(repo, productService);

            var controller = new BasketsController(service, productService);

            var result = controller.RemoveBasketItem("Aruba", "XX");

            Assert.IsNotNull(result, "There should be a result.");
            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
        }
Beispiel #11
0
        public async Task Should_Update_Bookings_When_Basket_Validated()
        {
            using (var context = await MockDbContext.GetDbContext())
            {
                controller = new BasketsController(context);
                var basketMock = context.Find <Basket.API.Models.Basket>(2);
                basketMock.State = BasketStates.VALIDATED;
                var actionResult = await controller.PutItemAsync(basketMock, 2);

                var basketBookings = context.Set <Booking>().Where(x => x.BasketID == basketMock.Id && x.DeletedAt == null).ToList();
                Assert.Empty(basketBookings);
            }
        }
Beispiel #12
0
        private BasketsControllerMock()
        {
            this.GetAllBasketsMock  = new Mock <IGetAllBaskets>();
            this.GetBasketsByIdMock = new Mock <IGetBasketsById>();
            this.InsertBasketMock   = new Mock <IInsertBasket>();
            this.DeleteBasketMock   = new Mock <IDeleteBasket>();

            this.Target = new BasketsController(
                GetAllBasketsMock.Object,
                GetBasketsByIdMock.Object,
                InsertBasketMock.Object,
                DeleteBasketMock.Object
                );
        }
Beispiel #13
0
        public void BasketsController_GetBasket_NotFound()
        {
            var productRepo    = new PhotoProductMemoryRepository();
            var productService = new PhotoProductService(productRepo);
            var repo           = new BasketMemoryRepository();
            var service        = new BasketService(repo, productService);

            var controller = new BasketsController(service, productService);

            var result = controller.Get("Congo");

            Assert.IsNotNull(result, "There should be a result.");
            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
Beispiel #14
0
        public async Task Should_Not_Create_Basket_If_It_Does_Exists()
        {
            using (var context = await MockDbContext.GetDbContext())
            {
                controller = new BasketsController(context);
                var basketMock = new Basket.API.Models.Basket
                {
                    User = 3
                };
                var actionResult = await controller.PostItemAsync(basketMock) as ObjectResult;

                Assert.Equal((int)HttpStatusCode.BadRequest, (actionResult as ObjectResult).StatusCode);
            }
        }
Beispiel #15
0
        public void BasketsController_Delete_Basket_Return_Success()
        {
            var baskets  = new InMemoryDataStore <Basket>();
            var products = new InMemoryDataStore <Product>();

            products.Seed();
            baskets.Seed(products);

            var bc = new BasketsController(products, baskets, _mapper);

            var actionResult = bc.DeleteBasketById(_testBasketId);

            OkResult okresult = actionResult as OkResult;

            Assert.IsNotNull(okresult);
        }
Beispiel #16
0
        public async Task CheckoutShouldReturn404NotFound()
        {
            var basketServiceMock = new Mock <IBasketService>(MockBehavior.Strict);

            basketServiceMock.Setup(service => service.Exists(null)).ReturnsAsync(false);

            var productServiceMock = new Mock <IProductService>(MockBehavior.Strict);

            var controller = new BasketsController(basketServiceMock.Object, productServiceMock.Object);

            var result = await controller.Checkout(null, new MoneyDataContract());

            Assert.IsType <NotFoundResult>(result);

            basketServiceMock.Verify(service => service.Exists(null), Times.Once);
            basketServiceMock.VerifyAll();
        }
Beispiel #17
0
        public async Task CreateShouldReturn201Created()
        {
            var basketServiceMock = new Mock <IBasketService>(MockBehavior.Strict);

            basketServiceMock.Setup(service => service.CreateBasket()).ReturnsAsync(new Basket());

            var productServiceMock = new Mock <IProductService>(MockBehavior.Strict);

            var controller = new BasketsController(basketServiceMock.Object, productServiceMock.Object);

            var result = await controller.Create();

            Assert.IsType <CreatedAtRouteResult>(result);

            basketServiceMock.Verify(service => service.CreateBasket(), Times.Once);
            basketServiceMock.VerifyAll();
        }
Beispiel #18
0
        public async Task Should_Create_Basket_If_It_Does_Not_Exists()
        {
            using (var context = await MockDbContext.GetDbContext())
            {
                controller = new BasketsController(context);
                var basketMock = new Basket.API.Models.Basket
                {
                    User = 10
                };
                var actionResult = await controller.PostItemAsync(basketMock);

                var returnedObject = (actionResult as ObjectResult).Value as Basket.API.Models.Basket;
                Assert.Equal(201, (actionResult as ObjectResult).StatusCode);
                Assert.Equal(4, returnedObject.Id);
                Assert.Equal(10, returnedObject.User);
                Assert.Equal(BasketStates.PENDING, returnedObject.State);
            }
        }
Beispiel #19
0
        public void BasketsController_RemoveBasketItem_InvalidBasketItemId_NotFound()
        {
            var productService = new PhotoProductService(new PhotoProductMemoryRepository());
            var basket         = new Basket {
                Id = "Aruba", Created = DateTime.Now, Modified = DateTime.Now
            };
            var repo = new BasketMemoryRepository();

            repo.Add(basket);
            var service = new BasketService(repo, productService);

            var controller = new BasketsController(service, productService);

            var result = controller.RemoveBasketItem("Aruba", "XX");

            Assert.IsNotNull(result, "There should be a result.");
            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
Beispiel #20
0
        public async Task GetByIdShouldReturn404NotFound()
        {
            var basketServiceMock = new Mock <IBasketService>(MockBehavior.Strict);

            basketServiceMock.Setup(service => service.GetById(null)).ReturnsAsync((Basket)null);

            var productServiceMock = new Mock <IProductService>(MockBehavior.Strict);

            var controller = new BasketsController(basketServiceMock.Object, productServiceMock.Object);

            var result = await controller.GetById(null);

            Assert.IsType <NotFoundResult>(result);

            productServiceMock.VerifyAll();

            basketServiceMock.Verify(service => service.GetById(null), Times.Once);
            basketServiceMock.VerifyAll();
        }
Beispiel #21
0
        public void BasketsController_AddBasketItem_InvalidProduct_BadRequest()
        {
            var productRepo    = new PhotoProductMemoryRepository();
            var productService = new PhotoProductService(productRepo);

            var repo = new BasketMemoryRepository();

            repo.Add(new Basket {
                Id = "Aruba", Created = DateTime.Now, Modified = DateTime.Now
            });
            var service = new BasketService(repo, productService);

            var controller = new BasketsController(service, productService);

            var result = controller.AddBasketItem("Aruba", "XXX", 1);

            Assert.IsNotNull(result, "There should be a result.");
            Assert.IsInstanceOfType(result, typeof(BadRequestObjectResult));
        }
Beispiel #22
0
        public async Task AddProductToBasketShouldReturn404NotFoundForBasket()
        {
            var basketServiceMock = new Mock <IBasketService>(MockBehavior.Strict);

            basketServiceMock.Setup(service => service.Exists(null)).ReturnsAsync(false);

            var productServiceMock = new Mock <IProductService>(MockBehavior.Strict);

            var controller = new BasketsController(basketServiceMock.Object, productServiceMock.Object);

            var result = await controller.AddProductToBasket(null, 0);

            Assert.IsType <NotFoundObjectResult>(result);

            productServiceMock.VerifyAll();

            basketServiceMock.Verify(service => service.AddProductToBasket(null, 0), Times.Never);
            basketServiceMock.Verify(service => service.Exists(null), Times.Once);
            basketServiceMock.VerifyAll();
        }
Beispiel #23
0
        public void BasketsController_Get_Single_Basket_Return_Correct_Basket()
        {
            var baskets  = new InMemoryDataStore <Basket>();
            var products = new InMemoryDataStore <Product>();

            products.Seed();
            baskets.Seed(products);

            var bc = new BasketsController(products, baskets, _mapper);

            var actionResult = bc.GetBasketById(_testBasketId);

            OkObjectResult okresult = actionResult as OkObjectResult;

            Assert.IsNotNull(okresult);

            BasketDto resultBasket = okresult.Value as BasketDto;

            Assert.IsNotNull(resultBasket);

            Assert.AreEqual(resultBasket.Id, _testBasketId);
        }
Beispiel #24
0
        public void BasketsController_Get_Basket_Order_Lines_Return_Correct_Number()
        {
            var baskets  = new InMemoryDataStore <Basket>();
            var products = new InMemoryDataStore <Product>();

            products.Seed();
            baskets.Seed(products);

            var bc = new BasketsController(products, baskets, _mapper);

            var actionResult = bc.GetBasketOrderLines(_testBasketId);

            OkObjectResult okresult = actionResult as OkObjectResult;

            Assert.IsNotNull(okresult);

            List <OrderLineDto> resultOrderLines = okresult.Value as List <OrderLineDto>;

            Assert.IsNotNull(resultOrderLines);

            Assert.AreEqual(resultOrderLines.Count, 2);
        }
Beispiel #25
0
        public void BasketsController_Get_Basket_Order_Line_Return_Correct_Order_Line()
        {
            var baskets  = new InMemoryDataStore <Basket>();
            var products = new InMemoryDataStore <Product>();

            products.Seed();
            baskets.Seed(products);

            var bc = new BasketsController(products, baskets, _mapper);

            var actionResult = bc.GetBasketOrderLine(_testBasketId, _testProductId);

            OkObjectResult okresult = actionResult as OkObjectResult;

            Assert.IsNotNull(okresult);

            OrderLineDto resultOrderLine = okresult.Value as OrderLineDto;

            Assert.IsNotNull(resultOrderLine);

            Assert.AreEqual(resultOrderLine.ProductId, _testProductId);
        }
Beispiel #26
0
        public void BasketsController_Clear_Basket_Check_Basket_Clear()
        {
            var baskets  = new InMemoryDataStore <Basket>();
            var products = new InMemoryDataStore <Product>();

            products.Seed();
            baskets.Seed(products);

            var bc = new BasketsController(products, baskets, _mapper);

            var actionResult = bc.PutClearBasket(_testBasketId);

            OkObjectResult okresult = actionResult as OkObjectResult;

            Assert.IsNotNull(okresult);

            BasketDto basket = okresult.Value as BasketDto;

            Assert.IsNotNull(basket);

            Assert.IsTrue(basket.NumberOfOrderLines == 0);
        }
Beispiel #27
0
        public void BasketsController_Get_All_Baskets_Return_Correct_Number()
        {
            var baskets  = new InMemoryDataStore <Basket>();
            var products = new InMemoryDataStore <Product>();

            products.Seed();
            baskets.Seed(products);

            var bc = new BasketsController(products, baskets, _mapper);

            var actionResult = bc.GetAllBaskets();

            OkObjectResult okresult = actionResult as OkObjectResult;

            Assert.IsNotNull(okresult);

            List <BasketDto> resultBaskets = okresult.Value as List <BasketDto>;

            Assert.IsNotNull(resultBaskets);

            Assert.AreEqual(resultBaskets.Count, baskets.Count());
        }
Beispiel #28
0
        public void BasketsController_Create_New_Basket_Return_Created_Basket()
        {
            var baskets  = new InMemoryDataStore <Basket>();
            var products = new InMemoryDataStore <Product>();

            products.Seed();
            baskets.Seed(products);

            var bc = new BasketsController(products, baskets, _mapper);

            var actionResult = bc.PostCreateBasket(new BasketCreateDto()
            {
                OwnerId = Guid.NewGuid()
            });

            CreatedAtRouteResult createdresult = actionResult as CreatedAtRouteResult;

            Assert.IsNotNull(createdresult);

            BasketDto resultBasket = createdresult.Value as BasketDto;

            Assert.IsNotNull(resultBasket);
        }
Beispiel #29
0
        public async Task AddProductToBasketShouldReturn204NoContent()
        {
            var basketServiceMock = new Mock <IBasketService>(MockBehavior.Strict);

            basketServiceMock.Setup(service => service.AddProductToBasket(null, 0)).Returns(Task.CompletedTask);
            basketServiceMock.Setup(service => service.Exists(null)).ReturnsAsync(true);

            var productServiceMock = new Mock <IProductService>(MockBehavior.Strict);

            productServiceMock.Setup(service => service.Exists(0)).ReturnsAsync(true);

            var controller = new BasketsController(basketServiceMock.Object, productServiceMock.Object);

            var result = await controller.AddProductToBasket(null, 0);

            Assert.IsType <NoContentResult>(result);

            productServiceMock.Verify(service => service.Exists(0), Times.Once);
            productServiceMock.VerifyAll();

            basketServiceMock.Verify(service => service.AddProductToBasket(null, 0), Times.Once);
            basketServiceMock.Verify(service => service.Exists(null), Times.Once);
            basketServiceMock.VerifyAll();
        }
Beispiel #30
0
 public BasketsControllerTests()
 {
     this.basketsRepositoryMock = new Mock <IBasketsRepository>();
     this.sut = new BasketsController(basketsRepositoryMock.Object);
 }