public void BuildPaymentInfoDictionary_MixedCaseLearnRefNumber()
        {
            var paymentInfo = new AppsCoInvestmentPaymentsInfo()
            {
                Payments = new List <PaymentInfo>()
                {
                    new PaymentInfo()
                    {
                        LearnerReferenceNumber = "MiXeD",
                    },
                    new PaymentInfo()
                    {
                        LearnerReferenceNumber = "mixed",
                    },
                    new PaymentInfo()
                    {
                        LearnerReferenceNumber = "MIXED",
                    }
                }
            };

            var builder = new AppsCoInvestmentContributionsModelBuilder(new AppsCoInvestmentRecordKeyEqualityComparer(), null);

            var result = builder.BuildPaymentInfoDictionary(paymentInfo);

            var key = new AppsCoInvestmentRecordKey("MIXED", null, 0, 0, 0, 0);

            result.Should().HaveCount(1);

            result.TryGetValue(key, out var check);

            check.Should().NotBeNull();
            check.Count.Should().Be(3);
        }
        public void GetPercentageOfInvestmentCollected_Returns_Zero_For_Zero_Inputs()
        {
            decimal?totalDueCurrentYear        = 0M;
            decimal?totalDuePreviousYear       = 0M;
            decimal?totalCollectedCurrentYear  = 0M;
            decimal?totalCollectedPreviousYear = 0M;

            var equalityComparerMock = new Mock <IEqualityComparer <AppsCoInvestmentRecordKey> >();
            var loggerMock           = new Mock <ILogger>();

            var builder = new AppsCoInvestmentContributionsModelBuilder(equalityComparerMock.Object, loggerMock.Object);
            var percent = builder.GetPercentageOfInvestmentCollected(
                totalDueCurrentYear,
                totalDuePreviousYear,
                totalCollectedCurrentYear,
                totalCollectedPreviousYear);

            percent.Should().Be(0);
        }
        public void GetPercentageOfInvestmentCollected_Returns_Min_For_Huge_Negative_Percent()
        {
            decimal min = -99999.99M;

            decimal?totalDueCurrentYear        = 10M;
            decimal?totalDuePreviousYear       = 10M;
            decimal?totalCollectedCurrentYear  = -100000000M;
            decimal?totalCollectedPreviousYear = 0M;

            var equalityComparerMock = new Mock <IEqualityComparer <AppsCoInvestmentRecordKey> >();
            var loggerMock           = new Mock <ILogger>();

            var builder = new AppsCoInvestmentContributionsModelBuilder(equalityComparerMock.Object, loggerMock.Object);
            var percent = builder.GetPercentageOfInvestmentCollected(
                totalDueCurrentYear,
                totalDuePreviousYear,
                totalCollectedCurrentYear,
                totalCollectedPreviousYear);

            percent.Should().Be(min);
        }
        public void GetLearnerForRecord_Test()
        {
            IDictionary <string, LearnerInfo> learnerDictionary = new Dictionary <string, LearnerInfo>(StringComparer.OrdinalIgnoreCase);

            learnerDictionary.Add("learnref1", new LearnerInfo()
            {
                LearnRefNumber = "learnref1"
            });
            learnerDictionary.Add("LearnRef2", new LearnerInfo()
            {
                LearnRefNumber = "LearnRef2"
            });
            learnerDictionary.Add("LEARNREF3", new LearnerInfo()
            {
                LearnRefNumber = "LEARNREF3"
            });

            var appsCoInvestmentContributionsModelBuilder = new AppsCoInvestmentContributionsModelBuilder(new AppsCoInvestmentRecordKeyEqualityComparer(), null);

            var result = appsCoInvestmentContributionsModelBuilder.GetLearnerForRecord(learnerDictionary, new AppsCoInvestmentRecordKey()
            {
                LearnerReferenceNumber = "LeaRnrEf1"
            });

            result.LearnRefNumber.Should().Be("learnref1");

            result = appsCoInvestmentContributionsModelBuilder.GetLearnerForRecord(learnerDictionary, new AppsCoInvestmentRecordKey()
            {
                LearnerReferenceNumber = "LearnRef2"
            });

            result.LearnRefNumber.Should().Be("LearnRef2");

            result = appsCoInvestmentContributionsModelBuilder.GetLearnerForRecord(learnerDictionary, new AppsCoInvestmentRecordKey()
            {
                LearnerReferenceNumber = "LEARNREF3"
            });

            result.LearnRefNumber.Should().Be("LEARNREF3");
        }
        public void RecordKeysUnion_Test()
        {
            List <string> learnRefNumbers = new List <string>()
            {
                "055300807083", "055300807081"
            };

            List <AppsCoInvestmentRecordKey> appsKeys = new List <AppsCoInvestmentRecordKey>()
            {
                new AppsCoInvestmentRecordKey()
                {
                    LearnerReferenceNumber = "055300807083", LearningAimReference = "ZPROG001", LearningStartDate = null, LearningAimProgrammeType = 3, LearningAimStandardCode = 0, LearningAimFrameworkCode = 462, LearningAimPathwayCode = 1
                },
                new AppsCoInvestmentRecordKey()
                {
                    LearnerReferenceNumber = "055300807083", LearningAimReference = "ZPROG001", LearningStartDate = null, LearningAimProgrammeType = 3, LearningAimStandardCode = 0, LearningAimFrameworkCode = 466, LearningAimPathwayCode = 1
                },
                new AppsCoInvestmentRecordKey()
                {
                    LearnerReferenceNumber = "055300807083", LearningAimReference = "ZPROG001", LearningStartDate = new DateTime(2019, 2, 21), LearningAimProgrammeType = 3, LearningAimStandardCode = 0, LearningAimFrameworkCode = 462, LearningAimPathwayCode = 1
                },
            };

            List <AppsCoInvestmentRecordKey> ilrKeys = new List <AppsCoInvestmentRecordKey>()
            {
                new AppsCoInvestmentRecordKey()
                {
                    LearnerReferenceNumber = "055300807083", LearningAimReference = "ZPROG001", LearningStartDate = new DateTime(2019, 2, 21), LearningAimProgrammeType = 3, LearningAimStandardCode = 0, LearningAimFrameworkCode = 462, LearningAimPathwayCode = 1
                }
            };

            Mock <ILogger> logger = new Mock <ILogger>();
            var            appsCoInvestmentContributionsModelBuilder = new AppsCoInvestmentContributionsModelBuilder(new AppsCoInvestmentRecordKeyEqualityComparer(), logger.Object);

            var result = appsCoInvestmentContributionsModelBuilder.UnionKeys(learnRefNumbers, ilrKeys, appsKeys);

            result.Count().Should().Be(3);
        }
        //[Theory]
        //[InlineData("EMPLOYER2", "EMPLOYER2", "A12345", "ZPROG001", "A12345", "ZPROG001")]
        public async Task TestCoInvestmentContributionsReportGeneration(
            string employerName,
            string employerNameExpected,
            string ilrLearnRefNumber,
            string ilrLearnAimRef,
            string dasLearnRefNumber,
            string dasLearnAimRef)
        {
            string   csv      = string.Empty;
            DateTime dateTime = DateTime.UtcNow;
            string   filename = $"10036143 Apps Co-Investment Contributions Report {dateTime:yyyyMMdd-HHmmss}";
            int      ukPrn    = 10036143;
            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(ukPrn);
            reportServiceContextMock.SetupGet(x => x.ReturnPeriod).Returns(1);

            Mock <ILogger>           logger = new Mock <ILogger>();
            Mock <IDateTimeProvider> dateTimeProviderMock        = new Mock <IDateTimeProvider>();
            Mock <IStreamableKeyValuePersistenceService> storage = new Mock <IStreamableKeyValuePersistenceService>();
            Mock <IIlrPeriodEndProviderService>          ilrPeriodEndProviderServiceMock = new Mock <IIlrPeriodEndProviderService>();
            Mock <IDASPaymentsProviderService>           dasPaymentProviderMock          = new Mock <IDASPaymentsProviderService>();
            Mock <IFM36PeriodEndProviderService>         fm36ProviderServiceMock         = new Mock <IFM36PeriodEndProviderService>();
            Mock <IPersistReportData> persistReportDataMock = new Mock <IPersistReportData>();

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

            var appsCoInvestmentIlrInfo         = BuildILRModel(ukPrn, ilrLearnRefNumber, ilrLearnAimRef, 1);
            var appsCoInvestmentRulebaseInfo    = BuildFm36Model(ukPrn, ilrLearnRefNumber, 1, ilrLearnAimRef);
            var appsCoInvestmentDasPaymentsInfo = BuildDasPaymentsModel(ukPrn, employerName, dasLearnRefNumber, dasLearnAimRef);

            ilrPeriodEndProviderServiceMock.Setup(x => x.GetILRInfoForAppsCoInvestmentReportAsync(It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(appsCoInvestmentIlrInfo);
            fm36ProviderServiceMock.Setup(x => x.GetFM36DataForAppsCoInvestmentReportAsync(It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(appsCoInvestmentRulebaseInfo);
            dasPaymentProviderMock.Setup(x => x.GetPaymentsInfoForAppsCoInvestmentReportAsync(It.IsAny <int>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(appsCoInvestmentDasPaymentsInfo);

            dateTimeProviderMock.Setup(x => x.GetNowUtc()).Returns(dateTime);
            dateTimeProviderMock.Setup(x => x.ConvertUtcToUk(It.IsAny <DateTime>())).Returns(dateTime);
            var appsCoInvestmentContributionsModelBuilder = new AppsCoInvestmentContributionsModelBuilder(new AppsCoInvestmentRecordKeyEqualityComparer(), logger.Object);

            var report = new AppsCoInvestmentContributionsReport(
                logger.Object,
                storage.Object,
                dateTimeProviderMock.Object,
                ilrPeriodEndProviderServiceMock.Object,
                dasPaymentProviderMock.Object,
                fm36ProviderServiceMock.Object,
                appsCoInvestmentContributionsModelBuilder,
                persistReportDataMock.Object);

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

            csv.Should().NotBeNullOrEmpty();
            File.WriteAllText($"{filename}.csv", csv);
            TestCsvHelper.CheckCsv(csv, new CsvEntry(new AppsCoInvestmentContributionsMapper(), 1));
            IEnumerable <AppsCoInvestmentContributionsModel> result;

            using (var csvReader = new CsvReader(new StringReader(csv)))
            {
                csvReader.Configuration.RegisterClassMap <AppsCoInvestmentContributionsMapper>();
                result = csvReader.GetRecords <AppsCoInvestmentContributionsModel>().ToList();
            }

            result.Should().NotBeNullOrEmpty();
            result.Count().Should().Be(1);
            result.First().CoInvestmentDueFromEmployerForAugust.Should().Be(20);
            result.First().CompletionPaymentsThisFundingYear.Should().Be(20);

            result.First().UniqueLearnerNumber.Should().Be(12345);
            result.First().EmployerNameFromApprenticeshipService.Should().Be(employerNameExpected);
        }