public async Task EditSpecification_GivenNullEditModeldWasProvided_ReturnsBadRequest() { //Arrange IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(SpecificationId) }, }); HttpRequest request = Substitute.For <HttpRequest>(); request .Query .Returns(queryStringValues); ILogger logger = CreateLogger(); SpecificationsService service = CreateService(logs: logger); //Act IActionResult result = await service.EditSpecification(request); //Arrange result .Should() .BeOfType <BadRequestObjectResult>(); logger .Received(1) .Error(Arg.Is("No edit modeld was provided to EditSpecification")); }
public async Task EditSpecification_WhenInvalidModelProvided_ThenValidationErrorReturned() { // Arrange ValidationResult validationResult = new ValidationResult(); validationResult.Errors.Add(new ValidationFailure("error", "error")); IValidator <SpecificationEditModel> validator = CreateEditSpecificationValidator(validationResult); SpecificationsService specificationsService = CreateService(specificationEditModelValidator: validator); SpecificationEditModel specificationEditModel = new SpecificationEditModel(); string json = JsonConvert.SerializeObject(specificationEditModel); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpContext context = Substitute.For <HttpContext>(); HttpRequest request = Substitute.For <HttpRequest>(); IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(SpecificationId) }, }); request .Query .Returns(queryStringValues); request .Body .Returns(stream); request .HttpContext .Returns(context); // Act IActionResult result = await specificationsService.EditSpecification(request); // Assert result .Should() .BeOfType <BadRequestObjectResult>() .Which .Value .Should() .BeOfType <SerializableError>() .Which .Should() .HaveCount(1); await validator .Received(1) .ValidateAsync(Arg.Any <SpecificationEditModel>()); }
public async Task EditSpecification_GivenSpecificationWasfoundAndFundingPeriodChangedButFailedToGetFundingPeriodsFromCosmos_ReturnsPreConditionFailedresult() { //Arrange SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10" }; string json = JsonConvert.SerializeObject(specificationEditModel); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpContext context = Substitute.For <HttpContext>(); HttpRequest request = Substitute.For <HttpRequest>(); IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(SpecificationId) }, }); request .Query .Returns(queryStringValues); request .Body .Returns(stream); request .HttpContext .Returns(context); ILogger logger = CreateLogger(); Specification specification = CreateSpecification(); ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository(); specificationsRepository .GetSpecificationById(Arg.Is(SpecificationId)) .Returns(specification); SpecificationsService service = CreateService(logs: logger, specificationsRepository: specificationsRepository); //Act IActionResult result = await service.EditSpecification(request); //Arrange result .Should() .BeOfType <PreconditionFailedResult>() .Which .Value .Should() .Be($"Unable to find funding period with ID '{specificationEditModel.FundingPeriodId}'."); }
public async Task EditSpecification_GivenSpecificationWasNotFound_ReturnsNotFoundResult() { //Arrange SpecificationEditModel specificationEditModel = new SpecificationEditModel(); string json = JsonConvert.SerializeObject(specificationEditModel); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpContext context = Substitute.For <HttpContext>(); HttpRequest request = Substitute.For <HttpRequest>(); IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(SpecificationId) }, }); request .Query .Returns(queryStringValues); request .Body .Returns(stream); request .HttpContext .Returns(context); ILogger logger = CreateLogger(); ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository(); specificationsRepository .GetSpecificationById(Arg.Is(SpecificationId)) .Returns((Specification)null); SpecificationsService service = CreateService(logs: logger, specificationsRepository: specificationsRepository); //Act IActionResult result = await service.EditSpecification(request); //Arrange result .Should() .BeOfType <NotFoundObjectResult>() .Which .Value .Should() .Be("Specification not found"); logger .Received(1) .Warning(Arg.Is($"Failed to find specification for id: {SpecificationId}")); }
public async Task EditSpecification_GivenNullEditModeldWasProvided_ReturnsBadRequest() { SpecificationsService service = CreateService(logs: _logger); IActionResult result = await service.EditSpecification(SpecificationId, null, null, null); result.Should().BeOfType <BadRequestObjectResult>(); _logger.Received(1) .Error(Arg.Is("No edit modeld was provided to EditSpecification")); }
public async Task EditSpecification_GivenChanges_CreatesNewVersionWithTrimmedName(string specificationEditModelName, string resultSpecificationName) { //Arrange SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10", Name = specificationEditModelName, ProviderVersionId = _specification.Current.ProviderVersionId }; Models.Specs.SpecificationVersion newSpecVersion = _specification.Current.Clone() as Models.Specs.SpecificationVersion; newSpecVersion.Name = specificationEditModel.Name.Trim(); newSpecVersion.FundingPeriod.Id = specificationEditModel.FundingPeriodId; newSpecVersion.FundingStreams = new[] { new Reference { Id = "fs11" } }; _versionRepository .CreateVersion(Arg.Any <Models.Specs.SpecificationVersion>(), Arg.Any <Models.Specs.SpecificationVersion>()) .Returns(newSpecVersion); AndGetFundingConfiguration( _specification.Current.FundingStreams.FirstOrDefault().Id, specificationEditModel.FundingPeriodId); SpecificationsService service = CreateSpecificationsService(newSpecVersion); //Act IActionResult result = await service.EditSpecification(SpecificationId, specificationEditModel, null, null); //Assert await _specificationIndexer .Received(1) .Index(Arg.Is <Specification>(_ => ReferenceEquals(_.Current, newSpecVersion))); await _cacheProvider .Received(1) .RemoveAsync <SpecificationSummary>(Arg.Is($"{CacheKeys.SpecificationSummaryById}{_specification.Id}")); await _messengerService .Received(1) .SendToTopic(Arg.Is(ServiceBusConstants.TopicNames.EditSpecification), Arg.Is <SpecificationVersionComparisonModel>( m => m.Id == SpecificationId && m.Current.Name == resultSpecificationName && m.Previous.Name == "Spec name" ), Arg.Any <IDictionary <string, string> >(), Arg.Is(true)); await _versionRepository .Received(1) .SaveVersion(Arg.Is(newSpecVersion)); }
public async Task EditSpecification_GivenProviderVersionChanges_CallsRegenerateScopedProviders() { //Arrange SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10", Name = "new spec name" }; Models.Specs.SpecificationVersion newSpecVersion = _specification.Current.Clone() as Models.Specs.SpecificationVersion; newSpecVersion.Name = specificationEditModel.Name; newSpecVersion.FundingPeriod.Id = specificationEditModel.FundingPeriodId; newSpecVersion.FundingStreams = new[] { new Reference { Id = "fs11" } }; newSpecVersion.ProviderVersionId = "Provider version 2"; _providersApiClient.RegenerateProviderSummariesForSpecification(_specification.Id, true) .Returns(new ApiResponse <bool>(HttpStatusCode.OK, true)); AndGetFundingConfiguration( _specification.Current.FundingStreams.FirstOrDefault().Id, specificationEditModel.FundingPeriodId); SpecificationsService service = CreateSpecificationsService(newSpecVersion); //Act IActionResult result = await service.EditSpecification(SpecificationId, specificationEditModel, null, null); await _specificationIndexer .Received(1) .Index(Arg.Is <Specification>(_ => ReferenceEquals(_.Current, newSpecVersion))); await _cacheProvider .Received(1) .RemoveAsync <SpecificationSummary>(Arg.Is($"{CacheKeys.SpecificationSummaryById}{_specification.Id}")); await _messengerService .Received(1) .SendToTopic(Arg.Is(ServiceBusConstants.TopicNames.EditSpecification), Arg.Is <SpecificationVersionComparisonModel>( m => m.Id == SpecificationId && m.Current.Name == "new spec name" && m.Previous.Name == "Spec name" ), Arg.Any <IDictionary <string, string> >(), Arg.Is(true)); await _versionRepository .Received(1) .SaveVersion(Arg.Is(newSpecVersion)); }
public async Task EditSpecification_GivenFailsToUpdateCosmosWithBadRequest_ReturnsBadRequest() { //Arrange IEnumerable <Reference> existingFundingStreams = _specification.Current.FundingStreams; SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10", ProviderVersionId = _specification.Current.ProviderVersionId }; PolicyModels.FundingStream fundingStream = new PolicyModels.FundingStream { Id = existingFundingStreams.First().Id, Name = existingFundingStreams.First().Name }; ApiResponse <PolicyModels.FundingStream> fundingStreamResponse = new ApiResponse <PolicyModels.FundingStream>(HttpStatusCode.OK, fundingStream); _specificationsRepository .GetSpecificationById(Arg.Is(SpecificationId)) .Returns(_specification); _policiesApiClient .GetFundingPeriodById(Arg.Is(_fundingPeriod.Id)) .Returns(_fundingPeriodResponse); _policiesApiClient .GetFundingStreamById(Arg.Is(fundingStream.Id)) .Returns(fundingStreamResponse); _specificationsRepository .UpdateSpecification(Arg.Any <Specification>()) .Returns(HttpStatusCode.BadRequest); AndGetFundingConfiguration( _specification.Current.FundingStreams.FirstOrDefault().Id, specificationEditModel.FundingPeriodId); SpecificationsService service = CreateService(mapper: _mapper, logs: _logger, specificationsRepository: _specificationsRepository, policiesApiClient: _policiesApiClient); //Act IActionResult result = await service.EditSpecification(SpecificationId, specificationEditModel, null, null); //Assert result .Should() .BeOfType <StatusCodeResult>() .Which .StatusCode .Should() .Be(400); }
public async Task EditSpecification_GivenSetLatestProviderVersionChangesFromManualToUseLatest_InstructToQueueProviderSnapshotDataLoadJob() { //Arrange _specification.Current.CoreProviderVersionUpdates = CoreProviderVersionUpdates.Manual; SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10", Name = "new spec name", ProviderVersionId = _specification.Current.ProviderVersionId, AssignedTemplateIds = new Dictionary <string, string>(), CoreProviderVersionUpdates = CoreProviderVersionUpdates.UseLatest }; Reference user = new Reference(); SpecificationVersion newSpecVersion = _specification.Current.DeepCopy(useCamelCase: false); newSpecVersion.Name = specificationEditModel.Name; newSpecVersion.FundingPeriod.Id = specificationEditModel.FundingPeriodId; newSpecVersion.FundingStreams = new[] { new Reference { Id = "fs11" } }; newSpecVersion.FundingPeriod.Name = "p10"; newSpecVersion.Author = user; newSpecVersion.Description = specificationEditModel.Description; string specFundingStreamId = _specification.Current.FundingStreams.FirstOrDefault().Id; AndGetFundingConfiguration( specFundingStreamId, specificationEditModel.FundingPeriodId); _providersApiClient.GetCurrentProviderMetadataForFundingStream(specFundingStreamId) .Returns(new ApiResponse <CurrentProviderVersionMetadata>(HttpStatusCode.NotFound, null)); SpecificationsService service = CreateSpecificationsService(newSpecVersion); string correlationId = NewRandomString(); //Act IActionResult result = await service.EditSpecification(SpecificationId, specificationEditModel, user, correlationId); result.Should() .BeOfType <InternalServerErrorResult>() .Which .Value .Should() .Be($"No current provider metadata returned for funding stream id '{specFundingStreamId}'."); }
public async Task EditSpecification_GivenChangesWithFDZProviderSourceAndProviderSnapshotIdNotSet_ReturnsPreconditionFailedResult() { //Arrange SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10", Name = "new spec name", ProviderVersionId = _specification.Current.ProviderVersionId, Description = "new spec description", AssignedTemplateIds = new Dictionary <string, string>() }; Reference user = new Reference(); SpecificationVersion newSpecVersion = _specification.Current.DeepCopy(useCamelCase: false); newSpecVersion.Name = specificationEditModel.Name; newSpecVersion.FundingPeriod.Id = specificationEditModel.FundingPeriodId; newSpecVersion.FundingPeriod.Name = "p10"; newSpecVersion.Author = user; newSpecVersion.Description = specificationEditModel.Description; AndGetFundingConfiguration( _specification.Current.FundingStreams.FirstOrDefault().Id, specificationEditModel.FundingPeriodId, ProviderSource.FDZ); SpecificationsService service = CreateSpecificationsService(newSpecVersion); string correlationId = NewRandomString(); SpecificationVersion previousSpecificationVersion = _specification.Current; //Act IActionResult actionResult = await service.EditSpecification(SpecificationId, specificationEditModel, user, correlationId); actionResult .Should() .BeOfType <PreconditionFailedResult>(); }
public async Task EditSpecification_GivenNoSpecificationIdWasProvided_ReturnsBadRequest() { //Arrange HttpRequest request = Substitute.For <HttpRequest>(); ILogger logger = CreateLogger(); SpecificationsService service = CreateService(logs: logger); //Act IActionResult result = await service.EditSpecification(request); //Arrange result .Should() .BeOfType <BadRequestObjectResult>(); logger .Received(1) .Error(Arg.Is("No specification Id was provided to EditSpecification")); }
public async Task EditSpecification_GivenSpecificationWasfoundAndFundingPeriodChangedButFailedToGetFundingPeriodsFromCosmos_ReturnsPreConditionFailedResult() { //Arrange SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10", ProviderVersionId = _specification.Current.ProviderVersionId }; _specificationsRepository.GetSpecificationById(Arg.Is(SpecificationId)) .Returns(_specification); SpecificationsService service = CreateService(logs: _logger, specificationsRepository: _specificationsRepository); //Act IActionResult result = await service.EditSpecification(SpecificationId, specificationEditModel, null, null); //Assert result .Should().BeOfType <PreconditionFailedResult>() .Which.Value.Should().Be($"Unable to find funding period with ID '{specificationEditModel.FundingPeriodId}'."); }
public async Task EditSpecification_GivenSpecificationWasNotFound_ReturnsNotFoundResult() { //Arrange SpecificationEditModel specificationEditModel = new SpecificationEditModel(); _specificationsRepository.GetSpecificationById(Arg.Is(SpecificationId)) .Returns((Specification)null); SpecificationsService service = CreateService(logs: _logger, specificationsRepository: _specificationsRepository); //Act IActionResult result = await service.EditSpecification(SpecificationId, specificationEditModel, null, null); //Assert result .Should().BeOfType <NotFoundObjectResult>() .Which.Value.Should() .Be("Specification not found"); _logger .Received(1) .Warning(Arg.Is($"Failed to find specification for id: {SpecificationId}")); }
public async Task EditSpecification_WhenInvalidModelProvided_ThenValidationErrorReturned() { // Arrange ValidationResult validationResult = new ValidationResult() { Errors = { new ValidationFailure("error", "error") } }; IValidator <SpecificationEditModel> validator = CreateEditSpecificationValidator(validationResult); SpecificationsService specificationsService = CreateService(specificationEditModelValidator: validator); SpecificationEditModel specificationEditModel = new SpecificationEditModel(); // Act IActionResult result = await specificationsService.EditSpecification(SpecificationId, specificationEditModel, null, null); // Assert result .Should().BeOfType <BadRequestObjectResult>() .Which.Value.Should().BeOfType <SerializableError>() .Which.Should().HaveCount(1); await validator .Received(1).ValidateAsync(Arg.Any <SpecificationEditModel>()); }
public async Task EditSpecification_GivenChangesWithFDZProviderSource_UpdatesSearchAndSendsMessage() { //Arrange SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10", Name = "new spec name", ProviderVersionId = _specification.Current.ProviderVersionId, Description = "new spec description", AssignedTemplateIds = new Dictionary <string, string>(), ProviderSnapshotId = 1 }; Reference user = new Reference(); SpecificationVersion newSpecVersion = _specification.Current.DeepCopy(useCamelCase: false); newSpecVersion.Name = specificationEditModel.Name; newSpecVersion.FundingPeriod.Id = specificationEditModel.FundingPeriodId; newSpecVersion.FundingPeriod.Name = "p10"; newSpecVersion.Author = user; newSpecVersion.Description = specificationEditModel.Description; newSpecVersion.ProviderSnapshotId = 1; newSpecVersion.ProviderSource = Models.Providers.ProviderSource.FDZ; AndGetFundingConfiguration( _specification.Current.FundingStreams.FirstOrDefault().Id, specificationEditModel.FundingPeriodId, ProviderSource.FDZ); SpecificationsService service = CreateSpecificationsService(newSpecVersion); string correlationId = NewRandomString(); SpecificationVersion previousSpecificationVersion = _specification.Current; //Act await service.EditSpecification(SpecificationId, specificationEditModel, user, correlationId); await _specificationIndexer .Received(1) .Index(Arg.Is <Specification>(_ => ReferenceEquals(_.Current, newSpecVersion))); await _cacheProvider .Received(1) .RemoveAsync <SpecificationSummary>(Arg.Is($"{CacheKeys.SpecificationSummaryById}{_specification.Id}")); await _messengerService .Received(1) .SendToTopic(Arg.Is(ServiceBusConstants.TopicNames.EditSpecification), Arg.Is <SpecificationVersionComparisonModel>( m => m.Id == SpecificationId && m.Current.Name == "new spec name" && m.Previous.Name == "Spec name" ), Arg.Any <IDictionary <string, string> >(), Arg.Is(true)); await _versionRepository .Received(1) .SaveVersion(Arg.Is(newSpecVersion)); await _templateVersionChangedHandler .Received(1) .HandleTemplateVersionChanged(Arg.Is(previousSpecificationVersion), Arg.Is <SpecificationVersion>(_ => newSpecVersion.AsJson(true) == _.AsJson(true)), Arg.Is(specificationEditModel.AssignedTemplateIds), Arg.Is(user), Arg.Is(correlationId)); }
public async Task EditSpecification_GivenChangesButFundingPeriodUnchanged_EnsuresCacheCorrectlyInvalidates() { //Arrange SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "FP1", Name = "new spec name", FundingStreamIds = new[] { "fs11" } }; Period fundingPeriod = new Period { Id = "FP1", Name = "fp 1" }; IEnumerable <FundingStream> fundingStreams = new[] { new FundingStream() }; string json = JsonConvert.SerializeObject(specificationEditModel); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpContext context = Substitute.For <HttpContext>(); HttpRequest request = Substitute.For <HttpRequest>(); IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(SpecificationId) }, }); request .Query .Returns(queryStringValues); request .Body .Returns(stream); request .HttpContext .Returns(context); ILogger logger = CreateLogger(); Specification specification = CreateSpecification(); ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository(); specificationsRepository .GetSpecificationById(Arg.Is(SpecificationId)) .Returns(specification); specificationsRepository .GetPeriodById(Arg.Is(fundingPeriod.Id)) .Returns(fundingPeriod); specificationsRepository .GetFundingStreams(Arg.Any <Expression <Func <FundingStream, bool> > >()) .Returns(fundingStreams); specificationsRepository .UpdateSpecification(Arg.Any <Specification>()) .Returns(HttpStatusCode.OK); ICacheProvider cacheProvider = CreateCacheProvider(); SpecificationVersion newSpecVersion = specification.Current.Clone() as SpecificationVersion; IVersionRepository <SpecificationVersion> versionRepository = CreateVersionRepository(); versionRepository .CreateVersion(Arg.Any <SpecificationVersion>(), Arg.Any <SpecificationVersion>()) .Returns(newSpecVersion); SpecificationsService service = CreateService( logs: logger, specificationsRepository: specificationsRepository, cacheProvider: cacheProvider, specificationVersionRepository: versionRepository); //Act IActionResult result = await service.EditSpecification(request); //Arrange await cacheProvider .Received(1) .RemoveAsync <SpecificationSummary>(Arg.Is($"{CacheKeys.SpecificationSummaryById}{specification.Id}")); await cacheProvider .DidNotReceive() .RemoveAsync <List <SpecificationSummary> >(Arg.Is($"{CacheKeys.SpecificationSummariesByFundingPeriodId}fp1")); await versionRepository .Received(1) .SaveVersion(Arg.Is(newSpecVersion)); }
public async Task EditSpecification_GivenChangesAndSpecContainsPoliciesAndCalculations_UpdatesSearchAndSendsMessage() { //Arrange SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10", Name = "new spec name", FundingStreamIds = new[] { "fs11" } }; Period fundingPeriod = new Period { Id = "fp10", Name = "fp 10" }; IEnumerable <FundingStream> fundingStreams = new[] { new FundingStream { AllocationLines = new List <AllocationLine> { new AllocationLine { Id = "al1", Name = "al2" } } } }; string json = JsonConvert.SerializeObject(specificationEditModel); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpContext context = Substitute.For <HttpContext>(); HttpRequest request = Substitute.For <HttpRequest>(); IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(SpecificationId) }, }); request .Query .Returns(queryStringValues); request .Body .Returns(stream); request .HttpContext .Returns(context); ILogger logger = CreateLogger(); Specification specification = CreateSpecification(); specification .Current .Policies = new[] { new Policy { Calculations = new[] { new Calculation { AllocationLine = new AllocationLine { Id = "oldallocationlineid" } } } } }; ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository(); specificationsRepository .GetSpecificationById(Arg.Is(SpecificationId)) .Returns(specification); specificationsRepository .GetPeriodById(Arg.Is(fundingPeriod.Id)) .Returns(fundingPeriod); specificationsRepository .GetFundingStreams(Arg.Any <Expression <Func <FundingStream, bool> > >()) .Returns(fundingStreams); specificationsRepository .UpdateSpecification(Arg.Any <Specification>()) .Returns(HttpStatusCode.OK); ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository(); ICacheProvider cacheProvider = CreateCacheProvider(); IMessengerService messengerService = CreateMessengerService(); SpecificationVersion newSpecVersion = specification.Current.Clone() as SpecificationVersion; newSpecVersion.Name = specificationEditModel.Name; newSpecVersion.FundingPeriod.Id = specificationEditModel.FundingPeriodId; newSpecVersion.FundingStreams = new[] { new FundingStream { Id = "fs11" } }; IVersionRepository <SpecificationVersion> versionRepository = CreateVersionRepository(); versionRepository .CreateVersion(Arg.Any <SpecificationVersion>(), Arg.Any <SpecificationVersion>()) .Returns(newSpecVersion); SpecificationsService service = CreateService( logs: logger, specificationsRepository: specificationsRepository, searchRepository: searchRepository, cacheProvider: cacheProvider, messengerService: messengerService, specificationVersionRepository: versionRepository); //Act IActionResult result = await service.EditSpecification(request); //Arrange await searchRepository .Received(1) .Index(Arg.Is <IEnumerable <SpecificationIndex> >( m => m.First().Id == SpecificationId && m.First().Name == "new spec name" && m.First().FundingPeriodId == "fp10" && m.First().FundingStreamIds.Count() == 1 )); await cacheProvider .Received(1) .RemoveAsync <SpecificationSummary>(Arg.Is($"{CacheKeys.SpecificationSummaryById}{specification.Id}")); await messengerService .Received(1) .SendToTopic(Arg.Is(ServiceBusConstants.TopicNames.EditSpecification), Arg.Is <SpecificationVersionComparisonModel>( m => m.Id == SpecificationId && m.Current.Name == "new spec name" && m.Previous.Name == "Spec name" ), Arg.Any <IDictionary <string, string> >(), Arg.Is(true)); await versionRepository .Received(1) .SaveVersion(Arg.Is(newSpecVersion)); }
public async Task EditSpecification_GivenChangesAndNoCurrentProviderVersionMetatdataForFundingStream_ReturnServerError() { //Arrange bool withRunCalculationEngineAfterCoreProviderUpdate = true; _specification.Current.CoreProviderVersionUpdates = CoreProviderVersionUpdates.Manual; SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10", Name = "new spec name", ProviderVersionId = _specification.Current.ProviderVersionId, AssignedTemplateIds = new Dictionary <string, string>(), CoreProviderVersionUpdates = CoreProviderVersionUpdates.UseLatest }; Reference user = new Reference(); SpecificationVersion newSpecVersion = _specification.Current.DeepCopy(useCamelCase: false); newSpecVersion.Name = specificationEditModel.Name; newSpecVersion.FundingPeriod.Id = specificationEditModel.FundingPeriodId; newSpecVersion.FundingStreams = new[] { new Reference { Id = "fs11" } }; newSpecVersion.FundingPeriod.Name = "p10"; newSpecVersion.Author = user; newSpecVersion.Description = specificationEditModel.Description; string specFundingStreamId = _specification.Current.FundingStreams.FirstOrDefault().Id; int providerSnapshotId = NewRandomInt(); CurrentProviderVersionMetadata currentProviderVersionMetadata = new CurrentProviderVersionMetadata { FundingStreamId = specFundingStreamId, ProviderSnapshotId = providerSnapshotId }; AndGetFundingConfiguration( specFundingStreamId, specificationEditModel.FundingPeriodId, withRunCalculationEngineAfterCoreProviderUpdate: withRunCalculationEngineAfterCoreProviderUpdate); _providersApiClient.GetCurrentProviderMetadataForFundingStream(specFundingStreamId) .Returns(new ApiResponse <CurrentProviderVersionMetadata>(HttpStatusCode.OK, currentProviderVersionMetadata)); SpecificationsService service = CreateSpecificationsService(newSpecVersion); string correlationId = NewRandomString(); SpecificationVersion previousSpecificationVersion = _specification.Current; //Act await service.EditSpecification(SpecificationId, specificationEditModel, user, correlationId); await _specificationIndexer .Received(1) .Index(Arg.Is <Specification>(_ => ReferenceEquals(_.Current, newSpecVersion))); await _cacheProvider .Received(1) .RemoveAsync <SpecificationSummary>(Arg.Is($"{CacheKeys.SpecificationSummaryById}{_specification.Id}")); await _messengerService .Received(1) .SendToTopic(Arg.Is(ServiceBusConstants.TopicNames.EditSpecification), Arg.Is <SpecificationVersionComparisonModel>( m => m.Id == SpecificationId && m.Current.Name == "new spec name" && m.Previous.Name == "Spec name" ), Arg.Any <IDictionary <string, string> >(), Arg.Is(true)); await _versionRepository .Received(1) .SaveVersion(Arg.Is(newSpecVersion)); await _templateVersionChangedHandler .Received(1) .HandleTemplateVersionChanged(Arg.Is(previousSpecificationVersion), Arg.Any <SpecificationVersion>(), Arg.Is(specificationEditModel.AssignedTemplateIds), Arg.Is(user), Arg.Is(correlationId)); await _editSpecificationJobActions .Received(1) .Run(Arg.Is <SpecificationVersion>( m => !string.IsNullOrWhiteSpace(m.EntityId) && m.Name == specificationEditModel.Name), Arg.Any <Reference>(), Arg.Any <string>(), true, withRunCalculationEngineAfterCoreProviderUpdate); }
public async Task EditSpecification_WhenIndexingReturnsErrors_ShouldThrowException() { //Arrange const string errorMessage = "Encountered error 802 code"; SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10", Name = "new spec name", FundingStreamIds = new[] { "fs11" } }; Period fundingPeriod = new Period { Id = "fp10", Name = "fp 10" }; IEnumerable <FundingStream> fundingStreams = new[] { new FundingStream { AllocationLines = new List <AllocationLine> { new AllocationLine { Id = "al1", Name = "al2" } } } }; string json = JsonConvert.SerializeObject(specificationEditModel); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpContext context = Substitute.For <HttpContext>(); HttpRequest request = Substitute.For <HttpRequest>(); IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(SpecificationId) }, }); request .Query .Returns(queryStringValues); request .Body .Returns(stream); request .HttpContext .Returns(context); ILogger logger = CreateLogger(); Specification specification = CreateSpecification(); ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository(); specificationsRepository .GetSpecificationById(Arg.Is(SpecificationId)) .Returns(specification); specificationsRepository .GetPeriodById(Arg.Is(fundingPeriod.Id)) .Returns(fundingPeriod); specificationsRepository .GetFundingStreams(Arg.Any <Expression <Func <FundingStream, bool> > >()) .Returns(fundingStreams); specificationsRepository .UpdateSpecification(Arg.Any <Specification>()) .Returns(HttpStatusCode.OK); ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository(); searchRepository .Index(Arg.Any <IEnumerable <SpecificationIndex> >()) .Returns(new[] { new IndexError() { ErrorMessage = errorMessage } }); ICacheProvider cacheProvider = CreateCacheProvider(); IMessengerService messengerService = CreateMessengerService(); SpecificationVersion newSpecVersion = specification.Current.Clone() as SpecificationVersion; newSpecVersion.Name = specificationEditModel.Name; newSpecVersion.FundingPeriod.Id = specificationEditModel.FundingPeriodId; newSpecVersion.FundingStreams = new[] { new FundingStream { Id = "fs11" } }; IVersionRepository <SpecificationVersion> versionRepository = CreateVersionRepository(); versionRepository .CreateVersion(Arg.Any <SpecificationVersion>(), Arg.Any <SpecificationVersion>()) .Returns(newSpecVersion); SpecificationsService service = CreateService( logs: logger, specificationsRepository: specificationsRepository, searchRepository: searchRepository, cacheProvider: cacheProvider, messengerService: messengerService, specificationVersionRepository: versionRepository); //Act Func <Task <IActionResult> > editSpecification = async() => await service.EditSpecification(request); //Assert editSpecification .Should() .Throw <ApplicationException>() .Which .Message .Should() .Be($"Could not index specification {specification.Current.Id} because: {errorMessage}"); }
public async Task EditSpecification_GivenFailsToUpdateCosomosWithBadRequest_ReturnsBadRequest() { //Arrange SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10" }; Period fundingPeriod = new Period { Id = "fp10", Name = "fp 10" }; IEnumerable <FundingStream> fundingStreams = new[] { new FundingStream() }; string json = JsonConvert.SerializeObject(specificationEditModel); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpContext context = Substitute.For <HttpContext>(); HttpRequest request = Substitute.For <HttpRequest>(); IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(SpecificationId) }, }); request .Query .Returns(queryStringValues); request .Body .Returns(stream); request .HttpContext .Returns(context); ILogger logger = CreateLogger(); Specification specification = CreateSpecification(); ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository(); specificationsRepository .GetSpecificationById(Arg.Is(SpecificationId)) .Returns(specification); specificationsRepository .GetPeriodById(Arg.Is(fundingPeriod.Id)) .Returns(fundingPeriod); specificationsRepository .GetFundingStreams(Arg.Any <Expression <Func <FundingStream, bool> > >()) .Returns(fundingStreams); specificationsRepository .UpdateSpecification(Arg.Any <Specification>()) .Returns(HttpStatusCode.BadRequest); SpecificationsService service = CreateService(logs: logger, specificationsRepository: specificationsRepository); //Act IActionResult result = await service.EditSpecification(request); //Arrange result .Should() .BeOfType <StatusCodeResult>() .Which .StatusCode .Should() .Be(400); }
public async Task EditSpecification_GivenSpecificationWasFoundAndFundingPeriodChangedAndfundinfgStreamsChangedButFailsToFindFundingStreams_ReturnsInternalServerErrorResult() { //Arrange SpecificationEditModel specificationEditModel = new SpecificationEditModel { FundingPeriodId = "fp10" }; Period fundingPeriod = new Period { Id = "fp10", Name = "fp 10" }; string json = JsonConvert.SerializeObject(specificationEditModel); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpContext context = Substitute.For <HttpContext>(); HttpRequest request = Substitute.For <HttpRequest>(); IQueryCollection queryStringValues = new QueryCollection(new Dictionary <string, StringValues> { { "specificationId", new StringValues(SpecificationId) }, }); request .Query .Returns(queryStringValues); request .Body .Returns(stream); request .HttpContext .Returns(context); ILogger logger = CreateLogger(); Specification specification = CreateSpecification(); ISpecificationsRepository specificationsRepository = CreateSpecificationsRepository(); specificationsRepository .GetSpecificationById(Arg.Is(SpecificationId)) .Returns(specification); specificationsRepository .GetPeriodById(Arg.Is(fundingPeriod.Id)) .Returns(fundingPeriod); SpecificationsService service = CreateService(logs: logger, specificationsRepository: specificationsRepository); //Act IActionResult result = await service.EditSpecification(request); //Arrange result .Should() .BeOfType <InternalServerErrorResult>() .Which .Value .Should() .Be("No funding streams were retrieved to add to the Specification"); }