public async Task PerformSearch_GivenFirstSearchResultReturnedCorrectly_EnsuresResults() { // Arrange IResultsApiClient resultClient = Substitute.For <IResultsApiClient>(); ILogger logger = CreateLogger(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); CalculationProviderResultsSearchService resultsSearchService = CreateSearchService(resultClient, mapper, logger); int numberOfItems = 25; PagedResult <CalculationProviderResultSearchResultItem> itemResult = GeneratePagedResult(numberOfItems); resultClient .FindCalculationProviderResults(Arg.Any <SearchFilterRequest>()) .Returns(itemResult); SearchRequestViewModel request = new SearchRequestViewModel(); // Act CalculationProviderResultSearchResultViewModel results = await resultsSearchService.PerformSearch(request); // Assert results.TotalResults.Should().Be(numberOfItems + 1); results.TotalErrorResults.Should().Be(1); }
public async Task PerformSearch_GivenFindCalculationProviderResultsServiceReturnsNotFound_ThenNullReturned() { // Arrange IResultsApiClient resultClient = Substitute.For <IResultsApiClient>(); ILogger logger = CreateLogger(); IMapper mapper = CreateMapper(); CalculationProviderResultsSearchService resultsSearchService = CreateSearchService(resultClient, mapper, logger); PagedResult <CalculationProviderResultSearchResultItem> expectedServiceResult = null; resultClient .FindCalculationProviderResults(Arg.Any <SearchFilterRequest>()) .Returns(expectedServiceResult); SearchRequestViewModel request = new SearchRequestViewModel(); // Act CalculationProviderResultSearchResultViewModel result = await resultsSearchService.PerformSearch(request); // Assert result .Should() .BeNull(); }
public async Task SearchTestScenarioResults_GivenPageNumberIsZero_LogsAndReturnsBadRequest() { //Arrange SearchModel model = new SearchModel(); string json = JsonConvert.SerializeObject(model); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpRequest request = Substitute.For <HttpRequest>(); request .Body .Returns(stream); ILogger logger = CreateLogger(); ISearchRepository <CalculationProviderResultsIndex> searchRepository = CreateSearchRepository(); CalculationProviderResultsSearchService service = CreateTestResultsSearchService(logger, searchRepository); //Act IActionResult result = await service.SearchCalculationProviderResults(request); //Assert logger .Received(1) .Error("A null or invalid search model was provided for searching calculation provider results"); result .Should() .BeOfType <BadRequestObjectResult>(); }
public async Task SearchTestScenarioResults_GivenValidModelWithOneFilterAndOverrridesFacets_ThenSearchIsPerformedWithTwoFilters() { //Arrange SearchModel model = new SearchModel { PageNumber = 1, Top = 50, IncludeFacets = true, Filters = new Dictionary <string, string[]>() { { "calculationId", new string [] { "test" } } }, SearchTerm = "testTerm", OverrideFacetFields = new[] { "providerId", "calculationId" } }; string json = JsonConvert.SerializeObject(model); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpRequest request = Substitute.For <HttpRequest>(); request .Body .Returns(stream); ILogger logger = CreateLogger(); SearchResults <CalculationProviderResultsIndex> searchResults = new SearchResults <CalculationProviderResultsIndex>(); ISearchRepository <CalculationProviderResultsIndex> searchRepository = CreateSearchRepository(); searchRepository .Search(Arg.Any <string>(), Arg.Any <SearchParameters>()) .Returns(searchResults); CalculationProviderResultsSearchService service = CreateTestResultsSearchService(logger, searchRepository); //Act IActionResult result = await service.SearchCalculationProviderResults(request); //Assert result .Should() .BeOfType <OkObjectResult>(); await searchRepository .Received(2) .Search(model.SearchTerm, Arg.Is <SearchParameters>(c => model.Filters.Keys.All(f => c.Filter.Contains(f)) && !string.IsNullOrWhiteSpace(c.Filter) )); }
public void TestInitialize() { _resultsClient = Substitute.For <IResultsApiClient>(); _calculationsClient = Substitute.For <ICalculationsApiClient>(); MapperConfiguration mapperConfiguration = new MapperConfiguration(c => { c.AddProfile <FrontEndMappingProfile>(); }); _mapper = mapperConfiguration.CreateMapper(); _logger = Substitute.For <ILogger>(); _resultsSearchService = new CalculationProviderResultsSearchService(_resultsClient, _calculationsClient, _mapper, _logger); }
public async Task SearchTestScenarioResults_GivenValidModelAndIncludesGettingFacetsAndFiltersIsNull_PerformsSearch() { //Arrange SearchModel model = new SearchModel { PageNumber = 1, Top = 50, IncludeFacets = true, Filters = null, }; string json = JsonConvert.SerializeObject(model); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpRequest request = Substitute.For <HttpRequest>(); request .Body .Returns(stream); ILogger logger = CreateLogger(); SearchResults <CalculationProviderResultsIndex> searchResults = new SearchResults <CalculationProviderResultsIndex>(); ISearchRepository <CalculationProviderResultsIndex> searchRepository = CreateSearchRepository(); searchRepository .Search(Arg.Any <string>(), Arg.Any <SearchParameters>()) .Returns(searchResults); CalculationProviderResultsSearchService service = CreateTestResultsSearchService(logger, searchRepository); //Act IActionResult result = await service.SearchCalculationProviderResults(request); //Assert result .Should() .BeOfType <OkObjectResult>(); await searchRepository .Received(11) .Search(Arg.Any <string>(), Arg.Any <SearchParameters>()); }
public async Task SearchCalculationProviderResults_GivenSearchRequestFails_ThenBadRequestReturned() { //Arrange SearchModel model = new SearchModel() { SearchTerm = "SearchTermTest", PageNumber = 1, IncludeFacets = false, Top = 50, }; string json = JsonConvert.SerializeObject(model); byte[] byteArray = Encoding.UTF8.GetBytes(json); MemoryStream stream = new MemoryStream(byteArray); HttpRequest request = Substitute.For <HttpRequest>(); request .Body .Returns(stream); ILogger logger = CreateLogger(); ISearchRepository <CalculationProviderResultsIndex> searchRepository = CreateSearchRepository(); searchRepository .When(s => s.Search(Arg.Any <string>(), Arg.Any <SearchParameters>())) .Do(x => { throw new FailedToQuerySearchException("Test Message", null); }); CalculationProviderResultsSearchService service = CreateTestResultsSearchService(logger, searchRepository); //Act IActionResult result = await service.SearchCalculationProviderResults(request); //Assert logger .Received(1) .Error(Arg.Any <FailedToQuerySearchException>(), "Failed to query search with term: SearchTermTest"); result .Should() .BeOfType <StatusCodeResult>() .Which.StatusCode.Should().Be(500); }
public async Task SearchCalculationProviderResults_GivenNullModel_ReturnsBadRequest() { //Arrange HttpRequest httpRequest = Substitute.For <HttpRequest>(); ILogger logger = CreateLogger(); CalculationProviderResultsSearchService service = CreateTestResultsSearchService(logger: logger); //Act IActionResult result = await service.SearchCalculationProviderResults(httpRequest); //Assert result .Should() .BeOfType <BadRequestObjectResult>(); logger .Received(1) .Error(Arg.Is("A null or invalid search model was provided for searching calculation provider results")); }
public void PerformSearch_GivenFindCalculationProviderResultsServiceUnavailable_ThenHttpExceptionThrown() { // Arrange IResultsApiClient resultClient = Substitute.For <IResultsApiClient>(); ILogger logger = CreateLogger(); IMapper mapper = CreateMapper(); CalculationProviderResultsSearchService resultsSearchService = CreateSearchService(resultClient, mapper, logger); resultClient .When(a => a.FindCalculationProviderResults(Arg.Any <SearchFilterRequest>())) .Do(x => { throw new HttpRequestException(); }); SearchRequestViewModel request = new SearchRequestViewModel(); // Act Func <Task> test = () => resultsSearchService.PerformSearch(request); // Assert test .Should() .Throw <HttpRequestException>(); }
public async Task PerformSearch_GivenFirstSearchResultReturnedCorrectly_EnsuresResult() { // Arrange IResultsApiClient resultClient = Substitute.For <IResultsApiClient>(); ILogger logger = CreateLogger(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); CalculationProviderResultsSearchService resultsSearchService = CreateSearchService(resultClient, mapper, logger); int numberOfItems = 25; PagedResult <CalculationProviderResultSearchResultItem> itemResult = GeneratePagedResult(numberOfItems); resultClient .FindCalculationProviderResults(Arg.Any <SearchFilterRequest>()) .Returns(itemResult); SearchRequestViewModel request = new SearchRequestViewModel(); // Act CalculationProviderResultSearchResultViewModel results = await resultsSearchService.PerformSearch(request); // Assert CalculationProviderResultSearchResultItemViewModel first = results.CalculationProviderResults.First(); first .Should() .NotBeNull(); first .Id .Should() .Be("10"); first .CalculationType .Should() .Be(CalculationSpecificationType.Number); first .Name .Should() .Be("prov-1"); first .CalculationResult .Should() .Be(1); CalculationProviderResultSearchResultItemViewModel last = results.CalculationProviderResults.Last(); last .CalculationExceptionType .Should() .Be("Exception"); last .CalculationExceptionMessage .Should() .Be("An exception has occurred"); }
public async Task PerformSearch_FirstSearchResultWithFacets_EnsuresFacetsLoadedCorrectly() { // Arrange IResultsApiClient resultClient = Substitute.For <IResultsApiClient>(); ILogger logger = CreateLogger(); IMapper mapper = MappingHelper.CreateFrontEndMapper(); CalculationProviderResultsSearchService resultsSearchService = CreateSearchService(resultClient, mapper, logger); int numberOfItems = 25; IEnumerable <SearchFacet> facets = new[] { new SearchFacet { Name = "facet 1", FacetValues = new[] { new SearchFacetValue { Name = "f1", Count = 5 } } }, new SearchFacet { Name = "facet 2", FacetValues = new[] { new SearchFacetValue { Name = "f2", Count = 11 }, new SearchFacetValue { Name = "f3", Count = 1 } } } }; PagedResult <CalculationProviderResultSearchResultItem> itemResult = GeneratePagedResult(numberOfItems, facets); resultClient .FindCalculationProviderResults(Arg.Any <SearchFilterRequest>()) .Returns(itemResult); SearchRequestViewModel request = new SearchRequestViewModel(); // Act CalculationProviderResultSearchResultViewModel results = await resultsSearchService.PerformSearch(request); // Assert CalculationProviderResultSearchResultItemViewModel first = results.CalculationProviderResults.First(); first .Should() .NotBeNull(); first .Id .Should() .Be("10"); first .CalculationType .Should() .Be(CalculationSpecificationType.Number); first .Name .Should() .Be("prov-1"); first .CalculationResult .Should() .Be(1); results.Facets.Count().Should().Be(2); results.Facets.First().Name.Should().Be("facet 1"); results.Facets.First().FacetValues.Count().Should().Be(1); results.Facets.First().FacetValues.First().Name.Should().Be("f1"); results.Facets.First().FacetValues.First().Count.Should().Be(5); results.Facets.Last().Name.Should().Be("facet 2"); results.Facets.Last().FacetValues.Count().Should().Be(2); results.Facets.Last().FacetValues.First().Name.Should().Be("f2"); results.Facets.Last().FacetValues.First().Count.Should().Be(11); results.Facets.Last().FacetValues.Last().Name.Should().Be("f3"); results.Facets.Last().FacetValues.Last().Count.Should().Be(1); }