Exemplo n.º 1
0
        public async Task <byte[]> GetCumulativeCsv(CumulativeData cumulativeData)
        {
            try
            {
                await using var memoryStream = new MemoryStream();
                await using var writer       = new StreamWriter(memoryStream);

                using var csvWriter = new CsvWriter(writer);
                csvWriter.Configuration.HasHeaderRecord = false;
                csvWriter.Configuration.RegisterClassMap <CumulativeValueMap>();

                csvWriter.WriteRecord(cumulativeData.Header);
                await csvWriter.NextRecordAsync();

                foreach (var value in cumulativeData.CumulativeValues)
                {
                    csvWriter.WriteRecord(value);
                    await csvWriter.NextRecordAsync();
                }

                await writer.FlushAsync();

                return(memoryStream.ToArray());
            }
            catch (Exception ex)
            {
                throw new CsvException(CsvExceptionType.FailedToWrite,
                                       "There was problem creating the cumulative file", ex);
            }
        }
        public async Task GetCumulativeCsv_ValidInput_ReturnsByteArray()
        {
            // Arrange
            var service = new CsvService();

            var cumulativeData = new CumulativeData
            {
                Header = new CumulativeHeader {
                    MinOriginYear = 2000, DevelopmentYears = 5
                },
                CumulativeValues = new List <CumulativeValue>
                {
                    new CumulativeValue
                    {
                        Type   = ProductType.Comp,
                        Values = new List <double> {
                            1, 2, 3
                        }
                    }
                }
            };

            // Act
            var result = await service.GetCumulativeCsv(cumulativeData);

            // Assert
            Assert.NotNull(result);
            Assert.True(result.Length > 0);
        }
        public async Task OnPostAsync_ReturnsFileStreamResult_WithFileName()
        {
            // Arrange
            var csvService        = new Mock <ICsvService>(MockBehavior.Strict);
            var cumulativeService = new Mock <ICumulativeService>(MockBehavior.Strict);
            var page = new IndexModel(csvService.Object, cumulativeService.Object)
            {
                CsvFile = GetCsvFile()
            };

            var incrementalData = new List <Claim> {
                new Claim()
            };
            var cumulativeData = new CumulativeData();

            csvService.Setup(cs => cs.GetIncrementalClaims(page.CsvFile))
            .ReturnsAsync(incrementalData);
            csvService.Setup(cs => cs.GetCumulativeCsv(cumulativeData))
            .ReturnsAsync(new byte[1]);
            cumulativeService.Setup(cs => cs.GetCumulativeData(incrementalData))
            .ReturnsAsync(cumulativeData);

            // Act
            var result = await page.OnPostAsync() as FileStreamResult;

            // Assert
            Assert.NotNull(result);
            Assert.True(result.FileDownloadName == FileUpload.CumulativeCsvFileName);
        }
        public async Task OnPostAsync_ReturnsFileStreamResult()
        {
            // Arrange
            var csvService        = new Mock <ICsvService>(MockBehavior.Strict);
            var cumulativeService = new Mock <ICumulativeService>(MockBehavior.Strict);
            var page = new IndexModel(csvService.Object, cumulativeService.Object)
            {
                CsvFile = GetCsvFile()
            };

            var incrementalData = new List <Claim> {
                new Claim()
            };
            var cumulativeData = new CumulativeData();

            csvService.Setup(cs => cs.GetIncrementalClaims(page.CsvFile))
            .ReturnsAsync(incrementalData);
            csvService.Setup(cs => cs.GetCumulativeCsv(cumulativeData))
            .ReturnsAsync(new byte[1]);
            cumulativeService.Setup(cs => cs.GetCumulativeData(incrementalData))
            .ReturnsAsync(cumulativeData);

            // Act
            var result = await page.OnPostAsync();

            // Assert
            Assert.IsInstanceOf(typeof(FileStreamResult), result);
        }
        public async Task OnPostAsync_ShowsError_IfCumulativeServiceThrowsException()
        {
            // Arrange
            var csvService        = new Mock <ICsvService>(MockBehavior.Strict);
            var cumulativeService = new Mock <ICumulativeService>(MockBehavior.Strict);
            var page = new IndexModel(csvService.Object, cumulativeService.Object)
            {
                CsvFile = GetCsvFile()
            };

            var incrementalData = new List <Claim> {
                new Claim()
            };
            var cumulativeData = new CumulativeData();

            csvService.Setup(cs => cs.GetIncrementalClaims(page.CsvFile))
            .ReturnsAsync(incrementalData);
            csvService.Setup(cs => cs.GetCumulativeCsv(cumulativeData))
            .ReturnsAsync(new byte[1]);
            cumulativeService.Setup(cs => cs.GetCumulativeData(incrementalData))
            .ThrowsAsync(new CumulativeException(CumulativeExceptionType.InvalidInput,
                                                 "Error calculating cumulative data"));

            // Act
            await page.OnPostAsync();

            // Assert
            Assert.False(page.ModelState.IsValid);
            Assert.True(page.ModelState.ErrorCount == 1);
        }
        public async Task OnPostAsync_ReturnsError_IfInvalidFileType()
        {
            // Arrange
            var csvService        = new Mock <ICsvService>(MockBehavior.Strict);
            var cumulativeService = new Mock <ICumulativeService>(MockBehavior.Strict);
            var page = new IndexModel(csvService.Object, cumulativeService.Object);

            var fileMock = new Mock <IFormFile>();

            fileMock.Setup(_ => _.FileName).Returns("input.jpg");

            page.CsvFile = fileMock.Object;

            var incrementalData = new List <Claim> {
                new Claim()
            };
            var cumulativeData = new CumulativeData();

            csvService.Setup(cs => cs.GetIncrementalClaims(page.CsvFile))
            .ReturnsAsync(incrementalData);
            csvService.Setup(cs => cs.GetCumulativeCsv(cumulativeData))
            .ReturnsAsync(new byte[1]);
            cumulativeService.Setup(cs => cs.GetCumulativeData(incrementalData))
            .ReturnsAsync(cumulativeData);

            // Act
            await page.OnPostAsync();

            // Assert
            Assert.False(page.ModelState.IsValid);
            Assert.True(page.ModelState.ErrorCount == 1);
        }