public async Task <JobBatch> CreateJobBatchAsync(CreateJobBatchRequest createJobBatchRequest)
        {
            if (createJobBatchRequest == null)
            {
                throw new ArgumentNullException(nameof(createJobBatchRequest));
            }

            _jobBatchServiceLogger.LogInformation(
                BlueshiftJobsResources.CreatingJobBatch,
                createJobBatchRequest.JobBatchRequestor,
                createJobBatchRequest.JobBatchDescription,
                createJobBatchRequest.BatchingJobFilter.MaximumItems);

            try
            {
                IReadOnlyCollection <Job> jobs = await _jobRepository
                                                 .GetJobsAsync(createJobBatchRequest.BatchingJobFilter)
                                                 .ConfigureAwait(false);

                var jobBatch = new JobBatch
                {
                    JobBatchOwnerId     = createJobBatchRequest.JobBatchRequestor,
                    JobBatchDescription = createJobBatchRequest.JobBatchDescription
                }
                .WithJobs(jobs);

                jobBatch = await _jobBatchRepository
                           .CreateJobBatchAsync(jobBatch)
                           .ConfigureAwait(false);

                _jobBatchServiceLogger.LogInformation(
                    BlueshiftJobsResources.CreatedJobBatch,
                    jobBatch.JobBatchOwnerId,
                    jobBatch.JobBatchDescription,
                    jobBatch.Jobs.Count);

                return(jobBatch);
            }
            catch (Exception e)
            {
                _jobBatchServiceLogger.LogError(
                    BlueshiftJobsResources.ErrorCreatingJobBatch,
                    _jobBatchServiceLogger.IsEnabled(LogLevel.Debug)
                        ? e.StackTrace
                        : e.Message);

                throw;
            }
        }
        public async Task CreateJobBatchAsync_requests_the_expected_number_of_jobs_and_saves_the_batch(int requestedJobCount)
        {
            _testJobBatchServiceFactory.MockJobRepository
            .Setup(jobRepository => jobRepository.GetJobsAsync(It.IsAny <JobSearchCriteria>()))
            .ReturnsAsync((JobSearchCriteria jobSearchCriteria) =>
                          _testJobBatchServiceFactory.AvailableJobs
                          .Take(jobSearchCriteria.MaximumItems.Value)
                          .ToList()
                          .AsReadOnly()
                          )
            .Verifiable();

            _testJobBatchServiceFactory.MockJobBatchRepository
            .Setup(jobBatchRepository => jobBatchRepository.CreateJobBatchAsync(It.IsAny <JobBatch>()))
            .ReturnsAsync((JobBatch batch) => batch)
            .Verifiable();

            JobBatchService jobBatchService = _testJobBatchServiceFactory.CreateJobBatchService();

            var createJobBatchRequest = new CreateJobBatchRequest()
            {
                BatchingJobFilter =
                {
                    MaximumItems = requestedJobCount
                }
            };

            JobBatch jobBatch = await jobBatchService.CreateJobBatchAsync(createJobBatchRequest);

            Assert.NotNull(jobBatch);

            Assert.Equal(
                _testJobBatchServiceFactory.AvailableJobs.Take(requestedJobCount),
                jobBatch.Jobs);

            _testJobBatchServiceFactory.MockJobRepository
            .Verify(
                jobRepository => jobRepository.GetJobsAsync(
                    It.Is <JobSearchCriteria>(jobSearchCriteria => jobSearchCriteria.MaximumItems == requestedJobCount)),
                Times.Once);

            _testJobBatchServiceFactory.MockJobBatchRepository
            .Verify(
                jobBatchRepository => jobBatchRepository.CreateJobBatchAsync(jobBatch),
                Times.Once);
        }