public async Task When_job_succeeded_builds_approval_event_for_removed_price_episode(
            [Frozen] Mock <IApprenticeshipRepository> apprenticeshipRepo,
            ICurrentPriceEpisodeForJobStore currentContext,
            IReceivedDataLockEventStore receivedContext,
            TestCaseData testCaseData,
            PriceEpisodesReceivedService sut,
            CurrentPriceEpisode priceEpisode)
        {
            testCaseData.CommonSetup();
            await testCaseData.AddDataLockEventToContext(receivedContext);

            priceEpisode.AssociateWith(testCaseData.earning);
            await currentContext.Add(priceEpisode);

            var changeMessages = await sut.GetPriceEpisodeChanges(testCaseData.earning.JobId, testCaseData.earning.Ukprn, testCaseData.earning.CollectionYear);

            changeMessages.Should().ContainEquivalentOf(
                new
            {
                DataLock = new
                {
                    priceEpisode.PriceEpisodeIdentifier,
                    Status = PriceEpisodeStatus.Removed,
                },
            });
        }
        public async Task When_job_succeeded_builds_approval_event_for_new_and_removed_price_episodes(
            ICurrentPriceEpisodeForJobStore currentContext,
            IReceivedDataLockEventStore receivedContext,
            PriceEpisodesReceivedService sut,
            TestCaseData testCaseData,
            CurrentPriceEpisode removed)
        {
            testCaseData.CommonSetup();

            await testCaseData.AddDataLockEventToContext(receivedContext);

            removed.AssociateWith(testCaseData.earning);
            await currentContext.Add(removed);

            var changeMessages = await sut.GetPriceEpisodeChanges(testCaseData.earning.JobId, testCaseData.earning.Ukprn, testCaseData.earning.CollectionYear);

            changeMessages.Should().ContainEquivalentOf(
                new
            {
                DataLock = new
                {
                    PriceEpisodeIdentifier = testCaseData.earning.PriceEpisodes[0].Identifier,
                    Status = PriceEpisodeStatus.New,
                },
            });
            changeMessages.Should().ContainEquivalentOf(
                new
            {
                DataLock = new
                {
                    removed.PriceEpisodeIdentifier,
                    Status = PriceEpisodeStatus.Removed,
                },
            });
        }
        public async Task When_job_succeeded_removes_received_dataLock_events(
            IReceivedDataLockEventStore receivedContext,
            PriceEpisodesReceivedService sut,
            TestCaseData testCaseData)
        {
            testCaseData.CommonSetup();

            await testCaseData.AddDataLockEventToContext(receivedContext);

            await sut.GetPriceEpisodeChanges(testCaseData.earning.JobId, testCaseData.earning.Ukprn, testCaseData.earning.CollectionYear);

            (await receivedContext.GetDataLocks(testCaseData.earning.JobId, testCaseData.earning.Ukprn))
            .Should().BeEmpty();
        }
        public async Task When_job_succeeded_builds_approval_event_for_new_and_updated_price_episodes(
            ICurrentPriceEpisodeForJobStore currentContext,
            IReceivedDataLockEventStore receivedContext,
            TestCaseData testCaseData,
            PriceEpisode newPriceEpisode,
            EarningPeriod newPeriod,
            PriceEpisodesReceivedService sut)
        {
            // Given
            testCaseData.earning.PriceEpisodes.Add(newPriceEpisode);

            newPeriod.ApprenticeshipId       = testCaseData.apprenticeship.Id;
            newPeriod.AccountId              = testCaseData.apprenticeship.AccountId;
            newPeriod.PriceEpisodeIdentifier = newPriceEpisode.Identifier;
            testCaseData.earning.OnProgrammeEarnings[0].Periods =
                testCaseData.earning.OnProgrammeEarnings[0].Periods.Append(newPeriod).ToList().AsReadOnly();

            await currentContext.Add(CreateCurrentPriceEpisodeFor(testCaseData.earning));

            await testCaseData.AddDataLockEventToContext(receivedContext);

            // When
            var changeMessages = await sut.GetPriceEpisodeChanges(testCaseData.earning.JobId, testCaseData.earning.Ukprn, testCaseData.earning.CollectionYear);

            // Then
            changeMessages.Should().ContainEquivalentOf(
                new
            {
                DataLock = new
                {
                    PriceEpisodeIdentifier = testCaseData.earning.PriceEpisodes[0].Identifier,
                    Status = PriceEpisodeStatus.Updated,
                },
            });
            changeMessages.Should().ContainEquivalentOf(
                new
            {
                DataLock = new
                {
                    PriceEpisodeIdentifier = newPriceEpisode.Identifier,
                    Status = PriceEpisodeStatus.New,
                },
            });
        }
        public async Task When_job_succeeded_replaces_current_price_episodes(
            [Frozen] Mock <IApprenticeshipRepository> apprenticeshipRepo,
            ICurrentPriceEpisodeForJobStore currentContext,
            IReceivedDataLockEventStore receivedContext,
            TestCaseData testCaseData,
            PriceEpisodesReceivedService sut)
        {
            testCaseData.CommonSetup();

            apprenticeshipRepo
            .Setup(x => x.Get(It.IsAny <List <long> >(), CancellationToken.None))
            .Returns(Task.FromResult(new List <ApprenticeshipModel> {
                testCaseData.apprenticeship
            }));

            await testCaseData.AddDataLockEventToContext(receivedContext);

            await sut.GetPriceEpisodeChanges(testCaseData.earning.JobId, testCaseData.earning.Ukprn, testCaseData.earning.CollectionYear);

            var expected = testCaseData.earning.PriceEpisodes.Select(x => new CurrentPriceEpisode
            {
                JobId = testCaseData.earning.JobId,
                Ukprn = testCaseData.earning.Ukprn,
                Uln   = testCaseData.earning.Learner.Uln,
                PriceEpisodeIdentifier = x.Identifier,
                AgreedPrice            = x.AgreedPrice,
                MessageType            = typeof(List <PriceEpisodeStatusChange>).AssemblyQualifiedName,
                Message = "[]"
            });

            var results = (await currentContext
                           .GetCurrentPriceEpisodes(testCaseData.earning.Ukprn)).ToList();

            results.Should().BeEquivalentTo(expected, c =>
            {
                c.Excluding(info => info.Id);
                c.Excluding(info => info.Message);
                return(c);
            });

            results[0].Message.Should().NotBeNull();
        }
        public async Task When_job_succeeded_builds_approval_event_for_new_price_episode(
            IReceivedDataLockEventStore receivedContext,
            TestCaseData testCaseData,
            PriceEpisodesReceivedService sut)
        {
            testCaseData.CommonSetup();

            await testCaseData.AddDataLockEventToContext(receivedContext);

            var changeMessages = await sut.GetPriceEpisodeChanges(testCaseData.earning.JobId, testCaseData.earning.Ukprn, testCaseData.earning.CollectionYear);

            changeMessages.Should().ContainEquivalentOf(
                new
            {
                DataLock = new
                {
                    PriceEpisodeIdentifier = testCaseData.earning.PriceEpisodes.First().Identifier,
                    Status = PriceEpisodeStatus.New,
                },
            });
        }