예제 #1
0
        public async Task AddProduct_ReturnsProductCRUDResultModelWithIsSuccessfulFalse_On_ProductException()
        {
            // Arrange
            var expectedException = new ProductException("msg");

            var userManager = new Mock <IUserManager>();

            userManager.Setup(x => x.GetByUserId(It.IsAny <int>()))
            .ReturnsAsync(new User());

            var prodManager = new Mock <IProductManager>();

            prodManager.Setup(x => x.AddNewProduct(It.IsAny <Product>(), It.IsAny <RoleType>()))
            .ThrowsAsync(expectedException);

            var productService = new ProductServiceBuilder()
                                 .WithUserManager(userManager.Object)
                                 .WithProductManager(prodManager.Object)
                                 .Build();

            // Assert
            var result = await productService.AddProduct(new AddProductModel(), It.IsAny <int>());

            // Act
            Assert.False(result.IsSuccessful);
            Assert.Contains(expectedException.Message, result.Message);
        }
예제 #2
0
        public async Task GetProduct_ReturnsIsSuccessfulEqualTrueAndModel_If_NoExceptions()
        {
            // Arrange
            var expectedProduct = new Product
            {
                Name      = "title",
                Details   = "details",
                Price     = 11,
                ThumbNail = new Image
                {
                    FullSizePath = "image",
                }
            };

            var prodManager = new Mock <IProductManager>();

            prodManager.Setup(x => x.GetProduct(It.IsAny <int>(), It.IsAny <int>()))
            .ReturnsAsync(expectedProduct);

            var productService = new ProductServiceBuilder()
                                 .WithProductManager(prodManager.Object)
                                 .Build();

            // Act
            var result = await productService.GetProduct(1, 1);

            // Assert
            Assert.True(result.IsSuccessful);
            Assert.Equal(result.ProductDetails.Titile, expectedProduct.Name);
            Assert.Equal(result.ProductDetails.Description, expectedProduct.Details);
            Assert.Equal(result.ProductDetails.Price, expectedProduct.Price);
            Assert.Contains(expectedProduct.ThumbNail.FullSizePath, result.ProductDetails.Image.FullSizePath);
        }
예제 #3
0
        public async Task AddProduct_CallsAddNewProductOnce()
        {
            // Arrange
            var fileManager = new Mock <IFileManager>();

            fileManager.Setup(x => x.ValidateFile(It.IsAny <IFormFile>()))
            .Returns(It.IsAny <string>());

            var userManager = new Mock <IUserManager>();

            userManager.Setup(x => x.GetByUserId(It.IsAny <int>()))
            .ReturnsAsync(new User());

            var prodManager = new Mock <IProductManager>();

            var productService = new ProductServiceBuilder()
                                 .WithFileManager(fileManager.Object)
                                 .WithUserManager(userManager.Object)
                                 .WithProductManager(prodManager.Object)
                                 .Build();

            // Assert
            await productService.AddProduct(new AddProductModel(), It.IsAny <int>());

            // Act
            prodManager.Verify(x => x.AddNewProduct(It.IsAny <Product>(), It.IsAny <RoleType>()), Times.Once);
        }
예제 #4
0
        public async Task DeleteProduct_CallsFileManagerRemoveFilesOnce()
        {
            // Arrange
            var product = new Product
            {
                ThumbNail = new Mock <Image>().Object
            };

            var prodManager = new Mock <IProductManager>();

            prodManager.Setup(x => x.GetProductById(It.IsAny <int>()))
            .ReturnsAsync(product);

            var fileManager = new Mock <IFileManager>();

            var productService = new ProductServiceBuilder()
                                 .WithProductManager(prodManager.Object)
                                 .WithFileManager(fileManager.Object)
                                 .Build();

            // Act
            var result = await productService.DeleteProduct(1);

            // Assert
            fileManager.Verify(x => x.RemoveFiles(It.IsAny <List <string> >()), Times.Once);
        }
예제 #5
0
        public async Task AddProduct_CallsValidateFileAndUploadFileTwoTimes()
        {
            // Arrange
            var fileManager = new Mock <IFileManager>();

            fileManager.Setup(x => x.ValidateFile(It.IsAny <IFormFile>()))
            .Returns(It.IsAny <string>());

            var userManager = new Mock <IUserManager>();

            userManager.Setup(x => x.GetByUserId(It.IsAny <int>()))
            .ReturnsAsync(new User());

            var productService = new ProductServiceBuilder()
                                 .WithFileManager(fileManager.Object)
                                 .WithUserManager(userManager.Object)
                                 .Build();

            // Assert
            await productService.AddProduct(new AddProductModel(), It.IsAny <int>());

            // Act
            fileManager.Verify(x => x.ValidateFile(It.IsAny <IFormFile>()), Times.Exactly(2));
            fileManager.Verify(x => x.UploadFile(It.IsAny <IFormFile>(), It.IsAny <string>()), Times.Exactly(2));
        }
예제 #6
0
        public async Task AddProduct_ReturnsProductCRUDResultModelWithIsSuccessfulFalse_On_InvalidUserException()
        {
            // Arrange
            var expectedException = new InvalidUserException("msg");

            var fileManager = new Mock <IFileManager>();

            fileManager.Setup(x => x.ValidateFile(It.IsAny <IFormFile>()))
            .Returns(It.IsAny <string>());

            var userManager = new Mock <IUserManager>();

            userManager.Setup(x => x.GetByUserId(It.IsAny <int>()))
            .ThrowsAsync(expectedException);

            var productService = new ProductServiceBuilder()
                                 .WithFileManager(fileManager.Object)
                                 .WithUserManager(userManager.Object)
                                 .Build();

            // Assert
            var result = await productService.AddProduct(new AddProductModel(), It.IsAny <int>());

            // Act
            Assert.False(result.IsSuccessful);
            Assert.Contains(expectedException.Message, result.Message);
        }
예제 #7
0
        public void GetManagerProducts_ReturnsManagerProductsModel_WithOneFilledProduct()
        {
            // Arrange
            var expectedProduct = new Product()
            {
                Id        = 1,
                Name      = "title",
                Price     = 13,
                ThumbNail = new Image
                {
                    ThumbNailPath = "path"
                }
            };

            var productList = new List <Product>()
            {
                expectedProduct
            };

            var prodManager = new Mock <IProductManager>();

            prodManager.Setup(x => x.GetProductsByManagerId(It.IsAny <int>()))
            .Returns(productList);

            // GetProductsByManagerId
            var productService = new ProductServiceBuilder()
                                 .WithProductManager(prodManager.Object)
                                 .Build();

            // Act
            var result = productService.GetManagerProducts(1);

            // Assert
            var returnedProd = result.ShowcaseProducts.First();

            Assert.Equal(expectedProduct.Id, returnedProd.Id);
            Assert.Equal(expectedProduct.Name, returnedProd.Title);
            Assert.Equal(expectedProduct.Price, returnedProd.Price);
            Assert.Contains(expectedProduct.ThumbNail.ThumbNailPath, returnedProd.ThumbNailPath);

            Assert.Equal(-1, result.DeleteProductId);
        }
예제 #8
0
        public async Task GetEditProductModel_ReturnsIsSuccessfulEqualFalse_If_CatchedPermissionException()
        {
            var expectedException = new PermissionException("msg");

            var prodManager = new Mock <IProductManager>();

            prodManager.Setup(x => x.GetProduct(It.IsAny <int>(), It.IsAny <int>()))
            .ThrowsAsync(expectedException);

            var productService = new ProductServiceBuilder()
                                 .WithProductManager(prodManager.Object)
                                 .Build();

            // Act
            var result = await productService.GetEditProductModel(1, 1);

            // Assert
            Assert.False(result.IsSuccessful);
            Assert.Contains(expectedException.Message, result.Message);
        }
예제 #9
0
        public async Task DeleteProduct_ReturnsProductCRUDResultModelIsSuccessfulFalse_On_ProductException()
        {
            // Arrange
            var expectedException = new ProductException("msg");

            var prodManager = new Mock <IProductManager>();

            prodManager.Setup(x => x.GetProductById(It.IsAny <int>()))
            .ThrowsAsync(expectedException);

            var productService = new ProductServiceBuilder()
                                 .WithProductManager(prodManager.Object)
                                 .Build();

            // Act
            var result = await productService.DeleteProduct(1);

            // Assert
            Assert.False(result.IsSuccessful);
            Assert.Contains(expectedException.Message, result.Message);
        }
예제 #10
0
        public async Task GetEditProductModel_ReturnsModel_FilledWithData()
        {
            // Arrange
            var expectedProduct = new Product()
            {
                Name      = "title",
                Price     = 13,
                ThumbNail = new Image
                {
                    Id            = 7,
                    ThumbNailPath = "path",
                    FullSizePath  = "path"
                }
            };

            var prodManager = new Mock <IProductManager>();

            prodManager.Setup(x => x.GetProduct(It.IsAny <int>(), It.IsAny <int>()))
            .ReturnsAsync(expectedProduct);

            var productService = new ProductServiceBuilder()
                                 .WithProductManager(prodManager.Object)
                                 .Build();

            // Act
            var result = await productService.GetEditProductModel(1, 1);

            // Assert
            var editModel = result.EditProductDetails;

            Assert.True(result.IsSuccessful);

            Assert.Equal(expectedProduct.Name, editModel.Title);
            Assert.Equal(expectedProduct.Details, editModel.Description);
            Assert.Equal(expectedProduct.Price, editModel.Price);

            Assert.Equal(expectedProduct.ThumbNail.Id, editModel.CurrentImage.Id);
            Assert.Contains(expectedProduct.ThumbNail.ThumbNailPath, editModel.CurrentImage.ThumbNailPath);
            Assert.Contains(expectedProduct.ThumbNail.FullSizePath, editModel.CurrentImage.FullSizePath);
        }
예제 #11
0
        public async Task DeleteProduct_ReturnsProductCRUDResultModelIsSuccessfulTrue_IfNoExceptionsCatched()
        {
            // Arrange
            var product = new Product
            {
                ThumbNail = new Mock <Image>().Object
            };

            var prodManager = new Mock <IProductManager>();

            prodManager.Setup(x => x.GetProductById(It.IsAny <int>()))
            .ReturnsAsync(product);

            var productService = new ProductServiceBuilder()
                                 .WithProductManager(prodManager.Object)
                                 .Build();

            // Act
            var result = await productService.DeleteProduct(1);

            // Assert
            Assert.True(result.IsSuccessful);
        }
예제 #12
0
        public async Task AddProduct_ReturnsProductCRUDResultModelWithIsSuccessfulTrue_If_NoExceptionsCatched()
        {
            // Arrange
            var fileManager = new Mock <IFileManager>();

            fileManager.Setup(x => x.ValidateFile(It.IsAny <IFormFile>()))
            .Returns(It.IsAny <string>());

            var userManager = new Mock <IUserManager>();

            userManager.Setup(x => x.GetByUserId(It.IsAny <int>()))
            .ReturnsAsync(new User());

            var productService = new ProductServiceBuilder()
                                 .WithFileManager(fileManager.Object)
                                 .WithUserManager(userManager.Object)
                                 .Build();

            // Assert
            var result = await productService.AddProduct(new AddProductModel(), It.IsAny <int>());

            // Act
            Assert.True(result.IsSuccessful);
        }