Example #1
0
        private static Func <SearchDescriptor <ApprenticeshipSearchResult>, ISearchRequest> SetupApprenticeshipClient(ApprenticeshipSearchRequestParameters parameters)
        {
            var searchResponse = new Mock <ISearchResponse <ApprenticeshipSearchResult> >();

            searchResponse.Setup(s => s.Total).Returns(0);
            searchResponse.Setup(s => s.Documents).Returns(Enumerable.Empty <ApprenticeshipSearchResult>().ToList());

            Func <SearchDescriptor <ApprenticeshipSearchResult>, ISearchRequest> actualSearchDescriptorFunc = null;

            var mockClient = new Mock <IElasticClient>();

            mockClient.Setup(c => c.Search <ApprenticeshipSearchResult>(It.IsAny <Func <SearchDescriptor <ApprenticeshipSearchResult>, ISearchRequest> >()))
            .Callback <Func <SearchDescriptor <ApprenticeshipSearchResult>, ISearchRequest> >(a => actualSearchDescriptorFunc = a)
            .Returns(searchResponse.Object);

            var mockFactory = new Mock <IElasticClientFactory>();

            mockFactory.Setup(f => f.CreateClient(It.IsAny <Action <IApiCallDetails> >())).Returns(mockClient.Object);

            var mockLogger = new Mock <ILog>();

            var sut = new ApprenticeshipSearchClient(mockFactory.Object, "apprenticeships", mockLogger.Object);

            var response = sut.Search(parameters);

            return(actualSearchDescriptorFunc);
        }
Example #2
0
        public void GetAllVacancyIds_ShouldScrollResults()
        {
            const string expectedJsonQuery = "{\"from\":0,\"size\":100,\"query\":{\"match_all\":{}}}";

            var searchResponse = new Mock <ISearchResponse <ApprenticeshipSearchResult> >();

            searchResponse.Setup(s => s.ScrollId).Returns("scrollId-100");

            Func <SearchDescriptor <ApprenticeshipSearchResult>, ISearchRequest> actualSearchDescriptorFunc = null;

            var mockClient = new Mock <IElasticClient>();

            mockClient.Setup(c => c.Search(It.IsAny <Func <SearchDescriptor <ApprenticeshipSearchResult>, ISearchRequest> >()))
            .Callback <Func <SearchDescriptor <ApprenticeshipSearchResult>, ISearchRequest> >(d => actualSearchDescriptorFunc = d)
            .Returns(searchResponse.Object);

            var scrollResponse1 = new Mock <ISearchResponse <ApprenticeshipSearchResult> >();

            scrollResponse1.Setup(r => r.Documents)
            .Returns(new List <ApprenticeshipSearchResult>
            {
                new ApprenticeshipSearchResult {
                    Id = 3
                },
                new ApprenticeshipSearchResult {
                    Id = 4
                }
            });

            var scrollResponse2 = new Mock <ISearchResponse <ApprenticeshipSearchResult> >();

            scrollResponse2.Setup(r => r.Documents)
            .Returns(new List <ApprenticeshipSearchResult>
            {
                new ApprenticeshipSearchResult {
                    Id = 5
                },
                new ApprenticeshipSearchResult {
                    Id = 6
                }
            });

            var scrollResponse3 = new Mock <ISearchResponse <ApprenticeshipSearchResult> >();

            scrollResponse3.Setup(r => r.Documents)
            .Returns(Enumerable.Empty <ApprenticeshipSearchResult>().ToList());

            mockClient.SetupSequence(c => c.Scroll <ApprenticeshipSearchResult>(It.Is <ScrollRequest>(r => r.ScrollId == "scrollId-100")))
            .Returns(scrollResponse1.Object)
            .Returns(scrollResponse2.Object)
            .Returns(scrollResponse3.Object);

            var mockFactory = new Mock <IElasticClientFactory>();

            mockFactory.Setup(f => f.CreateClient(It.IsAny <Action <IApiCallDetails> >())).Returns(mockClient.Object);

            var mockLogger = new Mock <ILog>();

            var sut = new ApprenticeshipSearchClient(mockFactory.Object, "apprenticeships", mockLogger.Object);

            var actualResponse = sut.GetAllVacancyIds().ToList();

            var baseSearchDescriptor = new SearchDescriptor <ApprenticeshipSearchResult>();
            var query = actualSearchDescriptorFunc(baseSearchDescriptor);

            var elasticClient = new ElasticClient();
            var stream        = new MemoryStream();

            elasticClient.RequestResponseSerializer.Serialize(query, stream);
            var actualJsonQuery = System.Text.Encoding.UTF8.GetString(stream.ToArray());

            var actualJsonQueryJToken = JToken.Parse(actualJsonQuery);

            var expectedJsonQueryJToken = JToken.Parse(expectedJsonQuery);

            actualResponse.Count.Should().Be(4);
            actualResponse[0].Should().Be(3);
            actualResponse[1].Should().Be(4);
            actualResponse[2].Should().Be(5);
            actualResponse[3].Should().Be(6);

            actualJsonQueryJToken.Should().BeEquivalentTo(expectedJsonQueryJToken);

            mockClient.Verify(c => c.Scroll <ApprenticeshipSearchResult>(It.IsAny <ScrollRequest>()), Times.Exactly(3));
        }