public async Task LearnerDataLockForEarningWithNoIncentivesShouldReturnValidNonPayableEarningEvent()
        {
            earningEvent.IncentiveEarnings = new List <IncentiveEarning>();

            learnerMatcherMock
            .Setup(x => x.MatchLearner(apprenticeships[0].Ukprn, apprenticeships[0].Uln))
            .ReturnsAsync(() => new LearnerMatchResult
            {
                DataLockErrorCode = DataLockErrorCode.DLOCK_01,
                Apprenticeships   = new List <ApprenticeshipModel>(apprenticeships)
            }).Verifiable();

            var dataLockProcessor = new DataLockProcessor(mapper, learnerMatcherMock.Object, onProgValidationMock.Object);
            var dataLockEvents    = await dataLockProcessor.GetPaymentEvents(earningEvent, default(CancellationToken));

            dataLockEvents.Should().NotBeNull();
            dataLockEvents.Should().HaveCount(1);
            var nonPayableEarningEvent = dataLockEvents[0] as EarningFailedDataLockMatching;

            nonPayableEarningEvent.Should().NotBeNull();
            nonPayableEarningEvent.OnProgrammeEarnings
            .SelectMany(x => x.Periods)
            .All(p => p.DataLockFailures.All(d => d.DataLockError == DataLockErrorCode.DLOCK_01))
            .Should().BeTrue();
        }
        public async Task GivenNoDataLockErrorAllEarningPeriodsShouldBePayableEvent()
        {
            learnerMatcherMock
            .Setup(x => x.MatchLearner(apprenticeships[0].Ukprn, apprenticeships[0].Uln))
            .ReturnsAsync(() => new LearnerMatchResult
            {
                DataLockErrorCode = null,
                Apprenticeships   = apprenticeships
            })
            .Verifiable();

            onProgValidationMock
            .Setup(x => x.ValidatePeriods(Ukprn, apprenticeships[0].Uln,
                                          earningEvent.PriceEpisodes,
                                          It.IsAny <List <EarningPeriod> >(),
                                          (TransactionType)earningEvent.OnProgrammeEarnings[0].Type,
                                          apprenticeships,
                                          aim,
                                          AcademicYear))
            .Returns(() =>
                     (new List <EarningPeriod> {
                earningEvent.OnProgrammeEarnings.FirstOrDefault()?.Periods.FirstOrDefault()
            },
                      new List <EarningPeriod>()
                     )).Verifiable();

            var dataLockProcessor = new DataLockProcessor(mapper, learnerMatcherMock.Object, onProgValidationMock.Object);
            var dataLockEvents    = await dataLockProcessor.GetPaymentEvents(earningEvent, default(CancellationToken));

            dataLockEvents.Should().NotBeNull();
            dataLockEvents.Should().HaveCount(1);

            var payableEarning = dataLockEvents[0] as PayableEarningEvent;

            payableEarning.Should().NotBeNull();
            payableEarning.OnProgrammeEarnings.Count.Should().Be(1);
            payableEarning.OnProgrammeEarnings.First().Periods.Count.Should().Be(1);

            payableEarning.IncentiveEarnings.Should().NotBeNull();
            payableEarning.IncentiveEarnings.Count.Should().Be(1);
            payableEarning.IncentiveEarnings.First().Periods.Count.Should().Be(1);
        }
        public async Task CourseValidationDataLockForEarningWithIncentivesMapBothValidAndInvalidIncentivesAndOnprogEarningPeriods()
        {
            learnerMatcherMock
            .Setup(x => x.MatchLearner(apprenticeships[0].Ukprn, apprenticeships[0].Uln))
            .ReturnsAsync(() => new LearnerMatchResult
            {
                DataLockErrorCode = null,
                Apprenticeships   = new List <ApprenticeshipModel>(apprenticeships)
            }).Verifiable();

            var testEarningEvent = CreateTestEarningEvent(2, 100m, aim);

            var periodExpected = testEarningEvent.OnProgrammeEarnings[0].Periods[1];

            periodExpected.DataLockFailures = new List <DataLockFailure>
            {
                new DataLockFailure
                {
                    DataLockError = DataLockErrorCode.DLOCK_09,
                    ApprenticeshipPriceEpisodeIds = new List <long>()
                }
            };

            onProgValidationMock
            .Setup(x => x.ValidatePeriods(Ukprn, apprenticeships[0].Uln,
                                          It.IsAny <List <PriceEpisode> >(),
                                          It.IsAny <List <EarningPeriod> >(),
                                          It.IsAny <TransactionType>(),
                                          It.IsAny <List <ApprenticeshipModel> >(),
                                          aim,
                                          AcademicYear))
            .Returns(() => (new List <EarningPeriod>
            {
                testEarningEvent.OnProgrammeEarnings[0].Periods[0]
            }, new List <EarningPeriod>
            {
                periodExpected
            }))
            .Verifiable();

            var dataLockProcessor    = new DataLockProcessor(mapper, learnerMatcherMock.Object, onProgValidationMock.Object);
            var actualDataLockEvents = await dataLockProcessor.GetPaymentEvents(testEarningEvent, default(CancellationToken))
                                       .ConfigureAwait(false);

            var payableEarnings = actualDataLockEvents.OfType <PayableEarningEvent>().ToList();

            payableEarnings.Should().HaveCount(1);
            var payableEarning = payableEarnings[0];

            //validate OnProgrammeEarnings
            payableEarning.OnProgrammeEarnings.Count.Should().Be(1);

            var onProgrammeEarning = payableEarning.OnProgrammeEarnings.First();

            onProgrammeEarning.Periods.Count.Should().Be(1);

            var earningPeriod = onProgrammeEarning.Periods.Single();

            earningPeriod.Period.Should().Be(1);


            var failedDatalockEarnings = actualDataLockEvents.OfType <EarningFailedDataLockMatching>().ToList();

            failedDatalockEarnings.Should().HaveCount(1);

            var failedDatalockEarning = failedDatalockEarnings[0];

            failedDatalockEarning.OnProgrammeEarnings.Count.Should().Be(1);

            var invalidOnProgrammeEarning = failedDatalockEarning.OnProgrammeEarnings[0];

            invalidOnProgrammeEarning.Periods.Count.Should().Be(1);

            var invalidEarningPeriod = invalidOnProgrammeEarning.Periods.Single();

            invalidEarningPeriod.Period.Should().Be(2);
            invalidEarningPeriod.DataLockFailures.Should().NotBeNull();
            invalidEarningPeriod.DataLockFailures.Should().HaveCount(1);
            invalidEarningPeriod.DataLockFailures[0].DataLockError.Should().Be(DataLockErrorCode.DLOCK_09);

            //Validate Incentives
            payableEarning.IncentiveEarnings.Count.Should().Be(1);
            var incentiveEarnings = payableEarning.IncentiveEarnings.First();

            incentiveEarnings.Periods.Count.Should().Be(1);

            var incentiveEarningPeriod = incentiveEarnings.Periods.Single();

            incentiveEarningPeriod.Period.Should().Be(1);

            failedDatalockEarning.IncentiveEarnings.Count.Should().Be(1);

            var invalidIncentiveEarning = failedDatalockEarning.IncentiveEarnings[0];

            invalidIncentiveEarning.Periods.Count.Should().Be(1);

            var invalidIncentiveEarningPeriod = invalidIncentiveEarning.Periods.Single();

            invalidIncentiveEarningPeriod.Period.Should().Be(2);
            invalidIncentiveEarningPeriod.DataLockFailures.Should().NotBeNull();
            invalidIncentiveEarningPeriod.DataLockFailures.Should().HaveCount(1);
            invalidIncentiveEarningPeriod.DataLockFailures[0].DataLockError.Should().Be(DataLockErrorCode.DLOCK_09);
        }