public void ParseEmptySerialNumber()
        {
            DeleteCertificatedWithEmptySerialNumber();

            var catalogProduct = new Catalog {
                Id = 1, Name = "catalog1"
            };

            var testSupplier      = TestSupplier.Create();
            var docSupplier       = Supplier.Find(testSupplier.Id);
            var certificateSource = CreateRealSourceForSupplier(docSupplier);

            var product = new Product {
                Id = 3, CatalogProduct = catalogProduct
            };

            var document = new Document {
                FirmCode = docSupplier.Id
            };

            document.NewLine(new DocumentLine {
                ProductEntity       = product,
                SerialNumber        = null,
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = product,
                SerialNumber        = "",
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = product,
                SerialNumber        = "  ",
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = product,
                SerialNumber        = "-",
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = product,
                SerialNumber        = "  - ",
                CertificateFilename = "cerFilename"
            });

            CertificateSourceDetector.DetectAndParse(document);

            Assert.That(document.Tasks.Count, Is.EqualTo(5));
            Assert.That(document.Tasks.TrueForAll(t => t.CertificateSource.Id == certificateSource.Id));

            var task = document.Tasks[0];

            Assert.That(task.CatalogProduct.Id == catalogProduct.Id && task.SerialNumber == DocumentLine.EmptySerialNumber);
        }
        public void CheckParse()
        {
            var certificateSource = CreateRealSourceForSupplier(supplier);

            var firstCatalog = new Catalog {
                Id = 1, Name = "catalog1"
            };
            var secondCatalog = new Catalog {
                Id = 2, Name = "catalog2"
            };
            var firstProduct = new Product {
                Id = 3, CatalogProduct = firstCatalog
            };
            var secondProduct = new Product {
                Id = 4, CatalogProduct = firstCatalog
            };
            var thirdProduct = new Product {
                Id = 5, CatalogProduct = secondCatalog
            };

            var document = new Document {
                FirmCode = supplier.Id
            };

            document.NewLine(new DocumentLine {
                ProductEntity = firstProduct,
                SerialNumber  = "крутая серия 1"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = firstProduct,
                SerialNumber        = "крутая серия 1",
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = secondProduct,
                SerialNumber        = "крутая серия 1",
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = secondProduct,
                SerialNumber        = "крутая серия 2",
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = thirdProduct,
                SerialNumber        = string.Empty,
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = thirdProduct,
                SerialNumber        = "крутая серия 1",
                CertificateFilename = "cerFilename"
            });

            CertificateSourceDetector.DetectAndParse(document);

            Assert.That(document.Tasks.Count, Is.EqualTo(5));
            Assert.That(document.Tasks.TrueForAll(t => t.CertificateSource.Id == certificateSource.Id));

            var task = document.Tasks.OrderBy(t => t.CatalogProduct.Id).ThenBy(t => t.SerialNumber).ToList();

            Assert.That(task[0].CatalogProduct.Id == firstCatalog.Id && task[0].SerialNumber == "крутая серия 1");
            Assert.AreEqual(firstCatalog.Id, task[2].CatalogProduct.Id);
            Assert.AreEqual("крутая серия 2", task[2].SerialNumber);

            Assert.AreEqual(secondCatalog.Id, task[3].CatalogProduct.Id);
            Assert.AreEqual("крутая серия 1", task[3].SerialNumber);

            Assert.AreEqual(secondCatalog.Id, task[4].CatalogProduct.Id);
            Assert.AreEqual(DocumentLine.EmptySerialNumber, task[4].SerialNumber);
        }
        public void CheckParseWithExistsCertificates()
        {
            var certificateSource     = CreateRealSourceForSupplier(supplier);
            var anotherSupplier       = Supplier.Queryable.First(s => s.Id != supplier.Id);
            var anotherSupplierSource = CreateRandomSourceForSupplier(anotherSupplier);

            var catalogs        = Catalog.Queryable.Take(2).ToList().OrderBy(c => c.Id).ToList();
            var existsCatalog   = catalogs[0];
            var nonExistCatalog = catalogs[1];
            var serialNumber    = "крутая серия 5";

            var firstProduct = new Product {
                Id = 3, CatalogProduct = existsCatalog
            };
            var secondProduct = new Product {
                Id = 4, CatalogProduct = nonExistCatalog
            };
            var thirdProduct = new Product {
                Id = 5, CatalogProduct = existsCatalog
            };

            var certificates =
                Certificate.Queryable.Where(c => c.SerialNumber == serialNumber).ToList();

            certificates.ForEach(c => c.Delete());
            session.Flush();

            var existsCertificate = new Certificate();

            existsCertificate.CatalogProduct = Catalog.Find(existsCatalog.Id);
            existsCertificate.SerialNumber   = serialNumber;
            existsCertificate.NewFile(
                new CertificateFile {
                OriginFilename    = Path.GetRandomFileName(),
                Extension         = ".tif",
                CertificateSource = certificateSource
            });
            existsCertificate.NewFile(
                new CertificateFile {
                OriginFilename    = Path.GetRandomFileName(),
                Extension         = ".tif",
                CertificateSource = certificateSource
            });
            existsCertificate.NewFile(
                new CertificateFile {
                OriginFilename    = Path.GetRandomFileName(),
                Extension         = ".tif",
                CertificateSource = anotherSupplierSource
            });
            existsCertificate.Save();

            var document = new Document {
                FirmCode = supplier.Id
            };

            document.NewLine(new DocumentLine {
                ProductEntity = firstProduct,
                SerialNumber  = serialNumber
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = firstProduct,
                SerialNumber        = "крутая серия 1",
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = secondProduct,
                SerialNumber        = serialNumber,
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = secondProduct,
                SerialNumber        = "крутая серия 2",
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = thirdProduct,
                SerialNumber        = serialNumber,
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = thirdProduct,
                SerialNumber        = "крутая серия 1",
                CertificateFilename = "cerFilename"
            });
            document.NewLine(new DocumentLine {
                ProductEntity       = thirdProduct,
                SerialNumber        = "крутая серия 2",
                CertificateFilename = "cerFilename"
            });

            CertificateSourceDetector.DetectAndParse(document);

            Assert.That(document.Tasks.Count, Is.EqualTo(5));
            Assert.That(document.Tasks.TrueForAll(t => t.CertificateSource.Id == certificateSource.Id));

            var task = document.Tasks.OrderBy(t => t.CatalogProduct.Id).ThenBy(t => t.SerialNumber).ToList();

            Assert.That(task[0].CatalogProduct.Id == existsCatalog.Id && task[0].SerialNumber == "крутая серия 1");
            Assert.AreEqual(existsCatalog.Id, task[2].CatalogProduct.Id);
            Assert.AreEqual("крутая серия 2", task[2].SerialNumber);

            Assert.AreEqual(nonExistCatalog.Id, task[3].CatalogProduct.Id);
            Assert.AreEqual("крутая серия 2", task[3].SerialNumber);
            Assert.That(task[4].CatalogProduct.Id == nonExistCatalog.Id && task[4].SerialNumber == serialNumber);

            Assert.That(document.Lines[0].Certificate.Id, Is.EqualTo(existsCertificate.Id));
            Assert.That(document.Lines[4].Certificate.Id, Is.EqualTo(existsCertificate.Id));
        }