public async Task <IActionResult> OnGetAsync(string scenarioId, int?pageNumber, string searchTerm)
        {
            TestScenario = await GetTestScenario(scenarioId);

            if (TestScenario == null)
            {
                return(new NotFoundResult());
            }

            SearchRequestViewModel searchRequest = new SearchRequestViewModel()
            {
                PageNumber    = pageNumber,
                IncludeFacets = false,
                SearchTerm    = searchTerm,
                Filters       = new Dictionary <string, string[]> {
                    { "testScenarioId", new[] { scenarioId } }
                }
            };

            SearchTerm = searchTerm;

            Task <ProviderTestsSearchResultViewModel> providerResultsTask           = _testResultsSearchService.PerformProviderTestResultsSearch(searchRequest);
            Task <ApiResponse <IEnumerable <TestScenarioResultCounts> > > countTask = _testEngineClient.GetTestResultCounts(new TestScenarioResultCountsRequestModel()
            {
                TestScenarioIds = new string[] { scenarioId }
            });


            await TaskHelper.WhenAllAndThrow(providerResultsTask, countTask);

            ProviderResults = providerResultsTask.Result;

            if (ProviderResults == null)
            {
                return(new InternalServerErrorResult("Provider Results returned null"));
            }

            if (countTask.Result == null)
            {
                return(new InternalServerErrorResult("Count Task result was null"));
            }

            if (countTask.Result.StatusCode != System.Net.HttpStatusCode.OK)
            {
                return(new InternalServerErrorResult($"Count Task didn't return OK, but instead '{countTask.Result.StatusCode}'"));
            }

            if (countTask.Result.Content == null)
            {
                return(new InternalServerErrorResult("Count Task result content was null"));
            }

            TestScenarioResultCounts scenarioResultCounts = countTask.Result.Content.FirstOrDefault();

            if (scenarioResultCounts != null)
            {
                TestCoverage = scenarioResultCounts.TestCoverage;
            }
            else
            {
                TestCoverage = 0;
            }

            ApiResponse <SpecificationSummary> specResponse = await _specsClient.GetSpecificationSummary(TestScenario.SpecificationId);

            if (specResponse == null)
            {
                return(new InternalServerErrorResult("Specification summary API call result was null"));
            }

            if (specResponse.StatusCode != System.Net.HttpStatusCode.OK)
            {
                return(new InternalServerErrorResult($"Specification summary API call didn't return OK, but instead '{countTask.Result.StatusCode}'"));
            }

            if (specResponse.Content == null)
            {
                return(new InternalServerErrorResult("Specification summary API call content was null"));
            }

            Specification = _mapper.Map <SpecificationSummaryViewModel>(specResponse.Content);

            return(Page());
        }
        public async Task <TestScenarioResultViewModel> PerformSearch(TestScenarioResultRequestViewModel request)
        {
            Guard.ArgumentNotNull(request, nameof(request));

            SearchRequestViewModel searchRequest = new SearchRequestViewModel()
            {
                IncludeFacets = false,
                SearchTerm    = request.SearchTerm,
                Filters       = request.Filters,
                PageNumber    = request.PageNumber,
                PageSize      = 20,
                SearchMode    = SearchMode.All
            };

            if (searchRequest.Filters == null)
            {
                searchRequest.Filters = new Dictionary <string, string[]>();
            }

            SetFilterValue(searchRequest, "specificationId", request.SpecificationId);
            SetFilterValue(searchRequest, "fundingPeriodId", request.FundingPeriodId);

            Task <ScenarioSearchResultViewModel> scenarioSearchResultsTask = _scenariosSearchService.PerformSearch(searchRequest);
            Task <ApiResponse <IEnumerable <SpecificationSummary> > > specificationsLookupTask;

            if (string.IsNullOrWhiteSpace(request.FundingPeriodId))
            {
                specificationsLookupTask = _specsClient.GetSpecificationSummaries();
            }
            else
            {
                specificationsLookupTask = _specsClient.GetSpecifications(request.FundingPeriodId);
            }

            await TaskHelper.WhenAllAndThrow(scenarioSearchResultsTask, specificationsLookupTask);

            ScenarioSearchResultViewModel scenarioSearchResults = scenarioSearchResultsTask.Result;

            if (scenarioSearchResults == null)
            {
                _logger.Warning("Scenario Search Results response was null");
                throw new InvalidOperationException("Scenario Search Results response was null");
            }

            ApiResponse <IEnumerable <SpecificationSummary> > specificationsApiResponse = specificationsLookupTask.Result;

            if (specificationsApiResponse == null)
            {
                _logger.Warning("Specifications API Response was null");
                throw new InvalidOperationException("Specifications API Response was null");
            }

            if (specificationsApiResponse.StatusCode != HttpStatusCode.OK)
            {
                _logger.Warning("Specifications API Response content did not return OK, but instead {specificationsApiResponse.StatusCode}", specificationsApiResponse.StatusCode);
                throw new InvalidOperationException($"Specifications API Response content did not return OK, but instead '{specificationsApiResponse.StatusCode}'");
            }

            if (specificationsApiResponse.Content == null)
            {
                _logger.Warning("Specifications API Response content was null");
                throw new InvalidOperationException("Specifications API Response content was null");
            }

            IEnumerable <string>        testScenarioIds = scenarioSearchResults.Scenarios.Select(s => s.Id);
            TestScenarioResultViewModel result          = _mapper.Map <TestScenarioResultViewModel>(scenarioSearchResults);

            List <ReferenceViewModel> specifications = new List <ReferenceViewModel>();

            foreach (SpecificationSummary specification in specificationsApiResponse.Content.OrderBy(s => s.Name))
            {
                specifications.Add(new ReferenceViewModel(specification.Id, specification.Name));
            }

            result.Specifications = specifications;

            if (testScenarioIds.Any())
            {
                ApiResponse <IEnumerable <TestScenarioResultCounts> > rowCounts = await _testEngineClient.GetTestResultCounts(new TestScenarioResultCountsRequestModel()
                {
                    TestScenarioIds = testScenarioIds,
                });

                if (rowCounts == null)
                {
                    _logger.Warning("Row counts api request failed with null response");
                    throw new InvalidOperationException($"Row counts api request failed with null response");
                }

                if (rowCounts.StatusCode != HttpStatusCode.OK)
                {
                    _logger.Warning("Row counts api request failed with status code: {rowCounts.StatusCode}", rowCounts.StatusCode);
                    throw new InvalidOperationException($"Row counts api request failed with status code: {rowCounts.StatusCode}");
                }

                if (rowCounts.Content == null)
                {
                    _logger.Warning("Row counts api request failed with null content");
                    throw new InvalidOperationException($"Row counts api request failed with null content");
                }

                foreach (TestScenarioResultItemViewModel vm in result.TestResults)
                {
                    TestScenarioResultCounts counts = rowCounts.Content.Where(r => r.TestScenarioId == vm.Id).FirstOrDefault();
                    if (counts != null)
                    {
                        vm.Failures = counts.Failed;
                        vm.Passes   = counts.Passed;
                        vm.Ignored  = counts.Ignored;
                    }
                }
            }

            return(result);
        }