public void Add_New_Content_In_Cart()
        {
            var notifyDto = new NotificationDto();

            // Setup Mock Product
            mockNotify.Setup(item => item.AddToCartNotify(It.IsAny <AddToCartNotifyDto>()))
            .Returns(() => notifyDto);

            // Create object Product
            var product = new Product()
            {
                Id = 1
            };

            // Setup Mock Product
            mockProduct.Setup(item => item.Get(It.IsAny <long>()))
            .Returns(() => product);

            // Create object ContentCart
            var objContentCart = new ContentCart()
            {
                Id = 5
            };
            var actual1 = objContentCart.Id;

            // Create List for search by content.Id
            List <ContentCart> contentCartList = new List <ContentCart>();

            // Setup mock object
            mock.Setup(item => item.Find(It.IsAny <Expression <Func <ContentCart, bool> > >()))
            .Returns(() => contentCartList);
            mock.Setup(repo => repo.Add(It.IsAny <ContentCart>()))
            .Returns(() => objContentCart).Callback(() => objContentCart.Id++);

            // Create CartService object with mock.Object and mockProduct.Object
            var service = new CartService(mock.Object, mockProduct.Object, mockNotify.Object);

            // Write rezalt method AddNewContentInCart in actual3
            var actual3 = service.AddInCart(objContentCart.Id, 1);

            // Verification rezalt with neсуssary number
            Assert.AreEqual((long)5, actual1);
            //Assert.AreEqual((long)5, actual2);
            Assert.AreEqual((long)6, actual3.Id);
        }
        public void Add_New_Content_In_Cart_If_Not_Save_In_Repository()
        {
            var notifyDto = new NotificationDto();

            // Setup Mock Product
            mockNotify.Setup(item => item.AddToCartNotify(It.IsAny <AddToCartNotifyDto>()))
            .Returns(() => notifyDto);

            // Create object Product
            var product = new Product()
            {
                Id = 1
            };

            // Setup Mock Product
            mockProduct.Setup(item => item.Get(It.IsAny <long>()))
            .Returns(() => product);

            // Create object ContentCart
            var objContentCart = new ContentCart()
            {
                Id = 5
            };
            var actual1 = objContentCart.Id;

            // Create List for search by content.Id
            List <ContentCart> contentCartList = new List <ContentCart>();

            // Setup mock object
            mock.Setup(item => item.Find(It.IsAny <Expression <Func <ContentCart, bool> > >()))
            .Returns(() => contentCartList);
            mock.Setup(repo => repo.Add(It.IsAny <ContentCart>()))
            .Returns(() => null);

            // Create CartService object with mock.Object and mockProduct.Object
            var service = new CartService(mock.Object, mockProduct.Object, mockNotify.Object);

            // Write rezalt method AddNewContentInCart in actual3
            Assert.Throws <AddContentInCartExceptions>(() => service.AddInCart(objContentCart.Id, 1));
        }
Пример #3
0
        /// <summary>
        /// Add new item in cart with return save item for update view
        /// </summary>
        /// <param name="contentId">contents identifier</param>
        /// <param name="userId">users identifier</param>
        /// <returns>this save item</returns>
        public ContentCartDto AddInCart(long contentId, long userId)
        {
            // Verify long contentId
            if (contentId <= 0)
            {
                throw new ArgumentException(Resources.InvalidContentId);
            }

            // Check exist Product in repository
            if (this.repositoryProduct.Get(contentId) == null)
            {
                throw new ExistContentInCartExceptions(Resources.ExistProductInDataBase);
            }

            // Check exist Content in Cart with state = InBougth for current user
            if (this.ExistInCart(contentId, userId, CartEnums.StateCartContent.InPaid))
            {
                throw new ExistContentInCartExceptions(Resources.ContentAlreadyBougth);
            }

            // Check exist Content in Cart with state = InCart for current user
            if (this.ExistInCart(contentId, userId, CartEnums.StateCartContent.InCart))
            {
                throw new ExistContentInCartExceptions(Resources.ExistContentInCart);
            }

            var contentCart = new ContentCart()
            {
                CreatorId = userId, ProductId = contentId
            };

            // Save object ContentCart in repository
            var addContentCart = this.repositoryContentCart.Add(contentCart);

            // If the object is not added to the database
            // return null
            if (addContentCart == null)
            {
                throw new AddContentInCartExceptions(Resources.ResourceManager.GetString("AddContentInCart"));
            }

            // Get information about Product by Id
            var product = this.repositoryProduct.Get(contentId);

            // Create object AddToCartNotifyDto
            var objectNotify = new AddToCartNotifyDto()
            {
                ReceiverId = userId, ProductName = product.ProductName
            };

            var notification = this.serviceNotification.AddToCartNotify(objectNotify);

            // Create ContentCartDto
            var contentCartDto = Mapper.Map <ContentCartDto>(product);

            contentCartDto.Id        = addContentCart.Id;
            contentCartDto.CreatorId = userId;

            // Output mapping object ContentCart to object ContentCartDto
            return(contentCartDto);
        }