Ejemplo n.º 1
0
        public void GetProducts_ShouldReturnListOfProducts()
        {
            var getProductResult = new GetProductsResult
            {
                CreationDate = mockProduct.CreationDate,
                Description  = mockProduct.Description,
                Id           = mockProduct.Id,
                Name         = mockProduct.Name,
                Price        = mockProduct.Price
            };

            var listUser = new List <Product>()
            {
                mockProduct
            };

            var listResult = new List <GetProductsResult>
            {
                getProductResult
            };

            mockMapper.Setup(m => m.Map <IEnumerable <GetProductsResult> >(It.IsAny <IEnumerable <Product> >()))
            .Returns(listResult);

            mockProductRepository.Setup(m => m.GetProducts(It.IsAny <GetProductsRequest>())).
            Returns(listUser);

            var result = productService.GetProducts(new GetProductsRequest());

            Assert.Equal(listResult, result);
        }
            public async Task GetCategories_CategoriesExist_ReturnsCategories(
                [Frozen] Mock <IProductApiClient> mockClient,
                ProductService sut,
                GetProductsResult products,
                int pageIndex,
                int pageSize,
                string category,
                string subcategory
                )
            {
                //Arrange
                mockClient.Setup(_ => _.GetProductsAsync(
                                     It.IsAny <int>(),
                                     It.IsAny <int>(),
                                     It.IsAny <string>(),
                                     It.IsAny <string>(),
                                     It.IsAny <string>()
                                     ))
                .ReturnsAsync(products);

                //Act
                var response = await sut.GetProductsAsync(
                    pageIndex,
                    pageSize,
                    category,
                    subcategory
                    );

                //Assert
                response.Should().BeEquivalentTo(products);
            }
Ejemplo n.º 3
0
        public async Task <ProductsResult> GetProducts(string searchString)
        {
            ProductsResult result = new ProductsResult();

            try
            {
                GetProductsResult output = (GetProductsResult)_objectCache.GetItem(_productCacheKey);
                if (output == null)
                {
                    throw new Exception("Products not found in cache. Reload the cache or reset it through the admin panel");
                }
                if (output.ResultCode == -1)
                {
                    throw new Exception();
                }
                result.Products = output.Products?.Select(p => new Classes.Products
                {
                    ProductGuid = p.ProductGuid,
                    Name        = p.Name
                }).ToList() ?? new List <Classes.Products>();

                result.Products = result.Products.Where(p => p.Name.IndexOf(searchString) != -1).ToList();
            }
            catch (Exception ex)
            {
                SetException(result, ex);
            }
            return(result);
        }
Ejemplo n.º 4
0
        public GetProductsResult GetProducts()
        {
            GetProductsResult result = new GetProductsResult();

            try
            {
                result.Products = db.Products.ToList();
            }
            catch (Exception ex)
            {
                SetException(result, ex);
            }
            return(result);
        }
Ejemplo n.º 5
0
            public async Task GetProducts_ProductsFound_ReturnsProducts(
                [Frozen] MockHttpMessageHandler handler,
                [Frozen] HttpClient httpClient,
                Uri uri,
                List <Product> list,
                ProductApiClient sut,
                string category,
                string subCategory,
                string orderBy
                )
            {
                //Arrange
                var products = new GetProductsResult
                {
                    Products      = list,
                    TotalProducts = list.Count
                };

                httpClient.BaseAddress = uri;
                handler.When(HttpMethod.Get, $"{uri}*")
                .Respond(HttpStatusCode.OK,
                         new StringContent(
                             JsonSerializer.Serialize(products, new JsonSerializerOptions
                {
                    Converters =
                    {
                        new JsonStringEnumConverter()
                    },
                    IgnoreReadOnlyProperties = true,
                    PropertyNamingPolicy     = JsonNamingPolicy.CamelCase
                })
                             )
                         );

                //Act
                var response = await sut.GetProductsAsync(0, 10, category, subCategory, orderBy);

                //Assert
                response.Should().NotBeNull();
                response.TotalProducts.Should().Be(list.Count);
                response.Products.Should().BeEquivalentTo(list);
            }
Ejemplo n.º 6
0
        public async Task <BaseResult> ReloadCache()
        {
            BaseResult response = new BaseResult();

            try
            {
                _objectCache.DeleteItem(_productCacheKey);

                GetProductsResult output = await _eatClient.GetProductsAsync();

                if (output == null)
                {
                    throw new Exception("Service did not return the products and returned an error: " + output.Error);
                }
                _objectCache.SetItem(_productCacheKey, output);
            }
            catch (Exception ex)
            {
                SetException(response, ex);
            }
            return(response);
        }
Ejemplo n.º 7
0
        public async Task <ProductsResult> GetProducts()
        {
            ProductsResult result = new ProductsResult();

            try
            {
                GetProductsResult output = await _eatClient.GetProductsAsync();

                if (output.ResultCode == -1)
                {
                    throw new Exception();
                }
                result.Products = output.Products?.Select(p => new Classes.Products
                {
                    ProductGuid = p.ProductGuid,
                    Name        = p.Name
                }).ToList() ?? new List <Classes.Products>();
            }
            catch (Exception ex)
            {
                SetException(result, ex);
            }
            return(result);
        }