Esempio n. 1
0
        public async Task PerformSearch_GivenFindDatasetDefinitionsServiceReturnsNotFound_ThenNullReturned()
        {
            // Arrange
            IDatasetsApiClient apiClient = CreateApiClient();
            ILogger            logger    = CreateLogger();
            IMapper            mapper    = CreateMapper();

            DatasetDefinitionSearchService searchService = CreateSearchService(apiClient, mapper, logger);

            PagedResult <DatasetDefinitionSearchResultItem> expectedServiceResult = null;

            apiClient
            .FindDatasetDefinitions(Arg.Any <SearchFilterRequest>())
            .Returns(expectedServiceResult);

            SearchRequestViewModel request = new SearchRequestViewModel();

            // Act
            DatasetDefinitionSearchResultViewModel result = await searchService.PerformSearch(request);

            // Assert
            result
            .Should()
            .BeNull();
        }
Esempio n. 2
0
        public async Task PerformSearch_GivenFirstSearchResultReturnedCorrectly_EnsuresResult()
        {
            // Arrange
            IDatasetsApiClient apiClient = CreateApiClient();
            ILogger            logger    = CreateLogger();
            IMapper            mapper    = MappingHelper.CreateFrontEndMapper();

            DatasetDefinitionSearchService searchService = CreateSearchService(apiClient, mapper, logger);

            int numberOfItems = 25;

            PagedResult <DatasetDefinitionSearchResultItem> itemResult = GeneratePagedResult(numberOfItems);

            apiClient
            .FindDatasetDefinitions(Arg.Any <SearchFilterRequest>())
            .Returns(itemResult);

            SearchRequestViewModel request = new SearchRequestViewModel();

            // Act
            DatasetDefinitionSearchResultViewModel results = await searchService.PerformSearch(request);

            // Assert
            results
            .DatasetDefinitions
            .First()
            .Should()
            .BeEquivalentTo(new DatasetDefinitionSearchResultItemViewModel()
            {
                Id                 = "10",
                Description        = "Description 0",
                LastUpdatedDate    = new DateTimeOffset(2018, 1, 2, 3, 4, 0, TimeSpan.Zero),
                Name               = "ds-1",
                ProviderIdentifier = "Provider 0",
            });

            await apiClient
            .Received(1)
            .FindDatasetDefinitions(Arg.Any <SearchFilterRequest>());
        }
Esempio n. 3
0
        public async Task <DatasetDefinitionSearchResultViewModel> PerformSearch(SearchRequestViewModel request)
        {
            SearchFilterRequest requestOptions = new SearchFilterRequest()
            {
                Page          = request.PageNumber.HasValue ? request.PageNumber.Value : 1,
                PageSize      = request.PageSize.HasValue ? request.PageSize.Value : 50,
                SearchTerm    = request.SearchTerm,
                IncludeFacets = request.IncludeFacets,
                Filters       = request.Filters,
                SearchMode    = _featureToggle.IsSearchModeAllEnabled() ? SearchMode.All : SearchMode.Any
            };

            if (request.PageNumber.HasValue && request.PageNumber.Value > 0)
            {
                requestOptions.Page = request.PageNumber.Value;
            }

            PagedResult <DatasetDefinitionSearchResultItem> searchRequestResult = await _datasetsClient.FindDatasetDefinitions(requestOptions);

            if (searchRequestResult == null)
            {
                _logger.Error("Find datasets HTTP request failed");
                return(null);
            }

            DatasetDefinitionSearchResultViewModel result = new DatasetDefinitionSearchResultViewModel
            {
                TotalResults = searchRequestResult.TotalItems,
                CurrentPage  = searchRequestResult.PageNumber,
            };

            List <SearchFacetViewModel> searchFacets = new List <SearchFacetViewModel>();

            if (searchRequestResult.Facets != null)
            {
                foreach (SearchFacet facet in searchRequestResult.Facets)
                {
                    searchFacets.Add(_mapper.Map <SearchFacetViewModel>(facet));
                }
            }

            result.Facets = searchFacets.AsEnumerable();

            List <DatasetDefinitionSearchResultItemViewModel> itemResults = new List <DatasetDefinitionSearchResultItemViewModel>();

            foreach (DatasetDefinitionSearchResultItem searchResult in searchRequestResult.Items)
            {
                itemResults.Add(_mapper.Map <DatasetDefinitionSearchResultItemViewModel>(searchResult));
            }

            result.DatasetDefinitions = itemResults.AsEnumerable();
            if (result.TotalResults == 0)
            {
                result.StartItemNumber = 0;
                result.EndItemNumber   = 0;
            }
            else
            {
                result.StartItemNumber = ((requestOptions.Page - 1) * requestOptions.PageSize) + 1;
                result.EndItemNumber   = result.StartItemNumber + requestOptions.PageSize - 1;
            }

            if (result.EndItemNumber > searchRequestResult.TotalItems)
            {
                result.EndItemNumber = searchRequestResult.TotalItems;
            }

            result.PagerState = new PagerState(requestOptions.Page, searchRequestResult.TotalPages, 4);

            return(result);
        }
Esempio n. 4
0
        public async Task PerformSearch_FirstSearchResultWithFacets_EnsuresFacetsLoadedCorrectly()
        {
            // Arrange
            IDatasetsApiClient apiClient = CreateApiClient();
            ILogger            logger    = CreateLogger();
            IMapper            mapper    = MappingHelper.CreateFrontEndMapper();

            DatasetDefinitionSearchService searchService = CreateSearchService(apiClient, 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 <DatasetDefinitionSearchResultItem> itemResult = GeneratePagedResult(numberOfItems, facets);

            apiClient
            .FindDatasetDefinitions(Arg.Any <SearchFilterRequest>())
            .Returns(itemResult);

            SearchRequestViewModel request = new SearchRequestViewModel();

            // Act
            DatasetDefinitionSearchResultViewModel results = await searchService.PerformSearch(request);

            // Assert
            results
            .DatasetDefinitions
            .First()
            .Should()
            .BeEquivalentTo(
                new DatasetDefinitionSearchResultItemViewModel()
            {
                Id                 = "10",
                Description        = "Description 0",
                LastUpdatedDate    = new DateTimeOffset(2018, 1, 2, 3, 4, 0, TimeSpan.Zero),
                Name               = "ds-1",
                ProviderIdentifier = "Provider 0",
            }
                );

            results
            .Facets
            .Should()
            .BeEquivalentTo(new List <SearchFacetViewModel>()
            {
                new SearchFacetViewModel
                {
                    Name        = "facet 1",
                    FacetValues = new[]
                    {
                        new SearchFacetValueViewModel {
                            Name = "f1", Count = 5
                        }
                    }
                },
                new SearchFacetViewModel
                {
                    Name        = "facet 2",
                    FacetValues = new[]
                    {
                        new SearchFacetValueViewModel {
                            Name = "f2", Count = 11
                        },
                        new SearchFacetValueViewModel {
                            Name = "f3", Count = 1
                        }
                    }
                }
            });

            await apiClient
            .Received(1)
            .FindDatasetDefinitions(Arg.Any <SearchFilterRequest>());
        }