public void UpdateProductTest() { // Arrange Product product = new Product(); product.ProductId = 1; product.SubCategoryId = 1025; product.ProductName = "dyd"; product.ModifiedOn = Convert.ToDateTime("12/13/2018"); product.ModifiedBy = 4; product.Price = 100; product.ProductDescription = "sf"; product.IsActive = true; product.Quantity = 5; ProductApiController controller = new ProductApiController(); controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); // Act var response = controller.UpdateProduct(product); // Assert Assert.IsNotNull(response); Assert.IsNotNull(response.Content); Assert.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode); }
public void SaveProducts() { var mockRepository = new Mock <ProductRepository>(); mockRepository.Setup(mock => mock.SaveAsync(It.IsAny <List <Product> >())) .Returns(Task.CompletedTask); List <ProductDto> dtoList = new List <ProductDto>(); dtoList.Add(new ProductDto { Name = "test", Quantity = 1, SalesAmount = 2 }); dtoList.Add(new ProductDto { Name = "test2", Quantity = 2, SalesAmount = 20 }); OkNegotiatedContentResult <RestResponse> response = null; Task.Run(() => { var controller = new ProductApiController(mockRepository.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; response = controller.Save(dtoList).GetAwaiter().GetResult() as OkNegotiatedContentResult <RestResponse>; } ).GetAwaiter().GetResult(); mockRepository.Verify(mock => mock.SaveAsync(It.IsAny <List <Product> >()), Times.Once()); Assert.IsNotNull(response); Assert.IsFalse(String.IsNullOrWhiteSpace(response.Content.Id)); Assert.IsTrue(response.Content.Timestamp > 0); }
public void DeleteProductCallsRepositoryRemove() { //// Arrange Guid removedKey = Guid.Empty; Guid productKey = new Guid(); ProductActual productActual = CreateFakeProduct(productKey, 20.0M); var MockProductService = new Mock <IProductService>(); MockProductService.Setup(cs => cs.Delete(productActual, It.IsAny <bool>())).Callback <IProductActual, bool>((p, b) => removedKey = p.Key); MockProductService.Setup(cs => cs.GetByKey(productKey)).Returns(productActual); MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object); ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext); ctrl.Request = new HttpRequestMessage(); ctrl.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); //// Act HttpResponseMessage response = ctrl.Delete(productKey); //// Assert Assert.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode); Assert.AreEqual(productKey, removedKey); }
public void PutProductUpdatesRepository() { //// Arrange bool wasCalled = false; Guid productKey = Guid.NewGuid(); ProductActual productActual = CreateFakeProduct(productKey, 20.0M); var MockProductService = new Mock <IProductService>(); MockProductService.Setup(cs => cs.Save(productActual, It.IsAny <bool>())).Callback(() => wasCalled = true); MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object); ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext); ctrl.Request = new HttpRequestMessage(); ctrl.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); //// Act HttpResponseMessage response = ctrl.PutProduct(productActual); //// Assert Assert.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode); Assert.True(wasCalled); }
public void ProductsList_ReturnsProductsItemByCategory_NotFound() { // arrange var mock = new Mock <EFProductRepository>(); var productCategoryMock = new Mock <ProductCategory>(); var products = new Product[] { new Product { ProductId = 1, Name = "N1", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 2, Name = "N2", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 3, Name = "N3", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 4, Name = "N4", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 5, Name = "N5", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 } }; mock.Setup(repo => repo.GetProducts()).Returns(products.AsQueryable <Product>()); var controller = new ProductApiController(mock.Object); // act var result = controller.ProductsList(123); // assert Assert.IsType <NotFoundResult>(result.Result); Assert.Null(result.Value); }
public void GetAllProductIds() { var mockRepository = new Mock <ProductRepository>(); var listIds = new List <long>(); listIds.Add(1); listIds.Add(2); mockRepository.Setup(mock => mock.GetAllIds()).Returns(listIds); OkNegotiatedContentResult <AllProductsRestResponse> response = null; var controller = new ProductApiController(mockRepository.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; controller.Request.RequestUri = new Uri("http://localhost/fake-api"); response = controller.GetProducts() as OkNegotiatedContentResult <AllProductsRestResponse>; mockRepository.Verify(mock => mock.GetAllIds(), Times.Once()); Assert.AreEqual(2, response.Content.Products.Count); var firstProduct = response.Content.Products.Find(a => a.Id == 1); var secondProduct = response.Content.Products.Find(a => a.Id == 2); Assert.IsNotNull(firstProduct); Assert.IsNotNull(secondProduct); Assert.IsTrue(firstProduct.DetailsUrl.Equals(controller.Request.RequestUri + "/1", StringComparison.OrdinalIgnoreCase)); Assert.IsTrue(secondProduct.DetailsUrl.Equals(controller.Request.RequestUri + "/2", StringComparison.OrdinalIgnoreCase)); }
public void GetProductByKeysReturnsCorrectItemsFromRepository() { //// Arrange Guid productKey = Guid.NewGuid(); ProductActual productActual = CreateFakeProduct(productKey, 20.0M); Guid productKey2 = Guid.NewGuid(); ProductActual product2 = CreateFakeProduct(productKey2, 30.0M); Guid productKey3 = Guid.NewGuid(); ProductActual product3 = CreateFakeProduct(productKey3, 40.0M); List <ProductActual> productsList = new List <ProductActual>(); productsList.Add(productActual); productsList.Add(product3); var productKeys = new[] { productKey, productKey3 }; var MockProductService = new Mock <IProductService>(); MockProductService.Setup(cs => cs.GetByKeys(productKeys)).Returns(productsList); MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object); ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext); //// Act var result = ctrl.GetProducts(productKeys); //// Assert Assert.AreEqual(productsList, result); }
public void GetIndividualProduct() { var mockRepository = new Mock <ProductRepository>(); mockRepository.Setup(mock => mock.FindById(1)) .ReturnsAsync(new Product { Id = 1, Name = "test", Quantity = 1, SalesAmount = 2 }); OkNegotiatedContentResult <ProductDetailsResponse> response = null; Task.Run(() => { var controller = new ProductApiController(mockRepository.Object) { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; response = controller.Details(1).GetAwaiter().GetResult() as OkNegotiatedContentResult <ProductDetailsResponse>; } ).GetAwaiter().GetResult(); mockRepository.Verify(mock => mock.FindById(1), Times.Once()); Assert.IsNotNull(response); Assert.AreEqual(1, response.Content.Product.Id); Assert.AreEqual("test", response.Content.Product.Name); }
public void Init() { unitOfWork = new UnitOfWorkMock(); apiClient = new ApiClientMock(); cache = new CacheMock(); controller = new ProductApiController(unitOfWork, new MailHelper(new MimeMailerMock(), new RegistryReaderMock()), apiClient, new JwtDecoderMock(), cache); controller.Request = new HttpRequestMessage(); }
public void Products_ReturnsProductsList_Ok() { // arrange var mock = new Mock <EFProductRepository>(); var productCategoryMock = new Mock <ProductCategory>(); var productsDTO = new ProductApiDTO[] { new ProductApiDTO { ProductId = 1, Name = "N1", Description = "D1", Price = 10, ProductCategoryName = "C1", ProductCategoryId = 1 }, new ProductApiDTO { ProductId = 2, Name = "N2", Description = "D2", Price = 11, ProductCategoryName = "C1", ProductCategoryId = 1 }, new ProductApiDTO { ProductId = 3, Name = "N3", Description = "D3", Price = 12, ProductCategoryName = "C1", ProductCategoryId = 1 }, new ProductApiDTO { ProductId = 4, Name = "N4", Description = "D4", Price = 13, ProductCategoryName = "C1", ProductCategoryId = 1 }, new ProductApiDTO { ProductId = 5, Name = "N5", Description = "D5", Price = 14, ProductCategoryName = "C1", ProductCategoryId = 1 }, }; var products = new Product[] { new Product { ProductId = 1, Name = "N1", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 2, Name = "N2", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 3, Name = "N3", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 4, Name = "N4", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 5, Name = "N5", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 } }; mock.Setup(repo => repo.GetProducts()).Returns(products.AsQueryable <Product>()); productCategoryMock.Setup(repo => repo.Name).Returns("C1"); productCategoryMock.Setup(repo => repo.ProductCategoryId).Returns(1); var controller = new ProductApiController(mock.Object); // act var result = controller.Products(); // assert mock.Verify(); Assert.Equal(productsDTO[0].ProductId, result.Value[0].ProductId); Assert.Equal(productsDTO[0].Description, result.Value[0].Description); Assert.Equal(productsDTO[0].ProductCategoryName, result.Value[0].ProductCategoryName); }
public void Products_ReturnsProductsList_NotFound() { // arrange var mock = new Mock <EFProductRepository>(); var productCategoryMock = new Mock <ProductCategory>(); var controller = new ProductApiController(mock.Object); // act var result = controller.Products(); // assert Assert.IsType <NotFoundResult>(result.Result); }
public void ProductItem_ReturnsProductsItemById_Ok() { // arrange var mock = new Mock <EFProductRepository>(); var productCategoryMock = new Mock <ProductCategory>(); var products = new Product[] { new Product { ProductId = 1, Name = "N1", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 2, Name = "N2", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 2 }, new Product { ProductId = 3, Name = "N3", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 3 }, new Product { ProductId = 4, Name = "N4", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 4 }, new Product { ProductId = 5, Name = "N5", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 } }; var productDTO = new ProductApiDTO() { ProductId = 1, Name = "N1", Description = "D1", Price = 10, ProductCategoryName = "C1", ProductCategoryId = 1 }; mock.Setup(repo => repo.GetProducts()).Returns(products.AsQueryable <Product>()); productCategoryMock.Setup(repo => repo.Name).Returns("C1"); productCategoryMock.Setup(repo => repo.ProductCategoryId).Returns(1); var controller = new ProductApiController(mock.Object); // act var result = controller.ProductItem(1); // assert Assert.IsType <ActionResult <ProductApiDTO> >(result); Assert.Equal(productDTO.ProductCategoryId, result.Value.ProductCategoryId); Assert.Equal(productDTO.ProductCategoryName, result.Value.ProductCategoryName); Assert.Equal(productDTO.ProductId, result.Value.ProductId); }
public void GetProductListNoContentTest() { // Arrange ProductApiController controller = new ProductApiController(); controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); // Act var response = controller.GetProductList(); // Assert Assert.IsNotNull(response); Assert.AreEqual(System.Net.HttpStatusCode.NoContent, response.StatusCode); }
public ProductApiControllerTest() { _mockRepository = new Mock <IRepository <Product> >(); _productApiController = new ProductApiController(_mockRepository.Object); _helper = new Helper(); products = new List <Product>() { new Product { Id = 1, Name = "Kalem", Price = 100, Stock = 50, Color = "Kırmızı" }, new Product { Id = 2, Name = "Defter", Price = 200, Stock = 500, Color = "Mavi" } }; }
public void GetProductByIdFailureTest() { // Arrange int ProductId = 1; ProductApiController controller = new ProductApiController(); controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); // Act var response = controller.GetProductById(ProductId); // Assert Assert.IsNotNull(response); Assert.AreEqual(System.Net.HttpStatusCode.InternalServerError, response.StatusCode); }
public void GetProductThrowsWhenRepositoryReturnsNull() { //// Arrange Guid productKey = Guid.NewGuid(); var MockProductService = new Mock <IProductService>(); MockProductService.Setup(cs => cs.GetByKey(productKey)).Returns((ProductActual)null); MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object); ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext); //// Act & Assert var ex = Assert.Throws <HttpResponseException>(() => ctrl.GetProduct(Guid.Empty)); }
public void DeleteProductTest() { // Arrange Product product = new Product(); product.ProductId = 1; ProductApiController controller = new ProductApiController(); controller.Request = new HttpRequestMessage(); controller.Configuration = new HttpConfiguration(); // Act var response = controller.DeleteProduct(product); // Assert Assert.IsNotNull(response); Assert.IsNotNull(response.Content); Assert.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode); }
public void DeleteProduct_RemovesExistingProductFromDatabase_Ok() { // arrange var mock = new Mock <EFProductRepository>(); var productCategoryMock = new Mock <ProductCategory>(); var products = new Product[] { new Product { ProductId = 1, Name = "N1", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 2, Name = "N2", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 3, Name = "N3", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 3 }, new Product { ProductId = 4, Name = "N4", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 4 }, new Product { ProductId = 5, Name = "N5", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 } }; Product productToRemove = new Product { ProductId = 3, Name = "N3", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 3 }; mock.Setup(repo => repo.DeleteProduct(It.IsAny <int>())).Returns(productToRemove); productCategoryMock.Setup(repo => repo.Name).Returns("CG3"); productCategoryMock.Setup(repo => repo.ProductCategoryId).Returns(3); var controller = new ProductApiController(mock.Object); // act var result = controller.DeleteProduct(3); // assert Assert.Equal("N3", result.Value.Name); Assert.Equal(3, result.Value.ProductId); Assert.Equal("CG3", result.Value.ProductCategory.Name); Assert.Equal(3, result.Value.ProductCategory.ProductCategoryId); Assert.Equal(3, result.Value.ProductCategoryId); }
public void UpdateProduct_UpdatesExistingProductInDatabase_Ok() { // arrange var mock = new Mock <EFProductRepository>(); var productCategoryMock = new Mock <ProductCategory>(); var products = new Product[] { new Product { ProductId = 1, Name = "N1", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 2, Name = "N2", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 3, Name = "N3", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 4, Name = "N4", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 4 }, new Product { ProductId = 5, Name = "N5", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 } }; Product productToInsert = new Product { ProductId = 4, Name = "Updated name", Description = "Updated description", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 4 }; mock.Setup(repo => repo.SaveProduct(It.IsAny <Product>())).Returns(productToInsert); productCategoryMock.Setup(repo => repo.Name).Returns("Updated category name"); productCategoryMock.Setup(repo => repo.ProductCategoryId).Returns(4); var controller = new ProductApiController(mock.Object); // act var result = controller.UpdateProduct(productToInsert); // assert Assert.Equal("Updated name", result.Value.Name); Assert.Equal(4, result.Value.ProductId); Assert.Equal("Updated category name", result.Value.ProductCategory.Name); Assert.Equal(4, result.Value.ProductCategory.ProductCategoryId); Assert.Equal(4, result.Value.ProductCategoryId); }
public void GetProductByKeyReturnsCorrectItemFromRepository() { //// Arrange Guid productKey = Guid.NewGuid(); ProductActual productActual = MockProductDataMaker.MockProductComplete(productKey) as ProductActual; var MockProductService = new Mock <IProductService>(); MockProductService.Setup(cs => cs.GetByKey(productKey)).Returns(productActual); MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object); ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext); //// Act var result = ctrl.GetProduct(productKey); //// Assert Assert.AreEqual(productActual, result); }
public void AddProduct_InsertsNewProductIntoDatabase_Ok() { // arrange var mock = new Mock <EFProductRepository>(); var productCategoryMock = new Mock <ProductCategory>(); var products = new Product[] { new Product { ProductId = 1, Name = "N1", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 2, Name = "N2", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 3, Name = "N3", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 4, Name = "N4", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 }, new Product { ProductId = 5, Name = "N5", Description = "D1", Price = 10, ProductCategory = productCategoryMock.Object, ProductCategoryId = 1 } }; Product productToInsert = new Product { ProductId = 66, Name = "N66", Description = "D6", Price = 66, ProductCategory = productCategoryMock.Object, ProductCategoryId = 66 }; mock.Setup(repo => repo.SaveProduct(It.IsAny <Product>())).Returns(productToInsert); productCategoryMock.Setup(repo => repo.Name).Returns("R92"); productCategoryMock.Setup(repo => repo.ProductCategoryId).Returns(192); var controller = new ProductApiController(mock.Object); // action var result = controller.AddProduct(productToInsert); // assert Assert.Equal(66, result.Value.ProductId); Assert.Equal(66, result.Value.ProductCategoryId); Assert.Equal("R92", result.Value.ProductCategory.Name); Assert.Equal(192, result.Value.ProductCategory.ProductCategoryId); }
public void NewProductReturnsCorrectProduct() { //// Arrange bool wasCalled = false; Guid productKey = Guid.NewGuid(); ProductActual productActual = CreateFakeProduct(productKey, 20.0M); var MockProductService = new Mock <IProductService>(); MockProductService.Setup(cs => cs.CreateProduct(productActual.Sku, productActual.Name, productActual.Price)).Returns(productActual).Callback(() => wasCalled = true); MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object); ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext); //// Act ProductActual result = ctrl.NewProduct(productActual.Sku, productActual.Name, productActual.Price); //// Assert Assert.AreEqual(productActual, result); Assert.True(wasCalled); }
public void PutProductReturns500WhenRepositoryUpdateReturnsError() { //// Arrange Guid productKey = Guid.NewGuid(); ProductActual productActual = CreateFakeProduct(productKey, 20.0M); var MockProductService = new Mock <IProductService>(); MockProductService.Setup(cs => cs.Save(productActual, It.IsAny <bool>())).Throws <InvalidOperationException>(); MerchelloContext merchelloContext = GetMerchelloContext(MockProductService.Object); ProductApiController ctrl = new ProductApiController(merchelloContext, tempUmbracoContext); ctrl.Request = new HttpRequestMessage(); ctrl.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); //// Act HttpResponseMessage response = ctrl.PutProduct(productActual); //// Assert Assert.AreEqual(System.Net.HttpStatusCode.NotFound, response.StatusCode); }
public ProductApiController CreateProductController() { return(productController = new ProductApiController(mockRequest.Object)); }