From f9b15a2a109c87d6f1aaaf555facf03a71e92d99 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Sat, 26 Oct 2019 15:05:14 +0100 Subject: [PATCH 1/5] Improve resiliency to formatting JSON in server Make a number of changes so that JSON in the server directory is more resilient to automatic formatting. This covers: * Reformatting multiline JSON to embed whitespace in the strings * Add helper method reformatJson(), to strip whitespace from a JSON document using XContent methods. This is sometimes necessary where a test is comparing some machine-generated JSON with an expected value. --- .../common/xcontent/XContentHelper.java | 12 ++ .../SearchPhaseExecutionExceptionTests.java | 65 ++++---- .../search/sort/ScriptSortBuilderTests.java | 25 +-- .../search/suggest/SuggestTests.java | 28 ++-- .../search/suggest/SuggestionOptionTests.java | 15 +- .../search/suggest/SuggestionTests.java | 150 +++++++++++------- .../term/TermSuggestionBuilderTests.java | 18 ++- 7 files changed, 189 insertions(+), 124 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java index d193cfd510823..fa0f153da4431 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java @@ -22,6 +22,7 @@ import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.compress.Compressor; @@ -161,6 +162,17 @@ public static String convertToJson(BytesReference bytes, boolean reformatJson, X return convertToJson(bytes, reformatJson, false, xContentType); } + /** + * Accepts a JSON string and reformats it. This is useful where a piece of JSON is formatted for legibility, but needs + * to be stripped of unnecessary whitespace e.g. for comparison in a test. + * @param json the JSON to format + * @return reformatted JSON + * @throws IOException if the reformatting fails, e.g. because the JSON is not well-formed + */ + public static String reformatJson(String json) throws IOException { + return convertToJson(new BytesArray(json), true, XContentType.JSON); + } + public static String convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint, XContentType xContentType) throws IOException { Objects.requireNonNull(xContentType); diff --git a/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java b/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java index 74005f681d6d9..4901e21a570fc 100644 --- a/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java @@ -28,6 +28,7 @@ import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContent; +import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.shard.IndexShardClosedException; @@ -56,36 +57,40 @@ public void testToXContent() throws IOException { }); // Failures are grouped (by default) - assertEquals("{" + - "\"type\":\"search_phase_execution_exception\"," + - "\"reason\":\"all shards failed\"," + - "\"phase\":\"test\"," + - "\"grouped\":true," + - "\"failed_shards\":[" + - "{" + - "\"shard\":0," + - "\"index\":\"foo\"," + - "\"node\":\"node_1\"," + - "\"reason\":{" + - "\"type\":\"parsing_exception\"," + - "\"reason\":\"foobar\"," + - "\"line\":1," + - "\"col\":2" + - "}" + - "}," + - "{" + - "\"shard\":1," + - "\"index\":\"foo\"," + - "\"node\":\"node_2\"," + - "\"reason\":{" + - "\"type\":\"index_shard_closed_exception\"," + - "\"reason\":\"CurrentState[CLOSED] Closed\"," + - "\"index_uuid\":\"_na_\"," + - "\"shard\":\"1\"," + - "\"index\":\"foo\"" + - "}" + - "}" + - "]}", Strings.toString(exception)); + final String expectedJson = XContentHelper.reformatJson( + "{" + + " \"type\": \"search_phase_execution_exception\"," + + " \"reason\": \"all shards failed\"," + + " \"phase\": \"test\"," + + " \"grouped\": true," + + " \"failed_shards\": [" + + " {" + + " \"shard\": 0," + + " \"index\": \"foo\"," + + " \"node\": \"node_1\"," + + " \"reason\": {" + + " \"type\": \"parsing_exception\"," + + " \"reason\": \"foobar\"," + + " \"line\": 1," + + " \"col\": 2" + + " }" + + " }," + + " {" + + " \"shard\": 1," + + " \"index\": \"foo\"," + + " \"node\": \"node_2\"," + + " \"reason\": {" + + " \"type\": \"index_shard_closed_exception\"," + + " \"reason\": \"CurrentState[CLOSED] Closed\"," + + " \"index_uuid\": \"_na_\"," + + " \"shard\": \"1\"," + + " \"index\": \"foo\"" + + " }" + + " }" + + " ]" + + "}" + ); + assertEquals(expectedJson, Strings.toString(exception)); } public void testToAndFromXContent() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java b/server/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java index 1d3b71bbf40db..2384d697e38dc 100644 --- a/server/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/sort/ScriptSortBuilderTests.java @@ -165,18 +165,19 @@ public void testScriptSortTypeIllegalArgument() { } public void testParseJson() throws IOException { - String scriptSort = "{\n" + - "\"_script\" : {\n" + - "\"type\" : \"number\",\n" + - "\"script\" : {\n" + - "\"source\": \"doc['field_name'].value * factor\",\n" + - "\"params\" : {\n" + - "\"factor\" : 1.1\n" + - "}\n" + - "},\n" + - "\"mode\" : \"max\",\n" + - "\"order\" : \"asc\"\n" + - "} }\n"; + String scriptSort = "{" + + " \"_script\": {" + + " \"type\": \"number\"," + + " \"script\": {" + + " \"source\": \"doc['field_name'].value * factor\"," + + " \"params\": {" + + " \"factor\": 1.1" + + " }" + + " }," + + " \"mode\": \"max\"," + + " \"order\": \"asc\"" + + " }" + + "}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort)) { parser.nextToken(); parser.nextToken(); diff --git a/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java b/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java index 5c247630d6b7f..8df25c1cbf10e 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java @@ -131,19 +131,21 @@ public void testToXContent() throws IOException { Suggest suggest = new Suggest(Collections.singletonList(suggestion)); BytesReference xContent = toXContent(suggest, XContentType.JSON, randomBoolean()); assertEquals( - "{\"suggest\":" - + "{\"suggestionName\":" - + "[{\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[{\"text\":\"someText\"," - + "\"highlighted\":\"somethingHighlighted\"," - + "\"score\":1.3," - + "\"collate_match\":true}]" - + "}]" - + "}" - +"}", - xContent.utf8ToString()); + // @formatter:off + "{\"suggest\":" + + "{\"suggestionName\":" + + "[{\"text\":\"entryText\"," + + "\"offset\":42," + + "\"length\":313," + + "\"options\":[{\"text\":\"someText\"," + + "\"highlighted\":\"somethingHighlighted\"," + + "\"score\":1.3," + + "\"collate_match\":true}]" + + "}]" + + "}" + +"}", + // @formatter:on + xContent.utf8ToString()); } public void testFilter() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/search/suggest/SuggestionOptionTests.java b/server/src/test/java/org/elasticsearch/search/suggest/SuggestionOptionTests.java index 37d886fd5d825..b5e40917ffe3c 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestionOptionTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestionOptionTests.java @@ -81,11 +81,14 @@ private void doTestFromXContent(boolean addRandomFields) throws IOException { public void testToXContent() throws IOException { Option option = new PhraseSuggestion.Entry.Option(new Text("someText"), new Text("somethingHighlighted"), 1.3f, true); BytesReference xContent = toXContent(option, XContentType.JSON, randomBoolean()); - assertEquals("{\"text\":\"someText\"," - + "\"highlighted\":\"somethingHighlighted\"," - + "\"score\":1.3," - + "\"collate_match\":true" - + "}" - , xContent.utf8ToString()); + assertEquals( + ("{" + + " \"text\": \"someText\"," + + " \"highlighted\": \"somethingHighlighted\"," + + " \"score\": 1.3," + + " \"collate_match\": true" + + "}").replaceAll("\\s+", ""), + xContent.utf8ToString() + ); } } diff --git a/server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java b/server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java index e05b42c102a75..36e49933ed524 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java @@ -164,17 +164,23 @@ public void testFromXContentWithoutTypeParam() throws IOException { public void testUnknownSuggestionTypeThrows() throws IOException { XContent xContent = JsonXContent.jsonXContent; - String suggestionString = - "{\"unknownType#suggestionName\":" - + "[{\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[{\"text\":\"someText\"," - + "\"highlighted\":\"somethingHighlighted\"," - + "\"score\":1.3," - + "\"collate_match\":true}]" - + "}]" - + "}"; + String suggestionString = ("{" + + " \"unknownType#suggestionName\": [" + + " {" + + " \"text\": \"entryText\"," + + " \"offset\": 42," + + " \"length\": 313," + + " \"options\": [" + + " {" + + " \"text\": \"someText\"," + + " \"highlighted\": \"somethingHighlighted\"," + + " \"score\": 1.3," + + " \"collate_match\": true" + + " }" + + " ]" + + " }" + + " ]" + + "}").replaceAll("\\s+", ""); try (XContentParser parser = xContent.createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, suggestionString)) { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); @@ -195,18 +201,25 @@ public void testToXContent() throws IOException { PhraseSuggestion suggestion = new PhraseSuggestion("suggestionName", 5); suggestion.addTerm(entry); BytesReference xContent = toXContent(suggestion, XContentType.JSON, params, randomBoolean()); - assertEquals( - "{\"phrase#suggestionName\":[{" - + "\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[{" - + "\"text\":\"someText\"," - + "\"highlighted\":\"somethingHighlighted\"," - + "\"score\":1.3," - + "\"collate_match\":true}]" - + "}]" - + "}", xContent.utf8ToString()); + assertEquals(("{" + + " \"phrase#suggestionName\": [" + + " {" + + " \"text\": \"entryText\"," + + " \"offset\": 42," + + " \"length\": 313," + + " \"options\": [" + + " {" + + " \"text\": \"someText\"," + + " \"highlighted\": \"somethingHighlighted\"," + + " \"score\": 1.3," + + " \"collate_match\": true" + + " }" + + " ]" + + " }" + + " ]" + + "}").replaceAll("\\s+", ""), + xContent.utf8ToString() + ); } { PhraseSuggestion.Entry.Option option = new PhraseSuggestion.Entry.Option(new Text("someText"), new Text("somethingHighlighted"), @@ -216,18 +229,25 @@ public void testToXContent() throws IOException { PhraseSuggestion suggestion = new PhraseSuggestion("suggestionName", 5); suggestion.addTerm(entry); BytesReference xContent = toXContent(suggestion, XContentType.JSON, params, randomBoolean()); - assertEquals( - "{\"phrase#suggestionName\":[{" - + "\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[{" - + "\"text\":\"someText\"," - + "\"highlighted\":\"somethingHighlighted\"," - + "\"score\":1.3," - + "\"collate_match\":true}]" - + "}]" - + "}", xContent.utf8ToString()); + assertEquals(("{" + + " \"phrase#suggestionName\": [" + + " {" + + " \"text\": \"entryText\"," + + " \"offset\": 42," + + " \"length\": 313," + + " \"options\": [" + + " {" + + " \"text\": \"someText\"," + + " \"highlighted\": \"somethingHighlighted\"," + + " \"score\": 1.3," + + " \"collate_match\": true" + + " }" + + " ]" + + " }" + + " ]" + + "}").replaceAll("\\s+", ""), + xContent.utf8ToString() + ); } { TermSuggestion.Entry.Option option = new TermSuggestion.Entry.Option(new Text("someText"), 10, 1.3f); @@ -237,16 +257,24 @@ public void testToXContent() throws IOException { suggestion.addTerm(entry); BytesReference xContent = toXContent(suggestion, XContentType.JSON, params, randomBoolean()); assertEquals( - "{\"term#suggestionName\":[{" - + "\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[{" - + "\"text\":\"someText\"," - + "\"score\":1.3," - + "\"freq\":10}]" - + "}]" - + "}", xContent.utf8ToString()); + ("{" + + " \"term#suggestionName\": [" + + " {" + + " \"text\": \"entryText\"," + + " \"offset\": 42," + + " \"length\": 313," + + " \"options\": [" + + " {" + + " \"text\": \"someText\"," + + " \"score\": 1.3," + + " \"freq\": 10" + + " }" + + " ]" + + " }" + + " ]" + + "}").replaceAll("\\s+", ""), + xContent.utf8ToString() + ); } { Map> contexts = Collections.singletonMap("key", Collections.singleton("value")); @@ -257,16 +285,28 @@ public void testToXContent() throws IOException { suggestion.addTerm(entry); BytesReference xContent = toXContent(suggestion, XContentType.JSON, params, randomBoolean()); assertEquals( - "{\"completion#suggestionName\":[{" - + "\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[{" - + "\"text\":\"someText\"," - + "\"score\":1.3," - + "\"contexts\":{\"key\":[\"value\"]}" - + "}]" - + "}]}", xContent.utf8ToString()); + ("{" + + " \"completion#suggestionName\": [" + + " {" + + " \"text\": \"entryText\"," + + " \"offset\": 42," + + " \"length\": 313," + + " \"options\": [" + + " {" + + " \"text\": \"someText\"," + + " \"score\": 1.3," + + " \"contexts\": {" + + " \"key\": [" + + " \"value\"" + + " ]" + + " }" + + " }" + + " ]" + + " }" + + " ]" + + "}").replaceAll("\\s+", ""), + xContent.utf8ToString() + ); } } } diff --git a/server/src/test/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilderTests.java b/server/src/test/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilderTests.java index d4292151a710e..5a135a0ab4cc9 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/term/TermSuggestionBuilderTests.java @@ -210,14 +210,16 @@ public void testDefaultValuesSet() { public void testMalformedJson() { final String field = RandomStrings.randomAsciiOfLength(random(), 10).toLowerCase(Locale.ROOT); - String suggest = "{\n" + - " \"bad-payload\" : {\n" + - " \"text\" : \"the amsterdma meetpu\",\n" + - " \"term\" : {\n" + - " \"field\" : { \"" + field + "\" : \"bad-object\" }\n" + - " }\n" + - " }\n" + - "}"; + String suggest = "{\n" + + " \"bad-payload\" : {\n" + + " \"text\" : \"the amsterdma meetpu\",\n" + + " \"term\" : {\n" + + " \"field\" : { \"" + + field + + "\" : \"bad-object\" }\n" + + " }\n" + + " }\n" + + "}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, suggest)) { final SuggestBuilder suggestBuilder = SuggestBuilder.fromXContent(parser); fail("Should not have been able to create SuggestBuilder from malformed JSON: " + suggestBuilder); From a046bf3fc26fc1c664c3cb1024526749110519b0 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Tue, 29 Oct 2019 09:55:58 +0000 Subject: [PATCH 2/5] Rename JSON helper to stripWhiteSpace --- .../org/elasticsearch/common/xcontent/XContentHelper.java | 8 +++++--- .../action/search/SearchPhaseExecutionExceptionTests.java | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java index fa0f153da4431..4f304466f9f49 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java @@ -163,13 +163,15 @@ public static String convertToJson(BytesReference bytes, boolean reformatJson, X } /** - * Accepts a JSON string and reformats it. This is useful where a piece of JSON is formatted for legibility, but needs - * to be stripped of unnecessary whitespace e.g. for comparison in a test. + * Accepts a JSON string, parses it and prints it without pretty-printing it. This is useful + * where a piece of JSON is formatted for legibility, but needs to be stripped of unnecessary + * whitespace e.g. for comparison in a test. + * * @param json the JSON to format * @return reformatted JSON * @throws IOException if the reformatting fails, e.g. because the JSON is not well-formed */ - public static String reformatJson(String json) throws IOException { + public static String stripWhiteSpace(String json) throws IOException { return convertToJson(new BytesArray(json), true, XContentType.JSON); } diff --git a/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java b/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java index 4901e21a570fc..77460cce038f3 100644 --- a/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java @@ -57,7 +57,7 @@ public void testToXContent() throws IOException { }); // Failures are grouped (by default) - final String expectedJson = XContentHelper.reformatJson( + final String expectedJson = XContentHelper.stripWhiteSpace( "{" + " \"type\": \"search_phase_execution_exception\"," + " \"reason\": \"all shards failed\"," From d761c273c2f99e17d52d14b9a49db0a7d4694cfe Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Tue, 29 Oct 2019 09:56:51 +0000 Subject: [PATCH 3/5] Fix silly camel-casing --- .../java/org/elasticsearch/common/xcontent/XContentHelper.java | 2 +- .../action/search/SearchPhaseExecutionExceptionTests.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java index 4f304466f9f49..cc3158b631660 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java @@ -171,7 +171,7 @@ public static String convertToJson(BytesReference bytes, boolean reformatJson, X * @return reformatted JSON * @throws IOException if the reformatting fails, e.g. because the JSON is not well-formed */ - public static String stripWhiteSpace(String json) throws IOException { + public static String stripWhitespace(String json) throws IOException { return convertToJson(new BytesArray(json), true, XContentType.JSON); } diff --git a/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java b/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java index 77460cce038f3..804f200e17caa 100644 --- a/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java +++ b/server/src/test/java/org/elasticsearch/action/search/SearchPhaseExecutionExceptionTests.java @@ -57,7 +57,7 @@ public void testToXContent() throws IOException { }); // Failures are grouped (by default) - final String expectedJson = XContentHelper.stripWhiteSpace( + final String expectedJson = XContentHelper.stripWhitespace( "{" + " \"type\": \"search_phase_execution_exception\"," + " \"reason\": \"all shards failed\"," From 5c72c53b9807173555aee73bbbb1b1e294fed000 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Tue, 29 Oct 2019 11:03:13 +0000 Subject: [PATCH 4/5] Remove some old formatter tags that were missed --- .../search/suggest/SuggestTests.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java b/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java index 8df25c1cbf10e..403b6b8a08abd 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java @@ -131,20 +131,25 @@ public void testToXContent() throws IOException { Suggest suggest = new Suggest(Collections.singletonList(suggestion)); BytesReference xContent = toXContent(suggest, XContentType.JSON, randomBoolean()); assertEquals( - // @formatter:off - "{\"suggest\":" - + "{\"suggestionName\":" - + "[{\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[{\"text\":\"someText\"," - + "\"highlighted\":\"somethingHighlighted\"," - + "\"score\":1.3," - + "\"collate_match\":true}]" - + "}]" - + "}" - +"}", - // @formatter:on + "{" + + " \"suggest\": {" + + " \"suggestionName\": [" + + " {" + + " \"text\": \"entryText\"," + + " \"offset\": 42," + + " \"length\": 313," + + " \"options\": [" + + " {" + + " \"text\": \"someText\"," + + " \"highlighted\": \"somethingHighlighted\"," + + " \"score\": 1.3," + + " \"collate_match\": true" + + " }" + + " ]" + + " }" + + " ]" + + " }" + + "}", xContent.utf8ToString()); } From 30fd4050cd3debe888eaf630195e8eefbe81de33 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Tue, 29 Oct 2019 11:51:53 +0000 Subject: [PATCH 5/5] Fix test failure --- .../search/suggest/SuggestTests.java | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java b/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java index 403b6b8a08abd..64dae21dd7fbf 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java @@ -52,6 +52,7 @@ import java.util.List; import static java.util.Collections.emptyList; +import static org.elasticsearch.common.xcontent.XContentHelper.stripWhitespace; import static org.elasticsearch.common.xcontent.XContentHelper.toXContent; import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureFieldName; @@ -131,25 +132,27 @@ public void testToXContent() throws IOException { Suggest suggest = new Suggest(Collections.singletonList(suggestion)); BytesReference xContent = toXContent(suggest, XContentType.JSON, randomBoolean()); assertEquals( - "{" - + " \"suggest\": {" - + " \"suggestionName\": [" - + " {" - + " \"text\": \"entryText\"," - + " \"offset\": 42," - + " \"length\": 313," - + " \"options\": [" - + " {" - + " \"text\": \"someText\"," - + " \"highlighted\": \"somethingHighlighted\"," - + " \"score\": 1.3," - + " \"collate_match\": true" - + " }" - + " ]" - + " }" - + " ]" - + " }" - + "}", + stripWhitespace( + "{" + + " \"suggest\": {" + + " \"suggestionName\": [" + + " {" + + " \"text\": \"entryText\"," + + " \"offset\": 42," + + " \"length\": 313," + + " \"options\": [" + + " {" + + " \"text\": \"someText\"," + + " \"highlighted\": \"somethingHighlighted\"," + + " \"score\": 1.3," + + " \"collate_match\": true" + + " }" + + " ]" + + " }" + + " ]" + + " }" + + "}" + ), xContent.utf8ToString()); }