public async Task PerformSearch_FirstSearchResultReturnedCorrectly()
        {
            // Arrange
            ISpecsApiClient specsClient   = Substitute.For <ISpecsApiClient>();
            ILogger         logger        = Substitute.For <ILogger>();
            IMapper         mapper        = MappingHelper.CreateFrontEndMapper();
            IFeatureToggle  featureToggle = CreateFeatureToggle();

            ISpecificationSearchService SpecificationSearchService = new SpecificationSearchService(specsClient, mapper, logger, featureToggle);

            int numberOfItems = 25;

            PagedResult <SpecificationSearchResultItem> itemResult = GeneratePagedResult(numberOfItems);

            specsClient
            .FindSpecifications(Arg.Any <SearchFilterRequest>())
            .Returns(itemResult);

            SearchRequestViewModel request = new SearchRequestViewModel();

            // Act
            SpecificationSearchResultViewModel results = await SpecificationSearchService.PerformSearch(request);

            // Assert
            SpecificationSearchResultItemViewModel first = results.Specifications.First();

            first.Should().NotBeNull();
            first.Id.Should().Be("10");
            first.Name.Should().Be("Specification 1");
            first.Status.Should().Be("Draft");
            first.FundingPeriodName.Should().Be("Test Period");
        }
        public async Task PerformSearch_StartAndEndItemsNumbersDisplayedCorrectlyOnSecondPageOfItemsWithMorePagesAvailable()
        {
            // Arrange
            ISpecsApiClient specsClient   = Substitute.For <ISpecsApiClient>();
            ILogger         logger        = Substitute.For <ILogger>();
            IMapper         mapper        = MappingHelper.CreateFrontEndMapper();
            IFeatureToggle  featureToggle = CreateFeatureToggle();

            ISpecificationSearchService SpecificationSearchService = new SpecificationSearchService(specsClient, mapper, logger, featureToggle);

            int numberOfItems = 50;

            PagedResult <SpecificationSearchResultItem> itemResult = GeneratePagedResult(numberOfItems);

            itemResult.PageNumber = 2;
            itemResult.PageSize   = 50;
            itemResult.TotalItems = 175;

            specsClient
            .FindSpecifications(Arg.Any <SearchFilterRequest>())
            .Returns(itemResult);

            SearchRequestViewModel request = new SearchRequestViewModel()
            {
                PageNumber = 2,
            };

            // Act
            SpecificationSearchResultViewModel results = await SpecificationSearchService.PerformSearch(request);

            // Assert
            results.StartItemNumber.Should().Be(51);
            results.EndItemNumber.Should().Be(100);
        }
        public void PerformSearch_WhenFindSpecificationsServiceUnavailable_ThenHttpExceptionThrown()
        {
            // Arrange
            ISpecsApiClient specsClient   = Substitute.For <ISpecsApiClient>();
            ILogger         logger        = Substitute.For <ILogger>();
            IMapper         mapper        = MappingHelper.CreateFrontEndMapper();
            IFeatureToggle  featureToggle = CreateFeatureToggle();

            ISpecificationSearchService SpecificationSearchService = new SpecificationSearchService(specsClient, mapper, logger, featureToggle);

            specsClient
            .When(a => a.FindSpecifications(Arg.Any <SearchFilterRequest>()))
            .Do(x => { throw new HttpRequestException(); });

            SearchRequestViewModel request = new SearchRequestViewModel();

            // Act
            Action pageAction = new Action(() =>
            {
                SpecificationSearchResultViewModel result = SpecificationSearchService.PerformSearch(request).Result;
            });

            // Assert
            pageAction.Should().Throw <HttpRequestException>();
        }
        public async Task PerformSearch_FirstSearchResultWithFacets_ReturnedCorrectly()
        {
            // Arrange
            ISpecificationsApiClient specsClient = Substitute.For <ISpecificationsApiClient>();
            ILogger logger = Substitute.For <ILogger>();
            IMapper mapper = MappingHelper.CreateFrontEndMapper();

            ISpecificationSearchService SpecificationSearchService = new SpecificationSearchService(specsClient, mapper, logger);

            int numberOfItems = 25;

            IEnumerable <SearchFacet> facets = new[]
            {
                new SearchFacet(), new SearchFacet()
            };

            PagedResult <SpecificationSearchResultItem> itemResult = GeneratePagedResult(numberOfItems, facets);

            specsClient
            .FindSpecifications(Arg.Any <SearchFilterRequest>())
            .Returns(itemResult);

            SearchRequestViewModel request = new SearchRequestViewModel();

            // Act
            SpecificationSearchResultViewModel results = await SpecificationSearchService.PerformSearch(request);

            // Assert
            SpecificationSearchResultItemViewModel first = results.Specifications.First();

            first
            .Should()
            .NotBeNull();

            first
            .Should()
            .BeEquivalentTo(new SpecificationSearchResultItemViewModel()
            {
                Id   = "10",
                Name = "Specification 1",
                FundingPeriodName    = "Test Period",
                FundingPeriodId      = "FundingPeriodID",
                Status               = "Draft",
                Description          = "Description",
                FundingStreamIds     = new[] { "fs1", "fs2" },
                FundingStreamNames   = new[] { "Funding Stream 1", "Funding Stream 2" },
                LastUpdatedDate      = new DateTime(2018, 12, 5, 12, 5, 6),
                IsSelectedForFunding = true
            });

            results
            .Facets
            .Count()
            .Should()
            .Be(2);
        }
        public async Task PerformSearch_WhenFindSpecificationsServiceReturnsNotFound_ThenNullReturned()
        {
            // Arrange
            ISpecificationsApiClient specsClient = Substitute.For <ISpecificationsApiClient>();
            ILogger logger = Substitute.For <ILogger>();
            IMapper mapper = MappingHelper.CreateFrontEndMapper();

            ISpecificationSearchService SpecificationSearchService = new SpecificationSearchService(specsClient, mapper, logger);

            PagedResult <SpecificationSearchResultItem> expectedServiceResult = null;

            specsClient
            .FindSpecifications(Arg.Any <SearchFilterRequest>())
            .Returns(expectedServiceResult);

            SearchRequestViewModel request = new SearchRequestViewModel();

            // Act
            SpecificationSearchResultViewModel result = await SpecificationSearchService.PerformSearch(request);

            // Assert
            result.Should().BeNull();
        }
        public async Task PerformSearch_WhenFeatureToggleIsSwitchedOn_EnsureSearchModeIsAll()
        {
            // Arrange
            ISpecsApiClient specsClient   = Substitute.For <ISpecsApiClient>();
            ILogger         logger        = Substitute.For <ILogger>();
            IMapper         mapper        = MappingHelper.CreateFrontEndMapper();
            IFeatureToggle  featureToggle = CreateFeatureToggle(true);

            ISpecificationSearchService SpecificationSearchService = new SpecificationSearchService(specsClient, mapper, logger, featureToggle);

            SearchRequestViewModel request = new SearchRequestViewModel()
            {
                PageNumber = 2,
            };

            // Act
            SpecificationSearchResultViewModel results = await SpecificationSearchService.PerformSearch(request);

            // Assert
            await
            specsClient
            .Received(1)
            .FindSpecifications(Arg.Is <SearchFilterRequest>(m => m.SearchMode == SearchMode.All));
        }
        public async Task PerformSearch_FirstSearchResultWithFacets_EnsuresFacetsLoadedCorrectly()
        {
            // Arrange
            ISpecificationsApiClient specsClient = Substitute.For <ISpecificationsApiClient>();
            ILogger logger = Substitute.For <ILogger>();
            IMapper mapper = MappingHelper.CreateFrontEndMapper();

            ISpecificationSearchService SpecificationSearchService = new SpecificationSearchService(specsClient, 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 <SpecificationSearchResultItem> itemResult = GeneratePagedResult(numberOfItems, facets);

            specsClient
            .FindSpecifications(Arg.Any <SearchFilterRequest>())
            .Returns(itemResult);

            SearchRequestViewModel request = new SearchRequestViewModel();

            // Act
            SpecificationSearchResultViewModel results = await SpecificationSearchService.PerformSearch(request);

            // Assert
            SpecificationSearchResultItemViewModel first = results.Specifications.First();

            first.Should().NotBeNull();
            first.Id.Should().Be("10");
            first.Name.Should().Be("Specification 1");
            first.Status.Should().Be("Draft");
            first.FundingPeriodName.Should().Be("Test Period");

            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);
        }