static IPublishedProviderResultsSettings CreatePublishedProviderResultsSettings()
        {
            IPublishedProviderResultsSettings publishedProviderResultsSettings = Substitute.For <IPublishedProviderResultsSettings>();

            publishedProviderResultsSettings
            .UpdateAllocationLineResultStatusBatchCount
            .Returns(100);

            return(publishedProviderResultsSettings);
        }
        static PublishedResultsService CreateResultsService(ILogger logger       = null,
                                                            IMapper mapper       = null,
                                                            ITelemetry telemetry = null,
                                                            ICalculationResultsRepository resultsRepository    = null,
                                                            ISpecificationsRepository specificationsRepository = null,
                                                            IResultsResiliencePolicies resiliencePolicies      = null,
                                                            IPublishedProviderResultsAssemblerService publishedProviderResultsAssemblerService = null,
                                                            IPublishedProviderResultsRepository publishedProviderResultsRepository             = null,
                                                            ICacheProvider cacheProvider = null,
                                                            ISearchRepository <AllocationNotificationFeedIndex> allocationNotificationFeedSearchRepository = null,
                                                            IProfilingApiClient profilingApiClient = null,
                                                            IMessengerService messengerService     = null,
                                                            IVersionRepository <PublishedAllocationLineResultVersion> publishedProviderResultsVersionRepository    = null,
                                                            IPublishedAllocationLineLogicalResultVersionService publishedAllocationLineLogicalResultVersionService = null,
                                                            IFeatureToggle featureToggle = null,
                                                            IJobsApiClient jobsApiClient = null,
                                                            IPublishedProviderResultsSettings publishedProviderResultsSettings = null,
                                                            IProviderChangesRepository providerChangesRepository = null,
                                                            IProviderVariationsService providerVariationsService = null,
                                                            IProviderVariationsStorageRepository providerVariationsStorageRepository = null)
        {
            ISpecificationsRepository specsRepo = specificationsRepository ?? CreateSpecificationsRepository();

            return(new PublishedResultsService(
                       logger ?? CreateLogger(),
                       mapper ?? CreateMapper(),
                       telemetry ?? CreateTelemetry(),
                       resultsRepository ?? CreateResultsRepository(),
                       specsRepo,
                       resiliencePolicies ?? ResultsResilienceTestHelper.GenerateTestPolicies(),
                       publishedProviderResultsAssemblerService ?? CreateResultsAssembler(),
                       publishedProviderResultsRepository ?? CreatePublishedProviderResultsRepository(),
                       cacheProvider ?? CreateCacheProvider(),
                       allocationNotificationFeedSearchRepository ?? CreateAllocationNotificationFeedSearchRepository(),
                       profilingApiClient ?? CreateProfilingRepository(),
                       messengerService ?? CreateMessengerService(),
                       publishedProviderResultsVersionRepository ?? CreatePublishedProviderResultsVersionRepository(),
                       publishedAllocationLineLogicalResultVersionService ?? CreatePublishedAllocationLineLogicalResultVersionService(),
                       featureToggle ?? CreateFeatureToggle(),
                       jobsApiClient ?? CreateJobsApiClient(),
                       publishedProviderResultsSettings ?? CreatePublishedProviderResultsSettings(),
                       providerChangesRepository ?? CreateProviderChangesRepository(),
                       providerVariationsService ?? CreateProviderVariationsService(CreateProviderVariationAssemblerService(), specsRepo),
                       providerVariationsStorageRepository ?? CreateProviderVariationsStorageRepository()
                       ));
        }
        public void CreateAllocationLineResultStatusUpdateJobs_GivenUpateModelWith10ProvidersAndMaxPartitionSizeOf2ButOnlyThreeJobsCreatedFromFive_ThrowsException()
        {
            //Arrange
            const string cacheKey = "cache-key";

            Message message = new Message();

            message.UserProperties.Add("jobId", jobId);

            ILogger logger = CreateLogger();

            JobViewModel job = new JobViewModel
            {
                Id = jobId,
                SpecificationId        = specificationId,
                InvokerUserDisplayName = "user-name",
                InvokerUserId          = "user-id",
                CorrelationId          = "coorelation-id",
                Properties             = new Dictionary <string, string>
                {
                    { "cache-key", cacheKey }
                },
                JobDefinitionId = JobConstants.DefinitionNames.CreateInstructAllocationLineResultStatusUpdateJob
            };

            UpdatePublishedAllocationLineResultStatusModel updateModel = new UpdatePublishedAllocationLineResultStatusModel
            {
                Status    = AllocationLineStatus.Approved,
                Providers = CreateProviderAllocationLineResults()
            };

            ApiResponse <JobViewModel> apiResponse = new ApiResponse <JobViewModel>(HttpStatusCode.OK, job);

            IJobsApiClient jobsApiClient = CreateJobsApiClient();

            jobsApiClient
            .GetJobById(jobId)
            .Returns(apiResponse);

            IEnumerable <Job> newJobs = new[]
            {
                new Job(),
                new Job(),
                new Job()
            };

            jobsApiClient
            .CreateJobs(Arg.Any <IEnumerable <JobCreateModel> >())
            .Returns(newJobs);

            ICacheProvider cacheProvider = CreateCacheProvider();

            cacheProvider
            .GetAsync <UpdatePublishedAllocationLineResultStatusModel>(Arg.Is(cacheKey))
            .Returns(updateModel);

            IPublishedProviderResultsSettings settings = CreatePublishedProviderResultsSettings();

            settings
            .UpdateAllocationLineResultStatusBatchCount
            .Returns(2);

            PublishedResultsService publishedResultsService = CreateResultsService(logger, jobsApiClient: jobsApiClient, cacheProvider: cacheProvider, publishedProviderResultsSettings: settings);

            //Act
            Func <Task> test = async() => await publishedResultsService.CreateAllocationLineResultStatusUpdateJobs(message);

            //Assert
            test
            .Should()
            .ThrowExactly <Exception>()
            .Which
            .Message
            .Should()
            .Be($"Only 3 jobs were created from 5 childJobs for parent job: '{job.Id}'");

            logger
            .Received(1)
            .Error(Arg.Is($"Only 3 jobs were created from 5 childJobs for parent job: '{job.Id}'"));
        }
        public async Task CreateAllocationLineResultStatusUpdateJobs_GivenUpateModelWith10ProvidersAndMaxPartitionSizeOf2_CreatesFiveChildJobs()
        {
            //Arrange
            const string cacheKey = "cache-key";

            Message message = new Message();

            message.UserProperties.Add("jobId", jobId);

            ILogger logger = CreateLogger();

            JobViewModel job = new JobViewModel
            {
                Id = jobId,
                SpecificationId        = specificationId,
                InvokerUserDisplayName = "user-name",
                InvokerUserId          = "user-id",
                CorrelationId          = "coorelation-id",
                Properties             = new Dictionary <string, string>
                {
                    { "cache-key", cacheKey }
                },
                JobDefinitionId = JobConstants.DefinitionNames.CreateInstructAllocationLineResultStatusUpdateJob
            };

            UpdatePublishedAllocationLineResultStatusModel updateModel = new UpdatePublishedAllocationLineResultStatusModel
            {
                Status    = AllocationLineStatus.Approved,
                Providers = CreateProviderAllocationLineResults()
            };

            ApiResponse <JobViewModel> apiResponse = new ApiResponse <JobViewModel>(HttpStatusCode.OK, job);

            IJobsApiClient jobsApiClient = CreateJobsApiClient();

            jobsApiClient
            .GetJobById(jobId)
            .Returns(apiResponse);

            IEnumerable <Job> newJobs = new[]
            {
                new Job(),
                new Job(),
                new Job(),
                new Job(),
                new Job(),
            };

            jobsApiClient
            .CreateJobs(Arg.Any <IEnumerable <JobCreateModel> >())
            .Returns(newJobs);

            ICacheProvider cacheProvider = CreateCacheProvider();

            cacheProvider
            .GetAsync <UpdatePublishedAllocationLineResultStatusModel>(Arg.Is(cacheKey))
            .Returns(updateModel);

            IPublishedProviderResultsSettings settings = CreatePublishedProviderResultsSettings();

            settings
            .UpdateAllocationLineResultStatusBatchCount
            .Returns(2);

            PublishedResultsService publishedResultsService = CreateResultsService(logger, jobsApiClient: jobsApiClient, cacheProvider: cacheProvider, publishedProviderResultsSettings: settings);

            //Act
            await publishedResultsService.CreateAllocationLineResultStatusUpdateJobs(message);

            //Assert
            await
            jobsApiClient
            .Received(1)
            .CreateJobs(Arg.Is <IEnumerable <JobCreateModel> >(m => m.Count() == 5));

            await
            cacheProvider
            .Received(1)
            .RemoveAsync <UpdatePublishedAllocationLineResultStatusModel>(Arg.Is(cacheKey));
        }