From 215a554555eb4fe30fd9169eb39d31e10e5466b8 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Fri, 16 Oct 2015 15:04:05 +0200 Subject: [PATCH] Tests: Fixing more tests in Watcher after query refactoring Original commit: elastic/x-pack-elasticsearch@1a0b654c5644257d8f6e2e1c0b090e72fb999036 --- .../watcher/support/WatcherUtils.java | 7 +-- .../search/SearchTransformTests.java | 48 +++++++++++-------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherUtils.java b/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherUtils.java index ded3110ec21..97a250807a6 100644 --- a/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherUtils.java +++ b/watcher/src/main/java/org/elasticsearch/watcher/support/WatcherUtils.java @@ -108,6 +108,9 @@ public final class WatcherUtils { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); + if (ParseFieldMatcher.STRICT.match(currentFieldName, BODY_FIELD)) { + searchRequest.source(SearchSourceBuilder.parseSearchSource(parser, context)); + } } else if (token == XContentParser.Token.START_ARRAY) { if (ParseFieldMatcher.STRICT.match(currentFieldName, INDICES_FIELD)) { List indices = new ArrayList<>(); @@ -133,9 +136,7 @@ public final class WatcherUtils { throw new ElasticsearchParseException("could not read search request. unexpected array field [" + currentFieldName + "]"); } } else if (token == XContentParser.Token.START_OBJECT) { - if (ParseFieldMatcher.STRICT.match(currentFieldName, BODY_FIELD)) { - searchRequest.source(SearchSourceBuilder.parseSearchSource(parser, context)); - } else if (ParseFieldMatcher.STRICT.match(currentFieldName, INDICES_OPTIONS_FIELD)) { + if (ParseFieldMatcher.STRICT.match(currentFieldName, INDICES_OPTIONS_FIELD)) { boolean expandOpen = DEFAULT_INDICES_OPTIONS.expandWildcardsOpen(); boolean expandClosed = DEFAULT_INDICES_OPTIONS.expandWildcardsClosed(); boolean allowNoIndices = DEFAULT_INDICES_OPTIONS.allowNoIndices(); diff --git a/watcher/src/test/java/org/elasticsearch/watcher/transform/search/SearchTransformTests.java b/watcher/src/test/java/org/elasticsearch/watcher/transform/search/SearchTransformTests.java index f4e8c61d674..474ceb233b7 100644 --- a/watcher/src/test/java/org/elasticsearch/watcher/transform/search/SearchTransformTests.java +++ b/watcher/src/test/java/org/elasticsearch/watcher/transform/search/SearchTransformTests.java @@ -5,18 +5,18 @@ */ package org.elasticsearch.watcher.transform.search; +import com.google.common.base.Charsets; import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptRequest; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.client.Requests; +import org.elasticsearch.common.Base64; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.common.xcontent.*; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.common.xcontent.support.XContentMapValues; import org.elasticsearch.index.query.QueryBuilders; @@ -60,23 +60,12 @@ import static java.util.Collections.emptyMap; import static org.elasticsearch.common.settings.Settings.settingsBuilder; import static org.elasticsearch.common.unit.TimeValue.timeValueSeconds; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; -import static org.elasticsearch.index.query.QueryBuilders.boolQuery; -import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; -import static org.elasticsearch.index.query.QueryBuilders.matchQuery; -import static org.elasticsearch.index.query.QueryBuilders.rangeQuery; -import static org.elasticsearch.index.query.QueryBuilders.termQuery; +import static org.elasticsearch.index.query.QueryBuilders.*; import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource; import static org.elasticsearch.test.ESIntegTestCase.Scope.SUITE; import static org.elasticsearch.watcher.support.WatcherDateTimeUtils.parseDate; -import static org.elasticsearch.watcher.test.WatcherTestUtils.EMPTY_PAYLOAD; -import static org.elasticsearch.watcher.test.WatcherTestUtils.getRandomSupportedSearchType; -import static org.elasticsearch.watcher.test.WatcherTestUtils.mockExecutionContext; -import static org.elasticsearch.watcher.test.WatcherTestUtils.simplePayload; -import static org.hamcrest.Matchers.arrayContainingInAnyOrder; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; +import static org.elasticsearch.watcher.test.WatcherTestUtils.*; +import static org.hamcrest.Matchers.*; import static org.joda.time.DateTimeZone.UTC; /** @@ -160,7 +149,6 @@ public class SearchTransformTests extends ESIntegTestCase { assertThat(resultData, equalTo(expectedData)); } - @AwaitsFix(bugUrl = "Need to find out a way of testing a bad query following the search request refactoring") @Test public void testExecute_Failure() throws Exception { @@ -182,7 +170,26 @@ public class SearchTransformTests extends ESIntegTestCase { assertThat(result.type(), is(SearchTransform.TYPE)); assertThat(result.status(), is(Transform.Result.Status.FAILURE)); assertThat(result.reason(), notNullValue()); - assertThat(result.executedRequest().template().getScript(), containsString("_unknown_query_")); + assertThat(result.reason(), containsString("No query registered for [_unknown_query_]")); + + // extract the base64 encoded query from the template script, path is: query -> wrapper -> query + String jsonQuery = result.executedRequest().template().getScript(); + Map map = XContentFactory.xContent(jsonQuery).createParser(jsonQuery).map(); + + assertThat(map, hasKey("query")); + assertThat(map.get("query"), instanceOf(Map.class)); + + map = (Map) map.get("query"); + assertThat(map, hasKey("wrapper")); + assertThat(map.get("wrapper"), instanceOf(Map.class)); + + map = (Map) map.get("wrapper"); + assertThat(map, hasKey("query")); + assertThat(map.get("query"), instanceOf(String.class)); + + String queryAsBase64 = (String) map.get("query"); + String decodedQuery = new String(Base64.decode(queryAsBase64), Charsets.UTF_8); + assertThat(decodedQuery, containsString("_unknown_query_")); } @Test @@ -267,8 +274,6 @@ public class SearchTransformTests extends ESIntegTestCase { builder.field("template", template); } - SearchSourceBuilder source = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()); - builder.startObject("body") .startObject("query") .startObject("match_all") @@ -303,6 +308,7 @@ public class SearchTransformTests extends ESIntegTestCase { assertThat(executable.transform().getRequest().template(), equalTo(new Template("template1", ScriptType.FILE, null, null, null))); } + SearchSourceBuilder source = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()); assertThat(executable.transform().getRequest().source(), equalTo(source)); assertThat(executable.transform().getTimeout(), equalTo(readTimeout)); }