public static async void NonSuccessfulHttpRequestThrows()
        {
            var client = Substitute.For<IElasticsearchClient>();

            client.SearchAsync<string>(
                    "_all",
                    "docType",
                    @"{""timeout"":""10s""}",
                    Arg.Any<Func<SearchRequestParameters, SearchRequestParameters>>())
                .Returns(Task.FromResult(ElasticsearchResponse<string>.Create(
                    new ConnectionConfiguration(),
                    404,
                    "_search",
                    "_all",
                    new byte[0])));

            var localConnection = new ElasticNetConnection(client);
            var request = new SearchRequest { DocumentType = "docType" };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            var ex = await Record.ExceptionAsync(() => localConnection.SearchAsync(
                formatter.Body,
                request,
                CancellationToken.None,
                log));

            Assert.IsType<HttpRequestException>(ex);
            Assert.Equal("Response status code does not indicate success: 404 (Not Found).", ex.Message);
        }
예제 #2
0
        public static async void NonSuccessfulHttpRequestThrows()
        {
            var client = Substitute.For <IElasticsearchClient>();

            client.SearchAsync <string>(
                "_all",
                "docType",
                @"{""timeout"":""10s""}",
                Arg.Any <Func <SearchRequestParameters, SearchRequestParameters> >())
            .Returns(Task.FromResult(ElasticsearchResponse <string> .Create(
                                         new ConnectionConfiguration(),
                                         404,
                                         "_search",
                                         "_all",
                                         new byte[0])));

            var localConnection = new ElasticNetConnection(client);
            var request         = new SearchRequest {
                DocumentType = "docType"
            };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            var ex = await Record.ExceptionAsync(() => localConnection.SearchAsync(
                                                     formatter.Body,
                                                     request,
                                                     CancellationToken.None,
                                                     log));

            Assert.IsType <HttpRequestException>(ex);
            Assert.Equal("Response status code does not indicate success: 404 (Not Found).", ex.Message);
        }
예제 #3
0
        public static void ParseResponseReturnsParsedResponseGivenValidStream()
        {
            const int    took   = 2;
            const int    shards = 1;
            const int    hits   = 1;
            const double score  = 0.3141;
            const string index  = "testIndex";
            const string type   = "testType";
            const string id     = "testId";

            var responseString = BuildResponseString(took, shards, hits, score, index, type, id);

            var response = ElasticNetConnection.ParseResponse(responseString, log);

            Assert.NotNull(response);
            Assert.Equal(took, response.took);
            Assert.Equal(hits, response.hits.total);
            Assert.Equal(score, response.hits.max_score);

            Assert.NotEmpty(response.hits.hits);
            Assert.Equal(score, response.hits.hits[0]._score);
            Assert.Equal(index, response.hits.hits[0]._index);
            Assert.Equal(type, response.hits.hits[0]._type);
            Assert.Equal(id, response.hits.hits[0]._id);
        }
        public void ConstructorWithAllArgsSetsPropertiesFromParameters()
        {
            var expectedTimeout = TimeSpan.FromSeconds(1234);
            const string expectedIndex = "h2g2";
            var expectedOptions = new ElasticConnectionOptions { Pretty = true };

            var actual = new ElasticNetConnection(Substitute.For<IElasticsearchClient>(), expectedIndex, expectedTimeout, expectedOptions);

            Assert.Equal(expectedTimeout, actual.Timeout);
            Assert.Equal(expectedIndex, actual.Index);
            Assert.Equal(expectedOptions, actual.Options);
        }
예제 #5
0
        public void ConstructorWithAllArgsSetsPropertiesFromParameters()
        {
            var          expectedTimeout = TimeSpan.FromSeconds(1234);
            const string expectedIndex   = "h2g2";
            var          expectedOptions = new ElasticConnectionOptions {
                Pretty = true
            };

            var actual = new ElasticNetConnection(Substitute.For <IElasticsearchClient>(), expectedIndex, expectedTimeout, expectedOptions);

            Assert.Equal(expectedTimeout, actual.Timeout);
            Assert.Equal(expectedIndex, actual.Index);
            Assert.Equal(expectedOptions, actual.Options);
        }
예제 #6
0
        public static async void SearchAsyncThrowsTaskCancelledExceptionWithAlreadyCancelledCancellationToken()
        {
            var client = Substitute.For <IElasticsearchClient>();

            var spyLog          = new SpyLog();
            var localConnection = new ElasticNetConnection(client, "SearchIndex");
            var request         = new SearchRequest {
                DocumentType = "docType"
            };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            var ex = await Record.ExceptionAsync(() => localConnection.SearchAsync(
                                                     formatter.Body,
                                                     request,
                                                     new CancellationToken(true),
                                                     spyLog));

            Assert.IsType <TaskCanceledException>(ex);
        }
예제 #7
0
        public static async Task LogsDebugMessagesDuringExecution()
        {
            var client = Substitute.For <IElasticsearchClient>();

            var responseString = BuildResponseString(2, 1, 1, 0.3141, "testIndex", "testType", "testId");
            var spyLog         = new SpyLog();

            client.SearchAsync <string>(
                "SearchIndex",
                "abc123",
                @"{""size"":2112,""timeout"":""10s""}",
                Arg.Any <Func <SearchRequestParameters, SearchRequestParameters> >())
            .Returns(Task.FromResult(ElasticsearchResponse <string> .Create(
                                         new ConnectionConfiguration(),
                                         200,
                                         "_search",
                                         "http://localhost/SearchIndex/abc123/_search",
                                         new byte[0],
                                         responseString)));

            var localConnection = new ElasticNetConnection(client, index: "SearchIndex");
            var request         = new SearchRequest {
                DocumentType = "abc123", Size = 2112
            };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            await localConnection.SearchAsync(
                formatter.Body,
                request,
                CancellationToken.None,
                spyLog);

            Assert.Equal(4, spyLog.Entries.Count);
            Assert.Equal(@"Request: POST http://localhost/SearchIndex/abc123/_search", spyLog.Entries[0].Message);
            Assert.Equal(@"Body:" + '\n' + @"{""size"":2112,""timeout"":""10s""}", spyLog.Entries[1].Message);
            Assert.True(new Regex(@"Response: 200 OK \(in \d+ms\)").Match(spyLog.Entries[2].Message).Success);
            Assert.True(new Regex(@"Deserialized \d+ characters into 1 hits in \d+ms").Match(spyLog.Entries[3].Message).Success);
        }
        public static async Task LogsDebugMessagesDuringExecution()
        {
            var client = Substitute.For<IElasticsearchClient>();

            var responseString = BuildResponseString(2, 1, 1, 0.3141, "testIndex", "testType", "testId");
            var spyLog = new SpyLog();

            client.SearchAsync<string>(
                    "SearchIndex",
                    "abc123",
                    @"{""size"":2112,""timeout"":""10s""}",
                    Arg.Any<Func<SearchRequestParameters, SearchRequestParameters>>())
                .Returns(Task.FromResult(ElasticsearchResponse<string>.Create(
                    new ConnectionConfiguration(),
                    200,
                    "_search",
                    "http://localhost/SearchIndex/abc123/_search",
                    new byte[0],
                    responseString)));

            var localConnection = new ElasticNetConnection(client, index: "SearchIndex");
            var request = new SearchRequest { DocumentType = "abc123", Size = 2112 };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            await localConnection.SearchAsync(
                formatter.Body,
                request,
                CancellationToken.None,
                spyLog);

            Assert.Equal(4, spyLog.Entries.Count);
            Assert.Equal(@"Request: POST http://localhost/SearchIndex/abc123/_search", spyLog.Entries[0].Message);
            Assert.Equal(@"Body:" + '\n' + @"{""size"":2112,""timeout"":""10s""}", spyLog.Entries[1].Message);
            Assert.True(new Regex(@"Response: 200 OK \(in \d+ms\)").Match(spyLog.Entries[2].Message).Success);
            Assert.True(new Regex(@"Deserialized \d+ characters into 1 hits in \d+ms").Match(spyLog.Entries[3].Message).Success);
        }
        public void ConstructorCreatesDefaultOptions()
        {
            var actual = new ElasticNetConnection(Substitute.For<IElasticsearchClient>());

            Assert.NotNull(actual.Options);
        }
        public static async void SearchAsyncThrowsTaskCancelledExceptionWithAlreadyCancelledCancellationToken()
        {
            var client = Substitute.For<IElasticsearchClient>();

            var spyLog = new SpyLog();
            var localConnection = new ElasticNetConnection(client, "SearchIndex");
            var request = new SearchRequest { DocumentType = "docType" };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            var ex = await Record.ExceptionAsync(() => localConnection.SearchAsync(
                formatter.Body,
                request,
                new CancellationToken(true),
                spyLog));

            Assert.IsType<TaskCanceledException>(ex);
        }
예제 #11
0
        public void ConstructorCreatesDefaultOptions()
        {
            var actual = new ElasticNetConnection(Substitute.For <IElasticsearchClient>());

            Assert.NotNull(actual.Options);
        }