public async Task UpdateCalculationsForSpecification_GivenModelHasNoChanges_LogsAndReturns() { //Arrange Models.Specs.SpecificationVersionComparisonModel specificationVersionComparison = new Models.Specs.SpecificationVersionComparisonModel() { Current = new Models.Specs.SpecificationVersion { FundingPeriod = new Reference { Id = "fp1" } }, Previous = new Models.Specs.SpecificationVersion { FundingPeriod = new Reference { Id = "fp1" } } }; string json = JsonConvert.SerializeObject(specificationVersionComparison); Message message = new Message(Encoding.UTF8.GetBytes(json)); ILogger logger = CreateLogger(); CalculationService service = CreateCalculationService(logger: logger); //Act await service.UpdateCalculationsForSpecification(message); //Assert logger .Received(1) .Information(Arg.Is("No changes detected")); }
public async Task UpdateTestResultsForSpecification_GivenNoChangesDetected_LogsAndReturns() { //Arrange const string specificationId = "spec-id"; Models.Specs.SpecificationVersionComparisonModel specificationVersionComparison = new Models.Specs.SpecificationVersionComparisonModel() { Id = specificationId, Current = new Models.Specs.SpecificationVersion { Name = "any name" }, Previous = new Models.Specs.SpecificationVersion { Name = "any name" } }; string json = JsonConvert.SerializeObject(specificationVersionComparison); Message message = new Message(Encoding.UTF8.GetBytes(json)); ILogger logger = CreateLogger(); TestResultsService service = CreateTestResultsService(logger: logger); //Act await service.UpdateTestResultsForSpecification(message); //Assert logger .Received(1) .Information(Arg.Is($"No changes detected")); }
public async Task UpdateTestResultsForSpecification_GivenNoResultsFoundInSearch_DoesNotUpdateSearch() { //Arrange const string specificationId = "spec-id"; Models.Specs.SpecificationVersionComparisonModel specificationVersionComparison = new Models.Specs.SpecificationVersionComparisonModel() { Id = specificationId, Current = new Models.Specs.SpecificationVersion { Name = "new name" }, Previous = new Models.Specs.SpecificationVersion { Name = "any name" } }; string json = JsonConvert.SerializeObject(specificationVersionComparison); Message message = new Message(Encoding.UTF8.GetBytes(json)); ILogger logger = CreateLogger(); SearchResults <TestScenarioResultIndex> searchResult = new SearchResults <TestScenarioResultIndex>(); ISearchRepository <TestScenarioResultIndex> searchRepository = CreateSearchRespository(); searchRepository .Search(Arg.Is(""), Arg.Any <SearchParameters>()) .Returns(searchResult); TestResultsService service = CreateTestResultsService(logger: logger, searchRepository: searchRepository); //Act await service.UpdateTestResultsForSpecification(message); //Assert await searchRepository .Received(1) .Search(Arg.Is(""), Arg.Is <SearchParameters>( m => m.Skip == 0 && m.Top == 1000 && m.SearchMode == SearchMode.Any && m.Filter == $"specificationId eq '{specificationVersionComparison.Id}' and specificationName ne '{specificationVersionComparison.Current.Name}'" )); await searchRepository .DidNotReceive() .Index(Arg.Any <IEnumerable <TestScenarioResultIndex> >()); }
public void GetMessageBodyStringFromMessage_GivenCompressedBody_ReturnsJson() { //Arrange Models.Specs.SpecificationVersionComparisonModel specificationVersionComparison = new Models.Specs.SpecificationVersionComparisonModel() { Id = "spec-1", Current = new Models.Specs.SpecificationVersion { FundingPeriod = new Reference { Id = "fp1" }, Name = "any-name", Policies = new[] { new Models.Specs.Policy { Id = "pol-id", Name = "policy2" } } }, Previous = new Models.Specs.SpecificationVersion { FundingPeriod = new Reference { Id = "fp1" }, Policies = new[] { new Models.Specs.Policy { Id = "pol-id", Name = "policy1" } } } }; string json = JsonConvert.SerializeObject(specificationVersionComparison); byte[] messageBytes = json.Compress(); Message message = new Message(messageBytes); message.UserProperties.Add("compressed", true); //Act string result = MessageExtensions.GetMessageBodyStringFromMessage(message); //Assert result .Should() .BeEquivalentTo(json); }
public async Task UpdateCalculationsForSpecification_GivenModelHasChangedFundingPeriodsButCalcculationsCouldNotBeFound_LogsAndReturns() { //Arrange const string specificationId = "spec-id"; Models.Specs.SpecificationVersionComparisonModel specificationVersionComparison = new Models.Specs.SpecificationVersionComparisonModel() { Id = specificationId, Current = new Models.Specs.SpecificationVersion { FundingPeriod = new Reference { Id = "fp2" } }, Previous = new Models.Specs.SpecificationVersion { FundingPeriod = new Reference { Id = "fp1" } } }; string json = JsonConvert.SerializeObject(specificationVersionComparison); Message message = new Message(Encoding.UTF8.GetBytes(json)); ILogger logger = CreateLogger(); ICalculationsRepository calculationsRepository = CreateCalculationsRepository(); calculationsRepository .GetCalculationsBySpecificationId(Arg.Is(specificationId)) .Returns((IEnumerable <Calculation>)null); CalculationService service = CreateCalculationService(calculationsRepository, logger); //Act await service.UpdateCalculationsForSpecification(message); //Assert logger .Received(1) .Information(Arg.Is($"No calculations found for specification id: {specificationId}")); }
public async Task UpdateTestResultsForSpecification_GivenResultsReturnedButIndexeingCausesErrors_LogsErrors() { //Arrange const string specificationId = "spec-id"; Models.Specs.SpecificationVersionComparisonModel specificationVersionComparison = new Models.Specs.SpecificationVersionComparisonModel() { Id = specificationId, Current = new Models.Specs.SpecificationVersion { Name = "new name" }, Previous = new Models.Specs.SpecificationVersion { Name = "any name" } }; string json = JsonConvert.SerializeObject(specificationVersionComparison); Message message = new Message(Encoding.UTF8.GetBytes(json)); ILogger logger = CreateLogger(); SearchResults <TestScenarioResultIndex> searchResult = new SearchResults <TestScenarioResultIndex> { Results = new List <CalculateFunding.Repositories.Common.Search.SearchResult <TestScenarioResultIndex> > { new CalculateFunding.Repositories.Common.Search.SearchResult <TestScenarioResultIndex> { Result = new TestScenarioResultIndex() }, new CalculateFunding.Repositories.Common.Search.SearchResult <TestScenarioResultIndex> { Result = new TestScenarioResultIndex() }, new CalculateFunding.Repositories.Common.Search.SearchResult <TestScenarioResultIndex> { Result = new TestScenarioResultIndex() } } }; IEnumerable <IndexError> indexErrors = new[] { new IndexError { ErrorMessage = "an error" } }; ISearchRepository <TestScenarioResultIndex> searchRepository = CreateSearchRespository(); searchRepository .Search(Arg.Is(""), Arg.Any <SearchParameters>()) .Returns(searchResult); searchRepository .Index(Arg.Any <IEnumerable <TestScenarioResultIndex> >()) .Returns(indexErrors); TestResultsService service = CreateTestResultsService(logger: logger, searchRepository: searchRepository); //Act await service.UpdateTestResultsForSpecification(message); //Assert await searchRepository .Received(1) .Search(Arg.Is(""), Arg.Is <SearchParameters>( m => m.Skip == 0 && m.Top == 1000 && m.SearchMode == SearchMode.Any && m.Filter == $"specificationId eq '{specificationVersionComparison.Id}' and specificationName ne '{specificationVersionComparison.Current.Name}'" )); await searchRepository .Received(1) .Index(Arg.Is <IEnumerable <TestScenarioResultIndex> >(m => m.Count() == 3)); logger .Received(1) .Error($"The following errors occcurred while updating test results for specification id: {specificationId}, an error"); }
public async Task UpdateCalculationsForSpecification_GivenModelHasChangedPolicyNameAndSourceCodeContainsCalculationAggregate_SavesChangesEnsuresGenerateAggregationsJobCreated() { // Arrange const string specificationId = "spec-id"; Models.Specs.SpecificationVersionComparisonModel specificationVersionComparison = new Models.Specs.SpecificationVersionComparisonModel() { Id = specificationId, Current = new Models.Specs.SpecificationVersion { FundingPeriod = new Reference { Id = "fp1" }, Name = "any-name", Policies = new[] { new Models.Specs.Policy { Id = "pol-id", Name = "policy2" } } }, Previous = new Models.Specs.SpecificationVersion { FundingPeriod = new Reference { Id = "fp1" }, Policies = new[] { new Models.Specs.Policy { Id = "pol-id", Name = "policy1" } } } }; string json = JsonConvert.SerializeObject(specificationVersionComparison); Message message = new Message(Encoding.UTF8.GetBytes(json)); message.UserProperties.Add("user-id", UserId); message.UserProperties.Add("user-name", Username); ILogger logger = CreateLogger(); IEnumerable <Calculation> calcs = new[] { new Calculation { SpecificationId = "spec-id", Name = "any name", Id = "any-id", CalculationSpecification = new Reference("any name", "any-id"), FundingPeriod = new Reference("18/19", "2018/2019"), CalculationType = CalculationType.Number, FundingStream = new Reference("fp1", "fs1-111"), Current = new CalculationVersion { Author = new Reference(UserId, Username), Date = DateTimeOffset.Now, PublishStatus = PublishStatus.Draft, SourceCode = "return Min(calc1)", Version = 1 }, Policies = new List <Reference> { new Reference { Id = "pol-id", Name = "policy1" } } } }; BuildProject buildProject = new BuildProject { Id = "build-project-1", SpecificationId = specificationId }; ICalculationsRepository calculationsRepository = CreateCalculationsRepository(); calculationsRepository .GetCalculationsBySpecificationId(Arg.Is(specificationId)) .Returns(calcs); IBuildProjectsService buildProjectsService = CreateBuildProjectsService(); buildProjectsService .GetBuildProjectForSpecificationId(Arg.Is(specificationId)) .Returns(buildProject); ISearchRepository <CalculationIndex> searchRepository = CreateSearchRepository(); IJobsApiClient jobsApiClient = CreateJobsApiClient(); jobsApiClient .CreateJob(Arg.Any <JobCreateModel>()) .Returns(new Job { Id = "job-id-1", JobDefinitionId = JobConstants.DefinitionNames.CreateInstructGenerateAggregationsAllocationJob }); CalculationService service = CreateCalculationService( calculationsRepository, logger, buildProjectsService: buildProjectsService, searchRepository: searchRepository, jobsApiClient: jobsApiClient); // Act await service.UpdateCalculationsForSpecification(message); // Assert await jobsApiClient .Received(1) .CreateJob(Arg.Is <JobCreateModel>( m => m.InvokerUserDisplayName == Username && m.InvokerUserId == UserId && m.JobDefinitionId == JobConstants.DefinitionNames.CreateInstructGenerateAggregationsAllocationJob && m.Properties["specification-id"] == specificationId && m.Trigger.EntityId == specificationId && m.Trigger.EntityType == nameof(Models.Specs.Specification) && m.Trigger.Message == $"Updating calculations for specification: '{specificationId}'" )); logger .Received(1) .Information(Arg.Is($"New job of type '{JobConstants.DefinitionNames.CreateInstructGenerateAggregationsAllocationJob}' created with id: 'job-id-1'")); }
public async Task UpdateCalculationsForSpecification_GivenModelHasChangedPolicyNameButCreatingJobReturnsNull_LogsError() { // Arrange const string specificationId = "spec-id"; Models.Specs.SpecificationVersionComparisonModel specificationVersionComparison = new Models.Specs.SpecificationVersionComparisonModel() { Id = specificationId, Current = new Models.Specs.SpecificationVersion { FundingPeriod = new Reference { Id = "fp1" }, Name = "any-name", Policies = new[] { new Models.Specs.Policy { Id = "pol-id", Name = "policy2" } } }, Previous = new Models.Specs.SpecificationVersion { FundingPeriod = new Reference { Id = "fp1" }, Policies = new[] { new Models.Specs.Policy { Id = "pol-id", Name = "policy1" } } } }; string json = JsonConvert.SerializeObject(specificationVersionComparison); Message message = new Message(Encoding.UTF8.GetBytes(json)); message.UserProperties.Add("user-id", UserId); message.UserProperties.Add("user-name", Username); ILogger logger = CreateLogger(); IEnumerable <Calculation> calcs = new[] { new Calculation { SpecificationId = "spec-id", Name = "any name", Id = "any-id", CalculationSpecification = new Reference("any name", "any-id"), FundingPeriod = new Reference("18/19", "2018/2019"), CalculationType = CalculationType.Number, FundingStream = new Reference("fp1", "fs1-111"), Current = new CalculationVersion { Author = new Reference(UserId, Username), Date = DateTimeOffset.Now, PublishStatus = PublishStatus.Draft, SourceCode = "source code", Version = 1 }, Policies = new List <Reference> { new Reference { Id = "pol-id", Name = "policy1" } } } }; BuildProject buildProject = new BuildProject { Id = "build-project-1", SpecificationId = specificationId }; ICalculationsRepository calculationsRepository = CreateCalculationsRepository(); calculationsRepository .GetCalculationsBySpecificationId(Arg.Is(specificationId)) .Returns(calcs); IBuildProjectsService buildProjectsService = CreateBuildProjectsService(); buildProjectsService .GetBuildProjectForSpecificationId(Arg.Is(specificationId)) .Returns(buildProject); ISearchRepository <CalculationIndex> searchRepository = CreateSearchRepository(); IJobsApiClient jobsApiClient = CreateJobsApiClient(); jobsApiClient .CreateJob(Arg.Any <JobCreateModel>()) .Returns((Job)null); CalculationService service = CreateCalculationService( calculationsRepository, logger, buildProjectsService: buildProjectsService, searchRepository: searchRepository, jobsApiClient: jobsApiClient); // Act Func <Task> test = async() => await service.UpdateCalculationsForSpecification(message); // Assert test .Should() .ThrowExactly <RetriableException>() .Which .Message .Should() .Be($"Failed to create job: '{JobConstants.DefinitionNames.CreateInstructAllocationJob} for specification id '{specificationId}'"); await jobsApiClient .Received(1) .CreateJob(Arg.Is <JobCreateModel>( m => m.InvokerUserDisplayName == Username && m.InvokerUserId == UserId && m.JobDefinitionId == JobConstants.DefinitionNames.CreateInstructAllocationJob && m.Properties["specification-id"] == specificationId && m.Trigger.EntityId == specificationId && m.Trigger.EntityType == nameof(Models.Specs.Specification) && m.Trigger.Message == $"Updating calculations for specification: '{specificationId}'" )); logger .Received(1) .Error(Arg.Is($"Failed to create job: '{JobConstants.DefinitionNames.CreateInstructAllocationJob} for specification id '{specificationId}'")); }
public async Task UpdateCalculationsForSpecification_GivenModelHasChangedFundingStreams_SetsTheAllocationLineAndFundingStreamToNull() { //Arrange const string specificationId = "spec-id"; Models.Specs.SpecificationVersionComparisonModel specificationVersionComparison = new Models.Specs.SpecificationVersionComparisonModel() { Id = specificationId, Current = new Models.Specs.SpecificationVersion { FundingPeriod = new Reference { Id = "fp1" }, Name = "any-name", FundingStreams = new List <Reference> { new Reference { Id = "fs2" } } }, Previous = new Models.Specs.SpecificationVersion { FundingPeriod = new Reference { Id = "fp1" }, FundingStreams = new List <Reference> { new Reference { Id = "fs1" } } } }; string json = JsonConvert.SerializeObject(specificationVersionComparison); Message message = new Message(Encoding.UTF8.GetBytes(json)); ILogger logger = CreateLogger(); IEnumerable <Calculation> calcs = new[] { new Calculation { SpecificationId = "spec-id", Name = "any name", Id = "any-id", CalculationSpecification = new Reference("any name", "any-id"), FundingPeriod = new Reference("18/19", "2018/2019"), CalculationType = CalculationType.Number, FundingStream = new Reference("fs1", "fs1-111"), Current = new CalculationVersion { Author = new Reference(UserId, Username), Date = DateTimeOffset.Now, PublishStatus = PublishStatus.Draft, SourceCode = "source code", Version = 1 }, Policies = new List <Reference>() } }; BuildProject buildProject = new BuildProject(); ICalculationsRepository calculationsRepository = CreateCalculationsRepository(); calculationsRepository .GetCalculationsBySpecificationId(Arg.Is(specificationId)) .Returns(calcs); IBuildProjectsService buildProjectsService = CreateBuildProjectsService(); buildProjectsService .GetBuildProjectForSpecificationId(Arg.Is(specificationId)) .Returns(buildProject); ISearchRepository <CalculationIndex> searchRepository = CreateSearchRepository(); IJobsApiClient jobsApiClient = CreateJobsApiClient(); jobsApiClient .CreateJob(Arg.Any <JobCreateModel>()) .Returns(new Job { Id = "job-id-1" }); CalculationService service = CreateCalculationService(calculationsRepository, logger, buildProjectsService: buildProjectsService, searchRepository: searchRepository, jobsApiClient: jobsApiClient); //Act await service.UpdateCalculationsForSpecification(message); //Assert calcs .First() .FundingStream .Should() .BeNull(); calcs .First() .AllocationLine .Should() .BeNull(); await searchRepository .Received(1) .Index(Arg.Is <IEnumerable <CalculationIndex> >(c => c.First().Id == calcs.First().Id&& c.First().FundingStreamId == "" && c.First().FundingStreamName == "No funding stream set")); }