public async Task UpdatePublishedAllocationLineResultsStatus_GivenUpdateModelWithNoProviders_ReturnsBadRequest() { //arrange IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(specificationId) }, }); UpdatePublishedAllocationLineResultStatusModel model = new UpdatePublishedAllocationLineResultStatusModel(); string json = JsonConvert.SerializeObject(model); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpRequest request = Substitute.For <HttpRequest>(); request .Query .Returns(queryStringValues); request .Body .Returns(stream); ILogger logger = CreateLogger(); PublishedResultsService resultsService = CreateResultsService(logger); //Act IActionResult actionResult = await resultsService.UpdatePublishedAllocationLineResultsStatus(request); //Arrange actionResult .Should() .BeOfType <BadRequestObjectResult>() .Which .Value .Should() .Be("Null or empty providers was provided"); logger .Received(1) .Error("Null or empty providers was provided to UpdateAllocationLineResultStatus"); }
public async Task UpdatePublishedAllocationLineResultsStatus_GivenBatchingButNoUpdateModel_ReturnsBadRequest() { //arrange IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(specificationId) }, }); IEnumerable <UpdatePublishedAllocationLineResultStatusProviderModel> Providers = new[] { new UpdatePublishedAllocationLineResultStatusProviderModel { ProviderId = "1111", AllocationLineIds = new[] { "AAAAA" } }, new UpdatePublishedAllocationLineResultStatusProviderModel { ProviderId = "1111-1", AllocationLineIds = new[] { "AAAAA" } }, new UpdatePublishedAllocationLineResultStatusProviderModel { ProviderId = "1111-2", AllocationLineIds = new[] { "AAAAA" } } }; UpdatePublishedAllocationLineResultStatusModel model = new UpdatePublishedAllocationLineResultStatusModel { Providers = Providers, Status = AllocationLineStatus.Approved }; string json = JsonConvert.SerializeObject(model); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpRequest request = Substitute.For <HttpRequest>(); request .Query .Returns(queryStringValues); request .Body .Returns(stream); ILogger logger = CreateLogger(); Job newJob = new Job { Id = "new-job-id" }; IEnumerable <PublishedProviderResult> publishedProviderResults = CreatePublishedProviderResultsWithDifferentProviders(); foreach (PublishedProviderResult publishedProviderResult in publishedProviderResults) { publishedProviderResult.FundingStreamResult.AllocationLineResult.Current.ProfilingPeriods = new[] { new ProfilingPeriod() }; } IJobsApiClient jobsApiClient = CreateJobsApiClient(); jobsApiClient .CreateJob(Arg.Any <JobCreateModel>()) .Returns(newJob); ICacheProvider cacheProvider = CreateCacheProvider(); PublishedResultsService resultsService = CreateResultsService(logger, jobsApiClient: jobsApiClient, cacheProvider: cacheProvider); //Act IActionResult actionResult = await resultsService.UpdatePublishedAllocationLineResultsStatus(request); //Arrange actionResult .Should() .BeAssignableTo <OkResult>(); logger .Received(1) .Information(Arg.Is($"New job: '{JobConstants.DefinitionNames.CreateInstructAllocationLineResultStatusUpdateJob}' created with id: '{newJob.Id}'")); await cacheProvider .Received(1) .SetAsync <UpdatePublishedAllocationLineResultStatusModel>(Arg.Any <string>(), Arg.Any <UpdatePublishedAllocationLineResultStatusModel>()); await jobsApiClient .Received(1) .CreateJob(Arg.Is <JobCreateModel>(m => !string.IsNullOrWhiteSpace(m.InvokerUserDisplayName) && !string.IsNullOrWhiteSpace(m.InvokerUserId) && m.JobDefinitionId == JobConstants.DefinitionNames.CreateInstructAllocationLineResultStatusUpdateJob && m.SpecificationId == specificationId && m.Properties["specification-id"] == specificationId && !string.IsNullOrWhiteSpace(m.Properties["cache-key"]) && m.Trigger.EntityId == specificationId && m.Trigger.EntityType == "Specification" && m.Trigger.Message == $"Updating allocation line results status" )); }
public async Task GetConfirmationDetailsForApprovePublishProviderResults_GivenPublishedResultsReturnsSingluar_ReturnsOkAndResult() { // Arrange IEnumerable <UpdatePublishedAllocationLineResultStatusProviderModel> Providers = new[] { new UpdatePublishedAllocationLineResultStatusProviderModel { ProviderId = "1" } }; IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(specificationId) }, }); UpdatePublishedAllocationLineResultStatusModel model = new UpdatePublishedAllocationLineResultStatusModel { Status = AllocationLineStatus.Held, Providers = Providers }; IEnumerable <PublishedProviderResult> publishedProviderResults = new List <PublishedProviderResult> { new PublishedProviderResult { FundingPeriod = new Models.Specs.Period { Name = "Period1" }, FundingStreamResult = new PublishedFundingStreamResult { FundingStream = new PublishedFundingStreamDefinition { Name = "Stream1" }, AllocationLineResult = new PublishedAllocationLineResult { AllocationLine = new PublishedAllocationLineDefinition { Name = "AllocLine1" }, Current = new PublishedAllocationLineResultVersion { Value = 12, Provider = new ProviderSummary { Id = "1", Authority = "Auth1", ProviderType = "PType1" } } } } } }; string json = JsonConvert.SerializeObject(model); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpRequest request = Substitute.For <HttpRequest>(); request .Query .Returns(queryStringValues); request .Body .Returns(stream); IPublishedProviderResultsRepository resultsProviderRepository = CreatePublishedProviderResultsRepository(); resultsProviderRepository .GetPublishedProviderResultsForSpecificationAndStatus(Arg.Is(specificationId), Arg.Any <UpdatePublishedAllocationLineResultStatusModel>()) .Returns(publishedProviderResults); PublishedResultsService resultsService = CreateResultsService(publishedProviderResultsRepository: resultsProviderRepository); // Act IActionResult actionResult = await resultsService.GetConfirmationDetailsForApprovePublishProviderResults(request); // Assert AssertConfirmPublishApproveModel(actionResult, publishedProviderResults.Count(), 1, 1, "Period1", 1, 12); }
public async Task GetConfirmationDetailsForApprovePublishProviderResults_GivenPublishedResultsReturnsMultiple_ReturnsOkAndAllocationLinesCoalesced() { // Arrange IEnumerable <UpdatePublishedAllocationLineResultStatusProviderModel> Providers = new[] { new UpdatePublishedAllocationLineResultStatusProviderModel { ProviderId = "1" }, new UpdatePublishedAllocationLineResultStatusProviderModel { ProviderId = "2" } }; IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(specificationId) }, }); UpdatePublishedAllocationLineResultStatusModel model = new UpdatePublishedAllocationLineResultStatusModel { Status = AllocationLineStatus.Held, Providers = Providers }; IEnumerable <PublishedProviderResult> publishedProviderResults = new List <PublishedProviderResult> { new PublishedProviderResult { FundingPeriod = new Models.Specs.Period { Name = "Period1" }, FundingStreamResult = new PublishedFundingStreamResult { FundingStream = new PublishedFundingStreamDefinition { Name = "Stream1" }, AllocationLineResult = new PublishedAllocationLineResult { AllocationLine = new PublishedAllocationLineDefinition { Name = "AllocLine1" }, Current = new PublishedAllocationLineResultVersion { Value = 12, Provider = new ProviderSummary { Id = "1", Authority = "B Auth", ProviderType = "B PType" } } } } }, new PublishedProviderResult { FundingPeriod = new Models.Specs.Period { Name = "Period1" }, FundingStreamResult = new PublishedFundingStreamResult { FundingStream = new PublishedFundingStreamDefinition { Name = "Stream1" }, AllocationLineResult = new PublishedAllocationLineResult { AllocationLine = new PublishedAllocationLineDefinition { Name = "AllocLine1" }, Current = new PublishedAllocationLineResultVersion { Value = 15, Provider = new ProviderSummary { Id = "2", Authority = "A Auth", ProviderType = "A PType" } } } } } }; string json = JsonConvert.SerializeObject(model); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpRequest request = Substitute.For <HttpRequest>(); request .Query .Returns(queryStringValues); request .Body .Returns(stream); IPublishedProviderResultsRepository resultsProviderRepository = CreatePublishedProviderResultsRepository(); resultsProviderRepository .GetPublishedProviderResultsForSpecificationAndStatus(Arg.Is(specificationId), Arg.Any <UpdatePublishedAllocationLineResultStatusModel>()) .Returns(publishedProviderResults); PublishedResultsService resultsService = CreateResultsService(publishedProviderResultsRepository: resultsProviderRepository); // Act IActionResult actionResult = await resultsService.GetConfirmationDetailsForApprovePublishProviderResults(request); // Assert OkObjectResult okResult = actionResult.Should().BeOfType <OkObjectResult>().Subject; ConfirmPublishApproveModel confDetalis = okResult.Value.Should().BeAssignableTo <ConfirmPublishApproveModel>().Subject; confDetalis.FundingStreams.Should().HaveCount(1, "Funding Stream"); confDetalis.FundingStreams.First().AllocationLines.Should().HaveCount(1, "Allocation Lines"); }
public async Task GetConfirmationDetailsForApprovePublishProviderResults_GivenNoPublishedResultsReturns_ReturnsZeroResults() { //arrange IEnumerable <UpdatePublishedAllocationLineResultStatusProviderModel> Providers = new[] { new UpdatePublishedAllocationLineResultStatusProviderModel { ProviderId = "1234" } }; IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(specificationId) }, }); UpdatePublishedAllocationLineResultStatusModel model = new UpdatePublishedAllocationLineResultStatusModel { Providers = Providers }; string json = JsonConvert.SerializeObject(model); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpRequest request = Substitute.For <HttpRequest>(); request .Query .Returns(queryStringValues); request .Body .Returns(stream); IPublishedProviderResultsRepository resultsProviderRepository = CreatePublishedProviderResultsRepository(); resultsProviderRepository .GetPublishedProviderResultsForSpecificationId(Arg.Is(specificationId)) .Returns(Enumerable.Empty <PublishedProviderResult>()); PublishedResultsService resultsService = CreateResultsService(publishedProviderResultsRepository: resultsProviderRepository); //Act IActionResult actionResult = await resultsService.GetConfirmationDetailsForApprovePublishProviderResults(request); //Arrange OkObjectResult okResult = actionResult .Should() .BeOfType <OkObjectResult>() .Subject; ConfirmPublishApproveModel x = okResult.Value.Should().BeAssignableTo <ConfirmPublishApproveModel>().Subject; x.NumberOfProviders.Should().Be(0, nameof(x.NumberOfProviders)); x.LocalAuthorities.Should().HaveCount(0, nameof(x.LocalAuthorities)); x.ProviderTypes.Should().HaveCount(0, nameof(x.ProviderTypes)); x.FundingPeriod.Should().BeNullOrEmpty("FundingPeriod should be blank"); x.FundingStreams.Should().BeEmpty(nameof(x.FundingStreams)); x.TotalFundingApproved.Should().Be(0, "TotalFundingApproved should be zero as there are no results"); }
public Task <IEnumerable <PublishedProviderResult> > GetPublishedProviderResultsForSpecificationAndStatus(string specificationId, UpdatePublishedAllocationLineResultStatusModel filterCriteria) { IQueryable <PublishedProviderResult> results = _cosmosRepository.Query <PublishedProviderResult>(enableCrossPartitionQuery: true).Where(m => m.SpecificationId == specificationId && m.FundingStreamResult.AllocationLineResult.Current.Status == filterCriteria.Status); ExpressionStarter <PublishedProviderResult> providerPredicate = PredicateBuilder.New <PublishedProviderResult>(false); foreach (UpdatePublishedAllocationLineResultStatusProviderModel provider in filterCriteria.Providers) { string providerId = provider.ProviderId; providerPredicate = providerPredicate.Or(p => p.ProviderId == providerId); ExpressionStarter <PublishedProviderResult> allocationLinePredicate = PredicateBuilder.New <PublishedProviderResult>(false); foreach (string allocationLineId in provider.AllocationLineIds) { string temp = allocationLineId; allocationLinePredicate = allocationLinePredicate.Or(a => a.FundingStreamResult.AllocationLineResult.AllocationLine.Id == temp); } providerPredicate = providerPredicate.And(allocationLinePredicate); } results = results.AsExpandable().Where(providerPredicate); List <PublishedProviderResult> result = new List <PublishedProviderResult>(results); return(Task.FromResult(result.AsEnumerable())); }
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)); }
public async Task CreateAllocationLineResultStatusUpdateJobs_GivenUpateModelWith10ProvidersAndDefaultMaxPartitionSize_CreatesOneChildJob() { //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() }; jobsApiClient .CreateJobs(Arg.Any <IEnumerable <JobCreateModel> >()) .Returns(newJobs); ICacheProvider cacheProvider = CreateCacheProvider(); cacheProvider .GetAsync <UpdatePublishedAllocationLineResultStatusModel>(Arg.Is(cacheKey)) .Returns(updateModel); PublishedResultsService publishedResultsService = CreateResultsService(logger, jobsApiClient: jobsApiClient, cacheProvider: cacheProvider); //Act await publishedResultsService.CreateAllocationLineResultStatusUpdateJobs(message); //Assert await jobsApiClient .Received(1) .CreateJobs(Arg.Is <IEnumerable <JobCreateModel> >( m => m.First().Trigger.EntityId == jobId && m.First().Trigger.EntityType == "Job" && m.First().Trigger.Message == $"Triggered by parent job" && m.First().SpecificationId == specificationId && m.First().ParentJobId == jobId && m.First().InvokerUserId == job.InvokerUserId && m.First().InvokerUserDisplayName == job.InvokerUserDisplayName && m.First().CorrelationId == job.CorrelationId && !string.IsNullOrWhiteSpace(m.First().MessageBody))); await cacheProvider .Received(1) .RemoveAsync <UpdatePublishedAllocationLineResultStatusModel>(Arg.Is(cacheKey)); }