Exemple #1
0
 public async Task<IActionResult> Add(long id, [FromBody] CartItems items)
 {
     var cartItems = new cart.grain.CartItems { Items = items.Items };
     var grain = _grainFactory.GetGrain<ICart>(id);
     var (status, newCartItems) = await grain.Add(cartItems);
     return BuildResponse(status, newCartItems);
 }
        public async Task Add_BadRequest_InvalidQuantity()
        {
            var repo = new Moq.MockRepository(Moq.MockBehavior.Strict);

            var cartId = 42;

            var items = new System.Collections.Generic.Dictionary <string, int>()
            {
                { "article1", 0 }
            };
            var queryItems = new webapi.Controllers.CartItems {
                Items = items
            };
            var cartItems = new cart.grain.CartItems {
                Items = items
            };

            // initialize grain
            var cartGrain = repo.Create <ICart>();

            cartGrain.Setup(x => x.Add(It.IsAny <cart.grain.CartItems>())).Returns(Task.FromResult((CartItemsStatusCode.InvalidQuantity, cartItems)));

            // initialize grain factory
            var grainFactory = repo.Create <IGrainFactory>();

            grainFactory.Setup(x => x.GetGrain <ICart>(cartId)).Returns(cartGrain.Object);


            var controller = new webapi.Controllers.CartController(grainFactory.Object);
            var response   = await controller.Add(cartId, queryItems) as BadRequestObjectResult;

            Assert.AreEqual(400, response.StatusCode);

            repo.VerifyAll();
        }
Exemple #3
0
        private IActionResult BuildResponse(CartItemsStatusCode status, cart.grain.CartItems newCartItems)
        {
            if (status == CartItemsStatusCode.Ok)
            {
                var newCart = new CartItems() { Items = newCartItems.Items };
                return Ok(newCart);
            }

            var map = new Dictionary<CartItemsStatusCode, CartError>
            {
                { CartItemsStatusCode.InvalidArguments, new CartError { ErrorCode = CartErrorCode.BadRequest, Reason = "Invalid request" } },
                { CartItemsStatusCode.InvalidId, new CartError { ErrorCode = CartErrorCode.InvalidItemId, Reason = "Invalid id" } },
                { CartItemsStatusCode.InvalidQuantity, new CartError { ErrorCode = CartErrorCode.InvalidQuantity, Reason = "Invalid quantity" } },
            };

            if (map.TryGetValue(status, out var res))
                return BadRequest(res);

            throw new ArgumentException($"Unknown error code {status}");
        }
        public async Task Remove_Ok()
        {
            var repo = new Moq.MockRepository(Moq.MockBehavior.Strict);

            var cartId = 42;

            var items = new System.Collections.Generic.Dictionary <string, int>()
            {
                { "article1", 3 }
            };
            var queryItems = new webapi.Controllers.CartItems {
                Items = items
            };
            var cartItems = new cart.grain.CartItems {
                Items = items
            };
            var emptyCartItems = new cart.grain.CartItems();

            // initialize grain
            var cartGrain = repo.Create <ICart>();

            cartGrain.Setup(x => x.Remove(It.Is <cart.grain.CartItems>(o => o.Items["article1"] == 3))).Returns(Task.FromResult((CartItemsStatusCode.Ok, emptyCartItems)));

            // initialize grain factory
            var grainFactory = repo.Create <IGrainFactory>();

            grainFactory.Setup(x => x.GetGrain <ICart>(cartId)).Returns(cartGrain.Object);


            var controller = new webapi.Controllers.CartController(grainFactory.Object);
            var response   = await controller.Remove(cartId, queryItems) as OkObjectResult;

            Assert.AreEqual(200, response.StatusCode);
            var result = (webapi.Controllers.CartItems)response.Value;

            Assert.AreEqual(0, result.Items.Count);

            repo.VerifyAll();
        }