public async Task TestScenarioResultsService_PerformSearch_WhenTestScenariosExistAndValidPeriodIdProvided_ThenResultsAreReturned() { // Arrange IScenarioSearchService searchService = CreateScenarioSearchService(); ISpecificationsApiClient specsApiClient = CreateSpecsApiClient(); ITestEngineApiClient testEngineApiClient = CreateTestEngineApiClient(); string periodId = "1819"; TestScenarioResultsService testScenarioResultsService = CreateService(searchService, specsApiClient, testEngineApiClient); TestScenarioResultRequestViewModel resultRequestViewModel = new TestScenarioResultRequestViewModel() { SearchTerm = "", PageNumber = 1, FundingPeriodId = periodId, 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 .GetSpecifications(Arg.Is(periodId)) .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 searchService .Received(1) .PerformSearch(Arg.Is <SearchRequestViewModel>(a => a.Filters["fundingPeriodId"][0] == periodId)); await specsApiClient .Received(1) .GetSpecifications(periodId); }
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); }