コード例 #1
0
        public async Task SearchSpecificationDatasetRelationships_GivenInvalidTop_ReturnsBadRequest()
        {
            //Arrange
            SearchModel model = new SearchModel
            {
                PageNumber = 1,
                Top        = 0
            };
            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();

            SpecificationsSearchService service = CreateSearchService(logger: logger);

            //Act
            IActionResult result = await service.SearchSpecificationDatasetRelationships(request);

            //Assert
            logger
            .Received(1)
            .Error("A null or invalid search model was provided for searching specifications");

            result
            .Should()
            .BeOfType <BadRequestObjectResult>();
        }
コード例 #2
0
        public async Task SearchSpecificationDatasetRelationships_GivenSearchReturnsResults_ReturnsOKResult(params string[] expectedSearchFields)
        {
            //Arrange
            SearchModel model = CreateSearchModel(expectedSearchFields);

            expectedSearchFields ??= new[]
            {
                "name"
            };

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>
            {
                TotalCount = 1
            };

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Is("SearchTermTest"), Arg.Is <SearchParameters>(sp =>
                                                                        sp.SearchFields != null &&
                                                                        sp.SearchFields.SequenceEqual(expectedSearchFields)))
            .Returns(searchResults);

            ILogger logger = CreateLogger();

            SpecificationsSearchService service = CreateSearchService(searchRepository, logger);

            //Act
            IActionResult result = await service.SearchSpecificationDatasetRelationships(model);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();
        }
コード例 #3
0
        public async Task SearchSpecifications_GivenValidModelAndIncludesGettingFacets_CallsSearchFourTimes()
        {
            //Arrange
            SearchModel model = new SearchModel
            {
                PageNumber    = 1,
                Top           = 50,
                IncludeFacets = true
            };

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>();

            ILogger logger = CreateLogger();

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Any <string>(), Arg.Any <SearchParameters>())
            .Returns(searchResults);

            SpecificationsSearchService service = CreateSearchService(logger: logger, searchRepository: searchRepository);

            //Act
            IActionResult result = await service.SearchSpecifications(model);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            await
            searchRepository
            .Received(4)
            .Search(Arg.Any <string>(), Arg.Any <SearchParameters>());
        }
コード例 #4
0
        public async Task SearchSpecificationDatasetRelationships_GivenSearchThrowsException_ReturnsStatusCode500()
        {
            //Arrange
            SearchModel model = CreateSearchModel();

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .When(x => x.Search(Arg.Any <string>(), Arg.Any <SearchParameters>()))
            .Do(x => { throw new FailedToQuerySearchException("main", new Exception("inner")); });

            ILogger logger = CreateLogger();

            SpecificationsSearchService service = CreateSearchService(searchRepository, logger);

            //Act
            IActionResult result = await service.SearchSpecificationDatasetRelationships(model);

            //Assert
            result
            .Should()
            .BeOfType <StatusCodeResult>();

            StatusCodeResult statusCodeResult = result as StatusCodeResult;

            statusCodeResult
            .StatusCode
            .Should()
            .Be(500);
        }
コード例 #5
0
        public async Task SearchSpecifications_GivenInvalidTop_ReturnsBadRequest()
        {
            //Arrange
            SearchModel model = new SearchModel
            {
                PageNumber = 1,
                Top        = 0
            };

            ILogger logger = CreateLogger();

            SpecificationsSearchService service = CreateSearchService(logger: logger);

            //Act
            IActionResult result = await service.SearchSpecifications(model);

            //Assert
            logger
            .Received(1)
            .Error("A null or invalid search model was provided for searching specifications");

            result
            .Should()
            .BeOfType <BadRequestObjectResult>();
        }
コード例 #6
0
        public async Task SearchSpecificationDatasetRelationships_GivenSearchReturnsResultsAndDataDefinitionsHasItems_ReturnsOKResult()
        {
            //Arrange
            SearchModel model = CreateSearchModel();

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>
            {
                TotalCount = 1,
                Results    = new List <Repositories.Common.Search.SearchResult <SpecificationIndex> >
                {
                    new Repositories.Common.Search.SearchResult <SpecificationIndex>
                    {
                        Result = new SpecificationIndex
                        {
                            DataDefinitionRelationshipIds = new[] { "def-1", "def-2" }
                        }
                    }
                }
            };

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Is("SearchTermTest"), Arg.Any <SearchParameters>())
            .Returns(searchResults);

            ILogger logger = CreateLogger();

            SpecificationsSearchService service = CreateSearchService(searchRepository, logger);

            //Act
            IActionResult result = await service.SearchSpecificationDatasetRelationships(model);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            OkObjectResult okObjectResult = result as OkObjectResult;

            SpecificationDatasetRelationshipsSearchResults specificationSearchResults = okObjectResult.Value as SpecificationDatasetRelationshipsSearchResults;

            specificationSearchResults
            .Results
            .Count()
            .Should()
            .Be(1);

            specificationSearchResults
            .TotalCount
            .Should()
            .Be(1);

            specificationSearchResults
            .Results
            .First()
            .DefinitionRelationshipCount
            .Should()
            .Be(2);
        }
        public async Task SearchSpecifications_GivenValidModel_CallsSearchWithCorrectSkipValue()
        {
            //Arrange
            SearchModel model = new SearchModel
            {
                PageNumber    = 10,
                Top           = 50,
                IncludeFacets = true
            };

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

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>
            {
                Facets = new List <Facet>
                {
                    new Facet
                    {
                        Name = "fundingPeriodName"
                    }
                }
            };

            ILogger logger = CreateLogger();

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Any <string>(), Arg.Any <SearchParameters>())
            .Returns(searchResults);

            SpecificationsSearchService service = CreateSearchService(logger: logger, searchRepository: searchRepository);

            //Act
            IActionResult result = await service.SearchSpecifications(request);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            await
            searchRepository
            .Received(4)
            .Search(Arg.Any <string>(), Arg.Any <SearchParameters>());
        }
        public async Task SearchSpecifications_GivenValidModelWithNullFilterWithMultipleOfSameFilter_ThenSearchIsPerformed()
        {
            //Arrange
            SearchModel model = new SearchModel
            {
                PageNumber    = 1,
                Top           = 50,
                IncludeFacets = true,
                Filters       = new Dictionary <string, string[]>()
                {
                    { "status", new string [] { "test", "" } }
                },
                SearchTerm = "testTerm",
            };

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

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>();

            ILogger logger = CreateLogger();

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Any <string>(), Arg.Any <SearchParameters>())
            .Returns(searchResults);

            SpecificationsSearchService service = CreateSearchService(logger: logger, searchRepository: searchRepository);

            //Act
            IActionResult result = await service.SearchSpecifications(request);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            await
            searchRepository
            .Received(3)
            .Search(model.SearchTerm, Arg.Is <SearchParameters>(c =>
                                                                model.Filters.Keys.All(f => c.Filter.Contains(f)) &&
                                                                !string.IsNullOrWhiteSpace(c.Filter)
                                                                ));
        }
        public async Task SearchSpecifications_GivenValidModelAndPageNumber2_CallsSearchWithCorrectSkipValue()
        {
            //Arrange
            const int skipValue = 50;

            SearchModel model = new SearchModel
            {
                PageNumber = 2,
                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);

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>();

            ILogger logger = CreateLogger();

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Any <string>(), Arg.Any <SearchParameters>())
            .Returns(searchResults);

            SpecificationsSearchService service = CreateSearchService(logger: logger, searchRepository: searchRepository);

            //Act
            IActionResult result = await service.SearchSpecifications(request);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            await
            searchRepository
            .Received(1)
            .Search(Arg.Any <string>(), Arg.Is <SearchParameters>(m => m.Skip == skipValue));
        }
コード例 #10
0
        public async Task SearchSpecifications_GivenValidModel_CallsSearchWithCorrectSkipValue()
        {
            //Arrange
            SearchModel model = new SearchModel
            {
                PageNumber    = 10,
                Top           = 50,
                IncludeFacets = true
            };

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>
            {
                Facets = new List <Facet>
                {
                    new Facet
                    {
                        Name = "fundingPeriodName"
                    }
                }
            };

            ILogger logger = CreateLogger();

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Any <string>(), Arg.Any <SearchParameters>())
            .Returns(searchResults);

            SpecificationsSearchService service = CreateSearchService(logger: logger, searchRepository: searchRepository);

            //Act
            IActionResult result = await service.SearchSpecifications(model);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            await
            searchRepository
            .Received(4)
            .Search(Arg.Any <string>(), Arg.Any <SearchParameters>());
        }
コード例 #11
0
        public async Task SearchSpecifications_GivenValidModelWithNullFilterWithMultipleOfSameFilter_ThenSearchIsPerformed()
        {
            //Arrange
            SearchModel model = new SearchModel
            {
                PageNumber    = 1,
                Top           = 50,
                IncludeFacets = true,
                Filters       = new Dictionary <string, string[]>()
                {
                    { "status", new string [] { "test", "" } }
                },
                SearchTerm = "testTerm",
            };

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>();

            ILogger logger = CreateLogger();

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Any <string>(), Arg.Any <SearchParameters>())
            .Returns(searchResults);

            SpecificationsSearchService service = CreateSearchService(logger: logger, searchRepository: searchRepository);

            //Act
            IActionResult result = await service.SearchSpecifications(model);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            await
            searchRepository
            .Received(3)
            .Search(model.SearchTerm, Arg.Is <SearchParameters>(c =>
                                                                model.Filters.Keys.All(f => c.Filter.Contains(f)) &&
                                                                !string.IsNullOrWhiteSpace(c.Filter)
                                                                ));
        }
コード例 #12
0
        public async Task SearchSpecificationDatasetRelationships_GivenNullSearchModel_ReturnsBadRequest()
        {
            //Arrange
            ILogger logger = CreateLogger();

            SpecificationsSearchService service = CreateSearchService(logger: logger);

            //Act
            IActionResult result = await service.SearchSpecificationDatasetRelationships(null);

            //Assert
            logger
            .Received(1)
            .Error("A null or invalid search model was provided for searching specifications");

            result
            .Should()
            .BeOfType <BadRequestObjectResult>();
        }
コード例 #13
0
        async public Task SearchSpecificationDatasetRelationships_GivenSearchThrowsException_ReturnsStatusCode500()
        {
            //Arrange
            SearchModel model = CreateSearchModel();
            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);

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .When(x => x.Search(Arg.Any <string>(), Arg.Any <SearchParameters>()))
            .Do(x => { throw new FailedToQuerySearchException("main", new Exception("inner")); });

            ILogger logger = CreateLogger();

            SpecificationsSearchService service = CreateSearchService(searchRepository, logger);

            //Act
            IActionResult result = await service.SearchSpecificationDatasetRelationships(request);

            //Assert
            result
            .Should()
            .BeOfType <StatusCodeResult>();

            StatusCodeResult statusCodeResult = result as StatusCodeResult;

            statusCodeResult
            .StatusCode
            .Should()
            .Be(500);
        }
コード例 #14
0
        async public Task SearchSpecificationDatasetRelationships_GivenSearchReturnsResults_ReturnsOKResult()
        {
            //Arrange
            SearchModel model = CreateSearchModel();
            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);

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>
            {
                TotalCount = 1
            };

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Is("SearchTermTest"), Arg.Any <SearchParameters>())
            .Returns(searchResults);

            ILogger logger = CreateLogger();

            SpecificationsSearchService service = CreateSearchService(searchRepository, logger);

            //Act
            IActionResult result = await service.SearchSpecificationDatasetRelationships(request);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();
        }
コード例 #15
0
        public async Task SearchSpecifications_GivenValidModelAndPageNumber10_CallsSearchWithCorrectSkipValue()
        {
            //Arrange
            const int skipValue = 450;

            SearchModel model = new SearchModel
            {
                PageNumber = 10,
                Top        = 50
            };

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>();

            ILogger logger = CreateLogger();

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Any <string>(), Arg.Any <SearchParameters>())
            .Returns(searchResults);

            SpecificationsSearchService service = CreateSearchService(logger: logger, searchRepository: searchRepository);

            //Act
            IActionResult result = await service.SearchSpecifications(model);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            await
            searchRepository
            .Received(1)
            .Search(Arg.Any <string>(), Arg.Is <SearchParameters>(m => m.Skip == skipValue));
        }
コード例 #16
0
        async public Task SearchSpecificationDatasetRelationships_GivenSearchReturnsResultsAndDataDefinitionsHasItems_ReturnsOKResult()
        {
            //Arrange
            SearchModel model = CreateSearchModel();
            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);

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>
            {
                TotalCount = 1,
                Results    = new List <Repositories.Common.Search.SearchResult <SpecificationIndex> >
                {
                    new Repositories.Common.Search.SearchResult <SpecificationIndex>
                    {
                        Result = new SpecificationIndex
                        {
                            DataDefinitionRelationshipIds = new[] { "def-1", "def-2" }
                        }
                    }
                }
            };

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Is("SearchTermTest"), Arg.Any <SearchParameters>())
            .Returns(searchResults);

            ILogger logger = CreateLogger();

            SpecificationsSearchService service = CreateSearchService(searchRepository, logger);

            //Act
            IActionResult result = await service.SearchSpecificationDatasetRelationships(request);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            OkObjectResult okObjectResult = result as OkObjectResult;

            SpecificationDatasetRelationshipsSearchResults specificationSearchResults = okObjectResult.Value as SpecificationDatasetRelationshipsSearchResults;

            specificationSearchResults
            .Results
            .Count()
            .Should()
            .Be(1);

            specificationSearchResults
            .TotalCount
            .Should()
            .Be(1);

            specificationSearchResults
            .Results
            .First()
            .DefinitionRelationshipCount
            .Should()
            .Be(2);
        }
コード例 #17
0
        public async Task SearchSpecifications_GivenValidModelWithOneFilter_ThenSearchIsPerformed()
        {
            //Arrange
            SearchModel model = new SearchModel
            {
                PageNumber    = 1,
                Top           = 50,
                IncludeFacets = true,
                Filters       = new Dictionary <string, string[]>()
                {
                    { "status", new string [] { "test" } }
                },
                SearchTerm = "testTerm",
            };

            SpecificationIndex expectedSpecificationIndex = new SpecificationIndex()
            {
                Id   = "test-sp1",
                Name = "test-sp1-name",
                FundingPeriodName    = "fp",
                FundingStreamNames   = new[] { "fs" },
                Status               = "test-status",
                Description          = "des",
                IsSelectedForFunding = true,
                LastUpdatedDate      = new DateTimeOffset(new DateTime(2020, 09, 08, 10, 40, 15))
            };

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>()
            {
                Results = new List <Repositories.Common.Search.SearchResult <SpecificationIndex> >()
                {
                    new Repositories.Common.Search.SearchResult <SpecificationIndex>()
                    {
                        Result = expectedSpecificationIndex
                    }
                }
            };

            ILogger logger = CreateLogger();

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Any <string>(), Arg.Any <SearchParameters>())
            .Returns(searchResults);

            SpecificationsSearchService service = CreateSearchService(logger: logger, searchRepository: searchRepository);

            //Act
            IActionResult result = await service.SearchSpecifications(model);

            //Assert
            SpecificationSearchResults searchResult = result
                                                      .Should()
                                                      .BeOfType <OkObjectResult>()
                                                      .Which
                                                      .Value
                                                      .As <SpecificationSearchResults>();

            searchResult.Results.Count().Should().Be(1);
            AssertSearchResults(expectedSpecificationIndex, searchResult.Results.First());

            await
            searchRepository
            .Received(3)
            .Search(model.SearchTerm, Arg.Is <SearchParameters>(c =>
                                                                model.Filters.Keys.All(f => c.Filter.Contains(f)) &&
                                                                !string.IsNullOrWhiteSpace(c.Filter)
                                                                ));
        }
コード例 #18
0
        public async Task SearchSpecificationDatasetRelationships_GivenSearchReturnsFacets_ReturnsOKResult()
        {
            //Arrange
            SearchModel model = CreateSearchModel();

            SearchResults <SpecificationIndex> searchResults = new SearchResults <SpecificationIndex>
            {
                TotalCount = 1,
                Facets     = new List <Facet>
                {
                    new Facet
                    {
                        Name        = "stuff",
                        FacetValues = new[]
                        {
                            new FacetValue
                            {
                                Count = 200,
                                Name  = "stuff one"
                            },
                        }
                    },
                }
            };

            ISearchRepository <SpecificationIndex> searchRepository = CreateSearchRepository();

            searchRepository
            .Search(Arg.Is("SearchTermTest"), Arg.Any <SearchParameters>())
            .Returns(searchResults);

            ILogger logger = CreateLogger();

            SpecificationsSearchService service = CreateSearchService(searchRepository, logger);

            //Act
            IActionResult result = await service.SearchSpecificationDatasetRelationships(model);

            //Assert
            result
            .Should()
            .BeOfType <OkObjectResult>();

            OkObjectResult okObjectResult = result as OkObjectResult;

            SpecificationDatasetRelationshipsSearchResults specificationSearchResults = okObjectResult.Value as SpecificationDatasetRelationshipsSearchResults;

            specificationSearchResults
            .Results
            .Should()
            .BeNullOrEmpty();

            specificationSearchResults
            .TotalCount
            .Should()
            .Be(0);

            searchResults
            .Facets
            .Should()
            .BeEquivalentTo(searchResults.Facets);
        }