Example #1
0
        public async Task TestSummaryOfFM35FundingReportGeneration()
        {
            string   csv      = string.Empty;
            DateTime dateTime = DateTime.UtcNow;
            string   filename = $"10033670_1_Summary of Funding Model 35 Funding Report {dateTime:yyyyMMdd-HHmmss}";

            byte[] xlsx = null;

            Mock <ILogger> logger = new Mock <ILogger>();
            Mock <IStreamableKeyValuePersistenceService> storage = new Mock <IStreamableKeyValuePersistenceService>();
            Mock <IStreamableKeyValuePersistenceService> redis   = new Mock <IStreamableKeyValuePersistenceService>();
            IJsonSerializationService jsonSerializationService   = new JsonSerializationService();
            IXmlSerializationService  xmlSerializationService    = new XmlSerializationService();
            Mock <IDateTimeProvider>  dateTimeProviderMock       = new Mock <IDateTimeProvider>();

            Mock <ILarsProviderService>          larsProviderService          = new Mock <ILarsProviderService>();
            Mock <IOrgProviderService>           orgProviderService           = new Mock <IOrgProviderService>();
            Mock <IPostcodeProviderService>      postcodeProverServiceMock    = new Mock <IPostcodeProviderService>();
            Mock <ILargeEmployerProviderService> largeEmployerProviderService = new Mock <ILargeEmployerProviderService>();

            ITotalBuilder  totalBuilder  = new TotalBuilder();
            IFm35Builder   fm35Builder   = new Fm35Builder(totalBuilder, new CacheProviderService <LearningDelivery[]>());
            IValueProvider valueProvider = new ValueProvider();
            IVersionInfo   versionInfo   = new VersionInfo {
                ServiceReleaseVersion = "1.2.3.4.5"
            };

            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-10033670-1819-20180704-120055-03");
            reportServiceContextMock.SetupGet(x => x.ValidLearnRefNumbersKey).Returns("ValidLearnRefNumbers");
            reportServiceContextMock.SetupGet(x => x.FundingFM35OutputKey).Returns("FundingFm35Output");
            reportServiceContextMock.SetupGet(x => x.CollectionName).Returns("ILR1819");

            redis.Setup(x => x.GetAsync("ValidLearnRefNumbers", It.IsAny <CancellationToken>())).ReturnsAsync(File.ReadAllText("ValidLearnRefNumbers.json"));
            redis.Setup(x => x.GetAsync("FundingFm35Output", It.IsAny <Stream>(), It.IsAny <CancellationToken>())).Callback <string, Stream, CancellationToken>((st, sr, ct) => File.OpenRead("Fm35.json").CopyTo(sr)).Returns(Task.CompletedTask);

            storage.Setup(x => x.GetAsync("ILR-10033670-1819-20180704-120055-03", It.IsAny <Stream>(), It.IsAny <CancellationToken>())).Callback <string, Stream, CancellationToken>((st, sr, ct) => File.OpenRead("ILR-10033670-1819-20180704-120055-03.xml").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}.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.ContainsAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(true);
            redis.Setup(x => x.ContainsAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(true);

            dateTimeProviderMock.Setup(x => x.GetNowUtc()).Returns(dateTime);
            dateTimeProviderMock.Setup(x => x.ConvertUtcToUk(It.IsAny <DateTime>())).Returns(dateTime);
            largeEmployerProviderService.Setup(x => x.GetVersionAsync(It.IsAny <CancellationToken>()))
            .ReturnsAsync("NA");
            postcodeProverServiceMock.Setup(x => x.GetVersionAsync(It.IsAny <CancellationToken>())).ReturnsAsync("NA");
            orgProviderService.Setup(x => x.GetProviderName(It.IsAny <IReportServiceContext>(), It.IsAny <CancellationToken>())).ReturnsAsync("Test Provider");
            larsProviderService.Setup(x => x.GetVersionAsync(It.IsAny <CancellationToken>())).ReturnsAsync("1.2.3.4.5");

            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));
            }

            IIlrProviderService         ilrProviderService         = new IlrFileServiceProvider(logger.Object, storage.Object, xmlSerializationService);
            IIlrMetadataProviderService ilrMetadataProviderService = new IlrMetadataProviderService(dateTimeProviderMock.Object, IlrValidContextFactory, IlrRulebaseContextFactory);

            IFM35ProviderService fm35ProviderService = new FM35ProviderService(logger.Object, redis.Object, jsonSerializationService, null, null);

            var summaryOfFm35FundingReport = new SummaryOfFm35FundingReport(
                logger.Object,
                storage.Object,
                ilrProviderService,
                ilrMetadataProviderService,
                fm35ProviderService,
                dateTimeProviderMock.Object,
                valueProvider,
                versionInfo,
                larsProviderService.Object,
                postcodeProverServiceMock.Object,
                largeEmployerProviderService.Object,
                orgProviderService.Object,
                fm35Builder);

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

            csv.Should().NotBeNullOrEmpty();
            xlsx.Should().NotBeNullOrEmpty();
#if DEBUG
            File.WriteAllBytes($"{filename}.csv", Encoding.ASCII.GetBytes(csv));
            File.WriteAllBytes($"{filename}.xlsx", xlsx);
#endif

            TestCsvHelper.CheckCsv(csv, new CsvEntry(new SummaryOfFM35FundingMapper(), 1));
        }
        public async Task TestSummaryOfFM35FundingReportGeneration()
        {
            string   csv      = string.Empty;
            DateTime dateTime = DateTime.UtcNow;
            string   filename = $"10033670_1_Summary of Funding Model 35 Funding Report {dateTime:yyyyMMdd-HHmmss}";

            Mock <ILogger> logger = new Mock <ILogger>();
            Mock <IStreamableKeyValuePersistenceService> storage = new Mock <IStreamableKeyValuePersistenceService>();
            Mock <IStreamableKeyValuePersistenceService> redis   = new Mock <IStreamableKeyValuePersistenceService>();
            IIntUtilitiesService      intUtilitiesService        = new IntUtilitiesService();
            IJsonSerializationService jsonSerializationService   = new JsonSerializationService();
            IStringUtilitiesService   stringUtilitiesService     = new StringUtilitiesService();
            Mock <IDateTimeProvider>  dateTimeProviderMock       = new Mock <IDateTimeProvider>();
            ITotalBuilder             totalBuilder = new TotalBuilder();
            IFm35Builder   fm35Builder             = new Fm35Builder(totalBuilder, new CacheProviderService <LearningDelivery[]>());
            IValueProvider valueProvider           = new ValueProvider();

            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-10033670-1819-20180704-120055-03");
            reportServiceContextMock.SetupGet(x => x.ValidLearnRefNumbersKey).Returns("ValidLearnRefNumbers");
            reportServiceContextMock.SetupGet(x => x.FundingFM35OutputKey).Returns("FundingFm35Output");

            redis.Setup(x => x.ContainsAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(true);
            redis.Setup(x => x.GetAsync("ValidLearnRefNumbers", It.IsAny <CancellationToken>())).ReturnsAsync(File.ReadAllText("ValidLearnRefNumbers.json"));
            redis.Setup(x => x.GetAsync("FundingFm35Output", It.IsAny <Stream>(), It.IsAny <CancellationToken>())).Callback <string, Stream, CancellationToken>((st, sr, ct) => File.OpenRead("Fm35.json").CopyTo(sr)).Returns(Task.CompletedTask);

            storage.Setup(x => x.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).ReturnsAsync(File.ReadAllText("ILR-10033670-1819-20180704-120055-03.xml"));
            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);

            dateTimeProviderMock.Setup(x => x.GetNowUtc()).Returns(dateTime);
            dateTimeProviderMock.Setup(x => x.ConvertUtcToUk(It.IsAny <DateTime>())).Returns(dateTime);
            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));
            }

            IFM35ProviderService        fm35ProviderService = new FM35ProviderService(logger.Object, redis.Object, jsonSerializationService, intUtilitiesService, IlrValidContextFactory, IlrRulebaseContextFactory);
            ITopicAndTaskSectionOptions topicsAndTasks      = TestConfigurationHelper.GetTopicsAndTasks();

            var summaryOfFm35FundingReport = new SummaryOfFm35FundingReport(
                logger.Object,
                storage.Object,
                fm35ProviderService,
                stringUtilitiesService,
                dateTimeProviderMock.Object,
                valueProvider,
                topicsAndTasks,
                fm35Builder);

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

            csv.Should().NotBeNullOrEmpty();

            TestCsvHelper.CheckCsv(csv, new CsvEntry(new SummaryOfFM35FundingMapper(), 1));
        }