public async Task EditCalculationPageModel_OnGetAsync_WhenSpecificationNotFound_ThenPreconditionFailedReturned() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; ISpecsApiClient specsClient = CreateSpecsClient(); ILogger logger = CreateLogger(); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.NotFound, new Specification())); EditCalculationPageModel pageModel = CreatePageModel(specsClient, logger); // Act IActionResult result = await pageModel.OnGetAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <PreconditionFailedResult>() .Which .Value .Should() .Be("Specification not found"); await specsClient .Received(1) .GetSpecification(Arg.Is(specificationId)); await specsClient .Received(0) .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)); }
public async Task EditCalculationPageModel_OnGetAsync_WhenCalculationApiResponseContentIsNull_ThenInternalErrorIsReturned() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; ISpecsApiClient specsClient = CreateSpecsClient(); ILogger logger = CreateLogger(); specsClient .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)) .Returns(new ApiResponse <CalculationCurrentVersion>(HttpStatusCode.OK, null)); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, new Specification())); EditCalculationPageModel pageModel = CreatePageModel(specsClient, logger); // Act IActionResult result = await pageModel.OnGetAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <InternalServerErrorResult>() .Which .Value .Should() .Be("Calculation content returned null"); logger .Received(1) .Warning("Calculation Result API response content returned null for calculation ID '{calculationId}' on specificationId '{specificationId}'", calculationId, specificationId); await specsClient .Received(1) .GetSpecification(Arg.Is(specificationId)); await specsClient .Received(1) .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)); }
public async Task EditCalculationPageModel_OnGetAsync_WhenCalculationApiResponseIsNotSuccessful_ThenInternalErrorIsReturned() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; ISpecsApiClient specsClient = CreateSpecsClient(); ILogger logger = CreateLogger(); specsClient .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)) .Returns(new ApiResponse <CalculationCurrentVersion>(HttpStatusCode.InternalServerError, null)); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, new Specification())); EditCalculationPageModel pageModel = CreatePageModel(specsClient, logger); // Act IActionResult result = await pageModel.OnGetAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <InternalServerErrorResult>() .Which .Value .Should() .Be("Unexpected status code from Calculation API call 'InternalServerError'"); logger .Received(1) .Warning($"Unexpected status code from Calculation API call 'InternalServerError'"); await specsClient .Received(1) .GetSpecification(Arg.Is(specificationId)); await specsClient .Received(1) .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)); }
public async Task OnPostAsync_GivenViewModelIsValidAndRedirectToSpecificationsActionProvided_ThenSpecificationIsEditedAndReturnsRedirectToSpecificationsPage() { //Arrange ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.NotFound); EditSpecificationModel editModel = new EditSpecificationModel(); EditSpecificationViewModel viewModel = CreateEditSpecificationViewModel(); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetSpecificationByName(Arg.Is(specName)) .Returns(existingSpecificationResponse); apiClient .UpdateSpecification(Arg.Is(specificationId), Arg.Any <EditSpecificationModel>()) .Returns(HttpStatusCode.OK); IMapper mapper = CreateMapper(); mapper .Map <EditSpecificationModel>(Arg.Is(viewModel)) .Returns(editModel); EditSpecificationPageModel pageModel = CreatePageModel(apiClient, mapper); pageModel.EditSpecificationViewModel = viewModel; pageModel.PageContext = new PageContext(); //Act IActionResult result = await pageModel.OnPostAsync(specificationId, EditSpecificationRedirectAction.Specifications); //Assert result .Should() .BeOfType <RedirectResult>(); RedirectResult redirectResult = result as RedirectResult; redirectResult .Url .Should() .Be("/specs?operationType=SpecificationUpdated&operationId=spec-id"); await apiClient .Received(1) .UpdateSpecification(Arg.Is(specificationId), Arg.Is(editModel)); }
public async Task EditCalculationPageModel_OnGetAsync_WhenCalculationNotFound_ThenNotFoundIsReturned() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; ISpecsApiClient specsClient = CreateSpecsClient(); specsClient .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)) .Returns(new ApiResponse <CalculationCurrentVersion>(HttpStatusCode.NotFound, null)); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, new Specification())); EditCalculationPageModel pageModel = CreatePageModel(specsClient); // Act IActionResult result = await pageModel.OnGetAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <NotFoundObjectResult>() .Which .Value .Should() .Be("Calculation not found"); await specsClient .Received(1) .GetSpecification(Arg.Is(specificationId)); await specsClient .Received(1) .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)); }
public async Task OnPostAsync_GivenViewModelIsValidAndUpdateSpecificationCallFails_ThenInternalServerErrorReturned() { // Arrange ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.NotFound); EditSpecificationModel editModel = new EditSpecificationModel(); EditSpecificationViewModel viewModel = CreateEditSpecificationViewModel(); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetSpecificationByName(Arg.Is(specName)) .Returns(existingSpecificationResponse); apiClient .UpdateSpecification(Arg.Is(specificationId), Arg.Any <EditSpecificationModel>()) .Returns(HttpStatusCode.InternalServerError); IMapper mapper = CreateMapper(); mapper .Map <EditSpecificationModel>(Arg.Is(viewModel)) .Returns(editModel); EditSpecificationPageModel pageModel = CreatePageModel(apiClient, mapper); pageModel.EditSpecificationViewModel = viewModel; pageModel.PageContext = new PageContext(); // Act IActionResult result = await pageModel.OnPostAsync(specificationId, EditSpecificationRedirectAction.Specifications); //Assert result .Should() .BeOfType <InternalServerErrorResult>() .Which .Value .Should() .Be("Unable to update specification. API returned 'InternalServerError'"); await apiClient .Received(1) .UpdateSpecification(Arg.Is(specificationId), Arg.Is(editModel)); }
public async Task TestScenarioResultsService_PerformSearch_WhenGetAllSpecificationsLookupIsNull_ThenExceptionThrown() { // Arrange IScenarioSearchService searchService = CreateScenarioSearchService(); ISpecsApiClient specsApiClient = CreateSpecsApiClient(); ITestEngineApiClient testEngineApiClient = CreateTestEngineApiClient(); ILogger logger = CreateLogger(); TestScenarioResultRequestViewModel resultRequestViewModel = new TestScenarioResultRequestViewModel() { SearchTerm = "", PageNumber = 1, FundingPeriodId = null, SpecificationId = null, }; TestScenarioResultsService testScenarioResultsService = CreateService(searchService, specsApiClient, testEngineApiClient, logger: logger); searchService.PerformSearch(Arg.Any <SearchRequestViewModel>()) .Returns(new ScenarioSearchResultViewModel()); List <SpecificationSummary> specifications = CreateSpecifications(); specsApiClient .GetSpecificationSummaries() .Returns((ApiResponse <IEnumerable <SpecificationSummary> >)null); // Act Func <Task> action = async() => await testScenarioResultsService.PerformSearch(resultRequestViewModel); // Assert action. Should() .ThrowExactly <InvalidOperationException>() .WithMessage("Specifications API Response was null"); logger .Received(1) .Warning(Arg.Is("Specifications API Response was null")); await specsApiClient .Received(1) .GetSpecificationSummaries(); }
public async Task OnGetAsync_ReturnsValidFundingPeriodAsync() { // Arrange IResultsApiClient resultsApiClient = CreateApiClient(); ISpecsApiClient specsClient = CreateSpecsApiClient(); ITestScenarioSearchService searchService = CreateTestScenarioSearchService(); ProviderScenarioResultsPageModel providerScenarioResultsPageModel = CreatePageModel(searchService, resultsApiClient, specsApiClient: specsClient); Provider provider = CreateProvider(); IEnumerable <Reference> fundingPeriods = new[] { new Reference("1617", "2016-2017"), new Reference("1718", "2017-2018"), new Reference("1819", "2018-2019") }; IEnumerable <string> specSummary = GetSpecificationsWithResults(); specsClient.GetFundingPeriods() .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods)); resultsApiClient.GetSpecificationIdsForProvider("2") .Returns(new ApiResponse <IEnumerable <string> >(HttpStatusCode.OK, specSummary)); resultsApiClient.GetProviderByProviderId(Arg.Any <string>()) .Returns(new ApiResponse <Provider>(HttpStatusCode.OK, provider)); specsClient .GetSpecificationSummaries(Arg.Any <IEnumerable <string> >()) .Returns(new ApiResponse <IEnumerable <Clients.SpecsClient.Models.SpecificationSummary> >(HttpStatusCode.OK, new List <Clients.SpecsClient.Models.SpecificationSummary>())); //Act IActionResult actionResult = await providerScenarioResultsPageModel.OnGetAsync("2", 1, "", "1819", "1"); // Assert actionResult .Should() .BeOfType <PageResult>(); await specsClient .Received(1) .GetFundingPeriods(); }
public async Task PerformSearch_WhenFeatureToggleIsSwitchedOn_EnsureSearchModeIsAll() { // Arrange ISpecsApiClient specsClient = Substitute.For <ISpecsApiClient>(); ILogger logger = Substitute.For <ILogger>(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); IFeatureToggle featureToggle = CreateFeatureToggle(true); ISpecificationSearchService SpecificationSearchService = new SpecificationSearchService(specsClient, mapper, logger, featureToggle); SearchRequestViewModel request = new SearchRequestViewModel() { PageNumber = 2, }; // Act SpecificationSearchResultViewModel results = await SpecificationSearchService.PerformSearch(request); // Assert await specsClient .Received(1) .FindSpecifications(Arg.Is <SearchFilterRequest>(m => m.SearchMode == SearchMode.All)); }
public async Task TestScenarioResultsService_PerformSearch_WhenTestScenariosExist_ThenResultsAreReturned() { // Arrange IScenarioSearchService searchService = CreateScenarioSearchService(); ISpecsApiClient specsApiClient = CreateSpecsApiClient(); ITestEngineApiClient testEngineApiClient = CreateTestEngineApiClient(); TestScenarioResultsService testScenarioResultsService = CreateService(searchService, specsApiClient, testEngineApiClient); TestScenarioResultRequestViewModel resultRequestViewModel = new TestScenarioResultRequestViewModel() { SearchTerm = "", PageNumber = 1, FundingPeriodId = null, SpecificationId = null, }; ScenarioSearchResultViewModel scenarioSearchResultViewModel = new ScenarioSearchResultViewModel() { CurrentPage = 1, TotalResults = 1, StartItemNumber = 1, EndItemNumber = 1, Scenarios = new List <ScenarioSearchResultItemViewModel>() { new ScenarioSearchResultItemViewModel() { Id = "ts1", Name = "Test Scenario 1", FundingPeriodName = "2018/2019", Status = "Passed", SpecificationName = "Specifcation 1", LastUpdatedDate = new DateTime(2018, 1, 5, 7, 8, 9), } } }; searchService.PerformSearch(Arg.Is <SearchRequestViewModel>(s => s.SearchTerm == resultRequestViewModel.SearchTerm)) .Returns(scenarioSearchResultViewModel); List <SpecificationSummary> specifications = CreateSpecifications(); specsApiClient .GetSpecificationSummaries() .Returns(new ApiResponse <IEnumerable <SpecificationSummary> >(HttpStatusCode.OK, specifications.AsEnumerable())); List <TestScenarioResultCounts> testScenarioResultCounts = new List <TestScenarioResultCounts>(); testScenarioResultCounts.Add(new TestScenarioResultCounts() { Passed = 5, Failed = 10, Ignored = 50, LastUpdatedDate = new DateTime(2018, 10, 5, 7, 8, 9), TestScenarioId = "ts1", TestScenarioName = "Test Scenario 1", }); testEngineApiClient .GetTestResultCounts(Arg.Any <TestScenarioResultCountsRequestModel>()) .Returns(new ApiResponse <IEnumerable <TestScenarioResultCounts> >(HttpStatusCode.OK, testScenarioResultCounts)); // Act TestScenarioResultViewModel resultViewModel = await testScenarioResultsService.PerformSearch(resultRequestViewModel); // Assert resultViewModel.Should().NotBeNull(); TestScenarioResultViewModel expectedResult = new TestScenarioResultViewModel() { CurrentPage = 1, EndItemNumber = 1, Facets = new List <SearchFacetViewModel>(), FundingPeriodId = null, Specifications = new List <ReferenceViewModel>() { new ReferenceViewModel("spec1", "Specification 1"), new ReferenceViewModel("spec2", "Specification 2"), new ReferenceViewModel("spec3", "Specification for 2018/2019"), }, StartItemNumber = 1, TotalResults = 1, TestResults = new List <TestScenarioResultItemViewModel>() { new TestScenarioResultItemViewModel() { Id = "ts1", Name = "Test Scenario 1", Passes = 5, Failures = 10, Ignored = 50, LastUpdatedDate = new DateTime(2018, 1, 5, 7, 8, 9), } } }; resultViewModel.Should().BeEquivalentTo(expectedResult); await specsApiClient .Received(1) .GetSpecificationSummaries(); }
public async Task OnGet_WhenCalculationExistsThenCalculationReturned() { // Arrange ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>(); ISpecsApiClient specsClient = Substitute.For <ISpecsApiClient>(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); const string calculationId = "5"; const string specificationId = "specId"; const string specificationName = "Spec Name"; Calculation calcsCalculation = new Calculation() { Id = calculationId, CalculationSpecification = new Reference(calculationId, "Test Calculation Specification"), SpecificationId = specificationId, SourceCode = "Test Source Code" }; Clients.SpecsClient.Models.CalculationCurrentVersion specsCalculation = new Clients.SpecsClient.Models.CalculationCurrentVersion() { Id = calculationId, Name = "Specs Calculation", Description = "Spec Description", }; calcsClient .GetCalculationById(calculationId) .Returns(new ApiResponse <Calculation>(System.Net.HttpStatusCode.OK, calcsCalculation)); specsClient .GetCalculationById(calcsCalculation.SpecificationId, calculationId) .Returns(new ApiResponse <Clients.SpecsClient.Models.CalculationCurrentVersion>(System.Net.HttpStatusCode.OK, specsCalculation)); Clients.SpecsClient.Models.SpecificationSummary specificationSummary = new Clients.SpecsClient.Models.SpecificationSummary() { Id = specificationId, Name = specificationName }; specsClient .GetSpecificationSummary(Arg.Is(specificationId)) .Returns(new ApiResponse <Clients.SpecsClient.Models.SpecificationSummary>(System.Net.HttpStatusCode.OK, specificationSummary)); EditCalculationPageModel pageModel = CreatePageModel(specsClient: specsClient, calcsClient: calcsClient, mapper: mapper); // Act IActionResult result = await pageModel.OnGet(calculationId); // Assert result.Should().NotBeNull(); result.Should().BeOfType <PageResult>(); pageModel.Calculation.Should().NotBeNull(); pageModel.Calculation.Name.Should().Be(calcsCalculation.Name); pageModel.Calculation.Description.Should().Be(specsCalculation.Description); pageModel.SpecificationId.Should().Be(calcsCalculation.SpecificationId); pageModel.EditModel.SourceCode.Should().Be(calcsCalculation.SourceCode); pageModel.SpecificationName.Should().Be(specificationName); await calcsClient .Received(1) .GetCalculationById(Arg.Is(calculationId)); await specsClient .Received(1) .GetSpecificationSummary(Arg.Is(specificationId)); await specsClient .Received(1) .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId)); }
public async Task EditCalculationPageModel_OnPostAsync_WhenInvalidCalculationDetailsProvidedAndValidationMessagesComeFromApi_ThenCalculationPageIsReturnedWithModelStateErrors() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; EditCalculationViewModel viewModel = new EditCalculationViewModel() { Name = null, AllocationLineId = "al2", CalculationType = "Number", Description = "Updated description", IsPublic = true, PolicyId = "pol2", }; Calculation resultCalculation = new Calculation() { Id = calculationId, Name = "Calculation Name", AllocationLine = new Reference("al1", "Allocation Line"), CalculationType = CalculationSpecificationType.Funding, Description = "Calculation Description", IsPublic = false, }; ISpecsApiClient specsClient = CreateSpecsClient(); ValidatedApiResponse <Calculation> validatedResponse = new ValidatedApiResponse <Calculation>(HttpStatusCode.BadRequest, resultCalculation) { ModelState = new Dictionary <string, IEnumerable <string> >() { { "name", new string[] { "Name was not provided" } } } }; specsClient .UpdateCalculation(Arg.Is(specificationId), Arg.Is(calculationId), Arg.Any <CalculationUpdateModel>()) .Returns(validatedResponse); Specification specification = CreateSpecification(specificationId); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, specification)); EditCalculationPageModel pageModel = CreatePageModel(specsClient); pageModel.PageContext = new PageContext(); pageModel.EditCalculationViewModel = viewModel; // Act IActionResult result = await pageModel.OnPostAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <PageResult>() .Which .Should() .NotBeNull(); await specsClient .Received(1) .GetSpecification(Arg.Is(specificationId)); pageModel .EditCalculationViewModel .Should() .BeEquivalentTo <EditCalculationViewModel>(new EditCalculationViewModel() { AllocationLineId = "al2", CalculationType = "Number", Description = "Updated description", IsPublic = true, PolicyId = "pol2", Name = null, }); pageModel .Specification .Should() .BeEquivalentTo(new SpecificationViewModel() { Id = specificationId, Name = "Specification Name", FundingStreams = new List <ReferenceViewModel>() { new ReferenceViewModel("fsId", "Funding Stream 1"), new ReferenceViewModel("fs2Id", "Funding Stream 2"), }, }, c => c.Excluding(m => m.Policies)); pageModel .Policies .Should() .BeEquivalentTo(new List <SelectListItem>() { new SelectListItem() { Disabled = false, Selected = false, Text = "Policy 1", Value = "p1", Group = new SelectListGroup() { Name = "Policies", Disabled = false, }, }, new SelectListItem() { Disabled = false, Selected = false, Text = "Sub Policy 1", Value = "sub1", Group = new SelectListGroup() { Name = "Subpolicies", Disabled = false, }, }, new SelectListItem() { Disabled = false, Selected = false, Text = "Sub Policy 2", Value = "sub2", Group = new SelectListGroup() { Name = "Subpolicies", Disabled = false, }, }, new SelectListItem { Disabled = false, Selected = false, Text = "Policy 2", Value = "p2", Group = new SelectListGroup() { Name = "Policies", Disabled = false, }, }, new SelectListItem { Disabled = false, Selected = false, Text = "Policy 3", Value = "p3", Group = new SelectListGroup() { Name = "Policies", Disabled = false, }, }, new SelectListItem() { Disabled = false, Selected = false, Text = "Sub Policy 3", Value = "sub3", Group = new SelectListGroup() { Name = "Subpolicies", Disabled = false, }, }, new SelectListItem { Disabled = false, Selected = false, Text = "Policy 4", Value = "p4", Group = new SelectListGroup() { Name = "Policies", Disabled = false, }, } }); pageModel .AllocationLines .Should() .BeEquivalentTo(new List <SelectListItem>() { new SelectListItem() { Disabled = false, Selected = false, Text = "Funding Stream - Allocation Line 1", Value = "al1", }, new SelectListItem { Disabled = false, Selected = true, Text = "Funding Stream - Allocation Line 2", Value = "al2", }, new SelectListItem { Disabled = false, Selected = false, Text = "Funding Stream 2 - Allocation Line 1", Value = "al3", }, new SelectListItem { Disabled = false, Selected = false, Text = "Funding Stream 2 - Allocation Line 2", Value = "al4", } }); pageModel .ModelState .IsValid .Should() .BeFalse(); pageModel .ModelState .Should() .HaveCount(1); pageModel .ModelState .Values .First() .Errors .First() .ErrorMessage .Should() .Be("Name was not provided"); pageModel .IsAuthorizedToEdit .Should().BeTrue(); }
public async Task EditCalculationPageModel_OnPostAsync_WhenValidCalculationDetailsProvided_ThenCalculationIsEdited() { // Arrange const string specificationId = "spec1"; const string calculationId = "calculationId"; EditCalculationViewModel viewModel = new EditCalculationViewModel() { Name = "Updated Name", AllocationLineId = "al2", CalculationType = "Number", Description = "Updated description", IsPublic = true, PolicyId = "pol2", }; Calculation resultCalculation = new Calculation() { Id = calculationId, Name = "Calculation Name", AllocationLine = new Reference("al1", "Allocation Line"), CalculationType = CalculationSpecificationType.Funding, Description = "Calculation Description", IsPublic = false, }; ISpecsApiClient specsClient = CreateSpecsClient(); specsClient .UpdateCalculation(Arg.Is(specificationId), Arg.Is(calculationId), Arg.Any <CalculationUpdateModel>()) .Returns(new ValidatedApiResponse <Calculation>(HttpStatusCode.OK, resultCalculation)); EditCalculationPageModel pageModel = CreatePageModel(specsClient); pageModel.PageContext = new PageContext(); pageModel.EditCalculationViewModel = viewModel; // Act IActionResult result = await pageModel.OnPostAsync(specificationId, calculationId); // Assert result .Should() .BeOfType <RedirectResult>() .Which .Url .Should() .Be($"/specs/policies/{specificationId}?operationId={calculationId}&operationType=CalculationUpdated"); await specsClient .Received(1) .UpdateCalculation( Arg.Is(specificationId), Arg.Is(calculationId), Arg.Is <CalculationUpdateModel>( m => m.AllocationLineId == viewModel.AllocationLineId && m.Description == viewModel.Description && m.IsPublic == viewModel.IsPublic && m.Name == viewModel.Name && m.PolicyId == viewModel.PolicyId )); }