public void CompanyProductBarcodesAreSaved()
        {
            // Setup
            ImportCompanyProductBarcodeCsvService.Setup(m => m.GetRecords(It.IsAny <Stream>(), It.IsAny <ClassMap <ImportCompanyProductBarcodeDto> >()))
            .Returns(new List <ImportCompanyProductBarcodeDto>()
            {
                new ImportCompanyProductBarcodeDto
                {
                    SupplierCode = TestData.SupplierCode,
                    ProductSku   = TestData.ProductSkuCompanyCodeA,
                    Barcode      = "Barcode1"
                },
                new ImportCompanyProductBarcodeDto
                {
                    SupplierCode = TestData.SupplierCode,
                    ProductSku   = TestData.ProductSkuCompanyCodeA,
                    Barcode      = "Barcode2"
                },
            });

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

            // Assert
            Assert.True(result.Success);
            UoW.Verify(m => m.CompanyProductBarcodes.Add(It.IsAny <CompanyProductBarcode>()), Times.Exactly(2));
        }
        public void CompanyCodeIsEmpty()
        {
            // Setup
            ImportCompanyProductBarcodeCsvService.Setup(m => m.GetRecords(It.IsAny <Stream>(), It.IsAny <ClassMap <ImportCompanyProductBarcodeDto> >()))
            .Returns(new List <ImportCompanyProductBarcodeDto>()
            {
                new ImportCompanyProductBarcodeDto
                {
                    SupplierCode = TestData.SupplierCode,
                    ProductSku   = TestData.ProductSkuCompanyCodeA,
                    Barcode      = "Barcode1"
                },
                new ImportCompanyProductBarcodeDto
                {
                    SupplierCode = TestData.SupplierCode,
                    ProductSku   = TestData.ProductSkuCompanyCodeA,
                    Barcode      = "Barcode2"
                },
            });

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

            // Assert
            Assert.Contains(typeof(RequiredFieldMissingError),
                            result.Errors.Where(m => m.Field == "CompanyCode").Select(e => e.GetType()).ToList());
        }
        public ImportCompanyProductBarcodeResult ImportCompanyProductBarcodesFromFileStream(ImportCompanyProductBarcodeRequest request)
        {
            var result = new ImportCompanyProductBarcodeResult();

            try
            {
                result.Errors.AddRange(ValidateCompanyCodeRequired(request.CompanyCode));
                result.Errors.AddRange(CompanyService.ValidateCompanyExist(request.CompanyCode));
                if (result.Errors.Any())
                {
                    return(result);
                }

                var records = ImportCompanyProductBarcodeCsvService.GetRecords(request.FileStream, new ImportCompanyProductBarcodeDtoClassMap());
                records.ForEach(barcode =>
                {
                    var existingCompanyProductBarcode = GetCompanyProductBarcode(barcode.Barcode, barcode.SupplierCode, barcode.ProductSku, request.CompanyCode);
                    if (existingCompanyProductBarcode == null)
                    {
                        var createCompanyProductBarcodeResult = CreateCompanyProductBarcode(new CreateCompanyProductBarcodeRequest()
                        {
                            Barcode = barcode.Barcode, ProductSku = barcode.ProductSku, SupplierCode = barcode.SupplierCode, CompanyCode = request.CompanyCode
                        });
                        if (!createCompanyProductBarcodeResult.Success)
                        {
                            result.Errors.AddRange(createCompanyProductBarcodeResult.Errors);
                        }
                    }
                });
            }
            catch (Exception e)
            {
                result.Success = false;
                result.Errors.Add(new ExceptionError(e.Message));
            }

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

            return(result);
        }
        public void CompanyCodeNotFound()
        {
            ImportCompanyProductBarcodeCsvService.Setup(m => m.GetRecords(It.IsAny <Stream>(), It.IsAny <ClassMap <ImportCompanyProductBarcodeDto> >()))
            .Returns(new List <ImportCompanyProductBarcodeDto>()
            {
                new ImportCompanyProductBarcodeDto
                {
                    SupplierCode = TestData.SupplierCode,
                    ProductSku   = TestData.ProductSkuCompanyCodeA,
                    Barcode      = "Barcode1"
                },
                new ImportCompanyProductBarcodeDto
                {
                    SupplierCode = TestData.SupplierCode,
                    ProductSku   = TestData.ProductSkuCompanyCodeA,
                    Barcode      = "Barcode2"
                },
            });

            var companyCode = "123";

            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.ImportCompanyProductBarcodesFromFileStream(new ImportCompanyProductBarcodeRequest()
            {
                FileStream = writer.BaseStream, CompanyCode = companyCode
            });

            // Assert
            Assert.Contains(typeof(CompanyCodeNotFoundError),
                            result.Errors.Where(m => m.Field == "CompanyCode").Select(e => e.GetType()).ToList());
        }