public void CompanyProductsAreSaved()
        {
            // Setup
            ImportCompanyProductCsvService.Setup(m => m.GetRecords(It.IsAny <Stream>(), It.IsAny <ClassMap <ImportCompanyProductDto> >()))
            .Returns(new List <ImportCompanyProductDto>()
            {
                new ImportCompanyProductDto
                {
                    ProductName = "ProductName1",
                    ProductSku  = "ProductSku1"
                },
                new ImportCompanyProductDto
                {
                    ProductName = "ProductName2",
                    ProductSku  = "ProductSku"
                },
            });

            using var writer = new StreamWriter(new MemoryStream());
            var result = Subject.ImportCompanyProductsFromFileStream(new ImportCompanyProductRequest()
            {
                FileStream = writer.BaseStream, CompanyCode = TestData.CompanyCodeA
            });

            // Assert
            Assert.True(result.Success);
            UoW.Verify(m => m.CompanyProducts.Add(It.IsAny <CompanyProduct>()), Times.Exactly(2));
        }
        public void CompanyCodeIsEmpty()
        {
            // Setup
            ImportCompanyProductCsvService.Setup(m => m.GetRecords(It.IsAny <Stream>(), It.IsAny <ClassMap <ImportCompanyProductDto> >()))
            .Returns(new List <ImportCompanyProductDto>()
            {
                new ImportCompanyProductDto
                {
                    ProductName = "ProductName1",
                    ProductSku  = "ProductSku1"
                },
                new ImportCompanyProductDto
                {
                    ProductName = "ProductName2",
                    ProductSku  = "ProductSku"
                },
            });

            CompanyService.Setup(m => m.GetCompany(It.IsAny <string>())).Returns((Company)null);

            using var writer = new StreamWriter(new MemoryStream());
            var result = Subject.ImportCompanyProductsFromFileStream(new ImportCompanyProductRequest()
            {
                FileStream = writer.BaseStream
            });

            // Assert
            Assert.Contains(typeof(RequiredFieldMissingError),
                            result.Errors.Where(m => m.Field == "CompanyCode").Select(e => e.GetType()).ToList());
        }
        public void CompanyCodeNotFound()
        {
            var companyCode = "123";

            ImportCompanyProductCsvService.Setup(m => m.GetRecords(It.IsAny <Stream>(), It.IsAny <ClassMap <ImportCompanyProductDto> >()))
            .Returns(new List <ImportCompanyProductDto>()
            {
                new ImportCompanyProductDto
                {
                    ProductName = "ProductName1",
                    ProductSku  = "ProductSku1"
                },
                new ImportCompanyProductDto
                {
                    ProductName = "ProductName2",
                    ProductSku  = "ProductSku"
                },
            });

            CompanyService.Setup(m => m.GetCompany(It.IsAny <string>())).Returns((Company)null);
            CompanyService.Setup(m => m.ValidateCompanyExist(It.IsAny <string>())).Returns(new List <Error>()
            {
                new CompanyCodeNotFoundError(companyCode)
            });

            using var writer = new StreamWriter(new MemoryStream());
            var result = Subject.ImportCompanyProductsFromFileStream(new ImportCompanyProductRequest()
            {
                FileStream = writer.BaseStream, CompanyCode = companyCode
            });

            // Assert
            Assert.Contains(typeof(CompanyCodeNotFoundError),
                            result.Errors.Where(m => m.Field == "CompanyCode").Select(e => e.GetType()).ToList());
        }
Exemplo n.º 4
0
        public ImportCompanyProductResult ImportCompanyProductsFromFileStream(ImportCompanyProductRequest request)
        {
            var result = new ImportCompanyProductResult();

            try
            {
                result.Errors.AddRange(ValidateCompanyCodeRequired(request.CompanyCode));
                result.Errors.AddRange(CompanyService.ValidateCompanyExist(request.CompanyCode));

                if (result.Errors.Any())
                {
                    return(result);
                }

                var records = ImportCompanyProductCsvService.GetRecords(request.FileStream, new ImportCompanyProductDtoClassMap());
                records.ForEach(companyProduct =>
                {
                    var existingCompanyProduct = GetCompanyProduct(companyProduct.ProductSku, request.CompanyCode);
                    if (existingCompanyProduct == null)
                    {
                        var createCompanyProductResult = CreateCompanyProduct(new CreateCompanyProductRequest()
                        {
                            ProductSku = companyProduct.ProductSku, ProductName = companyProduct.ProductName, CompanyCode = request.CompanyCode
                        });
                        if (!createCompanyProductResult.Success)
                        {
                            result.Errors.AddRange(createCompanyProductResult.Errors);
                        }
                    }
                });

                var companyProducts = UoW.CompanyProducts.GetAll().Where(i => i.Company.CompanyCode == request.CompanyCode).ToList();
                companyProducts.ForEach(companyProduct =>
                {
                    var deletedCompanyProduct = !records.Where(i => i.ProductSku.Trim().ToUpper() == companyProduct.ProductSku.Trim().ToUpper()).Any();
                    if (deletedCompanyProduct)
                    {
                        var deleteCompanyProductResult = DeleteCompanyProduct(new DeleteCompanyProductRequest()
                        {
                            ProductSku = companyProduct.ProductSku, CompanyCode = request.CompanyCode
                        });
                        if (!deleteCompanyProductResult.Success)
                        {
                            result.Errors.AddRange(deleteCompanyProductResult.Errors);
                        }
                    }
                });
            }
            catch (Exception e)
            {
                result.Success = false;
                result.Errors.Add(new ExceptionError(e.Message));
            }

            result.Success = !result.Errors.Any();

            return(result);
        }