public async Task SearchById_GivenIdDoesNotReturnSearchResult_ReturnsNull()
        {
            SearchRepositorySettings searchRepositorySettings = new SearchRepositorySettings
            {
                SearchKey         = string.Empty,
                SearchServiceName = string.Empty
            };

            ISearchInitializer searchInitializer = Substitute.For <ISearchInitializer>();

            ISearchIndexClient searchIndexClient = Substitute.For <ISearchIndexClient>();

            AzureOperationResponse <DocumentSearchResult <SpecificationIndex> > documentSearchResult =
                new AzureOperationResponse <DocumentSearchResult <SpecificationIndex> >
            {
                Body = new DocumentSearchResult <SpecificationIndex>(null, null, null, null, null)
            };

            IDocumentsOperations documentsOperations = Substitute.For <IDocumentsOperations>();

            documentsOperations.SearchWithHttpMessagesAsync <SpecificationIndex>(Arg.Any <string>(), Arg.Any <SearchParameters>()).Returns(Task.FromResult(documentSearchResult));

            ISearchServiceClient searchServiceClient = Substitute.For <ISearchServiceClient>();

            searchIndexClient.Documents.Returns(documentsOperations);

            SearchRepository <SpecificationIndex> searchRepository = new SearchRepository <SpecificationIndex>(searchRepositorySettings, searchInitializer, searchServiceClient, searchIndexClient);

            string notFoundId = "notFound";

            SpecificationIndex specificationIndex = await searchRepository.SearchById(notFoundId);

            specificationIndex.Should().BeNull();
        }
        public void SearchById_GivenIdReturnsSearchResult_TreatsMultipleResultsAsAnException()
        {
            string existingId = "existingId";

            SearchRepositorySettings searchRepositorySettings = new SearchRepositorySettings
            {
                SearchKey         = string.Empty,
                SearchServiceName = string.Empty
            };

            ISearchInitializer searchInitializer = Substitute.For <ISearchInitializer>();

            ISearchIndexClient searchIndexClient = Substitute.For <ISearchIndexClient>();

            Microsoft.Azure.Search.Models.SearchResult <SpecificationIndex> specificationIndexSearchResult = new Microsoft.Azure.Search.Models.SearchResult <SpecificationIndex>(new SpecificationIndex
            {
                Id = existingId
            });

            AzureOperationResponse <DocumentSearchResult <SpecificationIndex> > documentSearchResult =
                new AzureOperationResponse <DocumentSearchResult <SpecificationIndex> >
            {
                Body = new DocumentSearchResult <SpecificationIndex>(new[]
                {
                    specificationIndexSearchResult, new Microsoft.Azure.Search.Models.SearchResult <SpecificationIndex>()
                },
                                                                     null,
                                                                     null,
                                                                     null,
                                                                     null)
            };

            IDocumentsOperations documentsOperations = Substitute.For <IDocumentsOperations>();

            documentsOperations
            .SearchWithHttpMessagesAsync <SpecificationIndex>(Arg.Is <string>(_ => _ == $"\"{existingId}\""),
                                                              Arg.Is <SearchParameters>(_ => _.SearchFields.SequenceEqual(new[]
            {
                "id"
            })))
            .Returns(Task.FromResult(documentSearchResult));

            ISearchServiceClient searchServiceClient = Substitute.For <ISearchServiceClient>();

            searchIndexClient.Documents.Returns(documentsOperations);

            SearchRepository <SpecificationIndex> searchRepository = new SearchRepository <SpecificationIndex>(searchRepositorySettings, searchInitializer, searchServiceClient, searchIndexClient);

            Func <Task <SpecificationIndex> > invocation = () => searchRepository.SearchById(existingId);

            invocation
            .Should()
            .Throw <FailedToQuerySearchException>();
        }
        public async Task SearchById_GivenIdReturnsSearchResult_ReturnsResults()
        {
            string existingId = "existingId";

            SearchRepositorySettings searchRepositorySettings = new SearchRepositorySettings
            {
                SearchKey         = string.Empty,
                SearchServiceName = string.Empty
            };

            ISearchInitializer searchInitializer = Substitute.For <ISearchInitializer>();

            ISearchIndexClient searchIndexClient = Substitute.For <ISearchIndexClient>();

            Microsoft.Azure.Search.Models.SearchResult <SpecificationIndex> specificationIndexSearchResult = new Microsoft.Azure.Search.Models.SearchResult <SpecificationIndex>(new SpecificationIndex
            {
                Id = existingId
            });

            AzureOperationResponse <DocumentSearchResult <SpecificationIndex> > documentSearchResult =
                new AzureOperationResponse <DocumentSearchResult <SpecificationIndex> >
            {
                Body = new DocumentSearchResult <SpecificationIndex>(new[]
                {
                    specificationIndexSearchResult
                },
                                                                     null,
                                                                     null,
                                                                     null,
                                                                     null)
            };

            IDocumentsOperations documentsOperations = Substitute.For <IDocumentsOperations>();

            documentsOperations
            .SearchWithHttpMessagesAsync <SpecificationIndex>(Arg.Is <string>(_ => _ == $"\"{existingId}\""),
                                                              Arg.Is <SearchParameters>(_ => _.SearchFields.SequenceEqual(new[]
            {
                "id"
            })))
            .Returns(Task.FromResult(documentSearchResult));

            ISearchServiceClient searchServiceClient = Substitute.For <ISearchServiceClient>();

            searchIndexClient.Documents.Returns(documentsOperations);

            SearchRepository <SpecificationIndex> searchRepository = new SearchRepository <SpecificationIndex>(searchRepositorySettings, searchInitializer, searchServiceClient, searchIndexClient);

            SpecificationIndex specificationIndex = await searchRepository.SearchById(existingId);

            specificationIndex.Should().NotBeNull();
            specificationIndex.Id.Should().Be(existingId);
        }