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)); }
public void CreateAsync_NullInputModel_ThrowsArgumentNullException() { var repositoryFactoryMock = new Mock <IProductRepositoryFactory>(); var handler = new ProductCommandsHandler(repositoryFactoryMock.Object); Assert.ThrowsAsync <ArgumentNullException>(async() => await handler.CreateAsync(null)); }
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)); }
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)); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
public ProductsController(IProductRepository productRepository, ProductCommandsHandler productCommandsHandler) { _productRepository = productRepository; _productCommandsHandler = productCommandsHandler; }