Beispiel #1
0
        public void ReturnItemDetailForSingleProduct()
        {
            //Arrange
            WalmartApiConfig   urls           = ApiTestDataHelper.GetApiConfig();
            IApiRequestBuilder requestbuilder = new ApiRequestBuilder();
            var mockWalmartApiRequestManager  = new Mock <IHttpApiRequestManager>();
            var responseDataSample            = ApiTestDataHelper
                                                .GetApiResponseDataFromResource("Walmart.Api.Client.Tests.ProductLookupSingleItemSampleResponse.json");

            mockWalmartApiRequestManager.Setup(mgr => mgr.GetDataAsync(It.IsAny <String>()))
            .Returns(Task.FromResult <string>(responseDataSample));
            var mockLogger = new Mock <ILogger <ApiServiceBase> >();

            IWalmartApiService productService =
                new WalmartApiService(mockWalmartApiRequestManager.Object, requestbuilder, urls, mockLogger.Object);

            var itemId = 10294196;
            ProductLookupRequestInput request = new ProductLookupRequestInput
            {
                ids = new List <int> {
                    itemId
                }
            };

            //Act
            var result = productService.GetProductDetailAsync(request).Result;

            //Assert
            Assert.False(result.HasError);
            Assert.Single <FullResponseItem>(result.Response.items);
            Assert.Equal(itemId, result.Response.items[0].ItemId);
            mockWalmartApiRequestManager.VerifyAll();
        }
        public void ReturnItemsForQuery()
        {
            //Arrange
            WalmartApiConfig   urls           = ApiTestDataHelper.GetApiConfig();
            IApiRequestBuilder requestbuilder = new ApiRequestBuilder();
            var mockWalmartApiRequestManager  = new Mock <IHttpApiRequestManager>();
            var responseDataSample            = ApiTestDataHelper
                                                .GetApiResponseDataFromResource("Walmart.Api.Client.Tests.SearchApiSampleResponse.json");

            mockWalmartApiRequestManager.Setup(mgr => mgr.GetDataAsync(It.IsAny <String>()))
            .Returns(Task.FromResult <string>(responseDataSample));
            var mockLogger = new Mock <ILogger <ApiServiceBase> >();

            IWalmartApiService productService =
                new WalmartApiService(mockWalmartApiRequestManager.Object, requestbuilder, urls, mockLogger.Object);

            SearchRequestInput request = new SearchRequestInput
            {
                query = "Ben & Jerry's ice cream",
                sort  = SearchRequestSortType.bestseller
            };

            //Act
            var result = productService.SearchProductsAsync(request).Result;

            //Assert
            Assert.False(result.HasError);
            Assert.Equal(10, result.Response.items.Count);
            mockWalmartApiRequestManager.VerifyAll();
        }
        public void ReturnEmptyResponseWhenRecommendationServiceFails()
        {
            //Arrange
            WalmartApiConfig   urls           = ApiTestDataHelper.GetApiConfig();
            IApiRequestBuilder requestbuilder = new ApiRequestBuilder();
            var mockWalmartApiRequestManager  = new Mock <IHttpApiRequestManager>();
            var responseDataSample            = "{\"errors\":[{\"code\":4022,\"message\":\"No recommendations found for item 36461211\"}]}";

            mockWalmartApiRequestManager.Setup(mgr => mgr.GetDataAsync(It.IsAny <String>()))
            .Returns(Task.FromResult <string>(responseDataSample));
            var mockLogger = new Mock <ILogger <ApiServiceBase> >();

            IWalmartApiService productService =
                new WalmartApiService(mockWalmartApiRequestManager.Object, requestbuilder, urls, mockLogger.Object);

            var itemId = 36461211;

            //Act
            var result = productService.GetProductRecommendationAsync(itemId).Result;

            //Assert
            Assert.False(result.HasError);
            Assert.Null(result.Response);
            mockWalmartApiRequestManager.VerifyAll();
        }
        public void ReturnRecommendationsForProduct()
        {
            //Arrange
            WalmartApiConfig   urls           = ApiTestDataHelper.GetApiConfig();
            IApiRequestBuilder requestbuilder = new ApiRequestBuilder();
            var mockWalmartApiRequestManager  = new Mock <IHttpApiRequestManager>();
            var responseDataSample            = ApiTestDataHelper
                                                .GetApiResponseDataFromResource("Walmart.Api.Client.Tests.ProductRecommendationSampleResponse.json");

            mockWalmartApiRequestManager.Setup(mgr => mgr.GetDataAsync(It.IsAny <String>()))
            .Returns(Task.FromResult <string>(responseDataSample));
            var mockLogger = new Mock <ILogger <ApiServiceBase> >();

            IWalmartApiService productService =
                new WalmartApiService(mockWalmartApiRequestManager.Object, requestbuilder, urls, mockLogger.Object);

            var itemId = 36461211;

            //Act
            var result = productService.GetProductRecommendationAsync(itemId).Result;

            //Assert
            Assert.False(result.HasError);
            Assert.Equal(11, result.Response.Count);
            mockWalmartApiRequestManager.VerifyAll();
        }
Beispiel #5
0
        public void ReturnItemDetailsForMultipleProducts()
        {
            //Arrange
            WalmartApiConfig   urls           = ApiTestDataHelper.GetApiConfig();
            IApiRequestBuilder requestbuilder = new ApiRequestBuilder();
            var mockWalmartApiRequestManager  = new Mock <IHttpApiRequestManager>();
            var responseDataSample            = ApiTestDataHelper
                                                .GetApiResponseDataFromResource("Walmart.Api.Client.Tests.ProductLookupMultiItemSampleResponse.json");

            mockWalmartApiRequestManager.Setup(mgr => mgr.GetDataAsync(It.IsAny <String>()))
            .Returns(Task.FromResult <string>(responseDataSample));
            var mockLogger = new Mock <ILogger <ApiServiceBase> >();

            IWalmartApiService productService =
                new WalmartApiService(mockWalmartApiRequestManager.Object, requestbuilder, urls, mockLogger.Object);

            var itemIds = new List <int> {
                10294196, 893337, 817612485, 39661524, 654023063, 10294197,
                10403812, 156205102, 20691516, 10324477
            };
            ProductLookupRequestInput request = new ProductLookupRequestInput
            {
                ids = itemIds
            };

            //Act
            var result = productService.GetProductDetailAsync(request).Result;

            //Assert
            Assert.False(result.HasError);
            Assert.Equal(10, result.Response.items.Count);
            itemIds.ForEach(item =>
            {
                Assert.Single(result.Response.items, p => p.ItemId == item);
            });

            mockWalmartApiRequestManager.VerifyAll();
        }
        public void ReturnErrorCodeWhenApiCallFails()
        {
            //Arrange
            WalmartApiConfig   urls           = ApiTestDataHelper.GetApiConfig();
            IApiRequestBuilder requestbuilder = new ApiRequestBuilder();
            var mockWalmartApiRequestManager  = new Mock <IHttpApiRequestManager>();

            mockWalmartApiRequestManager.Setup(mgr => mgr.GetDataAsync(It.IsAny <String>()))
            .Returns(Task.FromResult <string>(""));

            var mockWebResponse = new Mock <HttpWebResponse>();

            mockWebResponse.Setup(res => res.StatusCode).Returns(HttpStatusCode.BadRequest);
            mockWalmartApiRequestManager.Setup(mgr => mgr.GetDataAsync(It.IsAny <String>()))
            .Throws(
                new WebException("Missing search query",
                                 null,
                                 WebExceptionStatus.UnknownError,
                                 mockWebResponse.Object));
            var mockLogger = new Mock <ILogger <ApiServiceBase> >();
            IWalmartApiService productService =
                new WalmartApiService(mockWalmartApiRequestManager.Object, requestbuilder, urls, mockLogger.Object);

            SearchRequestInput request = new SearchRequestInput
            {
                query = "Ben & Jerry's ice cream",
                sort  = SearchRequestSortType.bestseller
            };

            //Act
            var result = productService.SearchProductsAsync(request).Result;

            //Assert
            Assert.True(result.HasError);
            Assert.Equal("400", result.ErrorCode);
            mockWalmartApiRequestManager.VerifyAll();
            mockWebResponse.VerifyAll();
        }