protected WalmartApiResponse <T> DeserializeResponse <T>(string response, WebException ex) where T : class { var apiResponse = new WalmartApiResponse <T>(); if (ex != null) { var exceptionDetail = ReadWebException(ex); apiResponse.HasError = true; apiResponse.ErrorMessage = exceptionDetail.Details; apiResponse.ErrorCode = exceptionDetail.StatusCode; apiResponse.Response = null; return(apiResponse); } apiResponse.HasError = false; apiResponse.ErrorCode = ""; apiResponse.ErrorMessage = string.Empty; try { apiResponse.Response = JsonConvert.DeserializeObject <T>(response); } catch (JsonSerializationException serializationException) { var errorMessage = serializationException; Logger.LogError(response); Logger.LogError(errorMessage.ToString()); } return(apiResponse); }
public void ReturnProductDetails() { //Arrange var mockLogger = new Mock <ILogger <WalmartProductDataController> >(); var mockApiService = new Mock <IWalmartApiService>(); var itemId = 123; var productName = "tylenol a"; var productLookupApiResponse = new WalmartApiResponse <ProductLookupResult> { HasError = false, Response = new ProductLookupResult { items = new List <FullResponseItem> { new FullResponseItem { ItemId = itemId, BrandName = "", CategoryNode = "", CategoryPath = "", CustomerRating = 5, CustomerRatingImage = "", LargeImage = "", LongDescription = "", Marketplace = true, MediumImage = "", ModelNumber = "", Msrp = 3.3M, Name = productName, NumReviews = "4", ParentItemId = 1, ProductUrl = "", SalePrice = 3.1M, ShortDescription = "", StandardShipRate = 1.2M, ThumbnailImage = "", Upc = "" } } } }; mockApiService.Setup(s => s.GetProductDetailAsync( It.Is <ProductLookupRequestInput>(lookupParam => lookupParam.ids.Count == 1 && lookupParam.ids.Contains(123)))) .Returns(Task <WalmartApiResponse <ProductLookupResult> > .FromResult(productLookupApiResponse)); var controller = new WalmartProductDataController(mockApiService.Object, mockLogger.Object); //Act var result = (ProductInfoFull)((OkObjectResult)controller.ProductLookup(itemId).Result).Value; //Assert Assert.Equal(itemId, result.ItemId); Assert.Equal(result.Name, result.Name); mockApiService.VerifyAll(); }
List <ProductInfoFull> ProcessProductLookupResponse(WalmartApiResponse <ProductLookupResult> productLookupResponse) { var productList = new List <ProductInfoFull>(); if (!productLookupResponse.HasError) { productLookupResponse.Response.items.ForEach( product => productList.Add( new ProductInfoFull { ItemId = product.ItemId, Name = product.Name, ThumbnailImage = product.ThumbnailImage, MediumImage = product.MediumImage, LargeImage = product.LargeImage, SalePrice = product.SalePrice, ShortDescription = product.ShortDescription, LongDescription = product.LongDescription, CustomerRatingImage = product.CustomerRatingImage, NumReviews = product.NumReviews })); } return(productList); }
public void ReturnOkObjectActionResult() { //Arrange var mockLogger = new Mock <ILogger <WalmartProductDataController> >(); var mockApiService = new Mock <IWalmartApiService>(); var itemId = 123; var productLookupApiResponse = new WalmartApiResponse <ProductLookupResult> { HasError = false, Response = new ProductLookupResult { items = new List <FullResponseItem> { new FullResponseItem { ItemId = itemId } } } }; mockApiService.Setup(s => s.GetProductDetailAsync( It.Is <ProductLookupRequestInput>(lookupParam => lookupParam.ids.Count == 1 && lookupParam.ids.Contains(123)))) .Returns(Task <WalmartApiResponse <ProductLookupResult> > .FromResult(productLookupApiResponse)); var controller = new WalmartProductDataController(mockApiService.Object, mockLogger.Object); //Act var result = controller.ProductLookup(itemId).Result; //Assert Assert.IsType <OkObjectResult>(result); mockApiService.VerifyAll(); }
public void ReturnProductRecommendations() { //Arrange var mockLogger = new Mock <ILogger <WalmartProductDataController> >(); var mockApiService = new Mock <IWalmartApiService>(); var itemId = 2321; var recommendationApiResponse = new WalmartApiResponse <List <FullResponseItem> > { HasError = false, Response = new List <FullResponseItem> { new FullResponseItem { ItemId = 651 }, new FullResponseItem { ItemId = 221 } } }; var productLookupApiResponse = new WalmartApiResponse <ProductLookupResult> { HasError = false, Response = new ProductLookupResult { items = new List <FullResponseItem> { new FullResponseItem { ItemId = 651, BrandName = "", CategoryNode = "", CategoryPath = "", CustomerRating = 5, CustomerRatingImage = "", LargeImage = "", LongDescription = "", Marketplace = true, MediumImage = "", ModelNumber = "", Msrp = 3.3M, Name = "advil pm", NumReviews = "4", ParentItemId = 1, ProductUrl = "", SalePrice = 3.1M, ShortDescription = "", StandardShipRate = 1.2M, ThumbnailImage = "", Upc = "" }, new FullResponseItem { ItemId = 221, BrandName = "", CategoryNode = "", CategoryPath = "", CustomerRating = 2, CustomerRatingImage = "", LargeImage = "", LongDescription = "", Marketplace = true, MediumImage = "", ModelNumber = "", Msrp = 2.3M, Name = "Tylenol cold & flu syrup", NumReviews = "4", ParentItemId = 4, ProductUrl = "", SalePrice = 2.1M, ShortDescription = "", StandardShipRate = 1.1M, ThumbnailImage = "", Upc = "" } } } }; mockApiService.Setup(s => s.GetProductRecommendationAsync(It.Is <int>(val => val == itemId))) .Returns(Task <WalmartApiResponse <List <FullResponseItem> > > .FromResult(recommendationApiResponse)); mockApiService.Setup(s => s.GetProductDetailAsync( It.Is <ProductLookupRequestInput>(lookupParam => lookupParam.ids.Count == 2 && lookupParam.ids.Contains(651) && lookupParam.ids.Contains(221)))) .Returns(Task <WalmartApiResponse <ProductLookupResult> > .FromResult(productLookupApiResponse)); var controller = new WalmartProductDataController(mockApiService.Object, mockLogger.Object); //Act var result = (List <ProductInfoFull>)((OkObjectResult)controller.ProductRecommendations(itemId).Result).Value; //Assert Assert.Equal(2, result.Count); Assert.Single <ProductInfoFull>(result, item => item.ItemId == 651 && item.Name == "advil pm"); Assert.Single <ProductInfoFull>(result, item => item.ItemId == 221 && item.Name == "Tylenol cold & flu syrup"); mockApiService.VerifyAll(); }
public void ReturnProductsForQuery() { //Arrange var mockLogger = new Mock <ILogger <WalmartProductDataController> >(); var mockApiService = new Mock <IWalmartApiService>(); var query = "tylenol"; var sort = SearchRequestSortType.bestseller; var searchApiResponse = new WalmartApiResponse <SearchResult <FullResponseItem> > { HasError = false, Response = new SearchResult <FullResponseItem> { items = new List <FullResponseItem> { new FullResponseItem { ItemId = 123 }, new FullResponseItem { ItemId = 456 } } } }; var productLookupApiResponse = new WalmartApiResponse <ProductLookupResult> { HasError = false, Response = new ProductLookupResult { items = new List <FullResponseItem> { new FullResponseItem { ItemId = 123, BrandName = "", CategoryNode = "", CategoryPath = "", CustomerRating = 5, CustomerRatingImage = "", LargeImage = "", LongDescription = "", Marketplace = true, MediumImage = "", ModelNumber = "", Msrp = 3.3M, Name = "tylenol a", NumReviews = "4", ParentItemId = 1, ProductUrl = "", SalePrice = 3.1M, ShortDescription = "", StandardShipRate = 1.2M, ThumbnailImage = "", Upc = "" }, new FullResponseItem { ItemId = 456, BrandName = "", CategoryNode = "", CategoryPath = "", CustomerRating = 2, CustomerRatingImage = "", LargeImage = "", LongDescription = "", Marketplace = true, MediumImage = "", ModelNumber = "", Msrp = 2.3M, Name = "tylenol b", NumReviews = "4", ParentItemId = 4, ProductUrl = "", SalePrice = 2.1M, ShortDescription = "", StandardShipRate = 1.1M, ThumbnailImage = "", Upc = "" } } } }; mockApiService.Setup(s => s.SearchProductsAsync( It.Is <SearchRequestInput>(searchParam => searchParam.query == query && searchParam.sort == sort))) .Returns(Task <WalmartApiResponse <SearchResult <FullResponseItem> > > .FromResult(searchApiResponse)); mockApiService.Setup(s => s.GetProductDetailAsync( It.Is <ProductLookupRequestInput>(lookupParam => lookupParam.ids.Count == 2 && lookupParam.ids.Contains(123) && lookupParam.ids.Contains(456)))) .Returns(Task <WalmartApiResponse <ProductLookupResult> > .FromResult(productLookupApiResponse)); var controller = new WalmartProductDataController(mockApiService.Object, mockLogger.Object); //Act var result = (List <ProductInfoFull>)((OkObjectResult)controller.Search(query, sort).Result).Value; //Assert Assert.Equal(2, result.Count); Assert.Single <ProductInfoFull>(result, item => item.ItemId == 123); Assert.Single <ProductInfoFull>(result, item => item.ItemId == 456); mockApiService.VerifyAll(); }