public void SuppliersAreSaved()
        {
            // Setup
            ImportSupplierCsvService.Setup(m => m.GetRecords(It.IsAny <Stream>(), It.IsAny <ClassMap <ImportSupplierDto> >()))
            .Returns(new List <ImportSupplierDto>()
            {
                new ImportSupplierDto
                {
                    SupplierName = "Supplier 1000",
                    SupplierCode = "1000"
                },
                new ImportSupplierDto
                {
                    SupplierName = "Supplier 2000",
                    SupplierCode = "2000"
                },
            });

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

            // Assert
            //Assert
            Assert.True(result.Success);
            UoW.Verify(m => m.Suppliers.Add(It.IsAny <Supplier>()), Times.Exactly(2));
        }
        public void CompanyCodeIsEmpty()
        {
            // Setup
            ImportSupplierCsvService.Setup(m => m.GetRecords(It.IsAny <Stream>(), It.IsAny <ClassMap <ImportSupplierDto> >()))
            .Returns(new List <ImportSupplierDto>()
            {
                new ImportSupplierDto
                {
                    SupplierName = "Supplier 1000",
                    SupplierCode = "1000"
                },
                new ImportSupplierDto
                {
                    SupplierName = "Supplier 2000",
                    SupplierCode = "2000"
                },
            });

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

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

            // Assert
            Assert.Contains(typeof(RequiredFieldMissingError),
                            result.Errors.Where(m => m.Field == "CompanyCode").Select(e => e.GetType()).ToList());
        }
Exemplo n.º 3
0
        public ImportSupplierResult ImportSuppliersFromFileStream(ImportSupplierRequest request)
        {
            var result = new ImportSupplierResult();

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

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

                var records = ImportSupplierCsvService.GetRecords(request.FileStream, new ImportSupplierDtoClassMap());
                records.ForEach(supplier =>
                {
                    var existingSupplier = GetSupplier(supplier.SupplierCode, request.CompanyCode);
                    if (existingSupplier == null)
                    {
                        var createSupplierResult = CreateSupplier(new CreateSupplierRequest()
                        {
                            SupplierName = supplier.SupplierName, SupplierCode = supplier.SupplierCode, CompanyCode = request.CompanyCode
                        });
                        if (!createSupplierResult.Success)
                        {
                            result.Errors.AddRange(createSupplierResult.Errors);
                        }
                    }
                });
            }
            catch (Exception e)
            {
                result.Success = false;
                result.Errors.Add(new ExceptionError(e.Message));
            }


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

            return(result);
        }