public void OnGetAsync_GivenToPopulayeFundingPeriodsIsOKButFundingStreamsReturnsBadRequest_ThrowsInvalidOperationException() { //Arrange IEnumerable <Reference> fundingPeriods = new[] { new Reference { Id = "fp1", Name = "funding" } }; ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods); ApiResponse <IEnumerable <FundingStream> > fundingStreamsResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.BadRequest); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetFundingPeriods() .Returns(fundingPeriodsResponse); apiClient .GetFundingStreams() .Returns(fundingStreamsResponse); CreateSpecificationPageModel pageModel = CreatePageModel(apiClient); //Act/Assert Func <Task> test = async() => await pageModel.OnGetAsync(); test .Should() .ThrowExactly <InvalidOperationException>(); }
public void OnGetAsync_ReturnsErrorWhenFundingPeriodResponseIsNull() { // Arrange IResultsApiClient resultsApiClient = CreateApiClient(); ISpecsApiClient specsClient = CreateSpecsApiClient(); ITestScenarioSearchService searchService = CreateTestScenarioSearchService(); ProviderScenarioResultsPageModel providerScenarioResultsPageModel = CreatePageModel(searchService, resultsApiClient, specsApiClient: specsClient); Provider provider = CreateProvider(); IEnumerable <Reference> fundingPeriods = null; resultsApiClient.GetProviderByProviderId(Arg.Any <string>()) .Returns(new ApiResponse <Provider>(HttpStatusCode.OK, provider)); SearchRequestViewModel searchRequest = CreateSearchRequest(); specsClient.GetFundingPeriods() .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.NotFound, fundingPeriods)); // Act Func <Task> test = async() => await providerScenarioResultsPageModel.OnGetAsync("2", 1, "", "1819", "1"); // Assert test .Should() .ThrowExactly <System.InvalidOperationException>().WithMessage("Unable to retreive Periods: Status Code = NotFound"); }
public void OnPostAsync_GivenNameAlreadyExistsAndPopulateFundingPeriodsReturnsOKButNullContent_ThrowsInvalidOperationException() { //Arrange ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK); ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.OK); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetSpecificationByName(Arg.Is(specName)) .Returns(existingSpecificationResponse); apiClient .GetFundingPeriods() .Returns(fundingPeriodsResponse); EditSpecificationPageModel pageModel = CreatePageModel(apiClient); pageModel.PageContext = new PageContext(); pageModel.EditSpecificationViewModel = new EditSpecificationViewModel { Name = specName }; //Act/Assert Func <Task> test = async() => await pageModel.OnPostAsync(); test .Should() .ThrowExactly <InvalidOperationException>(); }
private async Task PopulateFundingPeriods(string fundingPeriodId = null) { ApiResponse <IEnumerable <Reference> > periodsResponse = await _specsApiClient.GetFundingPeriods(); if (periodsResponse == null) { throw new InvalidOperationException($"Unable to retreive Periods: response was null"); } if (periodsResponse.StatusCode != HttpStatusCode.OK) { throw new InvalidOperationException($"Unable to retreive Periods: Status Code = {periodsResponse.StatusCode}"); } IEnumerable <Reference> fundingPeriods = periodsResponse.Content; if (periodsResponse.Content == null) { throw new InvalidOperationException($"Unable to retreive Periods: Content was null"); } if (string.IsNullOrWhiteSpace(fundingPeriodId)) { fundingPeriodId = FundingPeriodId; } FundingPeriods = fundingPeriods.Select(m => new SelectListItem { Value = m.Id, Text = m.Name, Selected = m.Id == fundingPeriodId }).ToList(); }
private async Task PopulateFundingPeriods(string fundingPeriodId) { ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = await _specsClient.GetFundingPeriods(); if (fundingPeriodsResponse == null) { throw new InvalidOperationException($"Null funding periods response returned"); } if (fundingPeriodsResponse.StatusCode == HttpStatusCode.OK && !fundingPeriodsResponse.Content.IsNullOrEmpty()) { IEnumerable <Reference> fundingPeriods = fundingPeriodsResponse.Content; FundingPeriods = fundingPeriods.Select(m => new SelectListItem { Value = m.Id, Text = m.Name, Selected = m.Id == fundingPeriodId }).ToList(); } else { throw new InvalidOperationException($"Unable to retreive Funding Streams. Status Code = {fundingPeriodsResponse.StatusCode}"); } }
public async Task OnGetAsync_WhenGettingProviderResultsIsSuccess_CalculationDetails_Populated() { // Arrange IResultsApiClient resultsApiClient = CreateApiClient(); ISpecsApiClient specsClient = CreateSpecsApiClient(); IMapper mapper = CreateMapper(); ILogger logger = CreateLogger(); IFeatureToggle featureToggle = Substitute.For <IFeatureToggle>(); ProviderCalcsResultsPageModel provideCalcPageModel = CreatePageModel(resultsApiClient, specsClient, mapper, logger, featureToggle); IEnumerable <Reference> fundingPeriods = new[] { new Reference("1617", "2016-2017"), new Reference("1718", "2017-2018"), new Reference("1819", "2018-2019") }; Provider provider = CreateProvider(); IEnumerable <string> specSummary = GetSpecificationsWithResults(); IList <CalculationResultItem> calResult = GetCalcResults(); IList <AllocationLineResultItem> allocResult = GetAllocationResults(); ProviderResults providerResults = new ProviderResults() { AllocationResults = allocResult, CalculationResults = calResult }; resultsApiClient.GetProviderByProviderId(Arg.Any <string>()) .Returns(new ApiResponse <Provider>(HttpStatusCode.OK, provider)); specsClient.GetFundingPeriods() .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods)); resultsApiClient.GetProviderResults(Arg.Is("2"), Arg.Is("2")) .Returns(new ApiResponse <ProviderResults>(HttpStatusCode.OK, providerResults)); resultsApiClient.GetSpecificationIdsForProvider("2") .Returns(new ApiResponse <IEnumerable <string> >(HttpStatusCode.OK, specSummary)); specsClient .GetSpecificationSummaries(Arg.Any <IEnumerable <string> >()) .Returns(new ApiResponse <IEnumerable <Clients.SpecsClient.Models.SpecificationSummary> >(HttpStatusCode.OK, new List <Clients.SpecsClient.Models.SpecificationSummary>())); // Act IActionResult result = await provideCalcPageModel.OnGetAsync("2", "1617", "2"); // Assert provideCalcPageModel.CalculationErrorCount.Equals(1); provideCalcPageModel.ViewModel.CalculationItems.Should().HaveSameCount(calResult); provideCalcPageModel.ViewModel.CalculationItems.First().CalculationType.Should().Be(Clients.SpecsClient.Models.CalculationSpecificationType.Number); provideCalcPageModel.ViewModel.CalculationItems.Last().CalculationType.Should().Be(Clients.SpecsClient.Models.CalculationSpecificationType.Funding); }
public async Task OnGetAsync_ReturnsValidFundingPeriodAsync() { // Arrange IResultsApiClient resultsApiClient = CreateApiClient(); ISpecsApiClient specsClient = CreateSpecsApiClient(); IMapper mapper = CreateMapper(); ILogger logger = CreateLogger(); IFeatureToggle featureToggle = Substitute.For <IFeatureToggle>(); ProviderCalcsResultsPageModel provideCalcPageModel = CreatePageModel(resultsApiClient, specsClient, mapper, logger, featureToggle); 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(); IList <CalculationResultItem> calResult = GetCalcResults(); IList <AllocationLineResultItem> allocResult = GetAllocationResults(); ProviderResults providerResults = new ProviderResults() { AllocationResults = allocResult, CalculationResults = calResult }; resultsApiClient.GetProviderByProviderId(Arg.Any <string>()) .Returns(new ApiResponse <Provider>(HttpStatusCode.OK, provider)); specsClient.GetFundingPeriods() .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods)); resultsApiClient.GetProviderResults(Arg.Is("2"), Arg.Is("2")) .Returns(new ApiResponse <ProviderResults>(HttpStatusCode.OK, providerResults)); resultsApiClient.GetSpecificationIdsForProvider("2") .Returns(new ApiResponse <IEnumerable <string> >(HttpStatusCode.OK, specSummary)); specsClient .GetSpecificationSummaries(Arg.Any <IEnumerable <string> >()) .Returns(new ApiResponse <IEnumerable <Clients.SpecsClient.Models.SpecificationSummary> >(HttpStatusCode.OK, new List <Clients.SpecsClient.Models.SpecificationSummary>())); // Act IActionResult result = await provideCalcPageModel.OnGetAsync("2", "1617", "2"); // Assert result.Should() .BeOfType <PageResult>() .Which.Should().NotBeNull("Action result was null"); provideCalcPageModel.FundingPeriods.Should().NotBeNull(); }
public void OnGetAsync_WhenGettingProviderResponseIsNotSuccess_ThrowsException() { // Arrange IResultsApiClient resultsApiClient = CreateApiClient(); ISpecsApiClient specsClient = CreateSpecsApiClient(); IMapper mapper = CreateMapper(); ILogger logger = CreateLogger(); IFeatureToggle featureToggle = Substitute.For <IFeatureToggle>(); ProviderCalcsResultsPageModel provideCalcPageModel = CreatePageModel(resultsApiClient, specsClient, mapper, logger, featureToggle); IEnumerable <Reference> fundingPeriods = new[] { new Reference("1617", "2016-2017"), new Reference("1718", "2017-2018"), new Reference("1819", "2018-2019") }; Provider provider = null; IEnumerable <string> specSummary = GetSpecificationsWithResults(); IList <CalculationResultItem> calResult = GetCalcResults(); IList <AllocationLineResultItem> allocResult = GetAllocationResults(); ProviderResults providerResults = new ProviderResults() { AllocationResults = allocResult, CalculationResults = calResult }; resultsApiClient.GetProviderByProviderId(Arg.Any <string>()) .Returns(new ApiResponse <Provider>(HttpStatusCode.NotFound, provider)); specsClient.GetFundingPeriods() .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods)); resultsApiClient.GetProviderResults(Arg.Is("2"), Arg.Is("2")) .Returns(new ApiResponse <ProviderResults>(HttpStatusCode.OK, providerResults)); resultsApiClient.GetSpecificationIdsForProvider("2") .Returns(new ApiResponse <IEnumerable <string> >(HttpStatusCode.OK, specSummary)); specsClient .GetSpecificationSummaries(Arg.Any <IEnumerable <string> >()) .Returns(new ApiResponse <IEnumerable <Clients.SpecsClient.Models.SpecificationSummary> >(HttpStatusCode.OK, new List <Clients.SpecsClient.Models.SpecificationSummary>())); // Act Func <Task> test = async() => await provideCalcPageModel.OnGetAsync("2", "1617", "2"); // Assert test .Should() .ThrowExactly <System.InvalidOperationException>().WithMessage("Unable to retreive Provider information: Status Code = NotFound"); }
public async Task OnGetAsync_WhenGettingProviderResultsIsNotSuccess_AllocationDetails_NotPopulated() { // Arrange IResultsApiClient resultsApiClient = CreateApiClient(); ISpecsApiClient specsClient = CreateSpecsApiClient(); IMapper mapper = CreateMapper(); ILogger logger = CreateLogger(); ProviderAllocationLinePageModel providerAllocPageModel = CreatePageModel(resultsApiClient, specsClient, mapper, logger); IEnumerable <Reference> fundingPeriods = new[] { new Reference("1617", "2016-2017"), new Reference("1718", "2017-2018"), new Reference("1819", "2018-2019") }; Provider provider = CreateProvider(); IEnumerable <string> specSummary = GetSpecificationsWithResults(); IList <CalculationResultItem> calResult = GetCalcResults(); IList <AllocationLineResultItem> allocResult = GetAllocationResults(); ProviderResults providerResults = null; resultsApiClient.GetProviderByProviderId(Arg.Any <string>()) .Returns(new ApiResponse <Provider>(HttpStatusCode.OK, provider)); specsClient.GetFundingPeriods() .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods)); resultsApiClient.GetProviderResults(Arg.Is("2"), Arg.Is("2")) .Returns(new ApiResponse <ProviderResults>(HttpStatusCode.NoContent, providerResults)); resultsApiClient.GetSpecificationIdsForProvider("2") .Returns(new ApiResponse <IEnumerable <string> >(HttpStatusCode.OK, specSummary)); specsClient .GetSpecificationSummaries(Arg.Any <IEnumerable <string> >()) .Returns(new ApiResponse <IEnumerable <Clients.SpecsClient.Models.SpecificationSummary> >(HttpStatusCode.OK, new List <Clients.SpecsClient.Models.SpecificationSummary>())); specsClient .GetSpecificationSummaries(Arg.Any <IEnumerable <string> >()) .Returns(new ApiResponse <IEnumerable <Clients.SpecsClient.Models.SpecificationSummary> >(HttpStatusCode.OK, new List <Clients.SpecsClient.Models.SpecificationSummary>())); // Act IActionResult result = await providerAllocPageModel.OnGetAsync("2", "1617", "2"); // Assert PageResult pageResult = result as PageResult; pageResult.Should().NotBeNull(); providerAllocPageModel.ViewModel.AllocationLineItems.Should().HaveCount(0); providerAllocPageModel.ViewModel.CalculationItems.Should().HaveCount(0); }
public async Task OnGetAsync_WhenGettingProviderResponseIsSuccess_PopulatesProviderDetails() { // Arrange IResultsApiClient resultsApiClient = CreateApiClient(); ISpecsApiClient specsClient = CreateSpecsApiClient(); IMapper mapper = CreateMapper(); ILogger logger = CreateLogger(); ProviderAllocationLinePageModel providerAllocPageModel = CreatePageModel(resultsApiClient, specsClient, mapper, logger); IEnumerable <Reference> fundingPeriods = new[] { new Reference("1617", "2016-2017"), new Reference("1718", "2017-2018"), new Reference("1819", "2018-2019") }; Provider provider = CreateProvider(); IEnumerable <string> specSummary = GetSpecificationsWithResults(); IList <CalculationResultItem> calResult = GetCalcResults(); IList <AllocationLineResultItem> allocResult = GetAllocationResults(); ProviderResults providerResults = new ProviderResults() { AllocationResults = allocResult, CalculationResults = calResult }; resultsApiClient.GetProviderByProviderId(Arg.Any <string>()) .Returns(new ApiResponse <Provider>(HttpStatusCode.OK, provider)); specsClient.GetFundingPeriods() .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods)); resultsApiClient.GetProviderResults(Arg.Is("2"), Arg.Is("2")) .Returns(new ApiResponse <ProviderResults>(HttpStatusCode.OK, providerResults)); resultsApiClient.GetSpecificationIdsForProvider("2") .Returns(new ApiResponse <IEnumerable <string> >(HttpStatusCode.OK, specSummary)); specsClient .GetSpecificationSummaries(Arg.Any <IEnumerable <string> >()) .Returns(new ApiResponse <IEnumerable <Clients.SpecsClient.Models.SpecificationSummary> >(HttpStatusCode.OK, new List <Clients.SpecsClient.Models.SpecificationSummary>())); // Act IActionResult result = await providerAllocPageModel.OnGetAsync("2", "1617", "2"); // Assert providerAllocPageModel.ViewModel.Should().NotBeNull(); providerAllocPageModel.ViewModel.Upin.Should().Be(234234); providerAllocPageModel.ViewModel.Ukprn.Should().Be(345345); providerAllocPageModel.ViewModel.Urn.Should().Be(2234); providerAllocPageModel.ViewModel.ProviderType.Should().Be("Academy"); }
public async Task <IActionResult> GetFundingPeriods() { ApiResponse <IEnumerable <Reference> > response = await _specsClient.GetFundingPeriods(); if (response.StatusCode == HttpStatusCode.OK) { return(Ok(response.Content)); } throw new InvalidOperationException($"An error occurred while retrieving code context. Status code={response.StatusCode}"); }
public void OnGetAsync_ReturnsErrorWhenFundingPeriodResponseIsNull() { // Arrange IResultsApiClient resultsApiClient = CreateApiClient(); ISpecsApiClient specsClient = CreateSpecsApiClient(); IMapper mapper = CreateMapper(); ILogger logger = CreateLogger(); ProviderAllocationLinePageModel providerAllocPageModel = CreatePageModel(resultsApiClient, specsClient, mapper, logger); Provider provider = CreateProvider(); IEnumerable <Reference> academicYears = null; IEnumerable <string> specSummary = GetSpecificationsWithResults(); IList <CalculationResultItem> calResult = GetCalcResults(); IList <AllocationLineResultItem> allocResult = GetAllocationResults(); resultsApiClient.GetProviderByProviderId(Arg.Any <string>()) .Returns(new ApiResponse <Provider>(HttpStatusCode.OK, provider)); ProviderResults providerResults = new ProviderResults() { AllocationResults = allocResult, CalculationResults = calResult }; NSubstitute.Core.ConfiguredCall fundingPeriods = specsClient.GetFundingPeriods() .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.NotFound, academicYears)); resultsApiClient.GetProviderResults(Arg.Is("2"), Arg.Is("2")) .Returns(new ApiResponse <ProviderResults>(HttpStatusCode.OK, providerResults)); resultsApiClient.GetSpecificationIdsForProvider("2") .Returns(new ApiResponse <IEnumerable <string> >(HttpStatusCode.OK, specSummary)); // Act Func <Task> test = async() => await providerAllocPageModel.OnGetAsync("2", "1617", "2"); // Assert test .Should() .ThrowExactly <System.InvalidOperationException>().WithMessage("Unable to retreive Periods: Status Code = NotFound"); }
public void OnGetAsync_GivenToPopulateFundingPeriodsIsOKButFundingStreamsReturnsOKButNullContent_ThrowsInvalidOperationException() { //Arrange Specification specification = new Specification(); EditSpecificationViewModel viewModel = CreateEditSpecificationViewModel(); IEnumerable <Reference> fundingPeriods = new[] { new Reference { Id = "fp1", Name = "funding" } }; ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods); ApiResponse <IEnumerable <FundingStream> > fundingStreamsResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK); ApiResponse <Specification> specificationResponse = new ApiResponse <Specification>(HttpStatusCode.OK, specification); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetSpecification(Arg.Is(specificationId)) .Returns(specificationResponse); apiClient .GetFundingPeriods() .Returns(fundingPeriodsResponse); apiClient .GetFundingStreams() .Returns(fundingStreamsResponse); IMapper mapper = CreateMapper(); mapper .Map <EditSpecificationViewModel>(Arg.Is(specification)) .Returns(viewModel); EditSpecificationPageModel pageModel = CreatePageModel(apiClient, mapper); //Act/Assert Func <Task> test = async() => await pageModel.OnGetAsync(specificationId); test .Should() .ThrowExactly <InvalidOperationException>(); }
public async Task OnGetAsync_GivenUserDoesNotHaveCreateSpecificationPermissionForAnyFundingStream_ThenFundingStreamsShouldBeEmpty() { // Arrange IEnumerable <Reference> fundingPeriods = new[] { new Reference { Id = "fp1", Name = "Funding Period 1" }, new Reference { Id = "fp2", Name = "Funding Period 2" } }; IEnumerable <FundingStream> fundingStreams = new[] { new FundingStream { Id = "fp1", Name = "funding" } }; ISpecsApiClient specsClient = Substitute.For <ISpecsApiClient>(); specsClient .GetFundingPeriods() .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods)); specsClient .GetFundingStreams() .Returns(new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams)); IAuthorizationHelper authorizationHelper = Substitute.For <IAuthorizationHelper>(); authorizationHelper .SecurityTrimList(Arg.Any <ClaimsPrincipal>(), Arg.Is(fundingStreams), Arg.Is(FundingStreamActionTypes.CanCreateSpecification)) .Returns(Enumerable.Empty <FundingStream>()); CreateSpecificationPageModel pageModel = CreatePageModel(specsClient: specsClient, authorizationHelper: authorizationHelper); // Act IActionResult result = await pageModel.OnGetAsync(); // Assert result.Should().BeOfType <PageResult>(); pageModel.FundingStreams.Should().BeEmpty(); pageModel .IsAuthorizedToCreate .Should().BeFalse(); }
public async Task OnGetAsync_GivenNullSearchResultsReturns_ReturnsStatusCode500() { // Arrange IEnumerable <Reference> fundingPeriods = new[] { new Reference { Id = "1819", Name = "1018/19" } }; ApiResponse <IEnumerable <Reference> > apiResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods); ISpecsApiClient specsApiClient = CreateApiClient(); specsApiClient .GetFundingPeriods() .Returns(apiResponse); IDatasetRelationshipsSearchService searchService = CreateSearchService(); searchService .PerformSearch(Arg.Any <SearchRequestViewModel>()) .Returns((SpecificationDatasourceRelationshipSearchResultViewModel)null); DatasetRelationshipsPageModel pageModel = CreatePageModel(specsApiClient, searchService); // Act IActionResult actionResult = await pageModel.OnGetAsync(1, string.Empty, string.Empty); // Assert actionResult .Should() .BeOfType <StatusCodeResult>(); StatusCodeResult statusCodeResult = actionResult as StatusCodeResult; statusCodeResult .StatusCode .Should() .Be(500); pageModel .FundingPeriods .Count() .Should() .Be(1); }
public void OnGetAsync_WhenGettingSpecificationSummaryIsNotSuccess_ThrowsError() { // Arrange IResultsApiClient resultsApiClient = CreateApiClient(); ISpecsApiClient specsClient = CreateSpecsApiClient(); IMapper mapper = CreateMapper(); ILogger logger = CreateLogger(); ProviderAllocationLinePageModel providerAllocPageModel = CreatePageModel(resultsApiClient, specsClient, mapper, logger); IEnumerable <Reference> fundingPeriods = new[] { new Reference("1617", "2016-2017"), new Reference("1718", "2017-2018"), new Reference("1819", "2018-2019") }; Provider provider = CreateProvider(); IList <string> specSummary = null; IList <CalculationResultItem> calResult = GetCalcResults(); IList <AllocationLineResultItem> allocResult = GetAllocationResults(); ProviderResults providerResults = null; resultsApiClient.GetProviderByProviderId(Arg.Any <string>()) .Returns(new ApiResponse <Provider>(HttpStatusCode.OK, provider)); specsClient.GetFundingPeriods() .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods)); resultsApiClient.GetProviderResults(Arg.Is("2"), Arg.Is("2")) .Returns(new ApiResponse <ProviderResults>(HttpStatusCode.NoContent, providerResults)); resultsApiClient.GetSpecificationIdsForProvider("2") .Returns(new ApiResponse <IEnumerable <string> >(HttpStatusCode.OK, specSummary)); // Act Func <Task> test = async() => await providerAllocPageModel.OnGetAsync("2", "1617", "2"); // Assert test .Should() .ThrowExactly <System.InvalidOperationException>(); }
private async Task PopulatePeriods(string fundingPeriodId = null) { var periodsResponse = await _specsClient.GetFundingPeriods(); var fundingPeriods = periodsResponse.Content; if (string.IsNullOrWhiteSpace(fundingPeriodId)) { fundingPeriodId = FundingPeriodId; } FundingPeriods = fundingPeriods.Select(m => new SelectListItem { Value = m.Id, Text = m.Name, Selected = m.Id == fundingPeriodId }).ToList(); }
public async Task OnGetAsync_WithValidProviderResultsReturned_PopulatesProviderDetails() { // 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>(); providerScenarioResultsPageModel.ProviderInfoModel.Should().NotBeNull(); providerScenarioResultsPageModel.ProviderInfoModel.Upin.Should().Be(234234); providerScenarioResultsPageModel.ProviderInfoModel.Ukprn.Should().Be(345345); providerScenarioResultsPageModel.ProviderInfoModel.Urn.Should().Be(2234); providerScenarioResultsPageModel.ProviderInfoModel.ProviderType.Should().Be("Academy"); }
public void OnGetAsync_GivenToPopulateFundingPeriodsReturnsOKButNullContent_ThrowsInvalidOperationException() { //Arrange ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetFundingPeriods() .Returns(fundingPeriodsResponse); CreateSpecificationPageModel pageModel = CreatePageModel(apiClient); //Act/Assert Func <Task> test = async() => await pageModel.OnGetAsync(); test .Should() .ThrowExactly <InvalidOperationException>(); }
private async Task PopulateFundingPeriods(string fundingPeriodId = null) { ApiResponse <IEnumerable <Reference> > periodsResponse = await _specsClient.GetFundingPeriods(); if (periodsResponse.StatusCode.Equals(HttpStatusCode.OK) && periodsResponse.Content != null) { IEnumerable <Reference> fundingPeriods = periodsResponse.Content; Reference fundingPeriod = fundingPeriods.FirstOrDefault(); if (fundingPeriod != null) { FundingPeriodId = fundingPeriod.Id; } } else { throw new InvalidOperationException($"Unable to retreive funding period information: Status Code = {periodsResponse.StatusCode}"); } }
public async Task OnGetAsync_WithNullProviderResultsReturned_ReturnsStatusCode500() { // Arrange IResultsApiClient resultsApiClient = CreateApiClient(); ISpecsApiClient specsClient = CreateSpecsApiClient(); ITestScenarioSearchService searchService = CreateTestScenarioSearchService(); ProviderScenarioResultsPageModel providerScenarioResultsPageModel = CreatePageModel(searchService, resultsApiClient, specsApiClient: specsClient); Provider provider = null; 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.NotFound, 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 <StatusCodeResult>().Which.StatusCode.Should().Be(500); }
private async Task <Specification> GenerateSpecification(SpecGeneratorConfiguration configuration) { string periodId = configuration.PeriodId; if (string.IsNullOrWhiteSpace(periodId)) { ApiResponse <IEnumerable <Reference> > periodResponse = await _specsClient.GetFundingPeriods(); Reference firstPeriod = periodResponse.Content.First(); periodId = firstPeriod.Id; } IEnumerable <string> fundingStreamIds = configuration.FundingStreamIds; if (fundingStreamIds == null || !fundingStreamIds.Any()) { ApiResponse <IEnumerable <FundingStream> > fundingStreamResponse = await _specsClient.GetFundingStreams(); fundingStreamIds = new string[] { fundingStreamResponse.Content.First().Id }; } CreateSpecificationModel createModel = new CreateSpecificationModel() { Name = configuration.SpecificationName, FundingPeriodId = periodId, FundingStreamIds = fundingStreamIds, Description = "SpecGenerator " + DateTime.Now.ToLongDateString(), }; _logger.Information("Generating specification called '{Name}' in period '{FundingPeriodId}'", createModel.Name, createModel.FundingPeriodId); ValidatedApiResponse <Specification> specificationResult = await _specsClient.CreateSpecification(createModel); _logger.Information("Created Specification {Id}", specificationResult.Content.Id); return(specificationResult.Content); }
public async Task <IActionResult> OnGetAsync(string fundingPeriod, string fundingStream, ChoosePageBannerOperationType?operationType = null, string operationId = null) { List <Task> tasks = new List <Task>(); Task <ApiResponse <IEnumerable <SpecificationSummary> > > specificationsLookupTask = null; if (!string.IsNullOrWhiteSpace(fundingPeriod) && !string.IsNullOrWhiteSpace(fundingStream)) { specificationsLookupTask = _specsClient.GetApprovedSpecifications(fundingPeriod, fundingStream); tasks.Add(specificationsLookupTask); } Task <ApiResponse <IEnumerable <Reference> > > fundingPeriodsLookupTask = _specsClient.GetFundingPeriods(); Task <ApiResponse <IEnumerable <FundingStream> > > fundingStreamsLookupTask = _specsClient.GetFundingStreams(); tasks.Add(fundingPeriodsLookupTask); tasks.Add(fundingStreamsLookupTask); await TaskHelper.WhenAllAndThrow(tasks.ToArray()); IActionResult errorResult = fundingPeriodsLookupTask.Result.IsSuccessOrReturnFailureResult("Funding Period"); if (errorResult != null) { return(errorResult); } errorResult = fundingStreamsLookupTask.Result.IsSuccessOrReturnFailureResult("Funding Stream"); if (errorResult != null) { return(errorResult); } FundingStreams = fundingStreamsLookupTask.Result.Content.Select(s => new SelectListItem() { Text = s.Name, Value = s.Id, Selected = fundingStream == s.Id, }); FundingPeriods = fundingPeriodsLookupTask.Result.Content.Select(s => new SelectListItem() { Text = s.Name, Value = s.Id, Selected = fundingPeriod == s.Id, }); Dictionary <string, ChooseApprovalSpecificationViewModel> specifications = new Dictionary <string, ChooseApprovalSpecificationViewModel>(); if (!string.IsNullOrWhiteSpace(fundingPeriod) && !string.IsNullOrWhiteSpace(fundingStream)) { errorResult = specificationsLookupTask.Result.IsSuccessOrReturnFailureResult("Specification"); if (errorResult != null) { return(errorResult); } SelectedFundingPeriodId = fundingPeriod; SelectedFundingStreamId = fundingStream; IEnumerable <SpecificationSummary> specificationSummaries = specificationsLookupTask.Result.Content.ToList(); IEnumerable <SpecificationSummary> specificationSummariesTrimmed = await _authorizationHelper.SecurityTrimList(User, specificationSummaries, SpecificationActionTypes.CanChooseFunding); IEnumerable <SpecificationSummary> specificationSummariesUnauthorizedToChoose = specificationSummaries.Except(specificationSummariesTrimmed); IsSpecificationSelectedForThisFunding = specificationSummaries.Any(sc => sc.IsSelectedForFunding); IEnumerable <ChooseApprovalSpecificationViewModel> specificationsAuthorizedViewModel = ConvertToChooseApprovalSpecificationModelWithCanBeChosenFlag(specificationSummariesTrimmed, !IsSpecificationSelectedForThisFunding); IEnumerable <ChooseApprovalSpecificationViewModel> specificationsUnauthorizedViewModel = ConvertToChooseApprovalSpecificationModelWithCanBeChosenFlag(specificationSummariesUnauthorizedToChoose, false); ShouldDisplayPermissionsBanner = specificationsUnauthorizedViewModel.Any(); IEnumerable <ChooseApprovalSpecificationViewModel> specificationViewModels = specificationsAuthorizedViewModel.Concat(specificationsUnauthorizedViewModel); specifications = specificationViewModels.ToDictionary(vm => vm.Id); } if (specifications.Count > 0) { SpecificationIdsRequestModel specificationIdsRequest = new SpecificationIdsRequestModel() { SpecificationIds = specifications.Keys, }; Task <ApiResponse <IEnumerable <CalculationStatusCounts> > > calculationStatusCountsLookupTask = _calcsClient.GetCalculationStatusCounts(specificationIdsRequest); Task <ApiResponse <IEnumerable <FundingCalculationResultsTotals> > > resultTotalsCountsLookupTask = _resultsClient.GetFundingCalculationResultsTotals(specificationIdsRequest); Task <ApiResponse <IEnumerable <SpecificationTestScenarioResultCounts> > > testScenarioResultCountsLookupTask = _testEngineClient.GetTestScenarioCountsForSpecifications(specificationIdsRequest); await TaskHelper.WhenAllAndThrow(calculationStatusCountsLookupTask, resultTotalsCountsLookupTask, testScenarioResultCountsLookupTask); errorResult = calculationStatusCountsLookupTask.Result.IsSuccessOrReturnFailureResult("Calculation Status Counts"); if (errorResult != null) { return(errorResult); } errorResult = resultTotalsCountsLookupTask.Result.IsSuccessOrReturnFailureResult("Calculation Result"); if (errorResult != null) { return(errorResult); } errorResult = testScenarioResultCountsLookupTask.Result.IsSuccessOrReturnFailureResult("Test Scenario Counts"); if (errorResult != null) { return(errorResult); } if (calculationStatusCountsLookupTask.Result.Content.Count() != specificationIdsRequest.SpecificationIds.Count()) { return(new InternalServerErrorResult($"Number of calculation approvals counts ({calculationStatusCountsLookupTask.Result.Content.Count()} does not match number of specifications requested ({specificationIdsRequest.SpecificationIds.Count()}")); } foreach (CalculationStatusCounts counts in calculationStatusCountsLookupTask.Result.Content) { ChooseApprovalSpecificationViewModel chooseVm = specifications[counts.SpecificationId]; chooseVm.CalculationsApproved = counts.Approved; chooseVm.CalculationsTotal = counts.Total; } if (resultTotalsCountsLookupTask.Result.Content.Count() != specificationIdsRequest.SpecificationIds.Count()) { return(new InternalServerErrorResult($"Number of calculation result counts ({resultTotalsCountsLookupTask.Result.Content.Count()} does not match number of specifications requested ({specificationIdsRequest.SpecificationIds.Count()}")); } foreach (FundingCalculationResultsTotals counts in resultTotalsCountsLookupTask.Result.Content) { ChooseApprovalSpecificationViewModel chooseVm = specifications[counts.SpecificationId]; chooseVm.FundingAmount = counts.TotalResult; } if (testScenarioResultCountsLookupTask.Result.Content.Count() != specificationIdsRequest.SpecificationIds.Count()) { return(new InternalServerErrorResult($"Number of test scenario result counts ({testScenarioResultCountsLookupTask.Result.Content.Count()} does not match number of specifications requested ({specificationIdsRequest.SpecificationIds.Count()}")); } foreach (SpecificationTestScenarioResultCounts counts in testScenarioResultCountsLookupTask.Result.Content) { ChooseApprovalSpecificationViewModel chooseVm = specifications[counts.SpecificationId]; chooseVm.QaTestsPassed = counts.Passed; chooseVm.QaTestsTotal = counts.Passed + counts.Ignored + counts.Failed; chooseVm.ProviderQaCoverage = counts.TestCoverage; } } Specifications = specifications.Values.AsEnumerable(); if (operationType.HasValue && specifications.ContainsKey(operationId)) { ChooseApprovalSpecificationViewModel specification = specifications[operationId]; PageBannerOperation = new PageBannerOperation() { EntityName = specification.Name, EntityType = "Specification", OperationAction = "chosen for funding", OperationId = operationId, ActionText = "View funding", ActionUrl = "/approvals/viewfunding/" + specification.Id }; } return(Page()); }
public async Task OnGetAsync_WhenUserDoesNotHaveCreateSpecPermissionOnAllExistingFundingStream_ThenExistingFundingStreamsAvailableForList() { // Arrange Specification specification = new Specification { Id = specificationId, Name = "Test Spec", FundingStreams = new List <FundingStream> { new FundingStream { Id = "fs1", Name = "FS One" }, new FundingStream { Id = "fs2", Name = "FS Two" } }, FundingPeriod = new Reference { Id = "fp1", Name = "FP One" } }; IEnumerable <Reference> fundingPeriods = new[] { new Reference { Id = "fp1", Name = "Funding Period 1" }, new Reference { Id = "fp2", Name = "Funding Period 2" } }; IEnumerable <FundingStream> fundingStreams = new[] { new FundingStream { Id = "fs1", Name = "FS One" }, new FundingStream { Id = "fs2", Name = "FS Two" } }; ISpecsApiClient specsClient = Substitute.For <ISpecsApiClient>(); specsClient .GetSpecification(Arg.Is(specificationId)) .Returns(new ApiResponse <Specification>(HttpStatusCode.OK, specification)); specsClient .GetFundingPeriods() .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods)); specsClient .GetFundingStreams() .Returns(new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams)); IAuthorizationHelper authorizationHelper = Substitute.For <IAuthorizationHelper>(); authorizationHelper .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <ISpecificationAuthorizationEntity>(), Arg.Is(SpecificationActionTypes.CanEditSpecification)) .Returns(true); authorizationHelper .SecurityTrimList(Arg.Any <ClaimsPrincipal>(), Arg.Any <IEnumerable <FundingStream> >(), Arg.Is(FundingStreamActionTypes.CanCreateSpecification)) .Returns(new List <FundingStream> { new FundingStream { Id = "fs1", Name = "FS One" } }); EditSpecificationPageModel pageModel = CreatePageModel(specsClient: specsClient, authorizationHelper: authorizationHelper, mapper: MappingHelper.CreateFrontEndMapper()); // Act await pageModel.OnGetAsync(specificationId); // Assert pageModel.FundingStreams.Should().HaveCount(2); }
public async Task TestScenarioResults_OnGetAsync_WhenRequestedWithDefaultOptions_ThenPageIsReturned() { // Arrange ISpecsApiClient specsApiClient = CreateSpecsApiClient(); ITestScenarioResultsService testScenarioResultsService = CreateScenarioResultsService(); List <Reference> fundingPeriods = new List <Reference> { new Reference("1617", "2016/2017"), new Reference("1718", "2017/2018"), new Reference("1819", "2018/2019") }; TestScenarioResultsPageModel pageModel = CreatePageModel(testScenarioResultsService, specsApiClient); specsApiClient .GetFundingPeriods() .Returns(new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods)); TestScenarioResultViewModel testScenarioResultViewModel = new TestScenarioResultViewModel() { Specifications = new List <ReferenceViewModel>() { new ReferenceViewModel("spec1", "Specification 1"), new ReferenceViewModel("spec2", "Specification 2"), new ReferenceViewModel("spec3", "Specification 3"), }, TestResults = new List <TestScenarioResultItemViewModel>() { new TestScenarioResultItemViewModel() { Id = "ts1", Name = "Test Scenario 1", Passes = 5, Failures = 10, LastUpdatedDate = new DateTime(2018, 1, 5, 7, 8, 9), }, } }; testScenarioResultsService .PerformSearch(Arg.Any <TestScenarioResultRequestViewModel>()) .Returns(testScenarioResultViewModel); // Act IActionResult result = await pageModel.OnGetAsync(null, null, null, null); // Assert result .Should() .NotBeNull() .And .BeOfType <PageResult>(); pageModel .FundingPeriods .Should() .BeEquivalentTo(new List <SelectListItem>() { new SelectListItem() { Text = "2016/2017", Value = "1617", }, new SelectListItem() { Text = "2017/2018", Value = "1718", }, new SelectListItem() { Text = "2018/2019", Value = "1819", }, }); pageModel.SearchResults.Should().NotBeNull(); pageModel .SearchResults .Should() .BeEquivalentTo(testScenarioResultViewModel); }
public async Task OnPostAsync_GivenPagePopulatesButModelStateIsInvalid_ReturnsPage() { //Arrange IEnumerable <Reference> fundingPeriods = new[] { new Reference { Id = "fp1", Name = "funding" } }; IEnumerable <FundingStream> fundingStreams = new[] { new FundingStream { Id = "fs1", Name = "funding stream" } }; ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.OK); ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods); ApiResponse <IEnumerable <FundingStream> > fundingStreamsResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetSpecificationByName(Arg.Is(specName)) .Returns(existingSpecificationResponse); apiClient .GetFundingPeriods() .Returns(fundingPeriodsResponse); apiClient .GetFundingStreams() .Returns(fundingStreamsResponse); IAuthorizationHelper authorizationHelper = Substitute.For <IAuthorizationHelper>(); authorizationHelper .DoesUserHavePermission(Arg.Any <ClaimsPrincipal>(), Arg.Any <ISpecificationAuthorizationEntity>(), Arg.Is(SpecificationActionTypes.CanEditSpecification)) .Returns(true); authorizationHelper .SecurityTrimList(Arg.Any <ClaimsPrincipal>(), Arg.Is(fundingStreams), Arg.Is(FundingStreamActionTypes.CanCreateSpecification)) .Returns(fundingStreams); EditSpecificationPageModel pageModel = CreatePageModel(specsClient: apiClient, authorizationHelper: authorizationHelper); pageModel.EditSpecificationViewModel = new EditSpecificationViewModel { Name = specName, FundingStreamIds = new List <string> { "fs1" } }; pageModel.PageContext = new PageContext(); //Act IActionResult result = await pageModel.OnPostAsync(); //Assert result .Should() .BeOfType <PageResult>(); pageModel .FundingStreams .Count() .Should() .Be(1); pageModel .FundingPeriods .Count() .Should() .Be(1); pageModel .IsAuthorizedToEdit .Should().BeTrue(); }
public async Task OnGetAsync_WhenTestScenariosSearchResultsFoundThenTestCoverageIsCalculated_ThenSuccessfullyShown() { // Arrange IResultsApiClient resultsApiClient = CreateApiClient(); ISpecsApiClient specsClient = CreateSpecsApiClient(); ITestScenarioSearchService searchService = CreateTestScenarioSearchService(); 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(Arg.Any <string>()) .Returns(new ApiResponse <IEnumerable <string> >(HttpStatusCode.OK, specSummary)); resultsApiClient.GetProviderByProviderId(Arg.Any <string>()) .Returns(new ApiResponse <Provider>(HttpStatusCode.OK, provider)); IList <TestScenarioSearchResultItemViewModel> testScenarioSearchResultItems = GetTestScenarioSearchResults(); TestScenarioSearchResultItemViewModel ignoredItem = new TestScenarioSearchResultItemViewModel() { Id = "3", LastUpdatedDate = new DateTime(12, 01, 23), ProviderId = "1", ProviderName = "Provider 1", SpecificationId = "2", SpecificationName = "Spec 02", TestResult = "Ignored", TestScenarioId = "2", TestScenarioName = "Test Scenario 02", LastUpdatedDateDisplay = "1", }; testScenarioSearchResultItems.Add(ignoredItem); TestScenarioSearchResultItemViewModel failedItem = new TestScenarioSearchResultItemViewModel() { Id = "4", LastUpdatedDate = new DateTime(12, 01, 23), ProviderId = "1", ProviderName = "Provider 1", SpecificationId = "2", SpecificationName = "Spec 02", TestResult = "Failed", TestScenarioId = "2", TestScenarioName = "Test Scenario 02", LastUpdatedDateDisplay = "1", }; testScenarioSearchResultItems.Add(failedItem); string specificationId = "2"; TestScenarioSearchResultViewModel results = new TestScenarioSearchResultViewModel() { TestScenarios = testScenarioSearchResultItems, TotalResults = 4, CurrentPage = 1, }; SearchRequestViewModel searchRequest = CreateSearchRequest(); searchRequest.Filters["specificationId"][0] = specificationId; searchService.PerformSearch(Arg.Any <SearchRequestViewModel>()) .Returns(results); specsClient .GetSpecificationSummaries(Arg.Any <IEnumerable <string> >()) .Returns(new ApiResponse <IEnumerable <Clients.SpecsClient.Models.SpecificationSummary> >(HttpStatusCode.OK, new List <Clients.SpecsClient.Models.SpecificationSummary>())); ProviderScenarioResultsPageModel providerScenarioResultsPageModel = CreatePageModel(searchService, resultsApiClient, specsApiClient: specsClient); //Act IActionResult actionResult = await providerScenarioResultsPageModel.OnGetAsync("1", 1, "", "1819", specificationId); // Assert actionResult .Should() .BeOfType <PageResult>(); providerScenarioResultsPageModel.Passed.Should().Be(2); providerScenarioResultsPageModel.Failed.Should().Be(1); providerScenarioResultsPageModel.Ignored.Should().Be(1); providerScenarioResultsPageModel.TestCoverage.Should().Be(75); }
public async Task OnGetAsync_WhenTestScenariosSearchResultsFound_ThenSuccessfullyShown() { // Arrange IResultsApiClient resultsApiClient = CreateApiClient(); ISpecsApiClient specsClient = CreateSpecsApiClient(); ITestScenarioSearchService searchService = CreateTestScenarioSearchService(); 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(Arg.Any <string>()) .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>())); IList <TestScenarioSearchResultItemViewModel> testScenarioSearchResultItems = GetTestScenarioSearchResults(); string specificationId = "2"; TestScenarioSearchResultViewModel results = new TestScenarioSearchResultViewModel() { TestScenarios = testScenarioSearchResultItems, TotalResults = 2, CurrentPage = 1, }; SearchRequestViewModel searchRequest = CreateSearchRequest(); searchRequest.Filters["specificationId"][0] = specificationId; searchService.PerformSearch(Arg.Any <SearchRequestViewModel>()) .Returns(results); ProviderScenarioResultsPageModel providerScenarioResultsPageModel = CreatePageModel(searchService, resultsApiClient, specsApiClient: specsClient); //Act IActionResult actionResult = await providerScenarioResultsPageModel.OnGetAsync("1", 1, "", "1819", specificationId); // Assert actionResult .Should() .BeOfType <PageResult>(); providerScenarioResultsPageModel.TestScenarioSearchResults.Should().NotBeNull(); providerScenarioResultsPageModel.FundingPeriods.Count().Should().Be(3); await searchService.Received(1).PerformSearch(Arg.Is <SearchRequestViewModel>(r => r.PageNumber == searchRequest.PageNumber && r.IncludeFacets == searchRequest.IncludeFacets && r.SearchTerm == searchRequest.SearchTerm && r.Filters["providerId"][0] == searchRequest.Filters["providerId"][0] && r.Filters["specificationId"][0] == searchRequest.Filters["specificationId"][0] )); }
public async Task OnPostAsync_GivenPagePopulatesButModelStateIsInvalid_ReturnsPage() { //Arrange const string specName = "spec name"; IEnumerable <Reference> fundingPeriods = new[] { new Reference { Id = "fp1", Name = "funding" } }; IEnumerable <FundingStream> fundingStreams = new[] { new FundingStream { Id = "fp1", Name = "funding" } }; ApiResponse <Specification> existingSpecificationResponse = new ApiResponse <Specification>(HttpStatusCode.OK); ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods); ApiResponse <IEnumerable <FundingStream> > fundingStreamsResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetSpecificationByName(Arg.Is(specName)) .Returns(existingSpecificationResponse); apiClient .GetFundingPeriods() .Returns(fundingPeriodsResponse); apiClient .GetFundingStreams() .Returns(fundingStreamsResponse); IAuthorizationHelper authorizationHelper = Substitute.For <IAuthorizationHelper>(); authorizationHelper .SecurityTrimList(Arg.Any <ClaimsPrincipal>(), Arg.Is(fundingStreams), Arg.Is(FundingStreamActionTypes.CanCreateSpecification)) .Returns(fundingStreams); CreateSpecificationPageModel pageModel = CreatePageModel(specsClient: apiClient, authorizationHelper: authorizationHelper); pageModel.CreateSpecificationViewModel = new CreateSpecificationViewModel { Name = specName }; pageModel.PageContext.ModelState.AddModelError("test", "Invalid model"); //Act IActionResult result = await pageModel.OnPostAsync(); //Assert result .Should() .BeOfType <PageResult>(); pageModel .FundingStreams .Count() .Should() .Be(1); pageModel .FundingPeriods .Count() .Should() .Be(1); pageModel .IsAuthorizedToCreate .Should().BeTrue(); }
public async Task OnGetAsync_GivenPagePopulatesAndPeriodIdProvided_ReturnsPageSetsPeriodInSelectAsDefault() { //Arrange IEnumerable <Reference> fundingPeriods = new[] { new Reference { Id = "fp1", Name = "Funding Period 1" }, new Reference { Id = "fp2", Name = "Funding Period 2" } }; IEnumerable <FundingStream> fundingStreams = new[] { new FundingStream { Id = "fp1", Name = "funding" } }; ApiResponse <IEnumerable <Reference> > fundingPeriodsResponse = new ApiResponse <IEnumerable <Reference> >(HttpStatusCode.OK, fundingPeriods); ApiResponse <IEnumerable <FundingStream> > fundingStreamsResponse = new ApiResponse <IEnumerable <FundingStream> >(HttpStatusCode.OK, fundingStreams); ISpecsApiClient apiClient = CreateApiClient(); apiClient .GetFundingPeriods() .Returns(fundingPeriodsResponse); apiClient .GetFundingStreams() .Returns(fundingStreamsResponse); IAuthorizationHelper authorizationHelper = Substitute.For <IAuthorizationHelper>(); authorizationHelper .SecurityTrimList(Arg.Any <ClaimsPrincipal>(), Arg.Is(fundingStreams), Arg.Is(FundingStreamActionTypes.CanCreateSpecification)) .Returns(fundingStreams); CreateSpecificationPageModel pageModel = CreatePageModel(specsClient: apiClient, authorizationHelper: authorizationHelper); //Act IActionResult result = await pageModel.OnGetAsync("fp2"); //Assert result .Should() .BeOfType <PageResult>(); pageModel .FundingStreams .Count() .Should() .Be(1); pageModel .FundingPeriods .Count() .Should() .Be(2); pageModel .IsAuthorizedToCreate .Should().BeTrue(); pageModel .FundingPeriods .First(m => m.Value == "fp2") .Selected .Should() .BeTrue(); }