Esempio n. 1
0
        public void WritesOutputCsvFileIfNoErrorsFoundAndRecordsIsNotNull()
        {
            // Arrange
            _fileValidator.Validate(Arg.Any <FileModel>(), Arg.Any <List <YearlyData> >()).Returns(new Defects());
            _csvLoader.LoadCsvFile(Arg.Any <string>()).Returns(new List <YearlyData>()
            {
                new YearlyData()
                {
                    DevelopmentYear = 1990, OriginYear = 1990, ProductName = "Comp"
                }
            });
            _recordsPerProduct.SortRecordsByProduct(Arg.Any <List <YearlyData> >())
            .Returns(new List <List <YearlyData> >()
            {
                new List <YearlyData>
                {
                    new YearlyData {
                        DevelopmentYear = 1900, OriginYear = 1990, IncrementalValue = 0, ProductName = "Comp"
                    }
                }
            });


            // Act
            _target.ProcessFile(1);

            // Assert
            _csvOutput.Received().Write(Arg.Any <List <List <YearlyData> > >(), Arg.Any <string>());
        }
Esempio n. 2
0
        public IActionResult ProcessFile(int id)
        {
            /*
             * This solution assumes that this web application is running on an intranet (not the public internet) and none of the users are malicious
             * There is basic protection insofar as this method does not accept any string inputs
             * However if this were to go on the public internet, a security review might first be needed
             */
            var file = _csvLoader.GetFileById(id);

            if (file == null)
            {
                throw new System.Exception("File Not Found");
            }

            var records = _csvLoader.LoadCsvFile(_inputFilesPath + "\\" + file.Name);

            IncrementalValue.DefaultNullsToZeroes(records);

            var defects = _fileValidator.Validate(file, records);


            if (defects != null && defects.Errors.Any())
            {
                return(RedirectToAction("Index", "Failure", defects));
            }

            if (records != null)
            {
                MinAndMaxYears minAndMaxYears = new MinAndMaxYears
                {
                    Minimum = records.Min(p => p.OriginYear),
                    Maximum = records.Max(p => p.OriginYear)
                };

                var recordsPerProduct = _recordsPerProduct.SortRecordsByProduct(records);


                recordsPerProduct = new MissingYears().Add(recordsPerProduct, minAndMaxYears);

                string outputFileName = _csvOutput.GetOutputFileName(file.Name);
                _csvOutput.Write(recordsPerProduct, outputFileName);
            }


            return(RedirectToAction("Index", "Success", new { id = file.Id, fileName = file.Name }));
        }