public async Task TestDataMatchReportGeneration()
        {
            string   csv      = string.Empty;
            DateTime dateTime = DateTime.UtcNow;
            string   filename = $"10033670_1_Apprenticeship Data Match Report {dateTime:yyyyMMdd-HHmmss}";
            long     ukPrn    = 10033670;
            string   ilr      = "ILR-10033670-1819-20180704-120055-03";
            DataStoreConfiguration dataStoreConfiguration = new DataStoreConfiguration()
            {
                ILRDataStoreConnectionString      = new TestConfigurationHelper().GetSectionValues <DataStoreConfiguration>("DataStoreSection").ILRDataStoreConnectionString,
                ILRDataStoreValidConnectionString = new TestConfigurationHelper().GetSectionValues <DataStoreConfiguration>("DataStoreSection").ILRDataStoreValidConnectionString
            };

            IIlr1819ValidContext IlrValidContextFactory()
            {
                var options = new DbContextOptionsBuilder <ILR1819_DataStoreEntitiesValid>().UseSqlServer(dataStoreConfiguration.ILRDataStoreValidConnectionString).Options;

                return(new ILR1819_DataStoreEntitiesValid(options));
            }

            IIlr1819RulebaseContext IlrRulebaseContextFactory()
            {
                var options = new DbContextOptionsBuilder <ILR1819_DataStoreEntities>().UseSqlServer(dataStoreConfiguration.ILRDataStoreConnectionString).Options;

                return(new ILR1819_DataStoreEntities(options));
            }

            Mock <IReportServiceContext> reportServiceContextMock = new Mock <IReportServiceContext>();

            reportServiceContextMock.SetupGet(x => x.JobId).Returns(1);
            reportServiceContextMock.SetupGet(x => x.SubmissionDateTimeUtc).Returns(DateTime.UtcNow);
            reportServiceContextMock.SetupGet(x => x.Ukprn).Returns(10033670);
            reportServiceContextMock.SetupGet(x => x.Filename).Returns(ilr);
            reportServiceContextMock.SetupGet(x => x.FundingFM36OutputKey).Returns("FundingFm36Output");
            reportServiceContextMock.SetupGet(x => x.ValidLearnRefNumbersKey).Returns("ValidLearnRefNumbers");

            Mock <ILogger> logger = new Mock <ILogger>();
            Mock <IStreamableKeyValuePersistenceService> storage   = new Mock <IStreamableKeyValuePersistenceService>();
            IJsonSerializationService     jsonSerializationService = new JsonSerializationService();
            IIntUtilitiesService          intUtilitiesService      = new IntUtilitiesService();
            IFM36ProviderService          fm36ProviderService      = new FM36FileServiceProvider(logger.Object, storage.Object, jsonSerializationService);
            Mock <IDasCommitmentsService> dasCommitmentsService    = new Mock <IDasCommitmentsService>();
            Mock <IPeriodProviderService> periodProviderService    = new Mock <IPeriodProviderService>();
            Mock <IDateTimeProvider>      dateTimeProviderMock     = new Mock <IDateTimeProvider>();

            List <DasCommitment> dasCommitments = new List <DasCommitment>
            {
                new DasCommitment(new DasCommitments
                {
                    Uln   = 9900001906,
                    Ukprn = ukPrn,
                    //StandardCode = 0,
                    FrameworkCode = 421,      // No match - 420
                    PathwayCode   = 2,        // No match - 1
                    ProgrammeType = 3,        // No match - 2
                    AgreedCost    = 1.80M,    // No match?
                    StartDate     = dateTime, // No match
                    PaymentStatus = (int)PaymentStatus.Active
                })
            };

            storage.Setup(x => x.ContainsAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(true);
            storage.Setup(x => x.SaveAsync($"{filename}.csv", It.IsAny <string>(), It.IsAny <CancellationToken>())).Callback <string, string, CancellationToken>((key, value, ct) => csv = value).Returns(Task.CompletedTask);
            storage.Setup(x => x.GetAsync("FundingFm36Output", It.IsAny <CancellationToken>())).ReturnsAsync(File.ReadAllText("Fm36.json"));
            dasCommitmentsService
            .Setup(x => x.GetCommitments(It.IsAny <long>(), It.IsAny <List <long> >(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(dasCommitments);
            dateTimeProviderMock.Setup(x => x.GetNowUtc()).Returns(dateTime);
            dateTimeProviderMock.Setup(x => x.ConvertUtcToUk(It.IsAny <DateTime>())).Returns(dateTime);
            periodProviderService.Setup(x => x.MonthFromPeriod(It.IsAny <int>())).Returns(1);

            IValueProvider valueProvider = new ValueProvider();
            IValidationStageOutputCache      validationStageOutputCache      = new ValidationStageOutputCache();
            IDatalockValidationResultBuilder datalockValidationResultBuilder = new DatalockValidationResultBuilder();
            ITotalBuilder totalBuilder = new TotalBuilder();

            var report = new DataMatchReport(
                logger.Object,
                fm36ProviderService,
                dasCommitmentsService.Object,
                periodProviderService.Object,
                storage.Object,
                dateTimeProviderMock.Object,
                valueProvider,
                validationStageOutputCache,
                datalockValidationResultBuilder,
                totalBuilder);

            await report.GenerateReport(reportServiceContextMock.Object, null, false, CancellationToken.None);

            csv.Should().NotBeNullOrEmpty();
            TestCsvHelper.CheckCsv(csv, new CsvEntry(new DataMatchReportMapper(), 1));
        }
        public async Task TestValidationReportGeneration()
        {
            string csv  = string.Empty;
            string json = string.Empty;

            byte[]   xlsx        = null;
            DateTime dateTime    = DateTime.UtcNow;
            string   filename    = $"10000020_1_Rule Violation Report {dateTime:yyyyMMdd-HHmmss}";
            string   ilrFilename = @"Reports\Validation\ILR-10000020-1819-20181005-120953-03.xml";

            Mock <ILogger> logger = new Mock <ILogger>();
            Mock <IStreamableKeyValuePersistenceService> storage        = new Mock <IStreamableKeyValuePersistenceService>();
            IXmlSerializationService        xmlSerializationService     = new XmlSerializationService();
            IJsonSerializationService       jsonSerializationService    = new JsonSerializationService();
            Mock <IDateTimeProvider>        dateTimeProviderMock        = new Mock <IDateTimeProvider>();
            IValueProvider                  valueProvider               = new ValueProvider();
            IIntUtilitiesService            intUtilitiesService         = new IntUtilitiesService();
            Mock <IValidationErrorsService> validationErrorsServiceMock = new Mock <IValidationErrorsService>();

            storage.Setup(x => x.ContainsAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(true);
            storage.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(File.ReadAllText(ilrFilename));
            storage.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <Stream>(), It.IsAny <CancellationToken>()))
            .Callback <string, Stream, CancellationToken>((st, sr, ct) => File.OpenRead(ilrFilename).CopyTo(sr)).Returns(Task.CompletedTask);
            storage.Setup(x => x.SaveAsync($"{filename}.csv", It.IsAny <string>(), It.IsAny <CancellationToken>())).Callback <string, string, CancellationToken>((key, value, ct) => csv   = value).Returns(Task.CompletedTask);
            storage.Setup(x => x.SaveAsync($"{filename}.json", It.IsAny <string>(), It.IsAny <CancellationToken>())).Callback <string, string, CancellationToken>((key, value, ct) => json = value).Returns(Task.CompletedTask);
            storage.Setup(x => x.SaveAsync($"{filename}.xlsx", It.IsAny <Stream>(), It.IsAny <CancellationToken>())).Callback <string, Stream, CancellationToken>(
                (key, value, ct) =>
            {
                value.Seek(0, SeekOrigin.Begin);
                using (MemoryStream ms = new MemoryStream())
                {
                    value.CopyTo(ms);
                    xlsx = ms.ToArray();
                }
            })
            .Returns(Task.CompletedTask);
            storage.Setup(x => x.GetAsync("ValidationErrors", It.IsAny <CancellationToken>())).ReturnsAsync(File.ReadAllText(@"Reports\Validation\ValidationErrors.json"));
            dateTimeProviderMock.Setup(x => x.GetNowUtc()).Returns(dateTime);
            dateTimeProviderMock.Setup(x => x.ConvertUtcToUk(It.IsAny <DateTime>())).Returns(dateTime);
            validationErrorsServiceMock.Setup(x => x.PopulateValidationErrors(It.IsAny <string[]>(), It.IsAny <List <ValidationErrorDetails> >(), It.IsAny <CancellationToken>())).Callback <string[], List <ValidationErrorDetails>, CancellationToken>(
                (s, v, c) =>
            {
                List <ValidationErrorDetails> validationErrorDetails = jsonSerializationService.Deserialize <List <ValidationErrorDetails> >(File.ReadAllText(@"Reports\Validation\ValidationErrorsLookup.json"));
                foreach (ValidationErrorDetails veds in v)
                {
                    ValidationErrorDetails rule = validationErrorDetails.SingleOrDefault(x => string.Equals(x.RuleName, veds.RuleName, StringComparison.OrdinalIgnoreCase));
                    if (rule != null)
                    {
                        veds.Message  = rule.Message;
                        veds.Severity = rule.Severity;
                    }
                }
            }).Returns(Task.CompletedTask);

            IIlrProviderService ilrProviderService = new IlrProviderService(logger.Object, storage.Object, xmlSerializationService, dateTimeProviderMock.Object, intUtilitiesService, null, null);

            ITopicAndTaskSectionOptions topicsAndTasks             = TestConfigurationHelper.GetTopicsAndTasks();
            IValidationStageOutputCache validationStageOutputCache = new ValidationStageOutputCache();

            IReport validationErrorsReport = new ValidationErrorsReport(
                logger.Object,
                storage.Object,
                jsonSerializationService,
                ilrProviderService,
                dateTimeProviderMock.Object,
                valueProvider,
                topicsAndTasks,
                validationErrorsServiceMock.Object,
                validationStageOutputCache);

            Mock <IReportServiceContext> reportServiceContextMock = new Mock <IReportServiceContext>();

            reportServiceContextMock.SetupGet(x => x.JobId).Returns(1);
            reportServiceContextMock.SetupGet(x => x.SubmissionDateTimeUtc).Returns(DateTime.UtcNow);
            reportServiceContextMock.SetupGet(x => x.Ukprn).Returns(10000020);
            reportServiceContextMock.SetupGet(x => x.Filename).Returns(@"Reports\Validation\" + Path.GetFileNameWithoutExtension(ilrFilename));
            reportServiceContextMock.SetupGet(x => x.ValidationErrorsKey).Returns("ValidationErrors");
            reportServiceContextMock.SetupGet(x => x.ValidationErrorsLookupsKey).Returns("ValidationErrorsLookup");
            reportServiceContextMock.SetupGet(x => x.ValidLearnRefNumbersCount).Returns(2);
            reportServiceContextMock.SetupGet(x => x.InvalidLearnRefNumbersCount).Returns(3);
            reportServiceContextMock.SetupGet(x => x.CollectionName).Returns("ILR1819");

            await validationErrorsReport.GenerateReport(reportServiceContextMock.Object, null, false, CancellationToken.None);

            json.Should().NotBeNullOrEmpty();
            csv.Should().NotBeNullOrEmpty();
            xlsx.Should().NotBeNullOrEmpty();

#if DEBUG
            File.WriteAllBytes($"{filename}.xlsx", xlsx);
#endif

            ValidationErrorMapper helper = new ValidationErrorMapper();
            TestCsvHelper.CheckCsv(csv, new CsvEntry(helper, 1));
            TestXlsxHelper.CheckXlsx(xlsx, new XlsxEntry(helper, 5));
        }