[TEST] fix RequestTests#testSearch in case search source is not set

The search request body can never be null as `SearchRequest` doesn't allow the inner `SearchSourceBuilder` to be null. Instead, when search source is not set, the request body is going to be an empty json object (`{}``)
This commit is contained in:
javanna 2018-01-18 12:56:54 +01:00 committed by Luca Cavanna
parent b4c1c4a78c
commit 0dfb65a6ee
1 changed files with 41 additions and 42 deletions

View File

@ -794,44 +794,47 @@ public class RequestTests extends ESTestCase {
setRandomIndicesOptions(searchRequest::indicesOptions, searchRequest::indicesOptions, expectedParams); setRandomIndicesOptions(searchRequest::indicesOptions, searchRequest::indicesOptions, expectedParams);
SearchSourceBuilder searchSourceBuilder = null; SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//rarely skip setting the search source completely
if (frequently()) { if (frequently()) {
searchSourceBuilder = new SearchSourceBuilder(); //frequently set the search source to have some content, otherwise leave it empty but still set it
if (randomBoolean()) { if (frequently()) {
searchSourceBuilder.size(randomIntBetween(0, Integer.MAX_VALUE)); if (randomBoolean()) {
} searchSourceBuilder.size(randomIntBetween(0, Integer.MAX_VALUE));
if (randomBoolean()) { }
searchSourceBuilder.from(randomIntBetween(0, Integer.MAX_VALUE)); if (randomBoolean()) {
} searchSourceBuilder.from(randomIntBetween(0, Integer.MAX_VALUE));
if (randomBoolean()) { }
searchSourceBuilder.minScore(randomFloat()); if (randomBoolean()) {
} searchSourceBuilder.minScore(randomFloat());
if (randomBoolean()) { }
searchSourceBuilder.explain(randomBoolean()); if (randomBoolean()) {
} searchSourceBuilder.explain(randomBoolean());
if (randomBoolean()) { }
searchSourceBuilder.profile(randomBoolean()); if (randomBoolean()) {
} searchSourceBuilder.profile(randomBoolean());
if (randomBoolean()) { }
searchSourceBuilder.highlighter(new HighlightBuilder().field(randomAlphaOfLengthBetween(3, 10))); if (randomBoolean()) {
} searchSourceBuilder.highlighter(new HighlightBuilder().field(randomAlphaOfLengthBetween(3, 10)));
if (randomBoolean()) { }
searchSourceBuilder.query(new TermQueryBuilder(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10))); if (randomBoolean()) {
} searchSourceBuilder.query(new TermQueryBuilder(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10)));
if (randomBoolean()) { }
searchSourceBuilder.aggregation(new TermsAggregationBuilder(randomAlphaOfLengthBetween(3, 10), ValueType.STRING) if (randomBoolean()) {
.field(randomAlphaOfLengthBetween(3, 10))); searchSourceBuilder.aggregation(new TermsAggregationBuilder(randomAlphaOfLengthBetween(3, 10), ValueType.STRING)
} .field(randomAlphaOfLengthBetween(3, 10)));
if (randomBoolean()) { }
searchSourceBuilder.suggest(new SuggestBuilder().addSuggestion(randomAlphaOfLengthBetween(3, 10), if (randomBoolean()) {
new CompletionSuggestionBuilder(randomAlphaOfLengthBetween(3, 10)))); searchSourceBuilder.suggest(new SuggestBuilder().addSuggestion(randomAlphaOfLengthBetween(3, 10),
} new CompletionSuggestionBuilder(randomAlphaOfLengthBetween(3, 10))));
if (randomBoolean()) { }
searchSourceBuilder.addRescorer(new QueryRescorerBuilder( if (randomBoolean()) {
new TermQueryBuilder(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10)))); searchSourceBuilder.addRescorer(new QueryRescorerBuilder(
} new TermQueryBuilder(randomAlphaOfLengthBetween(3, 10), randomAlphaOfLengthBetween(3, 10))));
if (randomBoolean()) { }
searchSourceBuilder.collapse(new CollapseBuilder(randomAlphaOfLengthBetween(3, 10))); if (randomBoolean()) {
searchSourceBuilder.collapse(new CollapseBuilder(randomAlphaOfLengthBetween(3, 10)));
}
} }
searchRequest.source(searchSourceBuilder); searchRequest.source(searchSourceBuilder);
} }
@ -849,11 +852,7 @@ public class RequestTests extends ESTestCase {
endpoint.add("_search"); endpoint.add("_search");
assertEquals(endpoint.toString(), request.getEndpoint()); assertEquals(endpoint.toString(), request.getEndpoint());
assertEquals(expectedParams, request.getParameters()); assertEquals(expectedParams, request.getParameters());
if (searchSourceBuilder == null) { assertToXContentBody(searchSourceBuilder, request.getEntity());
assertNull(request.getEntity());
} else {
assertToXContentBody(searchSourceBuilder, request.getEntity());
}
} }
public void testMultiSearch() throws IOException { public void testMultiSearch() throws IOException {