Exemple #1
0
        public void UpdateAsync_NotValidInputData_ReturnsSuccess(string id, string code, string name, double price, string photo)
        {
            var product = new ProductInputModel
            {
                Code  = code,
                Name  = name,
                Price = price,
                Photo = photo
            };

            var productEntity = GetProductEntity(id, GetExistProduct());

            var repositoryMock = new Mock <IProductRepository>();

            repositoryMock.Setup(repository => repository.GetAsync(id)).ReturnsAsync(productEntity);
            repositoryMock.Setup(repository => repository.CheckIfProductCodeUniqueAsync(id, product.Code)).ReturnsAsync(true);

            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            repositoryFactoryMock.Setup(factory => factory.Get()).Returns(repositoryMock.Object);

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);

            Assert.ThrowsAsync <ArgumentException>(async() => await handler.UpdateAsync(id, product));
        }
Exemple #2
0
        public void CreateAsync_NullInputModel_ThrowsArgumentNullException()
        {
            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);

            Assert.ThrowsAsync <ArgumentNullException>(async() => await handler.CreateAsync(null));
        }
Exemple #3
0
        public void DeleteAsync_NotValidId_ThrowsArgumentNullException(string id)
        {
            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            repositoryFactoryMock.Setup(factory => factory.Get()).Returns(GetRepository());

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);

            Assert.ThrowsAsync <ArgumentException>(async() => await handler.DeleteAsync(id));
        }
Exemple #4
0
        public void UpdateAsync_NotValidId_ThrowsArgumentNullException(string id)
        {
            var product = GetExistProduct();

            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);

            Assert.ThrowsAsync <ArgumentException>(async() => await handler.UpdateAsync(id, product));
        }
Exemple #5
0
        public async Task DeleteAsync_ValidId_ReturnsRepositoryDeleteResult(string id, bool repositoryResult)
        {
            var repositoryMock = new Mock <IProductRepository>();

            repositoryMock.Setup(repository => repository.DeleteAsync(id)).ReturnsAsync(repositoryResult);
            repositoryMock.Setup(repository => repository.UnitOfWork).Returns(GetUnitOfWork());

            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            repositoryFactoryMock.Setup(factory => factory.Get()).Returns(repositoryMock.Object);

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);
            var result  = await handler.DeleteAsync(id);

            Assert.AreEqual(repositoryResult, result);
        }
Exemple #6
0
        public async Task CreateAsync_DuplicatedCode_ReturnsFalse()
        {
            var product = GetNewProduct();

            var repositoryMock = new Mock <IProductRepository>();

            repositoryMock.Setup(repository => repository.CheckIfProductCodeUniqueAsync(product.Code)).ReturnsAsync(false);

            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            repositoryFactoryMock.Setup(factory => factory.Get()).Returns(repositoryMock.Object);

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);
            var result  = await handler.CreateAsync(product);

            Assert.IsFalse(result);
        }
Exemple #7
0
        public void DeleteAsync_OnRepositoryException_ThrowSameException(string id)
        {
            var exception      = new Exception();
            var repositoryMock = new Mock <IProductRepository>();

            repositoryMock.Setup(repository => repository.DeleteAsync(id)).ThrowsAsync(exception);

            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            repositoryFactoryMock.Setup(factory => factory.Get()).Returns(repositoryMock.Object);

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);

            var result = Assert.ThrowsAsync(exception.GetType(), async() => await handler.DeleteAsync(id));

            Assert.AreSame(exception, result);
        }
Exemple #8
0
        public async Task UpdateAsync_ProductNotExists_ReturnsProductNotExists(string id)
        {
            var product = GetExistProduct();

            var repositoryMock = new Mock <IProductRepository>();

            repositoryMock.Setup(repository => repository.GetAsync(id)).ReturnsAsync((Product)null);

            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            repositoryFactoryMock.Setup(factory => factory.Get()).Returns(repositoryMock.Object);

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);

            var result = await handler.UpdateAsync(id, product);

            Assert.AreEqual(EUpdateProductCommandStatus.ProductNotExists, result);
        }
Exemple #9
0
        public async Task CreateAsync_ValidInputData_ReturnsTrue()
        {
            var product = GetNewProduct();

            var repositoryMock = new Mock <IProductRepository>();

            repositoryMock.Setup(repository => repository.CheckIfProductCodeUniqueAsync(product.Code)).ReturnsAsync(true);
            repositoryMock.Setup(repository => repository.UnitOfWork).Returns(GetUnitOfWork());

            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            repositoryFactoryMock.Setup(factory => factory.Get()).Returns(repositoryMock.Object);

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);
            var result  = await handler.CreateAsync(product);

            Assert.IsTrue(result);
        }
Exemple #10
0
        public async Task UpdateAsync_DuplicatedCode_ReturnsFailsBecauseDuplicatedCode(string id)
        {
            var product       = GetExistProduct();
            var productEntity = GetProductEntity(id, product);

            var repositoryMock = new Mock <IProductRepository>();

            repositoryMock.Setup(repository => repository.GetAsync(id)).ReturnsAsync(productEntity);
            repositoryMock.Setup(repository => repository.CheckIfProductCodeUniqueAsync(id, product.Code)).ReturnsAsync(false);

            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            repositoryFactoryMock.Setup(factory => factory.Get()).Returns(repositoryMock.Object);

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);

            var result = await handler.UpdateAsync(id, product);

            Assert.AreEqual(EUpdateProductCommandStatus.FailsBecauseDuplicatedCode, result);
        }
Exemple #11
0
        public void CreateAsync_OnRepositoryException_ThrowSameException()
        {
            var product   = GetNewProduct();
            var exception = new Exception();

            var repositoryMock = new Mock <IProductRepository>();

            repositoryMock.Setup(repository => repository.CheckIfProductCodeUniqueAsync(product.Code)).ReturnsAsync(true);
            repositoryMock.Setup(repository => repository.Add(It.IsAny <Product>())).Throws(exception);

            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            repositoryFactoryMock.Setup(factory => factory.Get()).Returns(repositoryMock.Object);

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);

            var result = Assert.ThrowsAsync(exception.GetType(), async() => await handler.CreateAsync(product));

            Assert.AreSame(exception, result);
        }
Exemple #12
0
        public async Task UpdateAsync_ValidInputData_ReturnsSuccess(string id)
        {
            var product       = GetExistProduct();
            var productEntity = GetProductEntity(id, product);

            var repositoryMock = new Mock <IProductRepository>();

            repositoryMock.Setup(repository => repository.CheckIfProductCodeUniqueAsync(id, product.Code)).ReturnsAsync(true);
            repositoryMock.Setup(repository => repository.GetAsync(id)).ReturnsAsync(productEntity);
            repositoryMock.Setup(repository => repository.UnitOfWork).Returns(GetUnitOfWork());

            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            repositoryFactoryMock.Setup(factory => factory.Get()).Returns(repositoryMock.Object);

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);

            var result = await handler.UpdateAsync(id, product);

            Assert.AreEqual(EUpdateProductCommandStatus.Success, result);
        }
Exemple #13
0
        public void UpdateAsync_OnRepositoryException_ThrowSameException(string id)
        {
            var product       = GetExistProduct();
            var productEntity = GetProductEntity(id, product);

            var exception = new Exception();

            var repositoryMock = new Mock <IProductRepository>();

            repositoryMock.Setup(repository => repository.CheckIfProductCodeUniqueAsync(id, product.Code)).ReturnsAsync(true);
            repositoryMock.Setup(repository => repository.GetAsync(id)).ReturnsAsync(productEntity);
            repositoryMock.Setup(repository => repository.UpdateAsync(productEntity)).Throws(exception);

            var repositoryFactoryMock = new Mock <IProductRepositoryFactory>();

            repositoryFactoryMock.Setup(factory => factory.Get()).Returns(repositoryMock.Object);

            var handler = new ProductCommandsHandler(repositoryFactoryMock.Object);

            var result = Assert.ThrowsAsync(exception.GetType(), async() => await handler.UpdateAsync(id, product));

            Assert.AreSame(exception, result);
        }
Exemple #14
0
 public ProductsController(IProductRepository productRepository, ProductCommandsHandler productCommandsHandler)
 {
     _productRepository      = productRepository;
     _productCommandsHandler = productCommandsHandler;
 }