From 7bf64a27309b6011b20324e2f9e7888b69a14c5e Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Mon, 30 Sep 2019 10:08:24 +0100 Subject: [PATCH 01/15] Change comment style to preserve ASCII art --- .../queries/BinaryDocValuesRangeQuery.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/server/src/main/java/org/apache/lucene/queries/BinaryDocValuesRangeQuery.java b/server/src/main/java/org/apache/lucene/queries/BinaryDocValuesRangeQuery.java index d12ecacbdc7d3..0c3de3b7ed2b6 100644 --- a/server/src/main/java/org/apache/lucene/queries/BinaryDocValuesRangeQuery.java +++ b/server/src/main/java/org/apache/lucene/queries/BinaryDocValuesRangeQuery.java @@ -145,25 +145,31 @@ public enum QueryType { INTERSECTS { @Override boolean matches(BytesRef from, BytesRef to, BytesRef otherFrom, BytesRef otherTo) { - // part of the other range must touch this range - // this: |---------------| - // other: |------| + /* + * part of the other range must touch this range + * this: |---------------| + * other: |------| + */ return from.compareTo(otherTo) <= 0 && to.compareTo(otherFrom) >= 0; } }, WITHIN { @Override boolean matches(BytesRef from, BytesRef to, BytesRef otherFrom, BytesRef otherTo) { - // other range must entirely lie within this range - // this: |---------------| - // other: |------| + /* + * other range must entirely lie within this range + * this: |---------------| + * other: |------| + */ return from.compareTo(otherFrom) <= 0 && to.compareTo(otherTo) >= 0; } }, CONTAINS { @Override boolean matches(BytesRef from, BytesRef to, BytesRef otherFrom, BytesRef otherTo) { - // this and other range must overlap - // this: |------| - // other: |---------------| + /* + * this and other range must overlap + * this: |------| + * other: |---------------| + */ return from.compareTo(otherFrom) >= 0 && to.compareTo(otherTo) <= 0; } }, CROSSES { From b50783b780db981934456582202107b9ecd37593 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Mon, 30 Sep 2019 14:20:19 +0100 Subject: [PATCH 02/15] Proof some code against the formatter --- .../common/ssl/SslConfigurationLoader.java | 2 + .../action/search/FetchSearchPhase.java | 6 +- .../cluster/routing/RoutingNodes.java | 7 +- .../search/suggest/SuggestTests.java | 28 +++-- .../search/suggest/SuggestionOptionTests.java | 16 ++- .../search/suggest/SuggestionTests.java | 118 ++++++++++-------- .../term/TermSuggestionBuilderTests.java | 2 + 7 files changed, 103 insertions(+), 76 deletions(-) diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationLoader.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationLoader.java index e9a1ccad3e950..21ce345d65f2b 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationLoader.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationLoader.java @@ -68,6 +68,7 @@ public abstract class SslConfigurationLoader { static final List DEFAULT_PROTOCOLS = List.of("TLSv1.3", "TLSv1.2", "TLSv1.1"); + // @formatter:off private static final List JDK11_CIPHERS = List.of( "TLS_AES_256_GCM_SHA384", "TLS_AES_128_GCM_SHA256", // TLSv1.3 cipher has PFS, AEAD, hardware support "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support @@ -93,6 +94,7 @@ public abstract class SslConfigurationLoader { "TLS_RSA_WITH_AES_256_GCM_SHA384", "TLS_RSA_WITH_AES_128_GCM_SHA256", // AEAD, hardware support "TLS_RSA_WITH_AES_256_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", // hardware support "TLS_RSA_WITH_AES_256_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA"); // hardware support + // @formatter:on static final List DEFAULT_CIPHERS = JavaVersion.current().compareTo(JavaVersion.parse("12")) > -1 ? JDK12_CIPHERS : JDK11_CIPHERS; diff --git a/server/src/main/java/org/elasticsearch/action/search/FetchSearchPhase.java b/server/src/main/java/org/elasticsearch/action/search/FetchSearchPhase.java index 548c21ab1b699..09f3368d0e708 100644 --- a/server/src/main/java/org/elasticsearch/action/search/FetchSearchPhase.java +++ b/server/src/main/java/org/elasticsearch/action/search/FetchSearchPhase.java @@ -111,10 +111,12 @@ private void innerRun() throws IOException { } else { ScoreDoc[] scoreDocs = reducedQueryPhase.sortedTopDocs.scoreDocs; final IntArrayList[] docIdsToLoad = searchPhaseController.fillDocIdsToLoad(numShards, scoreDocs); - if (scoreDocs.length == 0) { // no docs to fetch -- sidestep everything and return + // no docs to fetch -- sidestep everything and return + if (scoreDocs.length == 0) { + // we have to release contexts here to free up resources phaseResults.stream() .map(SearchPhaseResult::queryResult) - .forEach(this::releaseIrrelevantSearchContext); // we have to release contexts here to free up resources + .forEach(this::releaseIrrelevantSearchContext); finishPhase.run(); } else { final ScoreDoc[] lastEmittedDocPerShard = isScrollSearch ? diff --git a/server/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java b/server/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java index 4750476805d88..3c68bc362f004 100644 --- a/server/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java +++ b/server/src/main/java/org/elasticsearch/cluster/routing/RoutingNodes.java @@ -117,10 +117,11 @@ public RoutingNodes(ClusterState clusterState, boolean readOnly) { assignedShardsAdd(shard); if (shard.relocating()) { relocatingShards++; - entries = nodesToShards.computeIfAbsent(shard.relocatingNodeId(), - k -> new LinkedHashMap<>()); // LinkedHashMap to preserve order - // add the counterpart shard with relocatingNodeId reflecting the source from which + // LinkedHashMap to preserve order. + // Add the counterpart shard with relocatingNodeId reflecting the source from which // it's relocating from. + entries = nodesToShards.computeIfAbsent(shard.relocatingNodeId(), + k -> new LinkedHashMap<>()); ShardRouting targetShardRouting = shard.getTargetRelocatingShard(); addInitialRecovery(targetShardRouting, indexShard.primary); previousValue = entries.put(targetShardRouting.shardId(), targetShardRouting); 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..15cd5384934ab 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,15 @@ 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( + // @formatter:off + "{" + + "\"text\":\"someText\"," + + "\"highlighted\":\"somethingHighlighted\"," + + "\"score\":1.3," + + "\"collate_match\":true" + + "}", + // @formatter:on + 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..de10a7b7c1548 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java @@ -165,16 +165,18 @@ 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}]" - + "}]" - + "}"; + // @formatter:off + "{\"unknownType#suggestionName\":" + + "[{\"text\":\"entryText\"," + + "\"offset\":42," + + "\"length\":313," + + "\"options\":[{\"text\":\"someText\"," + + "\"highlighted\":\"somethingHighlighted\"," + + "\"score\":1.3," + + "\"collate_match\":true}]" + + "}]" + + "}"; + // @formatter:on try (XContentParser parser = xContent.createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, suggestionString)) { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); @@ -196,17 +198,20 @@ public void testToXContent() throws IOException { 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()); + // @formatter:off + "{\"phrase#suggestionName\":[{" + + "\"text\":\"entryText\"," + + "\"offset\":42," + + "\"length\":313," + + "\"options\":[{" + + "\"text\":\"someText\"," + + "\"highlighted\":\"somethingHighlighted\"," + + "\"score\":1.3," + + "\"collate_match\":true}]" + + "}]" + + "}", + // @formatter:on + xContent.utf8ToString()); } { PhraseSuggestion.Entry.Option option = new PhraseSuggestion.Entry.Option(new Text("someText"), new Text("somethingHighlighted"), @@ -217,17 +222,20 @@ public void testToXContent() throws IOException { 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()); + // @formatter:off + "{\"phrase#suggestionName\":[{" + + "\"text\":\"entryText\"," + + "\"offset\":42," + + "\"length\":313," + + "\"options\":[{" + + "\"text\":\"someText\"," + + "\"highlighted\":\"somethingHighlighted\"," + + "\"score\":1.3," + + "\"collate_match\":true}]" + + "}]" + + "}", + // @formatter:on + xContent.utf8ToString()); } { TermSuggestion.Entry.Option option = new TermSuggestion.Entry.Option(new Text("someText"), 10, 1.3f); @@ -237,16 +245,19 @@ 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()); + // @formatter:off + "{\"term#suggestionName\":[{" + + "\"text\":\"entryText\"," + + "\"offset\":42," + + "\"length\":313," + + "\"options\":[{" + + "\"text\":\"someText\"," + + "\"score\":1.3," + + "\"freq\":10}]" + + "}]" + + "}", + // @formatter:on + xContent.utf8ToString()); } { Map> contexts = Collections.singletonMap("key", Collections.singleton("value")); @@ -257,16 +268,19 @@ 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()); + // @formatter:off + "{\"completion#suggestionName\":[{" + + "\"text\":\"entryText\"," + + "\"offset\":42," + + "\"length\":313," + + "\"options\":[{" + + "\"text\":\"someText\"," + + "\"score\":1.3," + + "\"contexts\":{\"key\":[\"value\"]}" + + "}]" + + "}]}", + // @formatter:on + 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..36116b7b8a8d8 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,6 +210,7 @@ public void testDefaultValuesSet() { public void testMalformedJson() { final String field = RandomStrings.randomAsciiOfLength(random(), 10).toLowerCase(Locale.ROOT); + // @formatter:off String suggest = "{\n" + " \"bad-payload\" : {\n" + " \"text\" : \"the amsterdma meetpu\",\n" + @@ -218,6 +219,7 @@ public void testMalformedJson() { " }\n" + " }\n" + "}"; + // @formatter:on 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 568e48da2747f103dd90fc89eec2e97b7b024ae3 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Mon, 30 Sep 2019 14:22:58 +0100 Subject: [PATCH 03/15] Refine code formatter settings --- .eclipseformat.xml | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/.eclipseformat.xml b/.eclipseformat.xml index 4df52d1b60364..8d7abd35c4977 100644 --- a/.eclipseformat.xml +++ b/.eclipseformat.xml @@ -39,7 +39,7 @@ - + @@ -47,7 +47,7 @@ - + @@ -64,7 +64,7 @@ - + @@ -98,7 +98,7 @@ - + @@ -128,11 +128,11 @@ - + - + @@ -143,7 +143,7 @@ - + @@ -189,8 +189,8 @@ - - + + @@ -202,12 +202,12 @@ - + - + - + @@ -240,7 +240,7 @@ - + @@ -291,7 +291,7 @@ - + From c34c973bd2bbf27e8b2ce0b5a8d4451383b35cb1 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Tue, 1 Oct 2019 14:41:10 +0100 Subject: [PATCH 04/15] Note how to disable formatting in CONTRIBUTING.md --- CONTRIBUTING.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f96ef5dd1d5ed..c212e4c37f059 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -188,6 +188,9 @@ Please follow these formatting guidelines: There are two configuration options: `Class count to use import with '*'` and `Names count to use static import with '*'`. Set their values to 99999 or some other absurdly high value. +* If absolutely necessary, you can disable formatting for regions of code + with `// @formatter:off` and `// @formatter:on`. Please only do this + where the benefit clearly outweighs the decrease in consistency. #### Editor / IDE Support From ac88244a0a96f00deba779ef3ad61132d874f698 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Fri, 4 Oct 2019 13:19:57 +0100 Subject: [PATCH 05/15] Fix missing backtick in CONTRIBUTING.md --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c212e4c37f059..e66df6a706575 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -217,7 +217,7 @@ Spotless will write files to different copies of the formatted files, so that you can see how they differ and infer what is the problem. -The `paddedCell() option is disabled for normal operation in order to +The `paddedCell()` option is disabled for normal operation in order to detect any misbehaviour. You can enabled the option from the command line by running Gradle with `-Dspotless.paddedcell`. From 4919dec85bda206441c0f4070a4b714c0228b96b Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Sun, 13 Oct 2019 15:43:33 +0100 Subject: [PATCH 06/15] Better whitespace formatting, fix tag/end handling For cases where the formatter was disabled around JSON snippets, embed more whitespace within the strings to as to format the JSON in a way that the formatter won't break. Also, change the formatter's enable/disables tags to that the documentation comments i.e "// tag::NAME" and "// end::NAME" still work. This means that such snippets won't be automatically formatted, but since the formatter can't be configured different for specific regions in a file, it's the best we can do. --- .eclipseformat.xml | 4 +- CONTRIBUTING.md | 21 ++- .../elasticsearch/client/IndicesClientIT.java | 37 ++-- .../client/IngestRequestConvertersTests.java | 27 ++- .../org/elasticsearch/client/SearchIT.java | 36 +--- .../org/elasticsearch/client/TasksIT.java | 12 +- .../common/ssl/SslConfigurationLoader.java | 91 +++++++--- .../search/suggest/SuggestionOptionTests.java | 15 +- .../search/suggest/SuggestionTests.java | 160 ++++++++++-------- .../term/TermSuggestionBuilderTests.java | 20 +-- .../xpack/sql/type/DataType.java | 9 +- 11 files changed, 260 insertions(+), 172 deletions(-) diff --git a/.eclipseformat.xml b/.eclipseformat.xml index 8d7abd35c4977..f83f32b494e0f 100644 --- a/.eclipseformat.xml +++ b/.eclipseformat.xml @@ -29,7 +29,7 @@ - + @@ -43,7 +43,7 @@ - + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ca6bab5d00c86..f6245a0880608 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -109,15 +109,15 @@ and `JAVA11_HOME` available so that the tests can pass. Elasticsearch uses the Gradle wrapper for its build. You can execute Gradle using the wrapper via the `gradlew` script in the root of the repository. -We support development in the Eclipse and IntelliJ IDEs. -For Eclipse, the minimum version that we support is [4.13][eclipse]. +We support development in the Eclipse and IntelliJ IDEs. +For Eclipse, the minimum version that we support is [4.13][eclipse]. For IntelliJ, the minimum version that we support is [IntelliJ 2017.2][intellij]. ### Configuring IDEs And Running Tests Eclipse users can automatically configure their IDE: `./gradlew eclipse` then `File: Import: Gradle : Existing Gradle Project`. -Additionally you will want to ensure that Eclipse is using 2048m of heap by modifying +Additionally you will want to ensure that Eclipse is using 2048m of heap by modifying `eclipse.ini` accordingly to avoid GC overhead and OOM errors. IntelliJ users can automatically configure their IDE: `./gradlew idea` @@ -175,9 +175,12 @@ Please follow these formatting guidelines: * Java indent is 4 spaces * Line width is 140 characters -* Lines of code surrounded by `// tag` and `// end` comments are included +* Lines of code surrounded by `// tag::NAME` and `// end::NAME` comments are included in the documentation and should only be 76 characters wide not counting - leading indentation + leading indentation. Such regions of code are not formatted automatically as + it is not possible to change the line length rule of the formatter for + part of a file. Please format such sections sympathetically with the rest + of the code, while keeping lines to maximum length of 76 characters. * Wildcard imports (`import foo.bar.baz.*`) are forbidden and will cause the build to fail. This can be done automatically by your IDE: * Eclipse: `Preferences->Java->Code Style->Organize Imports`. There are @@ -187,9 +190,11 @@ Please follow these formatting guidelines: There are two configuration options: `Class count to use import with '*'` and `Names count to use static import with '*'`. Set their values to 99999 or some other absurdly high value. -* If absolutely necessary, you can disable formatting for regions of code - with `// @formatter:off` and `// @formatter:on`. Please only do this - where the benefit clearly outweighs the decrease in consistency. +* If *absolutely* necessary, you can disable formatting for regions of code + with the `// tag::NAME` and `// end::NAME` directives, but note that + these are intended for use in documentation, so please make it clear what + you have done, and only do this where the benefit clearly outweighs the + decrease in consistency. #### Editor / IDE Support diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java index 43e4b26ac3f17..80d74082dd3e9 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java @@ -1454,10 +1454,8 @@ public void testPutTemplate() throws Exception { .order(10) .create(randomBoolean()) .settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0")) - .mapping("{ \"properties\":{" - + "\"host_name\": {\"type\":\"keyword\"}" - + "}" - + "}", XContentType.JSON) + .mapping("{ \"properties\": { \"host_name\": { \"type\": \"keyword\" } } }", XContentType.JSON) + .alias(new Alias("alias-1").indexRouting("abc")) .alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz")); AcknowledgedResponse putTemplateResponse = execute(putTemplateRequest, @@ -1480,13 +1478,18 @@ public void testPutTemplateWithTypesUsingUntypedAPI() throws Exception { .order(10) .create(randomBoolean()) .settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0")) - .mapping("{ " - + "\"my_doc_type\":{" - + "\"properties\":{" - + "\"host_name\": {\"type\":\"keyword\"}" - + "}" - + "}" - + "}", XContentType.JSON) + .mapping( + "{" + + " \"my_doc_type\": {" + + " \"properties\": {" + + " \"host_name\": {" + + " \"type\": \"keyword\"" + + " }" + + " }" + + " }" + + "}", + XContentType.JSON + ) .alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz")); @@ -1581,8 +1584,16 @@ public void testInvalidValidateQuery() throws IOException{ createIndex(index, Settings.EMPTY); Request postDoc = new Request(HttpPost.METHOD_NAME, "/" + index + "/_doc"); postDoc.setJsonEntity( - "{\"type\":\"act\",\"line_id\":1,\"play_name\":\"Henry IV\", \"speech_number\":\"\"," + - "\"line_number\":\"\",\"speaker\":\"\",\"text_entry\":\"ACT I\"}"); + "{" + + " \"type\": \"act\"," + + " \"line_id\": 1," + + " \"play_name\": \"Henry IV\"," + + " \"speech_number\": \"\"," + + " \"line_number\": \"\"," + + " \"speaker\": \"\"," + + " \"text_entry\": \"ACT I\"" + + "}" + ); assertOK(client().performRequest(postDoc)); QueryBuilder builder = QueryBuilders diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/IngestRequestConvertersTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/IngestRequestConvertersTests.java index a615757fa22a9..1af55d8a93c55 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/IngestRequestConvertersTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/IngestRequestConvertersTests.java @@ -93,10 +93,29 @@ public void testDeletePipeline() { public void testSimulatePipeline() throws IOException { String pipelineId = ESTestCase.randomBoolean() ? "some_pipeline_id" : null; boolean verbose = ESTestCase.randomBoolean(); - String json = "{\"pipeline\":{" + - "\"description\":\"_description\"," + - "\"processors\":[{\"set\":{\"field\":\"field2\",\"value\":\"_value\"}}]}," + - "\"docs\":[{\"_index\":\"index\",\"_type\":\"_doc\",\"_id\":\"id\",\"_source\":{\"foo\":\"rab\"}}]}"; + String json = "{" + + " \"pipeline\": {" + + " \"description\": \"_description\"," + + " \"processors\": [" + + " {" + + " \"set\": {" + + " \"field\": \"field2\"," + + " \"value\": \"_value\"" + + " }" + + " }" + + " ]" + + " }," + + " \"docs\": [" + + " {" + + " \"_index\": \"index\"," + + " \"_type\": \"_doc\"," + + " \"_id\": \"id\"," + + " \"_source\": {" + + " \"foo\": \"rab\"" + + " }" + + " }" + + " ]" + + "}"; SimulatePipelineRequest request = new SimulatePipelineRequest( new BytesArray(json.getBytes(StandardCharsets.UTF_8)), XContentType.JSON diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java index 0760a13ca87d9..5618e18742267 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java @@ -886,14 +886,7 @@ public void testSearchTemplate() throws IOException { searchTemplateRequest.setRequest(new SearchRequest("index")); searchTemplateRequest.setScriptType(ScriptType.INLINE); - searchTemplateRequest.setScript( - "{" + - " \"query\": {" + - " \"match\": {" + - " \"num\": {{number}}" + - " }" + - " }" + - "}"); + searchTemplateRequest.setScript("{ \"query\": { \"match\": { \"num\": {{number}} } } }"); Map scriptParams = new HashMap<>(); scriptParams.put("number", 10); @@ -941,14 +934,7 @@ public void testRenderSearchTemplate() throws IOException { SearchTemplateRequest searchTemplateRequest = new SearchTemplateRequest(); searchTemplateRequest.setScriptType(ScriptType.INLINE); - searchTemplateRequest.setScript( - "{" + - " \"query\": {" + - " \"match\": {" + - " \"num\": {{number}}" + - " }" + - " }" + - "}"); + searchTemplateRequest.setScript("{ \"query\": { \"match\": { \"num\": {{number}} } } }"); Map scriptParams = new HashMap<>(); scriptParams.put("number", 10); @@ -985,14 +971,7 @@ public void testMultiSearchTemplate() throws Exception { SearchTemplateRequest goodRequest = new SearchTemplateRequest(); goodRequest.setRequest(new SearchRequest("index")); goodRequest.setScriptType(ScriptType.INLINE); - goodRequest.setScript( - "{" + - " \"query\": {" + - " \"match\": {" + - " \"num\": {{number}}" + - " }" + - " }" + - "}"); + goodRequest.setScript("{ \"query\": { \"match\": { \"num\": {{number}} } } }"); Map scriptParams = new HashMap<>(); scriptParams.put("number", 10); goodRequest.setScriptParams(scriptParams); @@ -1044,14 +1023,7 @@ public void testMultiSearchTemplateAllBad() throws Exception { SearchTemplateRequest badRequest1 = new SearchTemplateRequest(); badRequest1.setRequest(new SearchRequest("index")); badRequest1.setScriptType(ScriptType.INLINE); - badRequest1.setScript( - "{" + - " \"query\": {" + - " \"match\": {" + - " \"num\": {{number}}" + - " }" + - " }" + - "}"); + badRequest1.setScript("{ \"query\": { \"match\": { \"num\": {{number}} } } }"); Map scriptParams = new HashMap<>(); scriptParams.put("number", "BAD NUMBER"); badRequest1.setScriptParams(scriptParams); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/TasksIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/TasksIT.java index f0515c0d9e2d1..8f2b6cc1d1849 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/TasksIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/TasksIT.java @@ -93,8 +93,16 @@ public void testGetValidTask() throws Exception { RestClient lowClient = highLevelClient().getLowLevelClient(); Request request = new Request("POST", "_reindex"); request.addParameter("wait_for_completion", "false"); - request.setJsonEntity("{" + " \"source\": {\n" + " \"index\": \"source1\"\n" + " },\n" + " \"dest\": {\n" - + " \"index\": \"dest\"\n" + " }" + "}"); + request.setJsonEntity( + "{" + + " \"source\": {\n" + + " \"index\": \"source1\"\n" + + " },\n" + + " \"dest\": {\n" + + " \"index\": \"dest\"\n" + + " }" + + "}" + ); Response response = lowClient.performRequest(request); Map map = entityAsMap(response); Object taskId = map.get("task"); diff --git a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationLoader.java b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationLoader.java index 21ce345d65f2b..e4af77a878232 100644 --- a/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationLoader.java +++ b/libs/ssl-config/src/main/java/org/elasticsearch/common/ssl/SslConfigurationLoader.java @@ -68,33 +68,76 @@ public abstract class SslConfigurationLoader { static final List DEFAULT_PROTOCOLS = List.of("TLSv1.3", "TLSv1.2", "TLSv1.1"); - // @formatter:off private static final List JDK11_CIPHERS = List.of( - "TLS_AES_256_GCM_SHA384", "TLS_AES_128_GCM_SHA256", // TLSv1.3 cipher has PFS, AEAD, hardware support - "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", // PFS, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", // PFS, hardware support - "TLS_RSA_WITH_AES_256_GCM_SHA384", "TLS_RSA_WITH_AES_128_GCM_SHA256", // AEAD, hardware support - "TLS_RSA_WITH_AES_256_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", // hardware support - "TLS_RSA_WITH_AES_256_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA"); // hardware support + // TLSv1.3 cipher has PFS, AEAD, hardware support + "TLS_AES_256_GCM_SHA384", + "TLS_AES_128_GCM_SHA256", + // PFS, AEAD, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", + // PFS, AEAD, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", + // PFS, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", + // PFS, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", + // PFS, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", + // PFS, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", + // AEAD, hardware support + "TLS_RSA_WITH_AES_256_GCM_SHA384", + "TLS_RSA_WITH_AES_128_GCM_SHA256", + // hardware support + "TLS_RSA_WITH_AES_256_CBC_SHA256", + "TLS_RSA_WITH_AES_128_CBC_SHA256", + // hardware support + "TLS_RSA_WITH_AES_256_CBC_SHA", + "TLS_RSA_WITH_AES_128_CBC_SHA" + ); private static final List JDK12_CIPHERS = List.of( - "TLS_AES_256_GCM_SHA384", "TLS_AES_128_GCM_SHA256", // TLSv1.3 cipher has PFS, AEAD, hardware support - "TLS_CHACHA20_POLY1305_SHA256", // TLSv1.3 cipher has PFS, AEAD - "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support - "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", // PFS, AEAD - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", // PFS, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", // PFS, hardware support - "TLS_RSA_WITH_AES_256_GCM_SHA384", "TLS_RSA_WITH_AES_128_GCM_SHA256", // AEAD, hardware support - "TLS_RSA_WITH_AES_256_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", // hardware support - "TLS_RSA_WITH_AES_256_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA"); // hardware support - // @formatter:on + // TLSv1.3 cipher has PFS, AEAD, hardware support + "TLS_AES_256_GCM_SHA384", + "TLS_AES_128_GCM_SHA256", + // TLSv1.3 cipher has PFS, AEAD + "TLS_CHACHA20_POLY1305_SHA256", + // PFS, AEAD, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", + // PFS, AEAD, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", + // PFS, AEAD + "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", + "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", + // PFS, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", + // PFS, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", + // PFS, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", + // PFS, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", + // AEAD, hardware support + "TLS_RSA_WITH_AES_256_GCM_SHA384", + "TLS_RSA_WITH_AES_128_GCM_SHA256", + // hardware support + "TLS_RSA_WITH_AES_256_CBC_SHA256", + "TLS_RSA_WITH_AES_128_CBC_SHA256", + // hardware support + "TLS_RSA_WITH_AES_256_CBC_SHA", + "TLS_RSA_WITH_AES_128_CBC_SHA" + ); static final List DEFAULT_CIPHERS = JavaVersion.current().compareTo(JavaVersion.parse("12")) > -1 ? JDK12_CIPHERS : JDK11_CIPHERS; 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 15cd5384934ab..a045a1b6fa569 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestionOptionTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestionOptionTests.java @@ -82,14 +82,13 @@ 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( - // @formatter:off "{" - + "\"text\":\"someText\"," - + "\"highlighted\":\"somethingHighlighted\"," - + "\"score\":1.3," - + "\"collate_match\":true" - + "}", - // @formatter:on - xContent.utf8ToString()); + + " \"text\": \"someText\"," + + " \"highlighted\": \"somethingHighlighted\"," + + " \"score\": 1.3," + + " \"collate_match\": true" + + "}", + 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 de10a7b7c1548..78a1300402cc9 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestionTests.java @@ -164,19 +164,23 @@ public void testFromXContentWithoutTypeParam() throws IOException { public void testUnknownSuggestionTypeThrows() throws IOException { XContent xContent = JsonXContent.jsonXContent; - String suggestionString = - // @formatter:off - "{\"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" + + " }" + + " ]" + + " }" + + " ]" + "}"; - // @formatter:on try (XContentParser parser = xContent.createParser(xContentRegistry(), DeprecationHandler.THROW_UNSUPPORTED_OPERATION, suggestionString)) { ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); @@ -198,20 +202,25 @@ public void testToXContent() throws IOException { suggestion.addTerm(entry); BytesReference xContent = toXContent(suggestion, XContentType.JSON, params, randomBoolean()); assertEquals( - // @formatter:off - "{\"phrase#suggestionName\":[{" - + "\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[{" - + "\"text\":\"someText\"," - + "\"highlighted\":\"somethingHighlighted\"," - + "\"score\":1.3," - + "\"collate_match\":true}]" - + "}]" - + "}", - // @formatter:on - xContent.utf8ToString()); + "{" + + " \"phrase#suggestionName\": [" + + " {" + + " \"text\": \"entryText\"," + + " \"offset\": 42," + + " \"length\": 313," + + " \"options\": [" + + " {" + + " \"text\": \"someText\"," + + " \"highlighted\": \"somethingHighlighted\"," + + " \"score\": 1.3," + + " \"collate_match\": true" + + " }" + + " ]" + + " }" + + " ]" + + "}", + xContent.utf8ToString() + ); } { PhraseSuggestion.Entry.Option option = new PhraseSuggestion.Entry.Option(new Text("someText"), new Text("somethingHighlighted"), @@ -222,20 +231,25 @@ public void testToXContent() throws IOException { suggestion.addTerm(entry); BytesReference xContent = toXContent(suggestion, XContentType.JSON, params, randomBoolean()); assertEquals( - // @formatter:off - "{\"phrase#suggestionName\":[{" - + "\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[{" - + "\"text\":\"someText\"," - + "\"highlighted\":\"somethingHighlighted\"," - + "\"score\":1.3," - + "\"collate_match\":true}]" - + "}]" - + "}", - // @formatter:on - xContent.utf8ToString()); + "{" + + " \"phrase#suggestionName\": [" + + " {" + + " \"text\": \"entryText\"," + + " \"offset\": 42," + + " \"length\": 313," + + " \"options\": [" + + " {" + + " \"text\": \"someText\"," + + " \"highlighted\": \"somethingHighlighted\"," + + " \"score\": 1.3," + + " \"collate_match\": true" + + " }" + + " ]" + + " }" + + " ]" + + "}", + xContent.utf8ToString() + ); } { TermSuggestion.Entry.Option option = new TermSuggestion.Entry.Option(new Text("someText"), 10, 1.3f); @@ -245,19 +259,24 @@ public void testToXContent() throws IOException { suggestion.addTerm(entry); BytesReference xContent = toXContent(suggestion, XContentType.JSON, params, randomBoolean()); assertEquals( - // @formatter:off - "{\"term#suggestionName\":[{" - + "\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[{" - + "\"text\":\"someText\"," - + "\"score\":1.3," - + "\"freq\":10}]" - + "}]" - + "}", - // @formatter:on - xContent.utf8ToString()); + "{" + + " \"term#suggestionName\": [" + + " {" + + " \"text\": \"entryText\"," + + " \"offset\": 42," + + " \"length\": 313," + + " \"options\": [" + + " {" + + " \"text\": \"someText\"," + + " \"score\": 1.3," + + " \"freq\": 10" + + " }" + + " ]" + + " }" + + " ]" + + "}", + xContent.utf8ToString() + ); } { Map> contexts = Collections.singletonMap("key", Collections.singleton("value")); @@ -268,19 +287,28 @@ public void testToXContent() throws IOException { suggestion.addTerm(entry); BytesReference xContent = toXContent(suggestion, XContentType.JSON, params, randomBoolean()); assertEquals( - // @formatter:off - "{\"completion#suggestionName\":[{" - + "\"text\":\"entryText\"," - + "\"offset\":42," - + "\"length\":313," - + "\"options\":[{" - + "\"text\":\"someText\"," - + "\"score\":1.3," - + "\"contexts\":{\"key\":[\"value\"]}" - + "}]" - + "}]}", - // @formatter:on - xContent.utf8ToString()); + "{" + + " \"completion#suggestionName\": [" + + " {" + + " \"text\": \"entryText\"," + + " \"offset\": 42," + + " \"length\": 313," + + " \"options\": [" + + " {" + + " \"text\": \"someText\"," + + " \"score\": 1.3," + + " \"contexts\": {" + + " \"key\": [" + + " \"value\"" + + " ]" + + " }" + + " }" + + " ]" + + " }" + + " ]" + + "}", + 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 36116b7b8a8d8..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,16 +210,16 @@ public void testDefaultValuesSet() { public void testMalformedJson() { final String field = RandomStrings.randomAsciiOfLength(random(), 10).toLowerCase(Locale.ROOT); - // @formatter:off - String suggest = "{\n" + - " \"bad-payload\" : {\n" + - " \"text\" : \"the amsterdma meetpu\",\n" + - " \"term\" : {\n" + - " \"field\" : { \"" + field + "\" : \"bad-object\" }\n" + - " }\n" + - " }\n" + - "}"; - // @formatter:on + 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); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/DataType.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/DataType.java index 22f2a596e3515..69d72c674e991 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/DataType.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/DataType.java @@ -24,7 +24,10 @@ */ public enum DataType { - // @formatter:off + // The following code is more clearly expressed in a tabular formatter, + // so we disable automatic formatting with 'tag' and 'end'. + // + // tag::xpack-sql-datatypes-disable-formatting // esType jdbc type, size, defPrecision,dispSize, int, rat, docvals NULL( "null", JDBCType.NULL, 0, 0, 0, false, false, false), UNSUPPORTED( JDBCType.OTHER, 0, 0, 0, false, false, false), @@ -79,7 +82,7 @@ public enum DataType { INTERVAL_HOUR_TO_MINUTE( ExtTypes.INTERVAL_HOUR_TO_MINUTE, Long.BYTES, 23, 23, false, false, false), INTERVAL_HOUR_TO_SECOND( ExtTypes.INTERVAL_HOUR_TO_SECOND, Long.BYTES, 23, 23, false, false, false), INTERVAL_MINUTE_TO_SECOND(ExtTypes.INTERVAL_MINUTE_TO_SECOND,Long.BYTES, 23, 23, false, false, false); - // @formatter:on + // end::xpack-sql-datatypes-disable-formatting private static final Map ODBC_TO_ES = new HashMap<>(36); static { @@ -285,7 +288,7 @@ public boolean isDayTimeInterval() { return (ordinal >= INTERVAL_DAY.ordinal() && ordinal <= INTERVAL_SECOND.ordinal()) || (ordinal >= INTERVAL_DAY_TO_HOUR.ordinal() && ordinal <= INTERVAL_MINUTE_TO_SECOND.ordinal()); } - + // data type extract-able from _source or from docvalue_fields public boolean isFromDocValuesOnly() { return this == KEYWORD // because of ignore_above. Extracting this from _source wouldn't make sense if it wasn't indexed at all. From 0e8924515ddb2201fc57e67f0b7e42d52b5250d7 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Tue, 22 Oct 2019 16:27:04 +0100 Subject: [PATCH 07/15] Various formatting improvements Make a number of code changes so that when the codebase is automatically formatted, the code doens't end up looking mangled. Mostly this is: - Moving comments - Joining multiline strings that fit on one line - Formatting JSON so that indentation is inside the strings - This includes stripping out additional whitespace in some cases, where the test does an exact comparison with JSON from the API Also reformat XML documents in the SAML tests, using a formatter that can handle named parameters. This significantly improves legibility. --- .eclipseformat.xml | 8 +- CONTRIBUTING.md | 2 + .../IndicesClientDocumentationIT.java | 35 +- .../QueryDSLDocumentationTests.java | 20 +- .../documentation/SearchDocumentationIT.java | 9 +- .../StoredScriptsDocumentationIT.java | 4 +- .../GetIndexTemplatesResponseTests.java | 8 +- .../rollup/GetRollupCapsResponseTests.java | 9 +- .../GetRollupIndexCapsResponseTests.java | 4 +- .../ClearRealmCacheResponseTests.java | 13 +- .../ClearRolesCacheResponseTests.java | 13 +- .../security/CreateTokenRequestTests.java | 25 +- .../security/InvalidateTokenRequestTests.java | 27 +- .../security/PutRoleMappingRequestTests.java | 70 +- .../transforms/pivot/GroupConfigTests.java | 63 +- .../ConstructingObjectParserTests.java | 175 +- .../common/xcontent/ObjectParserTests.java | 55 +- .../analysis/common/CommonAnalysisPlugin.java | 4 +- .../elasticsearch/common/lucene/Lucene.java | 3 +- .../common/xcontent/XContentHelper.java | 12 + .../index/mapper/DocumentParser.java | 3 +- .../index/query/MoreLikeThisQueryBuilder.java | 50 +- .../cluster/IndicesClusterStateService.java | 4 +- .../metrics/HyperLogLogPlusPlus.java | 4 + .../search/suggest/phrase/WordScorer.java | 4 +- .../elasticsearch/transport/TcpTransport.java | 16 +- .../time/format/StrictISODateTimeFormat.java | 31 +- .../SearchPhaseExecutionExceptionTests.java | 65 +- .../TransportMasterNodeActionTests.java | 7 +- .../coordination/LeaderCheckerTests.java | 3 +- .../index/store/CorruptedFileIT.java | 3 +- .../plugins/PluginsServiceTests.java | 3 +- .../elasticsearch/recovery/RelocationIT.java | 3 +- .../aggregations/bucket/DateRangeIT.java | 3 +- .../search/preference/SearchPreferenceIT.java | 4 +- .../search/sort/ScriptSortBuilderTests.java | 25 +- .../suggest/CompletionSuggestSearchIT.java | 3 +- .../threadpool/ThreadPoolTests.java | 4 +- .../xpack/ccr/action/ShardFollowNodeTask.java | 24 +- .../AutoFollowStatsMonitoringDocTests.java | 91 +- .../ccr/FollowStatsMonitoringDocTests.java | 148 +- .../xpack/core/XPackSettings.java | 89 +- .../xpack/core/ilm/ShrinkStep.java | 4 +- .../core/ilm/WaitForRolloverReadyStep.java | 12 +- .../transforms/TransformCheckpoint.java | 9 +- .../license/ExpirationCallbackTests.java | 5 +- .../license/LicenseServiceClusterTests.java | 7 +- .../DataFrameAnalyticsConfigTests.java | 35 +- .../transforms/pivot/PivotConfigTests.java | 14 +- .../action/TransportGraphExploreAction.java | 9 +- .../ClusterStatsMonitoringDocTests.java | 477 ++--- .../IndexRecoveryMonitoringDocTests.java | 148 +- .../indices/IndexStatsMonitoringDocTests.java | 248 +-- .../IndicesStatsMonitoringDocTests.java | 109 +- .../ml/JobStatsMonitoringDocTests.java | 150 +- .../node/NodeStatsMonitoringDocTests.java | 341 +-- .../shards/ShardsMonitoringDocTests.java | 47 +- .../BaseFilteredMonitoringDocTestCase.java | 49 +- .../BytesReferenceMonitoringDocTests.java | 35 +- .../HttpExportBulkResponseListenerTests.java | 43 +- .../exporter/http/HttpExporterIT.java | 4 +- .../http/HttpExporterResourceTests.java | 2 +- .../xpack/rollup/job/IndexerUtilsTests.java | 3 +- .../security/authc/saml/NamedFormatter.java | 58 + .../authc/saml/NamedFormatterTests.java | 45 + .../authc/saml/SamlAuthenticatorTests.java | 1843 ++++++++++------- .../saml/SamlSpMetadataBuilderTests.java | 406 ++-- .../xpack/sql/optimizer/OptimizerTests.java | 74 +- .../TransformInfoTransportActionTests.java | 3 +- 69 files changed, 3024 insertions(+), 2302 deletions(-) create mode 100644 x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatter.java create mode 100644 x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatterTests.java diff --git a/.eclipseformat.xml b/.eclipseformat.xml index f83f32b494e0f..9ff0aa1f4081f 100644 --- a/.eclipseformat.xml +++ b/.eclipseformat.xml @@ -89,7 +89,7 @@ - + @@ -167,7 +167,7 @@ - + @@ -205,7 +205,7 @@ - + @@ -225,7 +225,7 @@ - + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f6245a0880608..1f9e967cb9aeb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -195,6 +195,8 @@ Please follow these formatting guidelines: these are intended for use in documentation, so please make it clear what you have done, and only do this where the benefit clearly outweighs the decrease in consistency. +* Note that JavaDoc and block comments i.e. `/* ... */` are not formatted, + but line comments i.e `// ...` are. #### Editor / IDE Support diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java index 601ff20030de0..8bc33f9d00eef 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/IndicesClientDocumentationIT.java @@ -564,15 +564,9 @@ public void testGetMapping() throws IOException { CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); PutMappingRequest request = new PutMappingRequest("twitter"); - request.source( - "{\n" + - " \"properties\": {\n" + - " \"message\": {\n" + - " \"type\": \"text\"\n" + - " }\n" + - " }\n" + - "}", // <1> - XContentType.JSON); + request.source("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", + XContentType.JSON + ); AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT); assertTrue(putMappingResponse.isAcknowledged()); } @@ -618,15 +612,9 @@ public void testGetMappingAsync() throws Exception { CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT); assertTrue(createIndexResponse.isAcknowledged()); PutMappingRequest request = new PutMappingRequest("twitter"); - request.source( - "{\n" + - " \"properties\": {\n" + - " \"message\": {\n" + - " \"type\": \"text\"\n" + - " }\n" + - " }\n" + - "}", // <1> - XContentType.JSON); + request.source("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", + XContentType.JSON + ); AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT); assertTrue(putMappingResponse.isAcknowledged()); } @@ -2296,14 +2284,9 @@ public void testGetTemplates() throws Exception { PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template", List.of("pattern-1", "log-*")); putRequest.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1)); - putRequest.mapping( - "{\n" + - " \"properties\": {\n" + - " \"message\": {\n" + - " \"type\": \"text\"\n" + - " }\n" + - " }\n" + - "}", XContentType.JSON); + putRequest.mapping("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", + XContentType.JSON + ); assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged()); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/QueryDSLDocumentationTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/QueryDSLDocumentationTests.java index 08d13dfc14e01..60e6248fede07 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/QueryDSLDocumentationTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/QueryDSLDocumentationTests.java @@ -441,26 +441,28 @@ public void testWrapper() { } public void testRankFeatureSaturation() { - RankFeatureQueryBuilders.saturation( - "pagerank"); // <1> + RankFeatureQueryBuilders.saturation("pagerank"); } public void testRankFeatureSaturationPivot() { RankFeatureQueryBuilders.saturation( - "pagerank", // <1> - 8); // <2> + "pagerank", + 8 + ); } public void testRankFeatureLog() { RankFeatureQueryBuilders.log( - "pagerank", // <1> - 4f); // <2> + "pagerank", + 4f + ); } public void testRankFeatureSigmoid() { RankFeatureQueryBuilders.sigmoid( - "pagerank", // <1> - 7, // <2> - 0.6f); // <3> + "pagerank", + 7, + 0.6f + ); } } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java index ff5deb5cbdfcc..47ae97fe3573f 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java @@ -773,11 +773,10 @@ public void testSearchTemplateWithInlineScript() throws Exception { // end::render-search-template-response assertNotNull(source); - assertEquals(( - "{" + - " \"size\" : \"5\"," + - " \"query\": { \"match\" : { \"title\" : \"elasticsearch\" } }" + - "}").replaceAll("\\s+", ""), source.utf8ToString()); + assertEquals( + ("{ \"size\" : \"5\", \"query\": { \"match\" : { \"title\" : \"elasticsearch\" } }}").replaceAll("\\s+", ""), + source.utf8ToString() + ); } public void testSearchTemplateWithStoredScript() throws Exception { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/StoredScriptsDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/StoredScriptsDocumentationIT.java index ac4f2269f9c79..fdaf92c00d19c 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/StoredScriptsDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/StoredScriptsDocumentationIT.java @@ -1,4 +1,6 @@ -package org.elasticsearch.client.documentation;/* +package org.elasticsearch.client.documentation; + +/* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetIndexTemplatesResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetIndexTemplatesResponseTests.java index d94d8572f3de9..b9b8f971aa905 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetIndexTemplatesResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/indices/GetIndexTemplatesResponseTests.java @@ -55,12 +55,8 @@ import static org.hamcrest.Matchers.equalTo; public class GetIndexTemplatesResponseTests extends ESTestCase { - - static final String mappingString = "{\"properties\":{" - + "\"f1\": {\"type\":\"text\"}," - + "\"f2\": {\"type\":\"keyword\"}" - + "}}"; - + + static final String mappingString = "{\"properties\": {\"f1\": {\"type\": \"text\"},\"f2\": {\"type\": \"keyword\"}}}"; public void testFromXContent() throws IOException { xContentTester(this::createParser, diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupCapsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupCapsResponseTests.java index a9c3a59faf5ae..f49b273a971b4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupCapsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupCapsResponseTests.java @@ -43,11 +43,12 @@ protected void toXContent(GetRollupCapsResponse response, XContentBuilder builde @Override protected Predicate randomFieldsExcludeFilter() { - return (field) -> + return (field) -> { // base cannot have extra things in it - "".equals(field) - // the field list expects to be a nested object of a certain type - || field.contains("fields"); + return "".equals(field) + // the field list expects to be a nested object of a certain type + || field.contains("fields"); + }; } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsResponseTests.java index 20e29aef0df64..ac138719bd0ce 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/rollup/GetRollupIndexCapsResponseTests.java @@ -44,10 +44,12 @@ protected void toXContent(GetRollupIndexCapsResponse response, XContentBuilder b @Override protected Predicate randomFieldsExcludeFilter() { return (field) -> + { // base cannot have extra things in it - "".equals(field) + return "".equals(field) // the field list expects to be a nested object of a certain type || field.contains("fields"); + }; } @Override diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRealmCacheResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRealmCacheResponseTests.java index d21ed1a71a0c4..4909ae804ef68 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRealmCacheResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRealmCacheResponseTests.java @@ -55,12 +55,13 @@ public void testParseFromXContent() throws IOException { assertThat(response.getHeader().getFailures().get(0).getMessage(), containsString("reason=test")); } - try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - "{" + nodesHeader + clusterName + "\"nodes\" : { " + - "\"id1\": { \"name\": \"a\"}, " + - "\"id2\": { \"name\": \"b\"}" + - "}}")) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + "{" + nodesHeader + clusterName + "\"nodes\" : { \"id1\": { \"name\": \"a\"}, \"id2\": { \"name\": \"b\"}}}" + ) + ) { ClearRealmCacheResponse response = ClearRealmCacheResponse.fromXContent(parser); assertNotNull(response); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRolesCacheResponseTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRolesCacheResponseTests.java index 068b6ec452908..b8ce5f203a6cf 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRolesCacheResponseTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/ClearRolesCacheResponseTests.java @@ -55,12 +55,13 @@ public void testParseFromXContent() throws IOException { assertThat(response.getHeader().getFailures().get(0).getMessage(), containsString("reason=test")); } - try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, - DeprecationHandler.THROW_UNSUPPORTED_OPERATION, - "{" + nodesHeader + clusterName + "\"nodes\" : { " + - "\"id1\": { \"name\": \"a\"}, " + - "\"id2\": { \"name\": \"b\"}" + - "}}")) { + try ( + XContentParser parser = JsonXContent.jsonXContent.createParser( + NamedXContentRegistry.EMPTY, + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, + "{" + nodesHeader + clusterName + "\"nodes\" : { \"id1\": { \"name\": \"a\"}, \"id2\": { \"name\": \"b\"}}}" + ) + ) { ClearRolesCacheResponse response = ClearRolesCacheResponse.fromXContent(parser); assertNotNull(response); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenRequestTests.java index 760d5e52cb38f..1c97dbe985562 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/CreateTokenRequestTests.java @@ -34,12 +34,10 @@ public void testCreateTokenFromPassword() { assertThat(new String(request.getPassword()), equalTo("top secret password")); assertThat(request.getScope(), nullValue()); assertThat(request.getRefreshToken(), nullValue()); - assertThat(Strings.toString(request), equalTo("{" + - "\"grant_type\":\"password\"," + - "\"username\":\"jsmith\"," + - "\"password\":\"top secret password\"" + - "}" - )); + assertThat( + Strings.toString(request), + equalTo("{\"grant_type\": \"password\", \"username\": \"jsmith\", \"password\": \"top secret password\"}") + ); } public void testCreateTokenFromRefreshToken() { @@ -49,11 +47,10 @@ public void testCreateTokenFromRefreshToken() { assertThat(request.getScope(), nullValue()); assertThat(request.getUsername(), nullValue()); assertThat(request.getPassword(), nullValue()); - assertThat(Strings.toString(request), equalTo("{" + - "\"grant_type\":\"refresh_token\"," + - "\"refresh_token\":\"9a7f41cf-9918-4d1f-bfaa-ad3f8f9f02b9\"" + - "}" - )); + assertThat( + Strings.toString(request), + equalTo("{\"grant_type\": \"refresh_token\", \"refresh_token\": \"9a7f41cf-9918-4d1f-bfaa-ad3f8f9f02b9\"}") + ); } public void testCreateTokenFromClientCredentials() { @@ -74,8 +71,10 @@ public void testCreateTokenFromKerberosTicket() { assertThat(request.getPassword(), nullValue()); assertThat(request.getRefreshToken(), nullValue()); assertThat(new String(request.getKerberosTicket()), equalTo("top secret kerberos ticket")); - assertThat(Strings.toString(request), equalTo("{\"grant_type\":\"_kerberos\"," + - "\"kerberos_ticket\":\"top secret kerberos ticket\"}")); + assertThat( + Strings.toString(request), + equalTo("{\"grant_type\": \"_kerberos\", \"kerberos_ticket\": \"top secret kerberos ticket\"}") + ); } public void testEqualsAndHashCode() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateTokenRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateTokenRequestTests.java index 0fa7ce2792ba5..e26c9c03d5ad4 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateTokenRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/InvalidateTokenRequestTests.java @@ -32,10 +32,7 @@ public void testInvalidateAccessToken() { final InvalidateTokenRequest request = InvalidateTokenRequest.accessToken(token); assertThat(request.getAccessToken(), equalTo(token)); assertThat(request.getRefreshToken(), nullValue()); - assertThat(Strings.toString(request), equalTo("{" + - "\"token\":\"Tf01rrAymdUjxMY4VlG3gV3gsFFUWxVVPrztX+4uhe0=\"" + - "}" - )); + assertThat(Strings.toString(request), equalTo("{\"token\":\"Tf01rrAymdUjxMY4VlG3gV3gsFFUWxVVPrztX+4uhe0=\"}")); } public void testInvalidateRefreshToken() { @@ -43,10 +40,7 @@ public void testInvalidateRefreshToken() { final InvalidateTokenRequest request = InvalidateTokenRequest.refreshToken(token); assertThat(request.getAccessToken(), nullValue()); assertThat(request.getRefreshToken(), equalTo(token)); - assertThat(Strings.toString(request), equalTo("{" + - "\"refresh_token\":\"4rE0YPT/oHODS83TbTtYmuh8\"" + - "}" - )); + assertThat(Strings.toString(request), equalTo("{\"refresh_token\":\"4rE0YPT/oHODS83TbTtYmuh8\"}")); } public void testInvalidateRealmTokens() { @@ -56,10 +50,7 @@ public void testInvalidateRealmTokens() { assertThat(request.getRefreshToken(), nullValue()); assertThat(request.getRealmName(), equalTo(realmName)); assertThat(request.getUsername(), nullValue()); - assertThat(Strings.toString(request), equalTo("{" + - "\"realm_name\":\"native\"" + - "}" - )); + assertThat(Strings.toString(request), equalTo("{\"realm_name\":\"native\"}")); } public void testInvalidateUserTokens() { @@ -69,10 +60,7 @@ public void testInvalidateUserTokens() { assertThat(request.getRefreshToken(), nullValue()); assertThat(request.getRealmName(), nullValue()); assertThat(request.getUsername(), equalTo(username)); - assertThat(Strings.toString(request), equalTo("{" + - "\"username\":\"user\"" + - "}" - )); + assertThat(Strings.toString(request), equalTo("{\"username\":\"user\"}")); } public void testInvalidateUserTokensInRealm() { @@ -83,12 +71,7 @@ public void testInvalidateUserTokensInRealm() { assertThat(request.getRefreshToken(), nullValue()); assertThat(request.getRealmName(), equalTo(realmName)); assertThat(request.getUsername(), equalTo(username)); - assertThat(Strings.toString(request), equalTo("{" + - "\"realm_name\":\"native\"," + - "\"username\":\"user\"" + - - "}" - )); + assertThat(Strings.toString(request), equalTo("{\"realm_name\":\"native\",\"username\":\"user\"}")); } public void testEqualsAndHashCode() { diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutRoleMappingRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutRoleMappingRequestTests.java index bf5ba34bffc5c..da33c97777e68 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutRoleMappingRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/PutRoleMappingRequestTests.java @@ -132,16 +132,26 @@ public void testPutRoleMappingRequestToXContent() throws IOException { final XContentBuilder builder = XContentFactory.jsonBuilder(); putRoleMappingRequest.toXContent(builder, ToXContent.EMPTY_PARAMS); final String output = Strings.toString(builder); - final String expected = - "{"+ - "\"enabled\":" + enabled + "," + - "\"roles\":[\"superuser\"]," + - "\"role_templates\":[]," + - "\"rules\":{" + - "\"field\":{\"username\":[\"user\"]}" + - "}," + - "\"metadata\":{\"k1\":\"v1\"}" + - "}"; + final String expected = String.format( + "{" + + " \"enabled\": %s," + + " \"roles\": [" + + " \"superuser\"" + + " ]," + + "\"role_templates\":[]," + + "\"rules\":{" + + " \"field\": {" + + " \"username\": [" + + " \"user\"" + + " ]" + + " }" + + "}," + + " \"metadata\": {" + + " \"k1\": \"v1\"" + + " }" + + "}", + enabled + ).replaceAll("\\s+", ""); assertThat(output, equalTo(expected)); } @@ -164,19 +174,33 @@ public void testPutRoleMappingRequestWithTemplateToXContent() throws IOException final XContentBuilder builder = XContentFactory.jsonBuilder(); putRoleMappingRequest.toXContent(builder, ToXContent.EMPTY_PARAMS); final String output = Strings.toString(builder); - final String expected = - "{"+ - "\"enabled\":" + enabled + "," + - "\"roles\":[]," + - "\"role_templates\":[" + - "{\"template\":\"{\\\"source\\\":\\\"_realm_{{realm.name}}\\\"}\",\"format\":\"string\"}," + - "{\"template\":\"{\\\"source\\\":\\\"some_role\\\"}\",\"format\":\"string\"}" + - "]," + - "\"rules\":{" + - "\"field\":{\"username\":[\"user\"]}" + - "}," + - "\"metadata\":{\"k1\":\"v1\"}" + - "}"; + final String expected = String.format( + "{" + + " \"enabled\": %s," + + "\"roles\":[]," + + "\"role_templates\":[" + + " {" + + " \"template\": \"{\\\"source\\\":\\\"_realm_{{realm.name}}\\\"}\"," + + " \"format\": \"string\"" + + " }," + + " {" + + " \"template\": \"{\\\"source\\\":\\\"some_role\\\"}\"," + + " \"format\": \"string\"" + + " }" + + "]," + + "\"rules\":{" + + " \"field\": {" + + " \"username\": [" + + " \"user\"" + + " ]" + + " }" + + "}," + + " \"metadata\": {" + + " \"k1\": \"v1\"" + + " }" + + "}", + enabled + ).replaceAll("\\s+", ""); assertThat(output, equalTo(expected)); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/GroupConfigTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/GroupConfigTests.java index f20fe098ecbca..1749c390994e6 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/GroupConfigTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/transform/transforms/pivot/GroupConfigTests.java @@ -87,21 +87,25 @@ protected Predicate getRandomFieldsExcludeFilter() { public void testLenientParsing() throws IOException { BytesArray json = new BytesArray( - "{ " + - "\"unknown-field\":\"foo\", " + - "\"destination-field\": {" + - "\"terms\": {" + - "\"field\": \"term-field\"" + - "}" + - "}," + - "\"unknown-field-2\":\"bar\"," + - "\"destination-field2\": {" + - "\"terms\": {" + - "\"field\": \"term-field2\"" + - "}" + - "}," + - "\"array-field\" : [1.0, 2.0]" + - "}"); + "{" + + " \"unknown-field\": \"foo\"," + + " \"destination-field\": {" + + " \"terms\": {" + + " \"field\": \"term-field\"" + + " }" + + " }," + + " \"unknown-field-2\": \"bar\"," + + " \"destination-field2\": {" + + " \"terms\": {" + + " \"field\": \"term-field2\"" + + " }" + + " }," + + " \"array-field\": [" + + " 1," + + " 2" + + " ]" + + "}" + ); XContentParser parser = JsonXContent.jsonXContent .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json.streamInput()); @@ -116,20 +120,21 @@ public void testLenientParsing() throws IOException { public void testLenientParsingUnknowGroupType() throws IOException { BytesArray json = new BytesArray( - "{ " + - "\"destination-field1\": {" + - "\"newgroup\": {" + - "\"field1\": \"bar\"," + - "\"field2\": \"foo\"" + - "}" + - "}," + - "\"unknown-field\":\"bar\"," + - "\"destination-field2\": {" + - "\"terms\": {" + - "\"field\": \"term-field\"" + - "}" + - "}" + - "}"); + "{" + + " \"destination-field1\": {" + + " \"newgroup\": {" + + " \"field1\": \"bar\"," + + " \"field2\": \"foo\"" + + " }" + + " }," + + " \"unknown-field\": \"bar\"," + + " \"destination-field2\": {" + + " \"terms\": {" + + " \"field\": \"term-field\"" + + " }" + + " }" + + "}" + ); XContentParser parser = JsonXContent.jsonXContent .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json.streamInput()); diff --git a/libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ConstructingObjectParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ConstructingObjectParserTests.java index 00e34183d2f78..69b2f305c8862 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ConstructingObjectParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ConstructingObjectParserTests.java @@ -91,10 +91,7 @@ public void testRandomOrder() throws Exception { } public void testMissingAllConstructorArgs() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"mineral\": 1\n" - + "}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1 }"); ConstructingObjectParser objectParser = randomBoolean() ? HasCtorArguments.PARSER : HasCtorArguments.PARSER_VEGETABLE_OPTIONAL; IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> objectParser.apply(parser, null)); @@ -106,31 +103,20 @@ public void testMissingAllConstructorArgs() throws IOException { } public void testMissingAllConstructorArgsButNotRequired() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"mineral\": 1\n" - + "}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1 }"); HasCtorArguments parsed = HasCtorArguments.PARSER_ALL_OPTIONAL.apply(parser, null); assertEquals(1, parsed.mineral); } public void testMissingSecondConstructorArg() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"mineral\": 1,\n" - + " \"animal\": \"cat\"\n" - + "}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1, \"animal\": \"cat\" }"); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> HasCtorArguments.PARSER.apply(parser, null)); assertEquals("Required [vegetable]", e.getMessage()); } public void testMissingSecondConstructorArgButNotRequired() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"mineral\": 1,\n" - + " \"animal\": \"cat\"\n" - + "}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1, \"animal\": \"cat\" }"); @SuppressWarnings("unchecked") HasCtorArguments parsed = randomFrom(HasCtorArguments.PARSER_VEGETABLE_OPTIONAL, HasCtorArguments.PARSER_ALL_OPTIONAL).apply(parser, null); @@ -139,11 +125,7 @@ public void testMissingSecondConstructorArgButNotRequired() throws IOException { } public void testMissingFirstConstructorArg() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"mineral\": 1,\n" - + " \"vegetable\": 2\n" - + "}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1, \"vegetable\": 2 }"); @SuppressWarnings("unchecked") IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> randomFrom(HasCtorArguments.PARSER, HasCtorArguments.PARSER_VEGETABLE_OPTIONAL).apply(parser, null)); @@ -151,23 +133,17 @@ public void testMissingFirstConstructorArg() throws IOException { } public void testMissingFirstConstructorArgButNotRequired() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"mineral\": 1,\n" - + " \"vegetable\": 2\n" - + "}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1, \"vegetable\": 2 }"); HasCtorArguments parsed = HasCtorArguments.PARSER_ALL_OPTIONAL.apply(parser, null); assertEquals(1, parsed.mineral); assertEquals((Integer) 2, parsed.vegetable); } public void testBadParam() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"animal\": \"cat\",\n" - + " \"vegetable\": 2,\n" - + " \"a\": \"supercalifragilisticexpialidocious\"\n" - + "}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{ \"animal\": \"cat\", \"vegetable\": 2, \"a\": \"supercalifragilisticexpialidocious\" }" + ); XContentParseException e = expectThrows(XContentParseException.class, () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null)); assertThat(e.getMessage(), containsString("[has_required_arguments] failed to parse field [a]")); @@ -177,12 +153,10 @@ public void testBadParam() throws IOException { } public void testBadParamBeforeObjectBuilt() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"a\": \"supercalifragilisticexpialidocious\",\n" - + " \"animal\": \"cat\"\n," - + " \"vegetable\": 2\n" - + "}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{ \"a\": \"supercalifragilisticexpialidocious\", \"animal\": \"cat\", \"vegetable\": 2 }" + ); XContentParseException e = expectThrows(XContentParseException.class, () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null)); assertThat(e.getMessage(), containsString("[has_required_arguments] failed to parse field [vegetable]")); @@ -238,40 +212,25 @@ void setFoo(String foo) { parser.declareString(ctorArgOptional ? optionalConstructorArg() : constructorArg(), new ParseField("yeah")); // ctor arg first so we can test for the bug we found one time - XContentParser xcontent = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"yeah\": \"!\",\n" - + " \"foo\": \"foo\"\n" - + "}"); + XContentParser xcontent = createParser(JsonXContent.jsonXContent, "{ \"yeah\": \"!\", \"foo\": \"foo\" }"); CalledOneTime result = parser.apply(xcontent, null); assertTrue(result.fooSet); // and ctor arg second just in case - xcontent = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"foo\": \"foo\",\n" - + " \"yeah\": \"!\"\n" - + "}"); + xcontent = createParser(JsonXContent.jsonXContent, "{ \"foo\": \"foo\", \"yeah\": \"!\" }"); result = parser.apply(xcontent, null); assertTrue(result.fooSet); if (ctorArgOptional) { // and without the constructor arg if we've made it optional - xcontent = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"foo\": \"foo\"\n" - + "}"); + xcontent = createParser(JsonXContent.jsonXContent, "{ \"foo\": \"foo\" }"); result = parser.apply(xcontent, null); } assertTrue(result.fooSet); } public void testIgnoreUnknownFields() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"test\" : \"foo\",\n" - + " \"junk\" : 2\n" - + "}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"test\" : \"foo\", \"junk\" : 2 }"); class TestStruct { public final String test; TestStruct(String test) { @@ -286,11 +245,7 @@ class TestStruct { } public void testConstructObjectUsingContext() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"animal\": \"dropbear\",\n" - + " \"mineral\": -8\n" - + "}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"animal\": \"dropbear\", \"mineral\": -8 }"); HasCtorArguments parsed = HasCtorArguments.PARSER_INT_CONTEXT.apply(parser, 42); assertEquals(Integer.valueOf(42), parsed.vegetable); assertEquals("dropbear", parsed.animal); @@ -410,12 +365,10 @@ private static void declareSetters(ConstructingObjectParser } public void testParseNamedObject() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": {\n" - + " \"a\": {}" - + "},\"named_in_constructor\": {\n" - + " \"b\": {}" - + "}}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{\"named\": { \"a\": {} }, \"named_in_constructor\": { \"b\": {} } }" + ); NamedObjectHolder h = NamedObjectHolder.PARSER.apply(parser, null); assertThat(h.named, hasSize(1)); assertEquals("a", h.named.get(0).name); @@ -425,12 +378,10 @@ public void testParseNamedObject() throws IOException { } public void testParseNamedObjectInOrder() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " {\"a\": {}}" - + "],\"named_in_constructor\": [\n" - + " {\"b\": {}}" - + "]}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{\"named\": [ {\"a\": {}} ], \"named_in_constructor\": [ {\"b\": {}} ]}" + ); NamedObjectHolder h = NamedObjectHolder.PARSER.apply(parser, null); assertThat(h.named, hasSize(1)); assertEquals("a", h.named.get(0).name); @@ -440,12 +391,10 @@ public void testParseNamedObjectInOrder() throws IOException { } public void testParseNamedObjectTwoFieldsInArray() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " {\"a\": {}, \"b\": {}}" - + "],\"named_in_constructor\": [\n" - + " {\"c\": {}}" - + "]}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{\"named\": [ {\"a\": {}, \"b\": {}}], \"named_in_constructor\": [ {\"c\": {}} ]}" + ); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]")); assertThat(e.getCause().getMessage(), @@ -454,12 +403,10 @@ public void testParseNamedObjectTwoFieldsInArray() throws IOException { } public void testParseNamedObjectTwoFieldsInArrayConstructorArg() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " {\"a\": {}}" - + "],\"named_in_constructor\": [\n" - + " {\"c\": {}, \"d\": {}}" - + "]}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{\"named\": [ {\"a\": {}}], \"named_in_constructor\": [ {\"c\": {}, \"d\": {}} ]}" + ); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]")); assertThat(e.getCause().getMessage(), @@ -468,12 +415,7 @@ public void testParseNamedObjectTwoFieldsInArrayConstructorArg() throws IOExcept } public void testParseNamedObjectNoFieldsInArray() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " {}" - + "],\"named_in_constructor\": [\n" - + " {\"a\": {}}" - + "]}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {} ], \"named_in_constructor\": [ {\"a\": {}} ]}"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]")); assertThat(e.getCause().getMessage(), @@ -482,12 +424,7 @@ public void testParseNamedObjectNoFieldsInArray() throws IOException { } public void testParseNamedObjectNoFieldsInArrayConstructorArg() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " {\"a\": {}}" - + "],\"named_in_constructor\": [\n" - + " {}" - + "]}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {\"a\": {}} ], \"named_in_constructor\": [ {} ]}"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]")); assertThat(e.getCause().getMessage(), @@ -496,12 +433,10 @@ public void testParseNamedObjectNoFieldsInArrayConstructorArg() throws IOExcepti } public void testParseNamedObjectJunkInArray() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " \"junk\"" - + "],\"named_in_constructor\": [\n" - + " {\"a\": {}}" - + "]}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{\"named\": [ \"junk\" ], \"named_in_constructor\": [ {\"a\": {}} ]}" + ); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]")); assertThat(e.getCause().getMessage(), @@ -510,12 +445,10 @@ public void testParseNamedObjectJunkInArray() throws IOException { } public void testParseNamedObjectJunkInArrayConstructorArg() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " {\"a\": {}}" - + "],\"named_in_constructor\": [\n" - + " \"junk\"" - + "]}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{\"named\": [ {\"a\": {}} ], \"named_in_constructor\": [ \"junk\" ]}" + ); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]")); assertThat(e.getCause().getMessage(), @@ -524,11 +457,10 @@ public void testParseNamedObjectJunkInArrayConstructorArg() throws IOException { } public void testParseNamedObjectInOrderNotSupported() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " {\"a\": {}}" - + "],\"named_in_constructor\": {\"b\": {}}" - + "}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{\"named\": [\n" + " {\"a\": {}}" + "],\"named_in_constructor\": {\"b\": {}}" + "}" + ); // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above @SuppressWarnings("unchecked") @@ -545,11 +477,10 @@ public void testParseNamedObjectInOrderNotSupported() throws IOException { } public void testParseNamedObjectInOrderNotSupportedConstructorArg() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": {\"a\": {}}" - + ",\"named_in_constructor\": [\n" - + " {\"b\": {}}" - + "]}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{\"named\": {\"a\": {}}, \"named_in_constructor\": [ {\"b\": {}} ]}" + ); // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above @SuppressWarnings("unchecked") diff --git a/libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java index 6002c6bd35076..a2c45eceb8d58 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ObjectParserTests.java @@ -44,12 +44,10 @@ public class ObjectParserTests extends ESTestCase { public void testBasics() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"test\" : \"foo\",\n" - + " \"test_number\" : 2,\n" - + " \"test_array\": [1,2,3,4]\n" - + "}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{ \"test\" : \"foo\", \"test_number\" : 2, \"test_array\": [1,2,3,4] }" + ); class TestStruct { public String test; int testNumber; @@ -176,8 +174,10 @@ public URI parseURI(XContentParser parser) { } } } - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"url\" : { \"host\": \"http://foobar\", \"port\" : 80}, \"name\" : \"foobarbaz\"}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{\"url\" : { \"host\": \"http://foobar\", \"port\" : 80}, \"name\" : \"foobarbaz\"}" + ); ObjectParser objectParser = new ObjectParser<>("foo"); objectParser.declareString(Foo::setName, new ParseField("name")); objectParser.declareObjectOrDefault(Foo::setURI, (p, s) -> s.parseURI(p), () -> null, new ParseField("url")); @@ -449,10 +449,7 @@ public void setString_or_null(String string_or_null) { } public void testParseNamedObject() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": {\n" - + " \"a\": {}" - + "}}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": { \"a\": {} }}"); NamedObjectHolder h = NamedObjectHolder.PARSER.apply(parser, null); assertThat(h.named, hasSize(1)); assertEquals("a", h.named.get(0).name); @@ -460,10 +457,7 @@ public void testParseNamedObject() throws IOException { } public void testParseNamedObjectInOrder() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " {\"a\": {}}" - + "]}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {\"a\": {}} ] }"); NamedObjectHolder h = NamedObjectHolder.PARSER.apply(parser, null); assertThat(h.named, hasSize(1)); assertEquals("a", h.named.get(0).name); @@ -471,10 +465,7 @@ public void testParseNamedObjectInOrder() throws IOException { } public void testParseNamedObjectTwoFieldsInArray() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " {\"a\": {}, \"b\": {}}" - + "]}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {\"a\": {}, \"b\": {}}]}"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]")); assertThat(e.getCause().getMessage(), @@ -483,10 +474,7 @@ public void testParseNamedObjectTwoFieldsInArray() throws IOException { } public void testParseNamedObjectNoFieldsInArray() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " {}" - + "]}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {} ]}"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]")); assertThat(e.getCause().getMessage(), @@ -495,10 +483,7 @@ public void testParseNamedObjectNoFieldsInArray() throws IOException { } public void testParseNamedObjectJunkInArray() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " \"junk\"" - + "]}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ \"junk\" ] }"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]")); assertThat(e.getCause().getMessage(), @@ -507,10 +492,7 @@ public void testParseNamedObjectJunkInArray() throws IOException { } public void testParseNamedObjectInOrderNotSupported() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": [\n" - + " {\"a\": {}}" - + "]}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {\"a\": {}} ] }"); // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above ObjectParser objectParser = new ObjectParser<>("named_object_holder", @@ -601,11 +583,10 @@ class TestStruct { } public void testArraysOfGenericValues() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\n" - + " \"test_array\": [ 1, null, \"3\", 4.2],\n" - + " \"int_array\": [ 1, 2, 3]\n" - + "}"); + XContentParser parser = createParser( + JsonXContent.jsonXContent, + "{ \"test_array\": [ 1, null, \"3\", 4.2], \"int_array\": [ 1, 2, 3] }" + ); class TestStruct { List testArray = new ArrayList<>(); diff --git a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonAnalysisPlugin.java b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonAnalysisPlugin.java index a655f42a36c26..07f20c1639b71 100644 --- a/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonAnalysisPlugin.java +++ b/modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonAnalysisPlugin.java @@ -408,8 +408,8 @@ public List getPreConfiguredTokenFilters() { filters.add(PreConfiguredTokenFilter.singleton("indic_normalization", true, IndicNormalizationFilter::new)); filters.add(PreConfiguredTokenFilter.singleton("keyword_repeat", false, false, KeywordRepeatFilter::new)); filters.add(PreConfiguredTokenFilter.singleton("kstem", false, KStemFilter::new)); - filters.add(PreConfiguredTokenFilter.singleton("length", false, input -> - new LengthFilter(input, 0, Integer.MAX_VALUE))); // TODO this one seems useless + // TODO this one seems useless + filters.add(PreConfiguredTokenFilter.singleton("length", false, input -> new LengthFilter(input, 0, Integer.MAX_VALUE))); filters.add(PreConfiguredTokenFilter.singleton("limit", false, input -> new LimitTokenCountFilter(input, LimitTokenCountFilterFactory.DEFAULT_MAX_TOKEN_COUNT, diff --git a/server/src/main/java/org/elasticsearch/common/lucene/Lucene.java b/server/src/main/java/org/elasticsearch/common/lucene/Lucene.java index 4b0ec9ea08773..f05e28c342e00 100644 --- a/server/src/main/java/org/elasticsearch/common/lucene/Lucene.java +++ b/server/src/main/java/org/elasticsearch/common/lucene/Lucene.java @@ -256,7 +256,8 @@ public static void cleanLuceneIndex(Directory directory) throws IOException { .setSoftDeletesField(Lucene.SOFT_DELETES_FIELD) .setMergePolicy(NoMergePolicy.INSTANCE) // no merges .setCommitOnClose(false) // no commits - .setOpenMode(IndexWriterConfig.OpenMode.CREATE))) // force creation - don't append... + .setOpenMode(IndexWriterConfig.OpenMode.CREATE) // force creation - don't append... + )) { // do nothing and close this will kick of IndexFileDeleter which will remove all pending files } 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/main/java/org/elasticsearch/index/mapper/DocumentParser.java b/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java index 7f98fb17e4de2..44b30f8784ebd 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java @@ -118,7 +118,8 @@ private static void internalParseDocument(Mapping mapping, MetadataFieldMapper[] private void validateType(SourceToParse source) { if (Objects.equals(source.type(), docMapper.type()) == false && - MapperService.SINGLE_MAPPING_NAME.equals(source.type()) == false) { // used by typeless APIs + // used by typeless APIs + MapperService.SINGLE_MAPPING_NAME.equals(source.type()) == false) { throw new MapperParsingException("Type mismatch, provide type [" + source.type() + "] but mapper is of type [" + docMapper.type() + "]"); } diff --git a/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java index 77395c389847e..bbbe83c5e5368 100644 --- a/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilder.java @@ -458,14 +458,14 @@ public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Item)) return false; Item other = (Item) o; - return Objects.equals(index, other.index) && - Objects.equals(id, other.id) && - Objects.equals(doc, other.doc) && - Arrays.equals(fields, other.fields) && // otherwise we are comparing pointers - Objects.equals(perFieldAnalyzer, other.perFieldAnalyzer) && - Objects.equals(routing, other.routing) && - Objects.equals(version, other.version) && - Objects.equals(versionType, other.versionType); + return Objects.equals(index, other.index) + && Objects.equals(id, other.id) + && Objects.equals(doc, other.doc) + && Arrays.equals(fields, other.fields) // otherwise we are comparing pointers + && Objects.equals(perFieldAnalyzer, other.perFieldAnalyzer) + && Objects.equals(routing, other.routing) + && Objects.equals(version, other.version) + && Objects.equals(versionType, other.versionType); } } @@ -1139,23 +1139,23 @@ protected int doHashCode() { @Override protected boolean doEquals(MoreLikeThisQueryBuilder other) { - return Arrays.equals(fields, other.fields) && - Arrays.equals(likeTexts, other.likeTexts) && - Arrays.equals(unlikeTexts, other.unlikeTexts) && - Arrays.equals(likeItems, other.likeItems) && - Arrays.equals(unlikeItems, other.unlikeItems) && - Objects.equals(maxQueryTerms, other.maxQueryTerms) && - Objects.equals(minTermFreq, other.minTermFreq) && - Objects.equals(minDocFreq, other.minDocFreq) && - Objects.equals(maxDocFreq, other.maxDocFreq) && - Objects.equals(minWordLength, other.minWordLength) && - Objects.equals(maxWordLength, other.maxWordLength) && - Arrays.equals(stopWords, other.stopWords) && // otherwise we are comparing pointers - Objects.equals(analyzer, other.analyzer) && - Objects.equals(minimumShouldMatch, other.minimumShouldMatch) && - Objects.equals(boostTerms, other.boostTerms) && - Objects.equals(include, other.include) && - Objects.equals(failOnUnsupportedField, other.failOnUnsupportedField); + return Arrays.equals(fields, other.fields) + && Arrays.equals(likeTexts, other.likeTexts) + && Arrays.equals(unlikeTexts, other.unlikeTexts) + && Arrays.equals(likeItems, other.likeItems) + && Arrays.equals(unlikeItems, other.unlikeItems) + && Objects.equals(maxQueryTerms, other.maxQueryTerms) + && Objects.equals(minTermFreq, other.minTermFreq) + && Objects.equals(minDocFreq, other.minDocFreq) + && Objects.equals(maxDocFreq, other.maxDocFreq) + && Objects.equals(minWordLength, other.minWordLength) + && Objects.equals(maxWordLength, other.maxWordLength) + && Arrays.equals(stopWords, other.stopWords) // otherwise we are comparing pointers + && Objects.equals(analyzer, other.analyzer) + && Objects.equals(minimumShouldMatch, other.minimumShouldMatch) + && Objects.equals(boostTerms, other.boostTerms) + && Objects.equals(include, other.include) + && Objects.equals(failOnUnsupportedField, other.failOnUnsupportedField); } @Override diff --git a/server/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java b/server/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java index d189dfd33395b..4be112096c8bf 100644 --- a/server/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java +++ b/server/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java @@ -229,8 +229,8 @@ public synchronized void applyClusterState(final ClusterChangedEvent event) { // TODO: feels hacky, a block disables state persistence, and then we clean the allocated shards, maybe another flag in blocks? if (state.blocks().disableStatePersistence()) { for (AllocatedIndex indexService : indicesService) { - indicesService.removeIndex(indexService.index(), NO_LONGER_ASSIGNED, - "cleaning index (disabled block persistence)"); // also cleans shards + // also cleans shards + indicesService.removeIndex(indexService.index(), NO_LONGER_ASSIGNED, "cleaning index (disabled block persistence)"); } return; } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/HyperLogLogPlusPlus.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/HyperLogLogPlusPlus.java index 2bd30a677a0af..94cda1630f4d5 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/HyperLogLogPlusPlus.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/HyperLogLogPlusPlus.java @@ -79,6 +79,8 @@ public static long memoryUsage(int precision) { return 1L << precision; } + // tag::hyperloglog-data-tables + // these static tables come from the appendix of the paper private static final double[][] RAW_ESTIMATE_DATA = { // precision 4 @@ -751,6 +753,8 @@ public static long memoryUsage(int precision) { private static final long[] THRESHOLDS = new long[] { 10, 20, 40, 80, 220, 400, 900, 1800, 3100, 6500, 11500, 20000, 50000, 120000, 350000 }; + // end::hyperloglog-data-tables + private final BigArrays bigArrays; private final OpenBitSet algorithm; private ByteArray runLens; diff --git a/server/src/main/java/org/elasticsearch/search/suggest/phrase/WordScorer.java b/server/src/main/java/org/elasticsearch/search/suggest/phrase/WordScorer.java index cc4a44590e79e..adee76e8a170e 100644 --- a/server/src/main/java/org/elasticsearch/search/suggest/phrase/WordScorer.java +++ b/server/src/main/java/org/elasticsearch/search/suggest/phrase/WordScorer.java @@ -62,8 +62,8 @@ public WordScorer(IndexReader reader, Terms terms, String field, double realWord // division by zero, by scoreUnigram. final long nTerms = terms.size(); this.numTerms = nTerms == -1 ? reader.maxDoc() : nTerms; - this.termsEnum = new FreqTermsEnum(reader, field, !useTotalTermFreq, useTotalTermFreq, null, - BigArrays.NON_RECYCLING_INSTANCE); // non recycling for now + // non recycling for now + this.termsEnum = new FreqTermsEnum(reader, field, !useTotalTermFreq, useTotalTermFreq, null, BigArrays.NON_RECYCLING_INSTANCE); this.reader = reader; this.realWordLikelihood = realWordLikelihood; this.separator = separator; diff --git a/server/src/main/java/org/elasticsearch/transport/TcpTransport.java b/server/src/main/java/org/elasticsearch/transport/TcpTransport.java index e6c1ca1c84b91..ec179e9883cff 100644 --- a/server/src/main/java/org/elasticsearch/transport/TcpTransport.java +++ b/server/src/main/java/org/elasticsearch/transport/TcpTransport.java @@ -782,15 +782,15 @@ private static int readHeaderBuffer(BytesReference headerBuffer) throws IOExcept } private static boolean appearsToBeHTTPRequest(BytesReference headerBuffer) { - return bufferStartsWith(headerBuffer, "GET") || - bufferStartsWith(headerBuffer, "POST") || - bufferStartsWith(headerBuffer, "PUT") || - bufferStartsWith(headerBuffer, "HEAD") || - bufferStartsWith(headerBuffer, "DELETE") || + return bufferStartsWith(headerBuffer, "GET") + || bufferStartsWith(headerBuffer, "POST") + || bufferStartsWith(headerBuffer, "PUT") + || bufferStartsWith(headerBuffer, "HEAD") + || bufferStartsWith(headerBuffer, "DELETE") // Actually 'OPTIONS'. But we are only guaranteed to have read six bytes at this point. - bufferStartsWith(headerBuffer, "OPTION") || - bufferStartsWith(headerBuffer, "PATCH") || - bufferStartsWith(headerBuffer, "TRACE"); + || bufferStartsWith(headerBuffer, "OPTION") + || bufferStartsWith(headerBuffer, "PATCH") + || bufferStartsWith(headerBuffer, "TRACE"); } private static boolean appearsToBeHTTPResponse(BytesReference headerBuffer) { diff --git a/server/src/main/java/org/joda/time/format/StrictISODateTimeFormat.java b/server/src/main/java/org/joda/time/format/StrictISODateTimeFormat.java index aacb7cf330687..a373c3862cd8b 100644 --- a/server/src/main/java/org/joda/time/format/StrictISODateTimeFormat.java +++ b/server/src/main/java/org/joda/time/format/StrictISODateTimeFormat.java @@ -34,6 +34,7 @@ * In case you compare this with the original ISODateTimeFormat, make sure you use a diff * call, that ignores whitespaces/tabs/indentations like 'diff -b' */ + /** * Factory that creates instances of DateTimeFormatter based on the ISO8601 standard. *

@@ -1720,7 +1721,8 @@ private static DateTimeFormatter basicOrdinalDateTimeNoMillis() { private static DateTimeFormatter basicWeekDate() { if (bwd == null) { return new DateTimeFormatterBuilder() - .appendFixedSignedDecimal(DateTimeFieldType.weekyear(), 4) // ES change, was .appendWeekyear(4, 4) + // ES change, was .appendWeekyear(4, 4) + .appendFixedSignedDecimal(DateTimeFieldType.weekyear(), 4) .appendLiteral('W') .appendFixedDecimal(DateTimeFieldType.weekOfWeekyear(), 2) .appendFixedDecimal(DateTimeFieldType.dayOfWeek(), 1) @@ -1897,7 +1899,8 @@ private static DateTimeFormatter dateHourMinuteSecondFraction() { private static DateTimeFormatter yearElement() { if (ye == null) { return new DateTimeFormatterBuilder() - .appendFixedSignedDecimal(DateTimeFieldType.year(), 4) // ES change, was .appendYear(4, 9) + // ES change, was .appendYear(4, 9) + .appendFixedSignedDecimal(DateTimeFieldType.year(), 4) .toFormatter(); } return ye; @@ -1907,7 +1910,8 @@ private static DateTimeFormatter monthElement() { if (mye == null) { return new DateTimeFormatterBuilder() .appendLiteral('-') - .appendFixedSignedDecimal(DateTimeFieldType.monthOfYear(), 2) // ES change, was .appendMonthOfYear(2) + // ES change, was .appendMonthOfYear(2) + .appendFixedSignedDecimal(DateTimeFieldType.monthOfYear(), 2) .toFormatter(); } return mye; @@ -1917,7 +1921,8 @@ private static DateTimeFormatter dayOfMonthElement() { if (dme == null) { return new DateTimeFormatterBuilder() .appendLiteral('-') - .appendFixedSignedDecimal(DateTimeFieldType.dayOfMonth(), 2) // ES change, was .appendDayOfMonth(2) + // ES change, was .appendDayOfMonth(2) + .appendFixedSignedDecimal(DateTimeFieldType.dayOfMonth(), 2) .toFormatter(); } return dme; @@ -1926,7 +1931,8 @@ private static DateTimeFormatter dayOfMonthElement() { private static DateTimeFormatter weekyearElement() { if (we == null) { return new DateTimeFormatterBuilder() - .appendFixedSignedDecimal(DateTimeFieldType.weekyear(), 4) // ES change, was .appendWeekyear(4, 9) + // ES change, was .appendWeekyear(4, 9) + .appendFixedSignedDecimal(DateTimeFieldType.weekyear(), 4) .toFormatter(); } return we; @@ -1936,7 +1942,8 @@ private static DateTimeFormatter weekElement() { if (wwe == null) { return new DateTimeFormatterBuilder() .appendLiteral("-W") - .appendFixedSignedDecimal(DateTimeFieldType.weekOfWeekyear(), 2) // ES change, was .appendWeekOfWeekyear(2) + // ES change, was .appendWeekOfWeekyear(2) + .appendFixedSignedDecimal(DateTimeFieldType.weekOfWeekyear(), 2) .toFormatter(); } return wwe; @@ -1956,7 +1963,8 @@ private static DateTimeFormatter dayOfYearElement() { if (dye == null) { return new DateTimeFormatterBuilder() .appendLiteral('-') - .appendFixedSignedDecimal(DateTimeFieldType.dayOfYear(), 3) // ES change, was .appendDayOfYear(3) + // ES change, was .appendDayOfYear(3) + .appendFixedSignedDecimal(DateTimeFieldType.dayOfYear(), 3) .toFormatter(); } return dye; @@ -1974,7 +1982,8 @@ private static DateTimeFormatter literalTElement() { private static DateTimeFormatter hourElement() { if (hde == null) { return new DateTimeFormatterBuilder() - .appendFixedSignedDecimal(DateTimeFieldType.hourOfDay(), 2) // ES change, was .appendHourOfDay(2) + // ES change, was .appendHourOfDay(2) + .appendFixedSignedDecimal(DateTimeFieldType.hourOfDay(), 2) .toFormatter(); } return hde; @@ -1984,7 +1993,8 @@ private static DateTimeFormatter minuteElement() { if (mhe == null) { return new DateTimeFormatterBuilder() .appendLiteral(':') - .appendFixedSignedDecimal(DateTimeFieldType.minuteOfHour(), 2) // ES change, was .appendMinuteOfHour(2) + // ES change, was .appendMinuteOfHour(2) + .appendFixedSignedDecimal(DateTimeFieldType.minuteOfHour(), 2) .toFormatter(); } return mhe; @@ -1994,7 +2004,8 @@ private static DateTimeFormatter secondElement() { if (sme == null) { return new DateTimeFormatterBuilder() .appendLiteral(':') - .appendFixedSignedDecimal(DateTimeFieldType.secondOfMinute(), 2) // ES change, was .appendSecondOfMinute(2) + // ES change, was .appendSecondOfMinute(2) + .appendFixedSignedDecimal(DateTimeFieldType.secondOfMinute(), 2) .toFormatter(); } return sme; 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/action/support/master/TransportMasterNodeActionTests.java b/server/src/test/java/org/elasticsearch/action/support/master/TransportMasterNodeActionTests.java index 51ea048a96cc7..cea57fa8f4e21 100644 --- a/server/src/test/java/org/elasticsearch/action/support/master/TransportMasterNodeActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/support/master/TransportMasterNodeActionTests.java @@ -375,8 +375,11 @@ public void testDelegateToFailingMaster() throws ExecutionException, Interrupted boolean rejoinSameMaster = failsWithConnectTransportException && randomBoolean(); Request request = new Request().masterNodeTimeout(TimeValue.timeValueSeconds(failsWithConnectTransportException ? 60 : 0)); DiscoveryNode masterNode = this.remoteNode; - setState(clusterService, ClusterState.builder(ClusterStateCreationUtils.state(localNode, masterNode, allNodes)) - .version(randomIntBetween(0, 10))); // use a random base version so it can go down when simulating a restart. + setState( + clusterService, + // use a random base version so it can go down when simulating a restart. + ClusterState.builder(ClusterStateCreationUtils.state(localNode, masterNode, allNodes)).version(randomIntBetween(0, 10)) + ); PlainActionFuture listener = new PlainActionFuture<>(); ActionTestUtils.execute(new Action("internal:testAction", transportService, clusterService, threadPool), null, request, listener); diff --git a/server/src/test/java/org/elasticsearch/cluster/coordination/LeaderCheckerTests.java b/server/src/test/java/org/elasticsearch/cluster/coordination/LeaderCheckerTests.java index 496a25e180262..b5681d4d486e3 100644 --- a/server/src/test/java/org/elasticsearch/cluster/coordination/LeaderCheckerTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/coordination/LeaderCheckerTests.java @@ -194,7 +194,8 @@ public String toString() { assertThat(deterministicTaskQueue.getCurrentTimeMillis() - failureTime, lessThanOrEqualTo((leaderCheckIntervalMillis + leaderCheckTimeoutMillis) * leaderCheckRetryCount - + leaderCheckTimeoutMillis // needed because a successful check response might be in flight at the time of failure + // needed because a successful check response might be in flight at the time of failure + + leaderCheckTimeoutMillis )); } leaderChecker.updateLeader(null); diff --git a/server/src/test/java/org/elasticsearch/index/store/CorruptedFileIT.java b/server/src/test/java/org/elasticsearch/index/store/CorruptedFileIT.java index 74d72edf71f31..6911e9891cc50 100644 --- a/server/src/test/java/org/elasticsearch/index/store/CorruptedFileIT.java +++ b/server/src/test/java/org/elasticsearch/index/store/CorruptedFileIT.java @@ -149,7 +149,8 @@ public void testCorruptFileAndRecover() throws ExecutionException, InterruptedEx .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, "1") .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, "1") .put(MergePolicyConfig.INDEX_MERGE_ENABLED, false) - .put(MockFSIndexStore.INDEX_CHECK_INDEX_ON_CLOSE_SETTING.getKey(), false) // no checkindex - we corrupt shards on purpose + // no checkindex - we corrupt shards on purpose + .put(MockFSIndexStore.INDEX_CHECK_INDEX_ON_CLOSE_SETTING.getKey(), false) // no translog based flush - it might change the .liv / segments.N files .put(IndexSettings.INDEX_TRANSLOG_FLUSH_THRESHOLD_SIZE_SETTING.getKey(), new ByteSizeValue(1, ByteSizeUnit.PB)) )); diff --git a/server/src/test/java/org/elasticsearch/plugins/PluginsServiceTests.java b/server/src/test/java/org/elasticsearch/plugins/PluginsServiceTests.java index deafe203ac8dc..89b8cf1a2f193 100644 --- a/server/src/test/java/org/elasticsearch/plugins/PluginsServiceTests.java +++ b/server/src/test/java/org/elasticsearch/plugins/PluginsServiceTests.java @@ -93,10 +93,11 @@ public void testAdditionalSettings() { Settings newSettings = service.updatedSettings(); assertEquals("test", newSettings.get("my.setting")); // previous settings still exist assertEquals("1", newSettings.get("foo.bar")); // added setting exists + // does not override pre existing settings assertEquals( IndexModule.Type.SIMPLEFS.getSettingsKey(), newSettings.get(IndexModule.INDEX_STORE_TYPE_SETTING.getKey()) - ); // does not override pre existing settings + ); } public void testAdditionalSettingsClash() { diff --git a/server/src/test/java/org/elasticsearch/recovery/RelocationIT.java b/server/src/test/java/org/elasticsearch/recovery/RelocationIT.java index a448736ed55ad..e315a1531ebfb 100644 --- a/server/src/test/java/org/elasticsearch/recovery/RelocationIT.java +++ b/server/src/test/java/org/elasticsearch/recovery/RelocationIT.java @@ -280,7 +280,8 @@ public void testRelocationWhileRefreshing() throws Exception { Settings.builder() .put("index.number_of_shards", 1) .put("index.number_of_replicas", numberOfReplicas) - .put("index.refresh_interval", -1) // we want to control refreshes + // we want to control refreshes + .put("index.refresh_interval", -1) ).get(); for (int i = 1; i < numberOfNodes; i++) { diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeIT.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeIT.java index 1f295fc33570e..d390982c6e3e0 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeIT.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/DateRangeIT.java @@ -97,7 +97,8 @@ public void setupSuiteScopeCluster() throws Exception { indexDoc(2, 15, 3), // Feb 15 indexDoc(3, 2, 4), // Mar 2 indexDoc(3, 15, 5), // Mar 15 - indexDoc(3, 23, 6))); // Mar 23 + indexDoc(3, 23, 6) // Mar 23 + )); // dummy docs for (int i = docs.size(); i < numDocs; ++i) { diff --git a/server/src/test/java/org/elasticsearch/search/preference/SearchPreferenceIT.java b/server/src/test/java/org/elasticsearch/search/preference/SearchPreferenceIT.java index 23c29ce9f4674..f5ed9a89e31a7 100644 --- a/server/src/test/java/org/elasticsearch/search/preference/SearchPreferenceIT.java +++ b/server/src/test/java/org/elasticsearch/search/preference/SearchPreferenceIT.java @@ -147,8 +147,8 @@ public void testNodesOnlyRandom() { refresh(); final Client client = internalCluster().smartClient(); - SearchRequestBuilder request = client.prepareSearch("test") - .setQuery(matchAllQuery()).setPreference("_only_nodes:*,nodes*"); // multiple wildchar to cover multi-param usecase + // multiple wildchar to cover multi-param usecase + SearchRequestBuilder request = client.prepareSearch("test").setQuery(matchAllQuery()).setPreference("_only_nodes:*,nodes*"); assertSearchOnRandomNodes(request); request = client.prepareSearch("test") 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/CompletionSuggestSearchIT.java b/server/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestSearchIT.java index 9a1480c8d8807..739ea65fe5acf 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestSearchIT.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestSearchIT.java @@ -1069,11 +1069,12 @@ public void testPrunedSegments() throws IOException { .startArray("input").value("The Beatles").endArray() .endObject().endObject() ).get(); + // we have 2 docs in a segment... client().prepareIndex(INDEX, TYPE, "2").setSource(jsonBuilder() .startObject() .field("somefield", "somevalue") .endObject() - ).get(); // we have 2 docs in a segment... + ).get(); ForceMergeResponse actionGet = client().admin().indices().prepareForceMerge().setFlush(true).setMaxNumSegments(1).get(); assertAllSuccessful(actionGet); refresh(); diff --git a/server/src/test/java/org/elasticsearch/threadpool/ThreadPoolTests.java b/server/src/test/java/org/elasticsearch/threadpool/ThreadPoolTests.java index 649b94099cc74..0be9858269af0 100644 --- a/server/src/test/java/org/elasticsearch/threadpool/ThreadPoolTests.java +++ b/server/src/test/java/org/elasticsearch/threadpool/ThreadPoolTests.java @@ -59,8 +59,8 @@ public void testAbsoluteTime() throws Exception { long currentTime = System.currentTimeMillis(); long gotTime = threadPool.absoluteTimeInMillis(); long delta = Math.abs(gotTime - currentTime); - assertTrue("thread pool cached absolute time " + gotTime + " is too far from real current time " + currentTime, - delta < 10000); // the delta can be large, we just care it is the same order of magnitude + // the delta can be large, we just care it is the same order of magnitude + assertTrue("thread pool cached absolute time " + gotTime + " is too far from real current time " + currentTime, delta < 10000); } finally { threadPool.shutdown(); threadPool.close(); diff --git a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTask.java b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTask.java index 4ad0fb1dfd0d6..3d7b189476a6b 100644 --- a/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTask.java +++ b/x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/ShardFollowNodeTask.java @@ -536,18 +536,18 @@ static boolean shouldRetry(String remoteCluster, Exception e) { } final Throwable actual = ExceptionsHelper.unwrapCause(e); - return actual instanceof ShardNotFoundException || - actual instanceof IllegalIndexShardStateException || - actual instanceof NoShardAvailableActionException || - actual instanceof UnavailableShardsException || - actual instanceof AlreadyClosedException || - actual instanceof ElasticsearchSecurityException || // If user does not have sufficient privileges - actual instanceof ClusterBlockException || // If leader index is closed or no elected master - actual instanceof IndexClosedException || // If follow index is closed - actual instanceof ConnectTransportException || - actual instanceof NodeClosedException || - actual instanceof NoSuchRemoteClusterException || - (actual.getMessage() != null && actual.getMessage().contains("TransportService is closed")); + return actual instanceof ShardNotFoundException + || actual instanceof IllegalIndexShardStateException + || actual instanceof NoShardAvailableActionException + || actual instanceof UnavailableShardsException + || actual instanceof AlreadyClosedException + || actual instanceof ElasticsearchSecurityException // If user does not have sufficient privileges + || actual instanceof ClusterBlockException // If leader index is closed or no elected master + || actual instanceof IndexClosedException // If follow index is closed + || actual instanceof ConnectTransportException + || actual instanceof NodeClosedException + || actual instanceof NoSuchRemoteClusterException + || (actual.getMessage() != null && actual.getMessage().contains("TransportService is closed")); } // These methods are protected for testing purposes: diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java index eebed6de676ec..436670fd0fae0 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java @@ -92,45 +92,58 @@ public void testToXContent() throws IOException { assertThat( xContent.utf8ToString(), equalTo( - "{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(timestamp) + "\"," - + "\"interval_ms\":" + intervalMillis + "," - + "\"type\":\"ccr_auto_follow_stats\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp) + "\"" - + "}," - + "\"ccr_auto_follow_stats\":{" - + "\"number_of_failed_follow_indices\":" + autoFollowStats.getNumberOfFailedFollowIndices() + "," - + "\"number_of_failed_remote_cluster_state_requests\":" + - autoFollowStats.getNumberOfFailedRemoteClusterStateRequests() + "," - + "\"number_of_successful_follow_indices\":" + autoFollowStats.getNumberOfSuccessfulFollowIndices() + "," - + "\"recent_auto_follow_errors\":[" - + "{" - + "\"leader_index\":\"" + recentAutoFollowExceptions.keySet().iterator().next() + "\"," - + "\"timestamp\":1," - + "\"auto_follow_exception\":{" - + "\"type\":\"exception\"," - + "\"reason\":\"cannot follow index\"" - + "}" - + "}" - + "]," - + "\"auto_followed_clusters\":[" - + "{" - + "\"cluster_name\":\"" + trackingClusters.keySet().iterator().next() + "\"," - + "\"time_since_last_check_millis\":" + - trackingClusters.values().iterator().next().getTimeSinceLastCheckMillis() + "," - + "\"last_seen_metadata_version\":" + - trackingClusters.values().iterator().next().getLastSeenMetadataVersion() - + "}" - + "]" - + "}" - + "}")); + XContentHelper.reformatJson( + String.format( + "{" + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": \"%s\"," + + " \"interval_ms\": %d," + + " \"type\": \"ccr_auto_follow_stats\"," + + " \"source_node\": {" + + " \"uuid\": \"_uuid\"," + + " \"host\": \"_host\"," + + " \"transport_address\": \"_addr\"," + + " \"ip\": \"_ip\"," + + " \"name\": \"_name\"," + + " \"timestamp\": \"%s\"" + + " }," + + " \"ccr_auto_follow_stats\": {" + + " \"number_of_failed_follow_indices\": %d," + + " \"number_of_failed_remote_cluster_state_requests\": %d," + + " \"number_of_successful_follow_indices\": %d," + + " \"recent_auto_follow_errors\": [" + + " {" + + " \"leader_index\": \"%s\"," + + " \"timestamp\": 1," + + " \"auto_follow_exception\": {" + + " \"type\": \"exception\"," + + " \"reason\": \"cannot follow index\"" + + " }" + + " }" + + " ]," + + " \"auto_followed_clusters\": [" + + " {" + + " \"cluster_name\": \"%s\"," + + " \"time_since_last_check_millis\": %d," + + " \"last_seen_metadata_version\": %d" + + " }" + + " ]" + + " }" + + "}", + DATE_TIME_FORMATTER.formatMillis(timestamp), + intervalMillis, + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp), + autoFollowStats.getNumberOfFailedFollowIndices(), + autoFollowStats.getNumberOfFailedRemoteClusterStateRequests(), + autoFollowStats.getNumberOfSuccessfulFollowIndices(), + recentAutoFollowExceptions.keySet().iterator().next(), + trackingClusters.keySet().iterator().next(), + trackingClusters.values().iterator().next().getTimeSinceLastCheckMillis(), + trackingClusters.values().iterator().next().getLastSeenMetadataVersion() + ) + ) + ) + ); } public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDocTests.java index 12c251bb9d775..92646de476f40 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDocTests.java @@ -144,62 +144,100 @@ public void testToXContent() throws IOException { final FollowStatsMonitoringDoc document = new FollowStatsMonitoringDoc("_cluster", timestamp, intervalMillis, node, status); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); assertThat( - xContent.utf8ToString(), - equalTo( + xContent.utf8ToString(), + equalTo( + XContentHelper.reformatJson( + String.format( "{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(timestamp) + "\"," - + "\"interval_ms\":" + intervalMillis + "," - + "\"type\":\"ccr_stats\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"" + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp) + "\"" - + "}," - + "\"ccr_stats\":{" - + "\"remote_cluster\":\"leader_cluster\"," - + "\"leader_index\":\"leader_index\"," - + "\"follower_index\":\"follower_index\"," - + "\"shard_id\":" + shardId + "," - + "\"leader_global_checkpoint\":" + leaderGlobalCheckpoint + "," - + "\"leader_max_seq_no\":" + leaderMaxSeqNo + "," - + "\"follower_global_checkpoint\":" + followerGlobalCheckpoint + "," - + "\"follower_max_seq_no\":" + followerMaxSeqNo + "," - + "\"last_requested_seq_no\":" + lastRequestedSeqNo + "," - + "\"outstanding_read_requests\":" + numberOfConcurrentReads + "," - + "\"outstanding_write_requests\":" + numberOfConcurrentWrites + "," - + "\"write_buffer_operation_count\":" + writeBufferOperationCount + "," - + "\"write_buffer_size_in_bytes\":" + writeBufferSizeInBytes + "," - + "\"follower_mapping_version\":" + followerMappingVersion + "," - + "\"follower_settings_version\":" + followerSettingsVersion + "," - + "\"follower_aliases_version\":" + followerAliasesVersion + "," - + "\"total_read_time_millis\":" + totalReadTimeMillis + "," - + "\"total_read_remote_exec_time_millis\":" + totalReadRemoteExecTimeMillis + "," - + "\"successful_read_requests\":" + successfulReadRequests + "," - + "\"failed_read_requests\":" + failedReadRequests + "," - + "\"operations_read\":" + operationsRead + "," - + "\"bytes_read\":" + bytesRead + "," - + "\"total_write_time_millis\":" + totalWriteTimeMillis +"," - + "\"successful_write_requests\":" + successfulWriteRequests + "," - + "\"failed_write_requests\":" + failedWriteRequests + "," - + "\"operations_written\":" + operationWritten + "," - + "\"read_exceptions\":[" - + "{" - + "\"from_seq_no\":" + fetchExceptions.keySet().iterator().next() + "," - + "\"retries\":" + fetchExceptions.values().iterator().next().v1() + "," - + "\"exception\":{" - + "\"type\":\"exception\"," - + "\"reason\":\"shard is sad\"" - + "}" - + "}" - + "]," - + "\"time_since_last_read_millis\":" + timeSinceLastReadMillis + "," - + "\"fatal_exception\":{\"type\":\"exception\",\"reason\":\"fatal error\"}" - + "}" - + "}")); + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": \"%s\"," + + " \"interval_ms\": %d," + + " \"type\": \"ccr_stats\"," + + " \"source_node\": {" + + " \"uuid\": \"_uuid\"," + + " \"host\": \"_host\"," + + " \"transport_address\": \"_addr\"," + + " \"ip\": \"_ip\"," + + " \"name\": \"_name\"," + + " \"timestamp\": \"%s\"" + + " }," + + " \"ccr_stats\": {" + + " \"remote_cluster\": \"leader_cluster\"," + + " \"leader_index\": \"leader_index\"," + + " \"follower_index\": \"follower_index\"," + + " \"shard_id\": %d," + + " \"leader_global_checkpoint\": %d," + + " \"leader_max_seq_no\": %d," + + " \"follower_global_checkpoint\": %d," + + " \"follower_max_seq_no\": %d," + + " \"last_requested_seq_no\": %d," + + " \"outstanding_read_requests\": %d," + + " \"outstanding_write_requests\": %d," + + " \"write_buffer_operation_count\": %d," + + " \"write_buffer_size_in_bytes\": %d," + + " \"follower_mapping_version\": %d," + + " \"follower_settings_version\": %d," + + " \"follower_aliases_version\": %d," + + " \"total_read_time_millis\": %d," + + " \"total_read_remote_exec_time_millis\": %d," + + " \"successful_read_requests\": %d," + + " \"failed_read_requests\": %d," + + " \"operations_read\": %d," + + " \"bytes_read\": %d," + + " \"total_write_time_millis\": %d," + + " \"successful_write_requests\": %d," + + " \"failed_write_requests\": %d," + + " \"operations_written\": %d," + + " \"read_exceptions\": [" + + " {" + + " \"from_seq_no\": %d," + + " \"retries\": %d," + + " \"exception\": {" + + " \"type\": \"exception\"," + + " \"reason\": \"shard is sad\"" + + " }" + + " }" + + " ]," + + " \"time_since_last_read_millis\": %d," + + " \"fatal_exception\": {" + + " \"type\": \"exception\"," + + " \"reason\": \"fatal error\"" + + " }" + + " }" + + "}", + DATE_TIME_FORMATTER.formatMillis(timestamp), + intervalMillis, + DATE_TIME_FORMATTER.formatMillis(nodeTimestamp), + shardId, + leaderGlobalCheckpoint, + leaderMaxSeqNo, + followerGlobalCheckpoint, + followerMaxSeqNo, + lastRequestedSeqNo, + numberOfConcurrentReads, + numberOfConcurrentWrites, + writeBufferOperationCount, + writeBufferSizeInBytes, + followerMappingVersion, + followerSettingsVersion, + followerAliasesVersion, + totalReadTimeMillis, + totalReadRemoteExecTimeMillis, + successfulReadRequests, + failedReadRequests, + operationsRead, + bytesRead, + totalWriteTimeMillis, + successfulWriteRequests, + failedWriteRequests, + operationWritten, + fetchExceptions.keySet().iterator().next(), + fetchExceptions.values().iterator().next().v1(), + timeSinceLastReadMillis + ) + ) + ) + ); } public void testShardFollowNodeTaskStatusFieldsMapped() throws IOException { diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java index ecb6df1e423aa..4857af5a747ef 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/XPackSettings.java @@ -131,30 +131,75 @@ private XPackSettings() { * but instead parse based on a prefix (eg *.ssl.*) */ private static final List JDK11_CIPHERS = List.of( - "TLS_AES_256_GCM_SHA384", "TLS_AES_128_GCM_SHA256", // TLSv1.3 cipher has PFS, AEAD, hardware support - "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", // PFS, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", // PFS, hardware support - "TLS_RSA_WITH_AES_256_GCM_SHA384", "TLS_RSA_WITH_AES_128_GCM_SHA256", // AEAD, hardware support - "TLS_RSA_WITH_AES_256_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", // hardware support - "TLS_RSA_WITH_AES_256_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA"); // hardware support + // TLSv1.3 cipher has PFS, AEAD, hardware support + "TLS_AES_256_GCM_SHA384", + "TLS_AES_128_GCM_SHA256", + // PFS, AEAD, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", + // PFS, AEAD, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", + // PFS, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", + // PFS, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", + // PFS, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", + // PFS, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", + // AEAD, hardware support + "TLS_RSA_WITH_AES_256_GCM_SHA384", + "TLS_RSA_WITH_AES_128_GCM_SHA256", + // hardware support + "TLS_RSA_WITH_AES_256_CBC_SHA256", + "TLS_RSA_WITH_AES_128_CBC_SHA256", + // hardware support + "TLS_RSA_WITH_AES_256_CBC_SHA", + "TLS_RSA_WITH_AES_128_CBC_SHA" + ); private static final List JDK12_CIPHERS = List.of( - "TLS_AES_256_GCM_SHA384", "TLS_AES_128_GCM_SHA256", // TLSv1.3 cipher has PFS, AEAD, hardware support - "TLS_CHACHA20_POLY1305_SHA256", // TLSv1.3 cipher has PFS, AEAD - "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", // PFS, AEAD, hardware support - "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", // PFS, AEAD - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", // PFS, hardware support - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", // PFS, hardware support - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", // PFS, hardware support - "TLS_RSA_WITH_AES_256_GCM_SHA384", "TLS_RSA_WITH_AES_128_GCM_SHA256", // AEAD, hardware support - "TLS_RSA_WITH_AES_256_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", // hardware support - "TLS_RSA_WITH_AES_256_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA"); // hardware support + // TLSv1.3 cipher has PFS, AEAD, hardware support + "TLS_AES_256_GCM_SHA384", + "TLS_AES_128_GCM_SHA256", + // TLSv1.3 cipher has PFS, AEAD + "TLS_CHACHA20_POLY1305_SHA256", + // PFS, AEAD, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", + // PFS, AEAD, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", + // PFS, AEAD + "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", + "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", + // PFS, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", + // PFS, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", + // PFS, hardware support + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", + // PFS, hardware support + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", + // AEAD, hardware support + "TLS_RSA_WITH_AES_256_GCM_SHA384", + "TLS_RSA_WITH_AES_128_GCM_SHA256", + // hardware support + "TLS_RSA_WITH_AES_256_CBC_SHA256", + "TLS_RSA_WITH_AES_128_CBC_SHA256", + // hardware support + "TLS_RSA_WITH_AES_256_CBC_SHA", + "TLS_RSA_WITH_AES_128_CBC_SHA" + ); public static final List DEFAULT_CIPHERS = JavaVersion.current().compareTo(JavaVersion.parse("12")) > -1 ? JDK12_CIPHERS : JDK11_CIPHERS; diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java index 2bddfe6771317..8c92847a44d66 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/ShrinkStep.java @@ -52,8 +52,8 @@ public void performAction(IndexMetaData indexMetaData, ClusterState currentState .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, numberOfShards) .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, indexMetaData.getNumberOfReplicas()) .put(LifecycleSettings.LIFECYCLE_NAME, lifecycle) - .put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", (String) null) // need to remove the single shard - // allocation so replicas can be allocated + // need to remove the single shard allocation so replicas can be allocated + .put(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING.getKey() + "_id", (String) null) .build(); String shrunkenIndexName = shrunkIndexPrefix + indexMetaData.getIndex().getName(); diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java index 3aba5df62c51c..68c3de13eb00e 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ilm/WaitForRolloverReadyStep.java @@ -68,11 +68,13 @@ public void evaluateCondition(IndexMetaData indexMetaData, Listener listener) { Boolean isWriteIndex = null; if (aliasPointsToThisIndex) { - // The writeIndex() call returns a tri-state boolean: - // true -> this index is the write index for this alias - // false -> this index is not the write index for this alias - // null -> this alias is a "classic-style" alias and does not have a write index configured, but only points to one index - // and is thus the write index by default + /* + * The writeIndex() call returns a tri-state boolean: + * true -> this index is the write index for this alias + * false -> this index is not the write index for this alias + * null -> this alias is a "classic-style" alias and does not have a write index configured, but only points to one index + * and is thus the write index by default + */ isWriteIndex = indexMetaData.getAliases().get(rolloverAlias).writeIndex(); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformCheckpoint.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformCheckpoint.java index 06586b60e1c34..699d3e6c1b4a0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformCheckpoint.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/transform/transforms/TransformCheckpoint.java @@ -230,10 +230,11 @@ public boolean matches (TransformCheckpoint that) { } return Objects.equals(this.transformId, that.transformId) - && this.indicesCheckpoints.size() == that.indicesCheckpoints.size() // quick check - // do the expensive deep equal operation last - && this.indicesCheckpoints.entrySet().stream() - .allMatch(e -> Arrays.equals(e.getValue(), that.indicesCheckpoints.get(e.getKey()))); + // quick check + && this.indicesCheckpoints.size() == that.indicesCheckpoints.size() + // do the expensive deep equal operation last + && this.indicesCheckpoints.entrySet().stream() + .allMatch(e -> Arrays.equals(e.getValue(), that.indicesCheckpoints.get(e.getKey()))); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/ExpirationCallbackTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/ExpirationCallbackTests.java index 1aff695546268..3ecf38f4b0543 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/ExpirationCallbackTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/ExpirationCallbackTests.java @@ -23,7 +23,8 @@ public void testPostExpirationDelay() throws Exception { long now = System.currentTimeMillis(); long expiryDate = now + expiryDuration.getMillis(); assertThat(post.delay(expiryDate, now), - equalTo(TimeValue.timeValueMillis(expiryDuration.getMillis() + min.getMillis()))); // before license expiry + // before license expiry + equalTo(TimeValue.timeValueMillis(expiryDuration.getMillis() + min.getMillis()))); assertThat(post.delay(expiryDate, expiryDate), equalTo(min)); // on license expiry int latestValidTriggerDelay = (int) (expiryDuration.getMillis() + max.getMillis()); int earliestValidTriggerDelay = (int) (expiryDuration.getMillis() + min.getMillis()); @@ -151,4 +152,4 @@ private static class NoopPreExpirationCallback extends ExpirationCallback.Pre { @Override public void on(License license) {} } -} \ No newline at end of file +} diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseServiceClusterTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseServiceClusterTests.java index 467a859b6c2d6..95533ae3f4163 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseServiceClusterTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/license/LicenseServiceClusterTests.java @@ -40,9 +40,10 @@ protected boolean addMockHttpTransport() { private Settings.Builder nodeSettingsBuilder(int nodeOrdinal) { return Settings.builder() - .put(super.nodeSettings(nodeOrdinal)) - .put("node.data", true) - .put("resource.reload.interval.high", "500ms"); // for license mode file watcher + .put(super.nodeSettings(nodeOrdinal)) + .put("node.data", true) + // for license mode file watcher + .put("resource.reload.interval.high", "500ms"); } @Override diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigTests.java index e9d338eb4d188..53701d6defead 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/dataframe/DataFrameAnalyticsConfigTests.java @@ -132,21 +132,20 @@ public static String randomValidId() { return generator.ofCodePointsLength(random(), 10, 10); } - private static final String ANACHRONISTIC_QUERY_DATA_FRAME_ANALYTICS = "{\n" + - " \"id\": \"old-data-frame\",\n" + - //query:match:type stopped being supported in 6.x - " \"source\": {\"index\":\"my-index\", \"query\": {\"match\" : {\"query\":\"fieldName\", \"type\": \"phrase\"}}},\n" + - " \"dest\": {\"index\":\"dest-index\"},\n" + - " \"analysis\": {\"outlier_detection\": {\"n_neighbors\": 10}}\n" + - "}"; - - private static final String MODERN_QUERY_DATA_FRAME_ANALYTICS = "{\n" + - " \"id\": \"data-frame\",\n" + - // match_all if parsed, adds default values in the options - " \"source\": {\"index\":\"my-index\", \"query\": {\"match_all\" : {}}},\n" + - " \"dest\": {\"index\":\"dest-index\"},\n" + - " \"analysis\": {\"outlier_detection\": {\"n_neighbors\": 10}}\n" + - "}"; + // query:match:type stopped being supported in 6.x + private static final String ANACHRONISTIC_QUERY_DATA_FRAME_ANALYTICS = "{\n" + + " \"id\": \"old-data-frame\",\n" + + " \"source\": {\"index\":\"my-index\", \"query\": {\"match\" : {\"query\":\"fieldName\", \"type\": \"phrase\"}}},\n" + + " \"dest\": {\"index\":\"dest-index\"},\n" + + " \"analysis\": {\"outlier_detection\": {\"n_neighbors\": 10}}\n" + + "}"; + + private static final String MODERN_QUERY_DATA_FRAME_ANALYTICS = "{\n" + + " \"id\": \"data-frame\",\n" + + " \"source\": {\"index\":\"my-index\", \"query\": {\"match_all\" : {}}},\n" // match_all if parsed, adds default values in + + " \"dest\": {\"index\":\"dest-index\"},\n" // the options + + " \"analysis\": {\"outlier_detection\": {\"n_neighbors\": 10}}\n" + + "}"; private boolean lenient; @@ -324,11 +323,7 @@ public void testPreventCreateTimeInjection() throws IOException { } public void testPreventVersionInjection() throws IOException { - String json = "{" - + " \"version\" : \"7.3.0\"," - + " \"source\" : {\"index\":\"src\"}," - + " \"dest\" : {\"index\": \"dest\"}," - + "}"; + String json = "{ \"version\" : \"7.3.0\", \"source\" : {\"index\":\"src\"}, \"dest\" : {\"index\": \"dest\"}}"; try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser( diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/PivotConfigTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/PivotConfigTests.java index 30c310fe3e412..5593a31ea9b17 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/PivotConfigTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/PivotConfigTests.java @@ -71,12 +71,7 @@ public void testAggsAbbreviations() throws IOException { } public void testMissingAggs() throws IOException { - String pivot = "{" - + " \"group_by\": {" - + " \"id\": {" - + " \"terms\": {" - + " \"field\": \"id\"" - + "} } } }"; + String pivot = "{ \"group_by\": { \"id\": { \"terms\": { \"field\": \"id\"} } } }"; expectThrows(IllegalArgumentException.class, () -> createPivotConfigFromString(pivot, false)); } @@ -115,12 +110,7 @@ public void testEmptyGroupBy() throws IOException { } public void testMissingGroupBy() { - String pivot = "{" - + " \"aggs\": {" - + " \"avg\": {" - + " \"avg\": {" - + " \"field\": \"points\"" - + "} } } }"; + String pivot = "{ \"aggs\": { \"avg\": { \"avg\": { \"field\": \"points\"} } } }"; expectThrows(IllegalArgumentException.class, () -> createPivotConfigFromString(pivot, false)); } diff --git a/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/action/TransportGraphExploreAction.java b/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/action/TransportGraphExploreAction.java index c0c8822ac232f..e43abb448f3f7 100644 --- a/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/action/TransportGraphExploreAction.java +++ b/x-pack/plugin/graph/src/main/java/org/elasticsearch/xpack/graph/action/TransportGraphExploreAction.java @@ -219,10 +219,11 @@ synchronized void expand() { rootBool.must(sourceTermsOrClause); - - //Now build the agg tree that will channel the content -> - // base agg is terms agg for terms from last wave (one per field), - // under each is a sig_terms agg to find next candidates (again, one per field)... + /* + * Now build the agg tree that will channel the content -> + * base agg is terms agg for terms from last wave (one per field), + * under each is a sig_terms agg to find next candidates (again, one per field)... + */ for (int fieldNum = 0; fieldNum < lastHop.getNumberVertexRequests(); fieldNum++) { VertexRequest lastVr = lastHop.getVertexRequest(fieldNum); Set lastWaveVerticesForField = lastHopFindings.get(lastVr.fieldName()); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java index a4e76d60f041c..017136ac82a24 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java @@ -343,240 +343,249 @@ public void testToXContent() throws IOException { needToEnableTLS); final BytesReference xContent = XContentHelper.toXContent(doc, XContentType.JSON, false); - assertEquals("{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"2017-08-07T12:03:22.133Z\"," - + "\"interval_ms\":1506593717631," - + "\"type\":\"cluster_stats\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}," - + "\"cluster_name\":\"_cluster_name\"," - + "\"version\":\"_version\"," - + "\"license\":{" - + "\"status\":\"expired\"," - + "\"uid\":\"442ca961-9c00-4bb2-b5c9-dfaacd547403\"," - + "\"type\":\"trial\"," - + "\"issue_date\":\"2016-01-01T00:00:00.000Z\"," - + "\"issue_date_in_millis\":1451606400000," - + "\"expiry_date\":\"2017-08-07T12:03:22.133Z\"," - + "\"expiry_date_in_millis\":1502107402133," - + "\"max_nodes\":2," - + "\"issued_to\":\"customer\"," - + "\"issuer\":\"elasticsearch\"," - + "\"start_date_in_millis\":-1" - + (needToEnableTLS ? ",\"cluster_needs_tls\":true" : "") - + "}," - + "\"cluster_stats\":{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":1451606400000," - + "\"status\":\"red\"," - + "\"indices\":{" - + "\"count\":1," - + "\"shards\":{" - + "\"total\":1," - + "\"primaries\":1," - + "\"replication\":0.0," - + "\"index\":{" - + "\"shards\":{" - + "\"min\":1," - + "\"max\":1," - + "\"avg\":1.0" - + "}," - + "\"primaries\":{" - + "\"min\":1," - + "\"max\":1," - + "\"avg\":1.0" - + "}," - + "\"replication\":{" - + "\"min\":0.0," - + "\"max\":0.0," - + "\"avg\":0.0" - + "}" - + "}" - + "}," - + "\"docs\":{" - + "\"count\":0," - + "\"deleted\":0" - + "}," - + "\"store\":{" - + "\"size_in_bytes\":0" - + "}," - + "\"fielddata\":{" - + "\"memory_size_in_bytes\":0," - + "\"evictions\":0" - + "}," - + "\"query_cache\":{" - + "\"memory_size_in_bytes\":0," - + "\"total_count\":0," - + "\"hit_count\":0," - + "\"miss_count\":0," - + "\"cache_size\":0," - + "\"cache_count\":0," - + "\"evictions\":0" - + "}," - + "\"completion\":{" - + "\"size_in_bytes\":0" - + "}," - + "\"segments\":{" - + "\"count\":0," - + "\"memory_in_bytes\":0," - + "\"terms_memory_in_bytes\":0," - + "\"stored_fields_memory_in_bytes\":0," - + "\"term_vectors_memory_in_bytes\":0," - + "\"norms_memory_in_bytes\":0," - + "\"points_memory_in_bytes\":0," - + "\"doc_values_memory_in_bytes\":0," - + "\"index_writer_memory_in_bytes\":0," - + "\"version_map_memory_in_bytes\":0," - + "\"fixed_bit_set_memory_in_bytes\":0," - + "\"max_unsafe_auto_id_timestamp\":-9223372036854775808," - + "\"file_sizes\":{}" - + "}" - + "}," - + "\"nodes\":{" - + "\"count\":{" - + "\"total\":1," - + "\"coordinating_only\":0," - + "\"data\":0," - + "\"ingest\":0," - + "\"master\":1" - + "}," - + "\"versions\":[" - + "\"" + mockNodeVersion + "\"" - + "]," - + "\"os\":{" - + "\"available_processors\":32," - + "\"allocated_processors\":16," - + "\"names\":[" - + "{" - + "\"name\":\"_os_name\"," - + "\"count\":1" - + "}" - + "]," - + "\"pretty_names\":[" - + "{" - + "\"pretty_name\":\"_pretty_os_name\"," - + "\"count\":1" - + "}" - + "]," - + "\"mem\":{" - + "\"total_in_bytes\":100," - + "\"free_in_bytes\":79," - + "\"used_in_bytes\":21," - + "\"free_percent\":79," - + "\"used_percent\":21" - + "}" - + "}," - + "\"process\":{" - + "\"cpu\":{" - + "\"percent\":3" - + "}," - + "\"open_file_descriptors\":{" - + "\"min\":42," - + "\"max\":42," - + "\"avg\":42" - + "}" - + "}," - + "\"jvm\":{" - + "\"max_uptime_in_millis\":10800000," - + "\"versions\":[" - + "{" - + "\"version\":\"_jvm_version\"," - + "\"vm_name\":\"_jvm_vm_name\"," - + "\"vm_version\":\"_jvm_vm_version\"," - + "\"vm_vendor\":\"_jvm_vm_vendor\"," - + "\"bundled_jdk\":true," - + "\"using_bundled_jdk\":true," - + "\"count\":1" - + "}" - + "]," - + "\"mem\":{" - + "\"heap_used_in_bytes\":536870912," - + "\"heap_max_in_bytes\":25769803776" - + "}," - + "\"threads\":9" - + "}," - + "\"fs\":{" - + "\"total_in_bytes\":100," - + "\"free_in_bytes\":49," - + "\"available_in_bytes\":51" - + "}," - + "\"plugins\":[" - + "{" - + "\"name\":\"_plugin\"," - + "\"version\":\"_plugin_version\"," - + "\"elasticsearch_version\":\"" + Version.CURRENT + "\"," - + "\"java_version\":\"1.8\"," - + "\"description\":\"_plugin_desc\"," - + "\"classname\":\"_plugin_class\"," - + "\"extended_plugins\":[]," - + "\"has_native_controller\":false" - + "}" - + "]," - + "\"network_types\":{" - + "\"transport_types\":{" - + "\"_transport\":1" - + "}," - + "\"http_types\":{" - + "\"_http\":1" - + "}" - + "}," - + "\"discovery_types\":{" - + "\"_disco\":1" - + "}," - + "\"packaging_types\":[" - + "{" - + "\"flavor\":\"default\"," - + "\"type\":\"docker\"," - + "\"count\":1" - + "}" - + "]" - + "}" - + "}," - + "\"cluster_state\":{" - + "\"nodes_hash\":1314980060," - + "\"status\":\"green\"," - + "\"cluster_uuid\":\"_cluster\"," - + "\"version\":12," - + "\"state_uuid\":\"_state_uuid\"," - + "\"master_node\":\"_node\"," - + "\"nodes\":{" - + "\"_node_id\":{" - + "\"name\":\"_node_name\"," - + "\"ephemeral_id\":\"_ephemeral_id\"," - + "\"transport_address\":\"0.0.0.0:9300\"," - + "\"attributes\":{" - + "\"attr\":\"value\"" - + "}" - + "}" - + "}" - + "}," - + "\"cluster_settings\":{" - + "\"cluster\":{" - + "\"metadata\":{" - + "\"display_name\":\"my_prod_cluster\"" - + "}" - + "}" - + "}," - + "\"stack_stats\":{" - + "\"apm\":{" - + "\"found\":" + apmIndicesExist - + "}," - + "\"xpack\":{" - + "\"monitoring\":{" - + "\"available\":false," - + "\"enabled\":true," - + "\"collection_enabled\":false" - + "}" - + "}" - + "}" - + "}" , xContent.utf8ToString()); + final String expected = XContentHelper.reformatJson( + String.format( + "{" + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": \"2017-08-07T12:03:22.133Z\"," + + " \"interval_ms\": 1506593717631," + + " \"type\": \"cluster_stats\"," + + " \"source_node\": {" + + " \"uuid\": \"_uuid\"," + + " \"host\": \"_host\"," + + " \"transport_address\": \"_addr\"," + + " \"ip\": \"_ip\"," + + " \"name\": \"_name\"," + + " \"timestamp\": \"2017-08-31T08:46:30.855Z\"" + + " }," + + " \"cluster_name\": \"_cluster_name\"," + + " \"version\": \"_version\"," + + " \"license\": {" + + " \"status\": \"expired\"," + + " \"uid\": \"442ca961-9c00-4bb2-b5c9-dfaacd547403\"," + + " \"type\": \"trial\"," + + " \"issue_date\": \"2016-01-01T00:00:00.000Z\"," + + " \"issue_date_in_millis\": 1451606400000," + + " \"expiry_date\": \"2017-08-07T12:03:22.133Z\"," + + " \"expiry_date_in_millis\": 1502107402133," + + " \"max_nodes\": 2," + + " \"issued_to\": \"customer\"," + + " \"issuer\": \"elasticsearch\"," + + " \"start_date_in_millis\": -1" + + " %s" // optional field: `cluster_needs_tls` + + " }," + + " \"cluster_stats\": {" + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": 1451606400000," + + " \"status\": \"red\"," + + " \"indices\": {" + + " \"count\": 1," + + " \"shards\": {" + + " \"total\": 1," + + " \"primaries\": 1," + + " \"replication\": 0," + + " \"index\": {" + + " \"shards\": {" + + " \"min\": 1," + + " \"max\": 1," + + " \"avg\": 1" + + " }," + + " \"primaries\": {" + + " \"min\": 1," + + " \"max\": 1," + + " \"avg\": 1" + + " }," + + " \"replication\": {" + + " \"min\": 0," + + " \"max\": 0," + + " \"avg\": 0" + + " }" + + " }" + + " }," + + " \"docs\": {" + + " \"count\": 0," + + " \"deleted\": 0" + + " }," + + " \"store\": {" + + " \"size_in_bytes\": 0" + + " }," + + " \"fielddata\": {" + + " \"memory_size_in_bytes\": 0," + + " \"evictions\": 0" + + " }," + + " \"query_cache\": {" + + " \"memory_size_in_bytes\": 0," + + " \"total_count\": 0," + + " \"hit_count\": 0," + + " \"miss_count\": 0," + + " \"cache_size\": 0," + + " \"cache_count\": 0," + + " \"evictions\": 0" + + " }," + + " \"completion\": {" + + " \"size_in_bytes\": 0" + + " }," + + " \"segments\": {" + + " \"count\": 0," + + " \"memory_in_bytes\": 0," + + " \"terms_memory_in_bytes\": 0," + + " \"stored_fields_memory_in_bytes\": 0," + + " \"term_vectors_memory_in_bytes\": 0," + + " \"norms_memory_in_bytes\": 0," + + " \"points_memory_in_bytes\": 0," + + " \"doc_values_memory_in_bytes\": 0," + + " \"index_writer_memory_in_bytes\": 0," + + " \"version_map_memory_in_bytes\": 0," + + " \"fixed_bit_set_memory_in_bytes\": 0," + + " \"max_unsafe_auto_id_timestamp\": -9223372036854776000," + + " \"file_sizes\": {}" + + " }" + + " }," + + " \"nodes\": {" + + " \"count\": {" + + " \"total\": 1," + + " \"coordinating_only\": 0," + + " \"data\": 0," + + " \"ingest\": 0," + + " \"master\": 1" + + " }," + + " \"versions\": [" + + " \"%s\"" + + " ]," + + " \"os\": {" + + " \"available_processors\": 32," + + " \"allocated_processors\": 16," + + " \"names\": [" + + " {" + + " \"name\": \"_os_name\"," + + " \"count\": 1" + + " }" + + " ]," + + " \"pretty_names\": [" + + " {" + + " \"pretty_name\": \"_pretty_os_name\"," + + " \"count\": 1" + + " }" + + " ]," + + " \"mem\": {" + + " \"total_in_bytes\": 100," + + " \"free_in_bytes\": 79," + + " \"used_in_bytes\": 21," + + " \"free_percent\": 79," + + " \"used_percent\": 21" + + " }" + + " }," + + " \"process\": {" + + " \"cpu\": {" + + " \"percent\": 3" + + " }," + + " \"open_file_descriptors\": {" + + " \"min\": 42," + + " \"max\": 42," + + " \"avg\": 42" + + " }" + + " }," + + " \"jvm\": {" + + " \"max_uptime_in_millis\": 10800000," + + " \"versions\": [" + + " {" + + " \"version\": \"_jvm_version\"," + + " \"vm_name\": \"_jvm_vm_name\"," + + " \"vm_version\": \"_jvm_vm_version\"," + + " \"vm_vendor\": \"_jvm_vm_vendor\"," + + " \"bundled_jdk\": true," + + " \"using_bundled_jdk\": true," + + " \"count\": 1" + + " }" + + " ]," + + " \"mem\": {" + + " \"heap_used_in_bytes\": 536870912," + + " \"heap_max_in_bytes\": 25769803776" + + " }," + + " \"threads\": 9" + + " }," + + " \"fs\": {" + + " \"total_in_bytes\": 100," + + " \"free_in_bytes\": 49," + + " \"available_in_bytes\": 51" + + " }," + + " \"plugins\": [" + + " {" + + " \"name\": \"_plugin\"," + + " \"version\": \"_plugin_version\"," + + " \"elasticsearch_version\": \"%s\"," + + " \"java_version\": \"1.8\"," + + " \"description\": \"_plugin_desc\"," + + " \"classname\": \"_plugin_class\"," + + " \"extended_plugins\": []," + + " \"has_native_controller\": false" + + " }" + + " ]," + + " \"network_types\": {" + + " \"transport_types\": {" + + " \"_transport\": 1" + + " }," + + " \"http_types\": {" + + " \"_http\": 1" + + " }" + + " }," + + " \"discovery_types\": {" + + " \"_disco\": 1" + + " }," + + " \"packaging_types\": [" + + " {" + + " \"flavor\": \"default\"," + + " \"type\": \"docker\"," + + " \"count\": 1" + + " }" + + " ]" + + " }" + + " }," + + " \"cluster_state\": {" + + " \"nodes_hash\": 1314980060," + + " \"status\": \"green\"," + + " \"cluster_uuid\": \"_cluster\"," + + " \"version\": 12," + + " \"state_uuid\": \"_state_uuid\"," + + " \"master_node\": \"_node\"," + + " \"nodes\": {" + + " \"_node_id\": {" + + " \"name\": \"_node_name\"," + + " \"ephemeral_id\": \"_ephemeral_id\"," + + " \"transport_address\": \"0.0.0.0:9300\"," + + " \"attributes\": {" + + " \"attr\": \"value\"" + + " }" + + " }" + + " }" + + " }," + + " \"cluster_settings\": {" + + " \"cluster\": {" + + " \"metadata\": {" + + " \"display_name\": \"my_prod_cluster\"" + + " }" + + " }" + + " }," + + " \"stack_stats\": {" + + " \"apm\": {" + + " \"found\": %s" + + " }," + + " \"xpack\": {" + + " \"monitoring\": {" + + " \"available\": false," + + " \"enabled\": true," + + " \"collection_enabled\": false" + + " }" + + " }" + + " }" + + "}", + needToEnableTLS ? ",\"cluster_needs_tls\":true" : "", + mockNodeVersion, + Version.CURRENT, + apmIndicesExist + ) + ); + assertEquals(expected, xContent.utf8ToString()); } private DiscoveryNode masterNode() { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDocTests.java index f018828c3a46d..bddb516db7e7a 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDocTests.java @@ -115,75 +115,83 @@ public void testToXContent() throws IOException { new IndexRecoveryMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, node, recoveryResponse); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - assertEquals("{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"2017-08-09T08:18:59.402Z\"," - + "\"interval_ms\":1506593717631," - + "\"type\":\"index_recovery\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}," - + "\"index_recovery\":{" - + "\"shards\":[" - + "{" - + "\"index_name\":\"_shard_0\"," - + "\"id\":0," - + "\"type\":\"PEER\"," - + "\"stage\":\"INIT\"," - + "\"primary\":true," - + "\"start_time_in_millis\":" + timer.startTime() + "," - + "\"stop_time_in_millis\":" + timer.stopTime() + "," - + "\"total_time_in_millis\":" + timer.time() + "," - + "\"source\":{" - + "\"id\":\"_node_id_1\"," - + "\"host\":\"_host_name_1\"," - + "\"transport_address\":\"0.0.0.0:9301\"," - + "\"ip\":\"_host_address_1\"," - + "\"name\":\"_node_1\"" - + "}," - + "\"target\":{" - + "\"id\":\"_node_id_1\"," - + "\"host\":\"_host_name_1\"," - + "\"transport_address\":\"0.0.0.0:9301\"," - + "\"ip\":\"_host_address_1\"," - + "\"name\":\"_node_1\"" - + "}," - + "\"index\":{" - + "\"size\":{" - + "\"total_in_bytes\":0," - + "\"reused_in_bytes\":0," - + "\"recovered_in_bytes\":0," - + "\"percent\":\"0.0%\"" - + "}," - + "\"files\":{" - + "\"total\":0," - + "\"reused\":0," - + "\"recovered\":0," - + "\"percent\":\"0.0%\"" - + "}," - + "\"total_time_in_millis\":0," - + "\"source_throttle_time_in_millis\":0," - + "\"target_throttle_time_in_millis\":0" - + "}," - + "\"translog\":{" - + "\"recovered\":0," - + "\"total\":-1," - + "\"percent\":\"-1.0%\"," - + "\"total_on_start\":-1," - + "\"total_time_in_millis\":0" - + "}," - + "\"verify_index\":{" - + "\"check_index_time_in_millis\":0," - + "\"total_time_in_millis\":0" - + "}" - + "}" - + "]" - + "}" - + "}", xContent.utf8ToString()); + final String expected = XContentHelper.reformatJson( + String.format( + "{" + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": \"2017-08-09T08:18:59.402Z\"," + + " \"interval_ms\": 1506593717631," + + " \"type\": \"index_recovery\"," + + " \"source_node\": {" + + " \"uuid\": \"_uuid\"," + + " \"host\": \"_host\"," + + " \"transport_address\": \"_addr\"," + + " \"ip\": \"_ip\"," + + " \"name\": \"_name\"," + + " \"timestamp\": \"2017-08-31T08:46:30.855Z\"" + + " }," + + " \"index_recovery\": {" + + " \"shards\": [" + + " {" + + " \"index_name\": \"_shard_0\"," + + " \"id\": 0," + + " \"type\": \"PEER\"," + + " \"stage\": \"INIT\"," + + " \"primary\": true," + + " \"start_time_in_millis\": %d," + + " \"stop_time_in_millis\": %d," + + " \"total_time_in_millis\": %d," + + " \"source\": {" + + " \"id\": \"_node_id_1\"," + + " \"host\": \"_host_name_1\"," + + " \"transport_address\": \"0.0.0.0:9301\"," + + " \"ip\": \"_host_address_1\"," + + " \"name\": \"_node_1\"" + + " }," + + " \"target\": {" + + " \"id\": \"_node_id_1\"," + + " \"host\": \"_host_name_1\"," + + " \"transport_address\": \"0.0.0.0:9301\"," + + " \"ip\": \"_host_address_1\"," + + " \"name\": \"_node_1\"" + + " }," + + " \"index\": {" + + " \"size\": {" + + " \"total_in_bytes\": 0," + + " \"reused_in_bytes\": 0," + + " \"recovered_in_bytes\": 0," + + " \"percent\": \"0.0%%\"" + + " }," + + " \"files\": {" + + " \"total\": 0," + + " \"reused\": 0," + + " \"recovered\": 0," + + " \"percent\": \"0.0%%\"" + + " }," + + " \"total_time_in_millis\": 0," + + " \"source_throttle_time_in_millis\": 0," + + " \"target_throttle_time_in_millis\": 0" + + " }," + + " \"translog\": {" + + " \"recovered\": 0," + + " \"total\": -1," + + " \"percent\": \"-1.0%%\"," + + " \"total_on_start\": -1," + + " \"total_time_in_millis\": 0" + + " }," + + " \"verify_index\": {" + + " \"check_index_time_in_millis\": 0," + + " \"total_time_in_millis\": 0" + + " }" + + " }" + + " ]" + + " }" + + "}", + timer.startTime(), + timer.stopTime(), + timer.time() + ) + ); + assertEquals(expected, xContent.utf8ToString()); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDocTests.java index 7f6a22e954c35..82a29ee17d0d3 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDocTests.java @@ -126,130 +126,130 @@ public void testToXContent() throws IOException { new IndexStatsMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, node, indexStats, metaData, routingTable); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - assertThat(xContent.utf8ToString(), equalTo( - "{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"2017-08-09T08:18:59.402Z\"," - + "\"interval_ms\":1506593717631," - + "\"type\":\"index_stats\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}," - + "\"index_stats\":{" - + indexStatsSummary() + "," - + "\"total\":{" - + "\"docs\":{" - + "\"count\":1" - + "}," - + "\"store\":{" - + "\"size_in_bytes\":13" - + "}," - + "\"indexing\":{" - + "\"index_total\":16," - + "\"index_time_in_millis\":17," - + "\"throttle_time_in_millis\":18" - + "}," - + "\"search\":{" - + "\"query_total\":19," - + "\"query_time_in_millis\":20" - + "}," - + "\"merges\":{" - + "\"total_size_in_bytes\":4" - + "}," - + "\"refresh\":{" - + "\"total_time_in_millis\":14," - + "\"external_total_time_in_millis\":15" - + "}," - + "\"query_cache\":{" - + "\"memory_size_in_bytes\":5," - + "\"hit_count\":6," - + "\"miss_count\":7," - + "\"evictions\":9" - + "}," - + "\"fielddata\":{" - + "\"memory_size_in_bytes\":2," - + "\"evictions\":3" - + "}," - + "\"segments\":{" - + "\"count\":21," - + "\"memory_in_bytes\":22," - + "\"terms_memory_in_bytes\":23," - + "\"stored_fields_memory_in_bytes\":24," - + "\"term_vectors_memory_in_bytes\":25," - + "\"norms_memory_in_bytes\":26," - + "\"points_memory_in_bytes\":27," - + "\"doc_values_memory_in_bytes\":28," - + "\"index_writer_memory_in_bytes\":29," - + "\"version_map_memory_in_bytes\":30," - + "\"fixed_bit_set_memory_in_bytes\":31" - + "}," - + "\"request_cache\":{" - + "\"memory_size_in_bytes\":9," - + "\"evictions\":10," - + "\"hit_count\":11," - + "\"miss_count\":12" - + "}" - + "}," - + "\"primaries\":{" - + "\"docs\":{" - + "\"count\":1" - + "}," - + "\"store\":{" - + "\"size_in_bytes\":13" - + "}," - + "\"indexing\":{" - + "\"index_total\":16," - + "\"index_time_in_millis\":17," - + "\"throttle_time_in_millis\":18" - + "}," - + "\"search\":{" - + "\"query_total\":19," - + "\"query_time_in_millis\":20" - + "}," - + "\"merges\":{" - + "\"total_size_in_bytes\":4" - + "}," - + "\"refresh\":{" - + "\"total_time_in_millis\":14," - + "\"external_total_time_in_millis\":15" - + "}," - + "\"query_cache\":{" - + "\"memory_size_in_bytes\":5," - + "\"hit_count\":6," - + "\"miss_count\":7," - + "\"evictions\":9" - + "}," - + "\"fielddata\":{" - + "\"memory_size_in_bytes\":2," - + "\"evictions\":3" - + "}," - + "\"segments\":{" - + "\"count\":21," - + "\"memory_in_bytes\":22," - + "\"terms_memory_in_bytes\":23," - + "\"stored_fields_memory_in_bytes\":24," - + "\"term_vectors_memory_in_bytes\":25," - + "\"norms_memory_in_bytes\":26," - + "\"points_memory_in_bytes\":27," - + "\"doc_values_memory_in_bytes\":28," - + "\"index_writer_memory_in_bytes\":29," - + "\"version_map_memory_in_bytes\":30," - + "\"fixed_bit_set_memory_in_bytes\":31" - + "}," - + "\"request_cache\":{" - + "\"memory_size_in_bytes\":9," - + "\"evictions\":10," - + "\"hit_count\":11," - + "\"miss_count\":12" - + "}" - + "}" - + "}" - + "}")); + final String expected = XContentHelper.reformatJson(String.format("{" + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": \"2017-08-09T08:18:59.402Z\"," + + " \"interval_ms\": 1506593717631," + + " \"type\": \"index_stats\"," + + " \"source_node\": {" + + " \"uuid\": \"_uuid\"," + + " \"host\": \"_host\"," + + " \"transport_address\": \"_addr\"," + + " \"ip\": \"_ip\"," + + " \"name\": \"_name\"," + + " \"timestamp\": \"2017-08-31T08:46:30.855Z\"" + + " }," + + " \"index_stats\": {" + + " %s," // indexStatsSummary() + + " \"total\": {" + + " \"docs\": {" + + " \"count\": 1" + + " }," + + " \"store\": {" + + " \"size_in_bytes\": 13" + + " }," + + " \"indexing\": {" + + " \"index_total\": 16," + + " \"index_time_in_millis\": 17," + + " \"throttle_time_in_millis\": 18" + + " }," + + " \"search\": {" + + " \"query_total\": 19," + + " \"query_time_in_millis\": 20" + + " }," + + " \"merges\": {" + + " \"total_size_in_bytes\": 4" + + " }," + + " \"refresh\": {" + + " \"total_time_in_millis\": 14," + + " \"external_total_time_in_millis\": 15" + + " }," + + " \"query_cache\": {" + + " \"memory_size_in_bytes\": 5," + + " \"hit_count\": 6," + + " \"miss_count\": 7," + + " \"evictions\": 9" + + " }," + + " \"fielddata\": {" + + " \"memory_size_in_bytes\": 2," + + " \"evictions\": 3" + + " }," + + " \"segments\": {" + + " \"count\": 21," + + " \"memory_in_bytes\": 22," + + " \"terms_memory_in_bytes\": 23," + + " \"stored_fields_memory_in_bytes\": 24," + + " \"term_vectors_memory_in_bytes\": 25," + + " \"norms_memory_in_bytes\": 26," + + " \"points_memory_in_bytes\": 27," + + " \"doc_values_memory_in_bytes\": 28," + + " \"index_writer_memory_in_bytes\": 29," + + " \"version_map_memory_in_bytes\": 30," + + " \"fixed_bit_set_memory_in_bytes\": 31" + + " }," + + " \"request_cache\": {" + + " \"memory_size_in_bytes\": 9," + + " \"evictions\": 10," + + " \"hit_count\": 11," + + " \"miss_count\": 12" + + " }" + + " }," + + " \"primaries\": {" + + " \"docs\": {" + + " \"count\": 1" + + " }," + + " \"store\": {" + + " \"size_in_bytes\": 13" + + " }," + + " \"indexing\": {" + + " \"index_total\": 16," + + " \"index_time_in_millis\": 17," + + " \"throttle_time_in_millis\": 18" + + " }," + + " \"search\": {" + + " \"query_total\": 19," + + " \"query_time_in_millis\": 20" + + " }," + + " \"merges\": {" + + " \"total_size_in_bytes\": 4" + + " }," + + " \"refresh\": {" + + " \"total_time_in_millis\": 14," + + " \"external_total_time_in_millis\": 15" + + " }," + + " \"query_cache\": {" + + " \"memory_size_in_bytes\": 5," + + " \"hit_count\": 6," + + " \"miss_count\": 7," + + " \"evictions\": 9" + + " }," + + " \"fielddata\": {" + + " \"memory_size_in_bytes\": 2," + + " \"evictions\": 3" + + " }," + + " \"segments\": {" + + " \"count\": 21," + + " \"memory_in_bytes\": 22," + + " \"terms_memory_in_bytes\": 23," + + " \"stored_fields_memory_in_bytes\": 24," + + " \"term_vectors_memory_in_bytes\": 25," + + " \"norms_memory_in_bytes\": 26," + + " \"points_memory_in_bytes\": 27," + + " \"doc_values_memory_in_bytes\": 28," + + " \"index_writer_memory_in_bytes\": 29," + + " \"version_map_memory_in_bytes\": 30," + + " \"fixed_bit_set_memory_in_bytes\": 31" + + " }," + + " \"request_cache\": {" + + " \"memory_size_in_bytes\": 9," + + " \"evictions\": 10," + + " \"hit_count\": 11," + + " \"miss_count\": 12" + + " }" + + " }" + + " }" + + "}", indexStatsSummary())); + assertThat(xContent.utf8ToString(), equalTo(expected)); } public void testToXContentWithNullStats() throws IOException { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java index e7408557ec72d..7e85f32eb474f 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndicesStatsMonitoringDocTests.java @@ -85,60 +85,63 @@ public void testToXContent() throws IOException { new IndicesStatsMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, node, indicesStats); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - assertEquals("{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"2017-08-09T08:18:59.402Z\"," - + "\"interval_ms\":1506593717631," - + "\"type\":\"indices_stats\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}," - + "\"indices_stats\":{" - + "\"_all\":{" - + "\"primaries\":{" - + "\"docs\":{" - + "\"count\":2" - + "}," - + "\"store\":{" - + "\"size_in_bytes\":4" - + "}," - + "\"indexing\":{" - + "\"index_total\":6," - + "\"index_time_in_millis\":8," - + "\"is_throttled\":true," - + "\"throttle_time_in_millis\":10" - + "}," - + "\"search\":{" - + "\"query_total\":12," - + "\"query_time_in_millis\":14" - + "}" - + "}," - + "\"total\":{" - + "\"docs\":{" - + "\"count\":3" - + "}," - + "\"store\":{" - + "\"size_in_bytes\":6" - + "}," - + "\"indexing\":{" - + "\"index_total\":9," - + "\"index_time_in_millis\":12," - + "\"is_throttled\":true," - + "\"throttle_time_in_millis\":15" - + "}," - + "\"search\":{" - + "\"query_total\":18," - + "\"query_time_in_millis\":21" - + "}" - + "}" - + "}" + final String expected = XContentHelper.reformatJson( + "{" + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": \"2017-08-09T08:18:59.402Z\"," + + " \"interval_ms\": 1506593717631," + + " \"type\": \"indices_stats\"," + + " \"source_node\": {" + + " \"uuid\": \"_uuid\"," + + " \"host\": \"_host\"," + + " \"transport_address\": \"_addr\"," + + " \"ip\": \"_ip\"," + + " \"name\": \"_name\"," + + " \"timestamp\": \"2017-08-31T08:46:30.855Z\"" + + " }," + + " \"indices_stats\": {" + + " \"_all\": {" + + " \"primaries\": {" + + " \"docs\": {" + + " \"count\": 2" + + " }," + + " \"store\": {" + + " \"size_in_bytes\": 4" + + " }," + + " \"indexing\": {" + + " \"index_total\": 6," + + " \"index_time_in_millis\": 8," + + " \"is_throttled\": true," + + " \"throttle_time_in_millis\": 10" + + " }," + + " \"search\": {" + + " \"query_total\": 12," + + " \"query_time_in_millis\": 14" + + " }" + + " }," + + " \"total\": {" + + " \"docs\": {" + + " \"count\": 3" + + " }," + + " \"store\": {" + + " \"size_in_bytes\": 6" + + " }," + + " \"indexing\": {" + + " \"index_total\": 9," + + " \"index_time_in_millis\": 12," + + " \"is_throttled\": true," + + " \"throttle_time_in_millis\": 15" + + " }," + + " \"search\": {" + + " \"query_total\": 18," + + " \"query_time_in_millis\": 21" + + " }" + + " }" + + " }" + + " }" + "}" - + "}", xContent.utf8ToString()); + ); + assertEquals(expected, xContent.utf8ToString()); } private CommonStats mockCommonStats() { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java index 23de6258b3fe0..e901de1937dc1 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java @@ -113,78 +113,82 @@ public void testToXContent() throws IOException { final JobStatsMonitoringDoc document = new JobStatsMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, node, jobStats); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - assertEquals("{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"2017-08-09T08:18:59.402Z\"," - + "\"interval_ms\":1506593717631," - + "\"type\":\"job_stats\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}," - + "\"job_stats\":{" - + "\"job_id\":\"_job\"," - + "\"data_counts\":{" - + "\"job_id\":\"_job_id\"," - + "\"processed_record_count\":0," - + "\"processed_field_count\":1," - + "\"input_bytes\":2," - + "\"input_field_count\":3," - + "\"invalid_date_count\":4," - + "\"missing_field_count\":5," - + "\"out_of_order_timestamp_count\":6," - + "\"empty_bucket_count\":7," - + "\"sparse_bucket_count\":8," - + "\"bucket_count\":9," - + "\"earliest_record_timestamp\":1483401783003," - + "\"latest_record_timestamp\":1483488244004," - + "\"last_data_time\":1483574705005," - + "\"latest_empty_bucket_timestamp\":1483661166006," - + "\"latest_sparse_bucket_timestamp\":1483747627007," - + "\"input_record_count\":10" - + "}," - + "\"model_size_stats\":{" - + "\"job_id\":\"_model\"," - + "\"result_type\":\"model_size_stats\"," - + "\"model_bytes\":100," - + "\"total_by_field_count\":101," - + "\"total_over_field_count\":102," - + "\"total_partition_field_count\":103," - + "\"bucket_allocation_failures_count\":104," - + "\"memory_status\":\"ok\"," - + "\"log_time\":1483315322002," - + "\"timestamp\":1483228861001" - + "}," - + "\"forecasts_stats\":{" - + "\"total\":0,\"forecasted_jobs\":0" - + "}," - + "\"state\":\"opened\"," - + "\"node\":{" - + "\"id\":\"_node_id\"," - + "\"name\":\"_node_name\"," - + "\"ephemeral_id\":\"_ephemeral_id\"," - + "\"transport_address\":\"0.0.0.0:9300\"," - + "\"attributes\":{" - + "\"attr\":\"value\"" - + "}" - + "}," - + "\"assignment_explanation\":\"_explanation\"," - + "\"open_time\":\"13h\"," - + "\"timing_stats\":{" - + "\"job_id\":\"_job_id\"," - + "\"bucket_count\":100," - + "\"total_bucket_processing_time_ms\":2000.0," - + "\"minimum_bucket_processing_time_ms\":10.0," - + "\"maximum_bucket_processing_time_ms\":30.0," - + "\"average_bucket_processing_time_ms\":20.0," - + "\"exponential_average_bucket_processing_time_ms\":25.0," - + "\"exponential_average_bucket_processing_time_per_hour_ms\":50.0" - + "}" - + "}" - + "}", xContent.utf8ToString()); + final String expected = XContentHelper.reformatJson( + "{" + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": \"2017-08-09T08:18:59.402Z\"," + + " \"interval_ms\": 1506593717631," + + " \"type\": \"job_stats\"," + + " \"source_node\": {" + + " \"uuid\": \"_uuid\"," + + " \"host\": \"_host\"," + + " \"transport_address\": \"_addr\"," + + " \"ip\": \"_ip\"," + + " \"name\": \"_name\"," + + " \"timestamp\": \"2017-08-31T08:46:30.855Z\"" + + " }," + + " \"job_stats\": {" + + " \"job_id\": \"_job\"," + + " \"data_counts\": {" + + " \"job_id\": \"_job_id\"," + + " \"processed_record_count\": 0," + + " \"processed_field_count\": 1," + + " \"input_bytes\": 2," + + " \"input_field_count\": 3," + + " \"invalid_date_count\": 4," + + " \"missing_field_count\": 5," + + " \"out_of_order_timestamp_count\": 6," + + " \"empty_bucket_count\": 7," + + " \"sparse_bucket_count\": 8," + + " \"bucket_count\": 9," + + " \"earliest_record_timestamp\": 1483401783003," + + " \"latest_record_timestamp\": 1483488244004," + + " \"last_data_time\": 1483574705005," + + " \"latest_empty_bucket_timestamp\": 1483661166006," + + " \"latest_sparse_bucket_timestamp\": 1483747627007," + + " \"input_record_count\": 10" + + " }," + + " \"model_size_stats\": {" + + " \"job_id\": \"_model\"," + + " \"result_type\": \"model_size_stats\"," + + " \"model_bytes\": 100," + + " \"total_by_field_count\": 101," + + " \"total_over_field_count\": 102," + + " \"total_partition_field_count\": 103," + + " \"bucket_allocation_failures_count\": 104," + + " \"memory_status\": \"ok\"," + + " \"log_time\": 1483315322002," + + " \"timestamp\": 1483228861001" + + " }," + + " \"forecasts_stats\": {" + + " \"total\": 0," + + " \"forecasted_jobs\": 0" + + " }," + + " \"state\": \"opened\"," + + " \"node\": {" + + " \"id\": \"_node_id\"," + + " \"name\": \"_node_name\"," + + " \"ephemeral_id\": \"_ephemeral_id\"," + + " \"transport_address\": \"0.0.0.0:9300\"," + + " \"attributes\": {" + + " \"attr\": \"value\"" + + " }" + + " }," + + " \"assignment_explanation\": \"_explanation\"," + + " \"open_time\": \"13h\"," + + " \"timing_stats\": {" + + " \"job_id\": \"_job_id\"," + + " \"bucket_count\": 100," + + " \"total_bucket_processing_time_ms\": 2000," + + " \"minimum_bucket_processing_time_ms\": 10," + + " \"maximum_bucket_processing_time_ms\": 30," + + " \"average_bucket_processing_time_ms\": 20," + + " \"exponential_average_bucket_processing_time_ms\": 25," + + " \"exponential_average_bucket_processing_time_per_hour_ms\": 50" + + " }" + + " }" + + "}" + ); + assertEquals(expected, xContent.utf8ToString()); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java index 425f1046ac82b..c2dfd3753ee21 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java @@ -105,175 +105,178 @@ public void testToXContent() throws IOException { new NodeStatsMonitoringDoc("_cluster", 1502107402133L, 1506593717631L, node, "_node_id", true, nodeStats, false); final BytesReference xContent = XContentHelper.toXContent(doc, XContentType.JSON, false); - assertEquals("{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"2017-08-07T12:03:22.133Z\"," - + "\"interval_ms\":1506593717631," - + "\"type\":\"node_stats\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}," - + "\"node_stats\":{" - + "\"node_id\":\"_node_id\"," - + "\"node_master\":true," - + "\"mlockall\":false," - + "\"indices\":{" - + "\"docs\":{" - + "\"count\":1" - + "}," - + "\"store\":{" - + "\"size_in_bytes\":4" - + "}," - + "\"indexing\":{" - + "\"index_total\":5," - + "\"index_time_in_millis\":6," - + "\"throttle_time_in_millis\":8" - + "}," - + "\"search\":{" - + "\"query_total\":17," - + "\"query_time_in_millis\":18" - + "}," - + "\"query_cache\":{" - + "\"memory_size_in_bytes\":9," - + "\"hit_count\":10," - + "\"miss_count\":11," - + "\"evictions\":13" - + "}," - + "\"fielddata\":{" - + "\"memory_size_in_bytes\":2," - + "\"evictions\":3" - + "}," - + "\"segments\":{" - + "\"count\":19," - + "\"memory_in_bytes\":20," - + "\"terms_memory_in_bytes\":21," - + "\"stored_fields_memory_in_bytes\":22," - + "\"term_vectors_memory_in_bytes\":23," - + "\"norms_memory_in_bytes\":24," - + "\"points_memory_in_bytes\":25," - + "\"doc_values_memory_in_bytes\":26," - + "\"index_writer_memory_in_bytes\":27," - + "\"version_map_memory_in_bytes\":28," - + "\"fixed_bit_set_memory_in_bytes\":29" - + "}," - + "\"request_cache\":{" - + "\"memory_size_in_bytes\":13," - + "\"evictions\":14," - + "\"hit_count\":15," - + "\"miss_count\":16" - + "}" - + "}," - + "\"os\":{" - + "\"cpu\":{" - + "\"load_average\":{" - + "\"1m\":41.0," - + "\"5m\":42.0," - + "\"15m\":43.0" - + "}" - + "}," - + "\"cgroup\":{" - + "\"cpuacct\":{" - + "\"control_group\":\"_cpu_acct_ctrl_group\"," - + "\"usage_nanos\":47" - + "}," - + "\"cpu\":{" - + "\"control_group\":\"_cpu_ctrl_group\"," - + "\"cfs_period_micros\":48," - + "\"cfs_quota_micros\":49," - + "\"stat\":{" - + "\"number_of_elapsed_periods\":44," - + "\"number_of_times_throttled\":45," - + "\"time_throttled_nanos\":46" - + "}" - + "}," - + "\"memory\":{" - + "\"control_group\":\"_memory_ctrl_group\"," - + "\"limit_in_bytes\":\"2000000000\"," - + "\"usage_in_bytes\":\"1000000000\"" - + "}" - + "}" - + "}," - + "\"process\":{" - + "\"open_file_descriptors\":51," - + "\"max_file_descriptors\":52," - + "\"cpu\":{" - + "\"percent\":50" - + "}" - + "}," - + "\"jvm\":{" - + "\"mem\":{" - + "\"heap_used_in_bytes\":53," - + "\"heap_used_percent\":98," - + "\"heap_max_in_bytes\":54" - + "}," - + "\"gc\":{" - + "\"collectors\":{" - + "\"young\":{" - + "\"collection_count\":55," - + "\"collection_time_in_millis\":56" - + "}," - + "\"old\":{" - + "\"collection_count\":57," - + "\"collection_time_in_millis\":58" - + "}" - + "}" - + "}" - + "}," - + "\"thread_pool\":{" - + "\"generic\":{" - + "\"threads\":59," - + "\"queue\":60," - + "\"rejected\":61" - + "}," - + "\"get\":{" - + "\"threads\":62," - + "\"queue\":63," - + "\"rejected\":64" - + "}," - + "\"management\":{" - + "\"threads\":65," - + "\"queue\":66," - + "\"rejected\":67" - + "}," - + "\"search\":{" - + "\"threads\":68," - + "\"queue\":69," - + "\"rejected\":70" - + "}," - + "\"watcher\":{" - + "\"threads\":71," - + "\"queue\":72," - + "\"rejected\":73" - + "}," - + "\"write\":{" - + "\"threads\":74," - + "\"queue\":75," - + "\"rejected\":76" - + "}" - + "}," - + "\"fs\":{" - + "\"total\":{" - + "\"total_in_bytes\":38," - + "\"free_in_bytes\":39," - + "\"available_in_bytes\":40" - + "}," - + "\"io_stats\":{" - + "\"total\":{" - + "\"operations\":8," - + "\"read_operations\":4," - + "\"write_operations\":4," - + "\"read_kilobytes\":2," - + "\"write_kilobytes\":2" - + "}" - + "}" - + "}" - + "}" - + "}", xContent.utf8ToString()); + final String expected = XContentHelper.reformatJson( + "{" + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": \"2017-08-07T12:03:22.133Z\"," + + " \"interval_ms\": 1506593717631," + + " \"type\": \"node_stats\"," + + " \"source_node\": {" + + " \"uuid\": \"_uuid\"," + + " \"host\": \"_host\"," + + " \"transport_address\": \"_addr\"," + + " \"ip\": \"_ip\"," + + " \"name\": \"_name\"," + + " \"timestamp\": \"2017-08-31T08:46:30.855Z\"" + + " }," + + " \"node_stats\": {" + + " \"node_id\": \"_node_id\"," + + " \"node_master\": true," + + " \"mlockall\": false," + + " \"indices\": {" + + " \"docs\": {" + + " \"count\": 1" + + " }," + + " \"store\": {" + + " \"size_in_bytes\": 4" + + " }," + + " \"indexing\": {" + + " \"index_total\": 5," + + " \"index_time_in_millis\": 6," + + " \"throttle_time_in_millis\": 8" + + " }," + + " \"search\": {" + + " \"query_total\": 17," + + " \"query_time_in_millis\": 18" + + " }," + + " \"query_cache\": {" + + " \"memory_size_in_bytes\": 9," + + " \"hit_count\": 10," + + " \"miss_count\": 11," + + " \"evictions\": 13" + + " }," + + " \"fielddata\": {" + + " \"memory_size_in_bytes\": 2," + + " \"evictions\": 3" + + " }," + + " \"segments\": {" + + " \"count\": 19," + + " \"memory_in_bytes\": 20," + + " \"terms_memory_in_bytes\": 21," + + " \"stored_fields_memory_in_bytes\": 22," + + " \"term_vectors_memory_in_bytes\": 23," + + " \"norms_memory_in_bytes\": 24," + + " \"points_memory_in_bytes\": 25," + + " \"doc_values_memory_in_bytes\": 26," + + " \"index_writer_memory_in_bytes\": 27," + + " \"version_map_memory_in_bytes\": 28," + + " \"fixed_bit_set_memory_in_bytes\": 29" + + " }," + + " \"request_cache\": {" + + " \"memory_size_in_bytes\": 13," + + " \"evictions\": 14," + + " \"hit_count\": 15," + + " \"miss_count\": 16" + + " }" + + " }," + + " \"os\": {" + + " \"cpu\": {" + + " \"load_average\": {" + + " \"1m\": 41," + + " \"5m\": 42," + + " \"15m\": 43" + + " }" + + " }," + + " \"cgroup\": {" + + " \"cpuacct\": {" + + " \"control_group\": \"_cpu_acct_ctrl_group\"," + + " \"usage_nanos\": 47" + + " }," + + " \"cpu\": {" + + " \"control_group\": \"_cpu_ctrl_group\"," + + " \"cfs_period_micros\": 48," + + " \"cfs_quota_micros\": 49," + + " \"stat\": {" + + " \"number_of_elapsed_periods\": 44," + + " \"number_of_times_throttled\": 45," + + " \"time_throttled_nanos\": 46" + + " }" + + " }," + + " \"memory\": {" + + " \"control_group\": \"_memory_ctrl_group\"," + + " \"limit_in_bytes\": \"2000000000\"," + + " \"usage_in_bytes\": \"1000000000\"" + + " }" + + " }" + + " }," + + " \"process\": {" + + " \"open_file_descriptors\": 51," + + " \"max_file_descriptors\": 52," + + " \"cpu\": {" + + " \"percent\": 50" + + " }" + + " }," + + " \"jvm\": {" + + " \"mem\": {" + + " \"heap_used_in_bytes\": 53," + + " \"heap_used_percent\": 98," + + " \"heap_max_in_bytes\": 54" + + " }," + + " \"gc\": {" + + " \"collectors\": {" + + " \"young\": {" + + " \"collection_count\": 55," + + " \"collection_time_in_millis\": 56" + + " }," + + " \"old\": {" + + " \"collection_count\": 57," + + " \"collection_time_in_millis\": 58" + + " }" + + " }" + + " }" + + " }," + + " \"thread_pool\": {" + + " \"generic\": {" + + " \"threads\": 59," + + " \"queue\": 60," + + " \"rejected\": 61" + + " }," + + " \"get\": {" + + " \"threads\": 62," + + " \"queue\": 63," + + " \"rejected\": 64" + + " }," + + " \"management\": {" + + " \"threads\": 65," + + " \"queue\": 66," + + " \"rejected\": 67" + + " }," + + " \"search\": {" + + " \"threads\": 68," + + " \"queue\": 69," + + " \"rejected\": 70" + + " }," + + " \"watcher\": {" + + " \"threads\": 71," + + " \"queue\": 72," + + " \"rejected\": 73" + + " }," + + " \"write\": {" + + " \"threads\": 74," + + " \"queue\": 75," + + " \"rejected\": 76" + + " }" + + " }," + + " \"fs\": {" + + " \"total\": {" + + " \"total_in_bytes\": 38," + + " \"free_in_bytes\": 39," + + " \"available_in_bytes\": 40" + + " }," + + " \"io_stats\": {" + + " \"total\": {" + + " \"operations\": 8," + + " \"read_operations\": 4," + + " \"write_operations\": 4," + + " \"read_kilobytes\": 2," + + " \"write_kilobytes\": 2" + + " }" + + " }" + + " }" + + " }" + + "}" + ); + assertEquals(expected, xContent.utf8ToString()); } private static NodeStats mockNodeStats() { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsMonitoringDocTests.java index ccec3421cd926..6a567d8052d59 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/shards/ShardsMonitoringDocTests.java @@ -107,28 +107,29 @@ public void testToXContent() throws IOException { new ShardMonitoringDoc("_cluster", 1502107402133L, 1506593717631L, node, shardRouting, "_state_uuid"); final BytesReference xContent = XContentHelper.toXContent(doc, XContentType.JSON, randomBoolean()); - assertEquals("{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"2017-08-07T12:03:22.133Z\"," - + "\"interval_ms\":1506593717631," - + "\"type\":\"shards\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}," - + "\"state_uuid\":\"_state_uuid\"," - + "\"shard\":{" - + "\"state\":\"INITIALIZING\"," - + "\"primary\":true," - + "\"node\":\"_index_uuid\"," - + "\"relocating_node\":\"_node_uuid\"," - + "\"shard\":1," - + "\"index\":\"_index\"" - + "}" - + "}" , xContent.utf8ToString()); + final String expected = "{" + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": \"2017-08-07T12:03:22.133Z\"," + + " \"interval_ms\": 1506593717631," + + " \"type\": \"shards\"," + + " \"source_node\": {" + + " \"uuid\": \"_uuid\"," + + " \"host\": \"_host\"," + + " \"transport_address\": \"_addr\"," + + " \"ip\": \"_ip\"," + + " \"name\": \"_name\"," + + " \"timestamp\": \"2017-08-31T08:46:30.855Z\"" + + " }," + + " \"state_uuid\": \"_state_uuid\"," + + " \"shard\": {" + + " \"state\": \"INITIALIZING\"," + + " \"primary\": true," + + " \"node\": \"_index_uuid\"," + + " \"relocating_node\": \"_node_uuid\"," + + " \"shard\": 1," + + " \"index\": \"_index\"" + + " }" + + "}"; + assertEquals(XContentHelper.reformatJson(expected), xContent.utf8ToString()); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseFilteredMonitoringDocTestCase.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseFilteredMonitoringDocTestCase.java index d81467677091a..d4d18a0738a1d 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseFilteredMonitoringDocTestCase.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BaseFilteredMonitoringDocTestCase.java @@ -80,29 +80,32 @@ public void testFilteredMonitoringDocToXContent() throws IOException { node, MonitoredSystem.ES, "_type", "_id", filters); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - assertEquals("{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"2017-08-09T08:18:59.402Z\"," - + "\"interval_ms\":1506593717631," - + "\"type\":\"_type\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}," - + "\"_type\":{" - + "\"field_1\":1," - + "\"field_3\":{" - + "\"sub_field_3\":3" - + "}," - + "\"field_5\":[" - + "{\"sub_field_5\":5}" - + "]" - + "}" - + "}", xContent.utf8ToString()); + final String expected = "{" + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": \"2017-08-09T08:18:59.402Z\"," + + " \"interval_ms\": 1506593717631," + + " \"type\": \"_type\"," + + " \"source_node\": {" + + " \"uuid\": \"_uuid\"," + + " \"host\": \"_host\"," + + " \"transport_address\": \"_addr\"," + + " \"ip\": \"_ip\"," + + " \"name\": \"_name\"," + + " \"timestamp\": \"2017-08-31T08:46:30.855Z\"" + + " }," + + " \"_type\": {" + + " \"field_1\": 1," + + " \"field_3\": {" + + " \"sub_field_3\": 3" + + " }," + + " \"field_5\": [" + + " {" + + " \"sub_field_5\": 5" + + " }" + + " ]" + + " }" + + "}"; + assertEquals(XContentHelper.reformatJson(expected), xContent.utf8ToString()); } class TestFilteredMonitoringDoc extends FilteredMonitoringDoc { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BytesReferenceMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BytesReferenceMonitoringDocTests.java index 68e5c5b657b5c..9afc6324a7532 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BytesReferenceMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/BytesReferenceMonitoringDocTests.java @@ -91,23 +91,24 @@ public void testToXContent() throws IOException { node, KIBANA, "_type", "_id", xContentType, BytesReference.bytes(builder)); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - assertEquals("{" - + "\"cluster_uuid\":\"_cluster\"," - + "\"timestamp\":\"2017-08-09T08:18:59.402Z\"," - + "\"interval_ms\":1506593717631," - + "\"type\":\"_type\"," - + "\"source_node\":{" - + "\"uuid\":\"_uuid\"," - + "\"host\":\"_host\"," - + "\"transport_address\":\"_addr\"," - + "\"ip\":\"_ip\"," - + "\"name\":\"_name\"," - + "\"timestamp\":\"2017-08-31T08:46:30.855Z\"" - + "}," - + "\"_type\":{" - + "\"field\":\"value\"" - + "}" - + "}", xContent.utf8ToString()); + final String expected = "{" + + " \"cluster_uuid\": \"_cluster\"," + + " \"timestamp\": \"2017-08-09T08:18:59.402Z\"," + + " \"interval_ms\": 1506593717631," + + " \"type\": \"_type\"," + + " \"source_node\": {" + + " \"uuid\": \"_uuid\"," + + " \"host\": \"_host\"," + + " \"transport_address\": \"_addr\"," + + " \"ip\": \"_ip\"," + + " \"name\": \"_name\"," + + " \"timestamp\": \"2017-08-31T08:46:30.855Z\"" + + " }," + + " \"_type\": {" + + " \"field\": \"value\"" + + " }" + + "}"; + assertEquals(XContentHelper.reformatJson(expected), xContent.utf8ToString()); } public void testEqualsAndHashcode() { diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulkResponseListenerTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulkResponseListenerTests.java index 5c5d0daa8086b..e915e35b51fd1 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulkResponseListenerTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExportBulkResponseListenerTests.java @@ -112,26 +112,29 @@ public void testOnSuccessParsingWithInnerErrors() throws IOException { when(xContent.createParser(Mockito.any(NamedXContentRegistry.class), Mockito.any(DeprecationHandler.class), Mockito.eq(stream))).thenReturn(parser); - // {, "took", 4, "errors", false nextToken, currentName - when(parser.nextToken()).thenReturn(Token.START_OBJECT, // 1 - Token.FIELD_NAME, Token.VALUE_NUMBER, // 3, 1 - Token.FIELD_NAME, Token.VALUE_BOOLEAN, // 5, 2 - Token.FIELD_NAME, Token.START_ARRAY, // 7, 3 - // no error: - Token.START_OBJECT, // 8 - Token.FIELD_NAME, Token.START_OBJECT, // 10, 4 - Token.FIELD_NAME, Token.VALUE_STRING, // 12, 5 - Token.FIELD_NAME, Token.VALUE_STRING, // 14, 6 - Token.FIELD_NAME, Token.VALUE_STRING, // 16, 7 - Token.END_OBJECT, // 17 - Token.START_OBJECT, // 18 - Token.FIELD_NAME, Token.START_OBJECT, // 20, 8 - Token.FIELD_NAME, Token.VALUE_STRING, // 22, 9 - Token.FIELD_NAME, Token.VALUE_STRING, // 24, 10 - Token.FIELD_NAME, Token.VALUE_STRING, // 26, 11 - Token.FIELD_NAME, Token.VALUE_STRING, // 28, 12 ("error") - Token.END_OBJECT, // 29 - Token.END_ARRAY); // 30 + // tag::disable-formatting + // {, "took", 4, "errors", false + when(parser.nextToken()).thenReturn( // nextToken, currentName + Token.START_OBJECT, // 1 + Token.FIELD_NAME, Token.VALUE_NUMBER, // 3, 1 + Token.FIELD_NAME, Token.VALUE_BOOLEAN, // 5, 2 + Token.FIELD_NAME, Token.START_ARRAY, // 7, 3 + // no error: + Token.START_OBJECT, // 8 + Token.FIELD_NAME, Token.START_OBJECT, // 10, 4 + Token.FIELD_NAME, Token.VALUE_STRING, // 12, 5 + Token.FIELD_NAME, Token.VALUE_STRING, // 14, 6 + Token.FIELD_NAME, Token.VALUE_STRING, // 16, 7 + Token.END_OBJECT, // 17 + Token.START_OBJECT, // 18 + Token.FIELD_NAME, Token.START_OBJECT, // 20, 8 + Token.FIELD_NAME, Token.VALUE_STRING, // 22, 9 + Token.FIELD_NAME, Token.VALUE_STRING, // 24, 10 + Token.FIELD_NAME, Token.VALUE_STRING, // 26, 11 + Token.FIELD_NAME, Token.VALUE_STRING, // 28, 12 ("error") + Token.END_OBJECT, // 29 + Token.END_ARRAY); // 30 + // end::disable-formatting when(parser.currentName()).thenReturn("took", "errors", "items", "index", "_index", "_type", "_id", "index", "_index", "_type", "_id", "error"); diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java index 1bed5f1c7fff9..4b75a6a075f4d 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterIT.java @@ -769,8 +769,8 @@ private void enqueueWatcherResponses(final MockWebServer webServer, } else { enqueueClusterAlertResponsesDoesNotExistYet(webServer); } - // otherwise we need to delete them from the remote cluster } else { + // otherwise we need to delete them from the remote cluster enqueueDeleteClusterAlertResponses(webServer); } } else { @@ -783,8 +783,8 @@ private void enqueueWatcherResponses(final MockWebServer webServer, ); enqueueResponse(webServer, 200, responseBody); - // X-Pack is not installed } else { + // X-Pack is not installed enqueueResponse(webServer, 404, "{}"); } } diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterResourceTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterResourceTests.java index e6f896a2aa050..c0804ad2060b5 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterResourceTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/exporter/http/HttpExporterResourceTests.java @@ -383,8 +383,8 @@ public void testWatchCheckBlocksAfterSuccessfulWatcherCheck() { // +1 for the "first" expectedGets += 1 + successful + unsuccessful; expectedPuts = (successfulFirst ? 0 : 1) + unsuccessful; - // deleting watches } else { + // deleting watches // - 1 from necessary failure after it! final int successful = randomIntBetween(1, EXPECTED_WATCHES - 1); diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/IndexerUtilsTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/IndexerUtilsTests.java index 9f8e26d425f5d..0009a74886776 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/IndexerUtilsTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/IndexerUtilsTests.java @@ -561,7 +561,8 @@ public void testTimezone() throws IOException { = new DateHistogramValuesSourceBuilder("the_histo." + DateHistogramAggregationBuilder.NAME) .field(timestampField) .calendarInterval(new DateHistogramInterval("1d")) - .timeZone(ZoneId.of("-01:00", ZoneId.SHORT_IDS)); // adds a timezone so that we aren't on default UTC + // adds a timezone so that we aren't on default UTC + .timeZone(ZoneId.of("-01:00", ZoneId.SHORT_IDS)); CompositeAggregationBuilder compositeBuilder = new CompositeAggregationBuilder(RollupIndexer.AGGREGATION_NAME, singletonList(dateHisto)); diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatter.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatter.java new file mode 100644 index 0000000000000..3053824284caf --- /dev/null +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatter.java @@ -0,0 +1,58 @@ +package org.elasticsearch.xpack.security.authc.saml; + +import java.util.Map; +import java.util.regex.Pattern; + +/** + * A formatter that allows named placeholders e.g. "%(var_name)" to be replaced. + * Unlike the original implementation, parameters without values will cause am + * {@link IllegalArgumentException} to be thrown. + * + * @see Original post on Stack Exchange. + */ +public class NamedFormatter { + private static final Pattern RE = Pattern.compile( + // Treat any character after a backslash literally, and look for %(keys) to replace + "\\\\(.) | (%\\(([^)]+)\\))", + Pattern.COMMENTS + ); + + private NamedFormatter() {} + + /** + * Expands format strings containing %(keys). + * + *

Examples:

+ * + *
    + *
  • NamedFormatter.format("Hello, %(name)!", Map.of("name", "200_success"))"Hello, 200_success!"
  • + *
  • NamedFormatter.format("Hello, \%(name)!", Map.of("name", "200_success"))"Hello, %(name)!"
  • + *
  • NamedFormatter.format("Hello, %(name)!", Map.of("foo", "bar")) → {@link IllegalArgumentException}
  • + *
+ * + * @param fmt The format string. Any character in the format string that + * follows a backslash is treated literally. Any + * %(key) is replaced by its corresponding value + * in the values map. If the key does not exist + * in the values map, then it is left unsubstituted. + * @param values Key-value pairs to be used in the substitutions. + * @return The formatted string. + */ + public static String format(String fmt, Map values) { + return RE.matcher(fmt) + .replaceAll(match -> { + // Escaped characters are unchanged + if (match.group(1) != null) { + return match.group(1); + } + + final String paramName = match.group(3); + if (values.containsKey(paramName)) { + return values.get(paramName).toString(); + } + + throw new IllegalArgumentException("No parameter value for %(" + paramName + ")"); + }); + } +} diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatterTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatterTests.java new file mode 100644 index 0000000000000..7af617bada515 --- /dev/null +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatterTests.java @@ -0,0 +1,45 @@ +package org.elasticsearch.xpack.security.authc.saml; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.util.Map; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +public class NamedFormatterTests { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void patternAreFormatted() { + assertThat(NamedFormatter.format("Hello, %(name)!", Map.of("name", "world")), equalTo("Hello, world!")); + } + + @Test + public void duplicatePatternsAreFormatted() { + assertThat(NamedFormatter.format("Hello, %(name) and %(name)!", Map.of("name", "world")), equalTo("Hello, world and world!")); + } + + @Test + public void multiplePatternsAreFormatted() { + assertThat( + NamedFormatter.format("Hello, %(name) and %(second_name)!", Map.of("name", "world", "second_name", "fred")), + equalTo("Hello, world and fred!") + ); + } + + @Test + public void escapedPatternsAreNotFormatted() { + assertThat(NamedFormatter.format("Hello, \\%(name)!", Map.of("name", "world")), equalTo("Hello, %(name)!")); + } + + @Test + public void unknownPatternsThrowException() { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("No parameter value for %(name)"); + NamedFormatter.format("Hello, %(name)!", Map.of("foo", "world")); + } +} diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java index 773272548dda1..03feb4c4782f4 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java @@ -209,13 +209,30 @@ public void testParseEmptyContentIsRejected() throws Exception { public void testParseContentWithNoAssertionsIsRejected() throws Exception { Instant now = clock.instant(); - SamlToken token = token("\n" + - "" + - "" + - IDP_ENTITY_ID + "" + - "" + - ""); + final String xml = "\n" + + "" + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("now", now); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("SP_ACS_URL", SP_ACS_URL); + + SamlToken token = token(NamedFormatter.format(xml, replacements)); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("No assertions found in SAML response")); assertThat(exception.getCause(), nullValue()); @@ -227,38 +244,57 @@ public void testSuccessfullyParseContentWithASingleValidAssertion() throws Excep Instant validUntil = now.plusSeconds(30); final String nameId = randomAlphaOfLengthBetween(12, 24); final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + nameId + "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = token(signDoc(xml)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " %(nameId)" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("nameId", nameId); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final SamlAttributes attributes = authenticator.authenticate(token); assertThat(attributes, notNullValue()); assertThat(attributes.attributes(), iterableWithSize(1)); @@ -409,38 +445,54 @@ public void testIncorrectResponseIssuerIsRejected() throws Exception { Instant now = clock.instant(); Instant validUntil = now.plusSeconds(30); final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "xxx" + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = token(signDoc(xml)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)xxx" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("Issuer")); assertThat(exception.getCause(), nullValue()); @@ -451,38 +503,54 @@ public void testIncorrectAssertionIssuerIsRejected() throws Exception { Instant now = clock.instant(); Instant validUntil = now.plusSeconds(30); final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "_" + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = token(signDoc(xml)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)_" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("Issuer")); assertThat(exception.getCause(), nullValue()); @@ -493,38 +561,61 @@ public void testIncorrectDestinationIsRejected() throws Exception { Instant now = clock.instant(); Instant validUntil = now.plusSeconds(30); String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = randomBoolean() ? token(signDoc(xml)) : token(signAssertions(xml, idpSigningCertificatePair)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + final String xmlWithReplacements = NamedFormatter.format(xml, replacements); + + SamlToken token = randomBoolean() + ? token(signDoc(xmlWithReplacements)) + : token(signAssertions(xmlWithReplacements, idpSigningCertificatePair)); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("destination")); assertThat(exception.getCause(), nullValue()); @@ -535,38 +626,54 @@ public void testMissingDestinationIsNotRejectedForNotSignedResponse() throws Exc Instant now = clock.instant(); Instant validUntil = now.plusSeconds(30); String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = token(signAssertions(xml, idpSigningCertificatePair)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + SamlToken token = token(signAssertions(NamedFormatter.format(xml, replacements), idpSigningCertificatePair)); final SamlAttributes attributes = authenticator.authenticate(token); assertThat(attributes, notNullValue()); assertThat(attributes.attributes(), iterableWithSize(1)); @@ -582,38 +689,58 @@ public void testIncorrectRequestIdIsRejected() throws Exception { Instant validUntil = now.plusSeconds(30); final String sessionindex = randomId(); final String incorrectId = "_012345"; - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = token(signDoc(xml)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("incorrectId", incorrectId); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("in-response-to")); assertThat(exception.getMessage(), containsString(requestId)); @@ -626,38 +753,60 @@ public void testIncorrectRecipientIsRejected() throws Exception { Instant now = clock.instant(); Instant validUntil = now.plusSeconds(30); String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = token(signDoc(xml)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("SAML Assertion SubjectConfirmationData Recipient")); assertThat(exception.getMessage(), containsString(SP_ACS_URL + "/fake")); @@ -667,25 +816,45 @@ public void testIncorrectRecipientIsRejected() throws Exception { public void testAssertionWithoutSubjectIsRejected() throws Exception { Instant now = clock.instant(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = token(signDoc(xml)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("randomId2", randomId()); + replacements.put("requestId", requestId); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + + SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("has no Subject")); assertThat(exception.getCause(), nullValue()); @@ -695,32 +864,52 @@ public void testAssertionWithoutSubjectIsRejected() throws Exception { public void testAssertionWithoutAuthnStatementIsRejected() throws Exception { Instant now = clock.instant(); Instant validUntil = now.plusSeconds(30); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = token(signDoc(xml)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("randomId2", randomId()); + replacements.put("requestId", requestId); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("Authn Statements while exactly one was expected.")); assertThat(exception.getCause(), nullValue()); @@ -733,39 +922,59 @@ public void testExpiredAuthnStatementSessionIsRejected() throws Exception { Instant sessionValidUntil = now.plusSeconds(60); final String nameId = randomAlphaOfLengthBetween(12, 24); final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + nameId + "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " %(nameId)" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("nameId", nameId); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("sessionValidUntil", sessionValidUntil); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + // check that the content is valid "now" - final SamlToken token = token(signDoc(xml)); + final SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); assertThat(authenticator.authenticate(token), notNullValue()); // and still valid if we advance partway through the session expiry time @@ -790,40 +999,59 @@ public void testIncorrectAuthnContextClassRefIsRejected() throws Exception { Instant validUntil = now.plusSeconds(30); final String nameId = randomAlphaOfLengthBetween(12, 24); final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + nameId + "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " %(nameId)" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("nameId", nameId); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + SamlAuthenticator authenticatorWithReqAuthnCtx = buildAuthenticator(() -> buildOpenSamlCredential(idpSigningCertificatePair), Arrays.asList(X509_AUTHN_CTX, KERBEROS_AUTHN_CTX)); - SamlToken token = token(signDoc(xml)); + SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticatorWithReqAuthnCtx.authenticate(token)); assertThat(exception.getMessage(), containsString("Rejecting SAML assertion as the AuthnContextClassRef")); assertThat(SamlUtils.isSamlException(exception), is(true)); @@ -831,28 +1059,46 @@ public void testIncorrectAuthnContextClassRefIsRejected() throws Exception { public void testAssertionWithoutSubjectConfirmationIsRejected() throws Exception { Instant now = clock.instant(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = token(signDoc(xml)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("now", now); + replacements.put("randomId", randomId()); + replacements.put("randomId2", randomId()); + replacements.put("requestId", requestId); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + + SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("SAML Assertion subject contains 0 bearer SubjectConfirmation")); assertThat(exception.getCause(), nullValue()); @@ -861,29 +1107,48 @@ public void testAssertionWithoutSubjectConfirmationIsRejected() throws Exception public void testAssertionWithoutSubjectConfirmationDataIsRejected() throws Exception { Instant now = clock.instant(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = token(signDoc(xml)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("randomId", randomId()); + replacements.put("randomId2", randomId()); + replacements.put("requestId", requestId); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + + SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("bearer SubjectConfirmation, while exactly one was expected.")); assertThat(exception.getCause(), nullValue()); @@ -894,38 +1159,57 @@ public void testAssetionWithoutBearerSubjectConfirmationMethodIsRejected() throw Instant now = clock.instant(); Instant validUntil = now.plusSeconds(30); final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = token(signDoc(xml)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_ATTRIB_NAME", METHOD_ATTRIB_NAME); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("bearer SubjectConfirmation, while exactly one was expected.")); assertThat(exception.getCause(), nullValue()); @@ -937,38 +1221,61 @@ public void testIncorrectSubjectConfirmationDataInResponseToIsRejected() throws Instant validUntil = now.plusSeconds(30); final String incorrectId = "_123456"; final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - SamlToken token = token(signDoc(xml)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("incorrectId", incorrectId); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final ElasticsearchSecurityException exception = expectSamlException(() -> authenticator.authenticate(token)); assertThat(exception.getMessage(), containsString("SAML Assertion SubjectConfirmationData is in-response-to")); assertThat(exception.getMessage(), containsString(requestId)); @@ -981,40 +1288,58 @@ public void testExpiredSubjectConfirmationDataIsRejected() throws Exception { Instant now = clock.instant(); Instant validUntil = now.plusSeconds(120); final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); // check that the content is valid "now" - final SamlToken token = token(signDoc(xml)); + final SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); assertThat(authenticator.authenticate(token), notNullValue()); // and still valid if we advance partway through the expiry time @@ -1043,37 +1368,56 @@ public void testIdpInitiatedLoginIsAllowed() throws Exception { Instant now = clock.instant(); Instant validUntil = now.plusSeconds(30); final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; - final SamlToken token = token(signDoc(xml)); + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + final SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); final SamlAttributes attributes = authenticator.authenticate(token); assertThat(attributes, notNullValue()); assertThat(attributes.attributes(), iterableWithSize(1)); @@ -1084,45 +1428,67 @@ public void testIncorrectSigningKeyIsRejected() throws Exception { Instant now = clock.instant(); Instant validUntil = now.plusSeconds(30); final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + final String xmlWithReplacements = NamedFormatter.format(xml, replacements); // check that the content is valid when signed by the correct key-pair - assertThat(authenticator.authenticate(token(signer.transform(xml, idpSigningCertificatePair))), notNullValue()); + assertThat(authenticator.authenticate(token(signer.transform(xmlWithReplacements, idpSigningCertificatePair))), notNullValue()); // check is rejected when signed by a different key-pair final Tuple wrongKey = readKeyPair("RSA_4096_updated"); - final ElasticsearchSecurityException exception = expectThrows(ElasticsearchSecurityException.class, - () -> authenticator.authenticate(token(signer.transform(xml, wrongKey)))); + final ElasticsearchSecurityException exception = expectThrows( + ElasticsearchSecurityException.class, + () -> authenticator.authenticate(token(signer.transform(xmlWithReplacements, wrongKey))) + ); assertThat(exception.getMessage(), containsString("SAML Signature")); assertThat(exception.getMessage(), containsString("could not be validated")); assertThat(exception.getCause(), nullValue()); @@ -1149,40 +1515,58 @@ public void testParsingRejectsTamperedContent() throws Exception { Instant now = clock.instant(); Instant validUntil = now.plusSeconds(30); final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); // check that the original signed content is valid - final String signed = signer.transform(xml, idpSigningCertificatePair); + final String signed = signer.transform(NamedFormatter.format(xml, replacements), idpSigningCertificatePair); assertThat(authenticator.authenticate(token(signed)), notNullValue()); // but altered content is rejected @@ -1208,41 +1592,61 @@ public void testSigningWhenIdpHasMultipleKeys() throws Exception { Instant now = clock.instant(); Instant validUntil = now.plusSeconds(30); final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + final String xmlWithReplacements = NamedFormatter.format(xml, replacements); // check that the content is valid when signed by the each of the key-pairs for (Tuple key : keys) { - assertThat(authenticator.authenticate(token(signer.transform(xml, key))), notNullValue()); + assertThat(authenticator.authenticate(token(signer.transform(xmlWithReplacements, key))), notNullValue()); } } @@ -1305,41 +1709,59 @@ public void testExpiredContentIsRejected() throws Exception { Instant now = clock.instant(); Instant validUntil = now.plusSeconds(120); final String sessionindex = randomId(); - final String xml = "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "randomopaquestring" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - ""; + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " randomopaquestring" + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); // check that the content is valid "now" - final SamlToken token = token(signDoc(xml)); + final SamlToken token = token(signDoc(NamedFormatter.format(xml, replacements))); assertThat(authenticator.authenticate(token), notNullValue()); // and still valid if we advance partway through the expiry time @@ -2079,19 +2501,31 @@ private Response toResponse(String xml) throws SAXException, IOException, Parser private String getStatusFailedResponse() { final Instant now = clock.instant(); - return "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + - ""; + final String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("now", now); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("SP_ACS_URL", SP_ACS_URL); + + return NamedFormatter.format(xml, replacements); } private String getSimpleResponse(Instant now) { @@ -2101,43 +2535,66 @@ private String getSimpleResponse(Instant now) { private String getSimpleResponse(Instant now, String nameId, String sessionindex) { Instant validUntil = now.plusSeconds(30); - return "\n" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + - "" + IDP_ENTITY_ID + "" + - "" + - "" + nameId + "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + PASSWORD_AUTHN_CTX + "" + - "" + - "" + - "" + - "daredevil" + - "" + - "" + - "defenders" + - "netflix" + - "" + - "" + - ""; + String xml = "\n" + + "" + + " %(IDP_ENTITY_ID)" + + " " + + " " + + " %(IDP_ENTITY_ID)" + + " " + + " %(nameId)" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(PASSWORD_AUTHN_CTX)" + + " " + + " " + + " " + + " " + + " daredevil" + + " " + + " " + + " " + + " " + + " defenders" + + " netflix" + + " " + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("IDP_ENTITY_ID", IDP_ENTITY_ID); + replacements.put("METHOD_BEARER", METHOD_BEARER); + replacements.put("nameId", nameId); + replacements.put("now", now); + replacements.put("PASSWORD_AUTHN_CTX", PASSWORD_AUTHN_CTX); + replacements.put("randomId", randomId()); + replacements.put("requestId", requestId); + replacements.put("sessionindex", sessionindex); + replacements.put("SP_ACS_URL", SP_ACS_URL); + replacements.put("SP_ENTITY_ID", SP_ENTITY_ID); + replacements.put("TRANSIENT", TRANSIENT); + replacements.put("validUntil", validUntil); + + return NamedFormatter.format(xml, replacements); } private String getResponseWithAudienceRestrictions(String... requiredAudiences) { diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilderTests.java index 066de5cfa6092..6eab867ec5db0 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilderTests.java @@ -20,10 +20,14 @@ import java.security.cert.X509Certificate; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.stream.Collectors; +import static org.hamcrest.Matchers.equalTo; + public class SamlSpMetadataBuilderTests extends SamlTestCase { private X509Certificate certificate; @@ -67,7 +71,7 @@ public void testBuildMinimalistMetadata() throws Exception { final Element element = new EntityDescriptorMarshaller().marshall(descriptor); final String xml = SamlUtils.toString(element); - assertThat(xml, Matchers.equalTo("" + + assertThat(xml, equalTo("" + "" + "" + @@ -100,66 +104,87 @@ public void testBuildFullMetadata() throws Exception { final Element element = new EntityDescriptorMarshaller().marshall(descriptor); final String xml = SamlUtils.toString(element); - assertThat(xml, Matchers.equalTo("" + - "" + - "" + - "" + - "" + - "MIIDWDCCAkCgAwIBAgIVANRTZaFrK+Pz19O8TZsb3HSJmAWpMA0GCSqGSIb3DQEBCwUAMB0xGzAZ" + System.lineSeparator() + - "BgNVBAMTEkVsYXN0aWNzZWFyY2gtU0FNTDAeFw0xNzExMjkwMjQ3MjZaFw0yMDExMjgwMjQ3MjZa" + System.lineSeparator() + - "MB0xGzAZBgNVBAMTEkVsYXN0aWNzZWFyY2gtU0FNTDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC" + System.lineSeparator() + - "AQoCggEBALHTuPGOieCbD2mZUdYrdH4ofo7qFze6rQUROCLKqf69uBuwvraNWOcwxHUTKVlLMV3d" + System.lineSeparator() + - "dKzYo+yfC44AMXrrV+79xVWsTCNHu9sxQzcDwiEx2OtOOX9MAk6tJQ3svNrMPNXWh8ftwmmY9XdF" + System.lineSeparator() + - "ZwMYUdo6FPjSQj5uQTDmGWRgF08f7VRlk6N92d/fzn9DlDm+TFuaOr17OTSR4B6RTrNwKC29AmXQ" + System.lineSeparator() + - "TwCijCObjLqyMEqP20dZCQeVf2qw8JKUHhW4r6mCLzqmeR+kRTqiHMSWxJddzxDGw6X7fOS7iuzB" + System.lineSeparator() + - "0+TnsKwgu8nYrEXds9MkGf1Yco7WsM43g+Es+LhNHP+es70CAwEAAaOBjjCBizAdBgNVHQ4EFgQU" + System.lineSeparator() + - "ILqVKGhIi8p5Xffsow/IKFLhRbIwWQYDVR0jBFIwUIAUILqVKGhIi8p5Xffsow/IKFLhRbKhIaQf" + System.lineSeparator() + - "MB0xGzAZBgNVBAMTEkVsYXN0aWNzZWFyY2gtU0FNTIIVANRTZaFrK+Pz19O8TZsb3HSJmAWpMA8G" + System.lineSeparator() + - "A1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAGhl4V9mp4SWSV2E3HAJ1PX+Vmp6k27K" + System.lineSeparator() + - "d0tkOk1B9fyA13QB30teyiL7RR0vSHRyWFY8rQH1mHD366GKRWLITRG/QPULamGdYXX4h0pFj5ld" + System.lineSeparator() + - "aubLxM/O9vEAxOgmo/lsdkeIq9tLBqY06r/5A/Mcgo63KGi00AFYBoyvqfOu6nRLPnQr+rKVfdNO" + System.lineSeparator() + - "pWeIiFY1i2XTNZ3CZjNPSTwiQMUzrCxKXB9lL0vF6QL2Gj2iBhzNfXi88wf7xaR6XKY1wNuv3HLP" + System.lineSeparator() + - "sL7n+PWby7LRX188dyS1dmKfQcrKL65OssBA5NC8CAYyBiygBmWN+5kVJM5fSb0SwPSoVWrNyz+8" + System.lineSeparator() + - "IUldQE8=" + - "" + - "" + - "" + - "urn:oasis:names:tc:SAML:2.0:nameid-format:persistent" + - "" + - "" + - "Hydra Kibana" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "Hydra" + - "Hydra" + - "https://hail.hydra/" + - "" + - "" + - "Wolfgang" + - "von Strucker" + - "baron.strucker@supreme.hydra" + - "" + - "" + - "Paul" + - "Ebersol" + - "pne@tech.hydra" + - "" + - "" - )); + + final String expectedCertificate = joinCertificateLines( + "MIIDWDCCAkCgAwIBAgIVANRTZaFrK+Pz19O8TZsb3HSJmAWpMA0GCSqGSIb3DQEBCwUAMB0xGzAZ", + "BgNVBAMTEkVsYXN0aWNzZWFyY2gtU0FNTDAeFw0xNzExMjkwMjQ3MjZaFw0yMDExMjgwMjQ3MjZa", + "MB0xGzAZBgNVBAMTEkVsYXN0aWNzZWFyY2gtU0FNTDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC", + "AQoCggEBALHTuPGOieCbD2mZUdYrdH4ofo7qFze6rQUROCLKqf69uBuwvraNWOcwxHUTKVlLMV3d", + "dKzYo+yfC44AMXrrV+79xVWsTCNHu9sxQzcDwiEx2OtOOX9MAk6tJQ3svNrMPNXWh8ftwmmY9XdF", + "ZwMYUdo6FPjSQj5uQTDmGWRgF08f7VRlk6N92d/fzn9DlDm+TFuaOr17OTSR4B6RTrNwKC29AmXQ", + "TwCijCObjLqyMEqP20dZCQeVf2qw8JKUHhW4r6mCLzqmeR+kRTqiHMSWxJddzxDGw6X7fOS7iuzB", + "0+TnsKwgu8nYrEXds9MkGf1Yco7WsM43g+Es+LhNHP+es70CAwEAAaOBjjCBizAdBgNVHQ4EFgQU", + "ILqVKGhIi8p5Xffsow/IKFLhRbIwWQYDVR0jBFIwUIAUILqVKGhIi8p5Xffsow/IKFLhRbKhIaQf", + "MB0xGzAZBgNVBAMTEkVsYXN0aWNzZWFyY2gtU0FNTIIVANRTZaFrK+Pz19O8TZsb3HSJmAWpMA8G", + "A1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAGhl4V9mp4SWSV2E3HAJ1PX+Vmp6k27K", + "d0tkOk1B9fyA13QB30teyiL7RR0vSHRyWFY8rQH1mHD366GKRWLITRG/QPULamGdYXX4h0pFj5ld", + "aubLxM/O9vEAxOgmo/lsdkeIq9tLBqY06r/5A/Mcgo63KGi00AFYBoyvqfOu6nRLPnQr+rKVfdNO", + "pWeIiFY1i2XTNZ3CZjNPSTwiQMUzrCxKXB9lL0vF6QL2Gj2iBhzNfXi88wf7xaR6XKY1wNuv3HLP", + "sL7n+PWby7LRX188dyS1dmKfQcrKL65OssBA5NC8CAYyBiygBmWN+5kVJM5fSb0SwPSoVWrNyz+8", + "IUldQE8=" + ); + + final String expectedXml = "" + + "" + + " " + + " " + + " " + + " " + + " %(expectedCertificate)" + + " " + + " " + + " " + + " " + + " urn:oasis:names:tc:SAML:2.0:nameid-format:persistent" + + " " + + " " + + " Hydra Kibana" + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " Hydra" + + " Hydra" + + " https://hail.hydra/" + + " " + + " " + + " Wolfgang" + + " von Strucker" + + " baron.strucker@supreme.hydra" + + " " + + " " + + " Paul" + + " Ebersol" + + " pne@tech.hydra" + + " " + + ""; + + final Map replacements = Map.of("expectedCertificate", expectedCertificate); + final String expectedXmlWithCertificate = NamedFormatter.format(expectedXml, replacements); + + assertThat(xml, equalTo(normaliseXml(expectedXmlWithCertificate))); + assertValidXml(xml); } @@ -184,110 +209,151 @@ public void testBuildFullMetadataWithSigningAndTwoEncryptionCerts() throws Excep final Element element = new EntityDescriptorMarshaller().marshall(descriptor); final String xml = SamlUtils.toString(element); - assertThat(xml, Matchers.equalTo("" + - "" + - "" + - "" + - "" + - "MIIDWDCCAkCgAwIBAgIVANRTZaFrK+Pz19O8TZsb3HSJmAWpMA0GCSqGSIb3DQEBCwUAMB0xGzAZ" + System.lineSeparator() + - "BgNVBAMTEkVsYXN0aWNzZWFyY2gtU0FNTDAeFw0xNzExMjkwMjQ3MjZaFw0yMDExMjgwMjQ3MjZa" + System.lineSeparator() + - "MB0xGzAZBgNVBAMTEkVsYXN0aWNzZWFyY2gtU0FNTDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC" + System.lineSeparator() + - "AQoCggEBALHTuPGOieCbD2mZUdYrdH4ofo7qFze6rQUROCLKqf69uBuwvraNWOcwxHUTKVlLMV3d" + System.lineSeparator() + - "dKzYo+yfC44AMXrrV+79xVWsTCNHu9sxQzcDwiEx2OtOOX9MAk6tJQ3svNrMPNXWh8ftwmmY9XdF" + System.lineSeparator() + - "ZwMYUdo6FPjSQj5uQTDmGWRgF08f7VRlk6N92d/fzn9DlDm+TFuaOr17OTSR4B6RTrNwKC29AmXQ" + System.lineSeparator() + - "TwCijCObjLqyMEqP20dZCQeVf2qw8JKUHhW4r6mCLzqmeR+kRTqiHMSWxJddzxDGw6X7fOS7iuzB" + System.lineSeparator() + - "0+TnsKwgu8nYrEXds9MkGf1Yco7WsM43g+Es+LhNHP+es70CAwEAAaOBjjCBizAdBgNVHQ4EFgQU" + System.lineSeparator() + - "ILqVKGhIi8p5Xffsow/IKFLhRbIwWQYDVR0jBFIwUIAUILqVKGhIi8p5Xffsow/IKFLhRbKhIaQf" + System.lineSeparator() + - "MB0xGzAZBgNVBAMTEkVsYXN0aWNzZWFyY2gtU0FNTIIVANRTZaFrK+Pz19O8TZsb3HSJmAWpMA8G" + System.lineSeparator() + - "A1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAGhl4V9mp4SWSV2E3HAJ1PX+Vmp6k27K" + System.lineSeparator() + - "d0tkOk1B9fyA13QB30teyiL7RR0vSHRyWFY8rQH1mHD366GKRWLITRG/QPULamGdYXX4h0pFj5ld" + System.lineSeparator() + - "aubLxM/O9vEAxOgmo/lsdkeIq9tLBqY06r/5A/Mcgo63KGi00AFYBoyvqfOu6nRLPnQr+rKVfdNO" + System.lineSeparator() + - "pWeIiFY1i2XTNZ3CZjNPSTwiQMUzrCxKXB9lL0vF6QL2Gj2iBhzNfXi88wf7xaR6XKY1wNuv3HLP" + System.lineSeparator() + - "sL7n+PWby7LRX188dyS1dmKfQcrKL65OssBA5NC8CAYyBiygBmWN+5kVJM5fSb0SwPSoVWrNyz+8" + System.lineSeparator() + - "IUldQE8=" + - "" + - "" + - "" + - "" + - "MIID0zCCArugAwIBAgIJALi5bDfjMszLMA0GCSqGSIb3DQEBCwUAMEgxDDAKBgNVBAoTA29yZzEW" + System.lineSeparator() + - "MBQGA1UECxMNZWxhc3RpY3NlYXJjaDEgMB4GA1UEAxMXRWxhc3RpY3NlYXJjaCBUZXN0IE5vZGUw" + System.lineSeparator() + - "HhcNMTUwOTIzMTg1MjU3WhcNMTkwOTIyMTg1MjU3WjBIMQwwCgYDVQQKEwNvcmcxFjAUBgNVBAsT" + System.lineSeparator() + - "DWVsYXN0aWNzZWFyY2gxIDAeBgNVBAMTF0VsYXN0aWNzZWFyY2ggVGVzdCBOb2RlMIIBIjANBgkq" + System.lineSeparator() + - "hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3rGZ1QbsW0+MuyrSLmMfDFKtLBkIFW8V0gRuurFg1PUK" + System.lineSeparator() + - "KNR1Mq2tMVwjjYETAU/UY0iKZOzjgvYPKhDTYBTte/WHR1ZK4CYVv7TQX/gtFQG/ge/c7u0sLch9" + System.lineSeparator() + - "p7fbd+/HZiLS/rBEZDIohvgUvzvnA8+OIYnw4kuxKo/5iboAIS41klMg/lATm8V71LMY68inht71" + System.lineSeparator() + - "/ZkQoAHKgcR9z4yNYvQ1WqKG8DG8KROXltll3sTrKbl5zJhn660es/1ZnR6nvwt6xnSTl/mNHMjk" + System.lineSeparator() + - "fv1bs4rJ/py3qPxicdoSIn/KyojUcgHVF38fuAy2CQTdjVG5fWj9iz+mQvLm3+qsIYQdFwIDAQAB" + System.lineSeparator() + - "o4G/MIG8MAkGA1UdEwQCMAAwHQYDVR0OBBYEFEMMWLWQi/g83PzlHYqAVnty5L7HMIGPBgNVHREE" + System.lineSeparator() + - "gYcwgYSCCWxvY2FsaG9zdIIVbG9jYWxob3N0LmxvY2FsZG9tYWluggpsb2NhbGhvc3Q0ghdsb2Nh" + System.lineSeparator() + - "bGhvc3Q0LmxvY2FsZG9tYWluNIIKbG9jYWxob3N0NoIXbG9jYWxob3N0Ni5sb2NhbGRvbWFpbjaH" + System.lineSeparator() + - "BH8AAAGHEAAAAAAAAAAAAAAAAAAAAAEwDQYJKoZIhvcNAQELBQADggEBAMjGGXT8Nt1tbl2GkiKt" + System.lineSeparator() + - "miuGE2Ej66YuZ37WSJViaRNDVHLlg87TCcHek2rdO+6sFqQbbzEfwQ05T7xGmVu7tm54HwKMRugo" + System.lineSeparator() + - "Q3wct0bQC5wEWYN+oMDvSyO6M28mZwWb4VtR2IRyWP+ve5DHwTM9mxWa6rBlGzsQqH6YkJpZojzq" + System.lineSeparator() + - "k/mQTug+Y8aEmVoqRIPMHq9ob+S9qd5lp09+MtYpwPfTPx/NN+xMEooXWW/ARfpGhWPkg/FuCu4z" + System.lineSeparator() + - "1tFmCqHgNcWirzMm3dQpF78muE9ng6OB2MXQwL4VgnVkxmlZNHbkR2v/t8MyZJxCy4g6cTMM3S/U" + System.lineSeparator() + - "Mt5/+aIB2JAuMKyuD+A=" + - "" + - "" + - "" + - "" + - "MIID1zCCAr+gAwIBAgIJALnUl/KSS74pMA0GCSqGSIb3DQEBCwUAMEoxDDAKBgNVBAoTA29yZzEW" + System.lineSeparator() + - "MBQGA1UECxMNZWxhc3RpY3NlYXJjaDEiMCAGA1UEAxMZRWxhc3RpY3NlYXJjaCBUZXN0IENsaWVu" + System.lineSeparator() + - "dDAeFw0xNTA5MjMxODUyNTVaFw0xOTA5MjIxODUyNTVaMEoxDDAKBgNVBAoTA29yZzEWMBQGA1UE" + System.lineSeparator() + - "CxMNZWxhc3RpY3NlYXJjaDEiMCAGA1UEAxMZRWxhc3RpY3NlYXJjaCBUZXN0IENsaWVudDCCASIw" + System.lineSeparator() + - "DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMKm+P6vDAff0c6BWKGdhnYoNl9HijLIgfU3d9CQ" + System.lineSeparator() + - "cqKtwT+yUW3DPSVjIfaLmDIGj6Hl8jTHWPB7ZP4fzhrPi6m4qlRGclJMECBuNASZFiPDtEDv3mso" + System.lineSeparator() + - "eqOKQet6n7PZvgpWM7hxYZO4P1aMKJtRsFAdvBAdZUnv0spR5G4UZTHzSKmMeanIKFkLaD0XVKiL" + System.lineSeparator() + - "Qu9/z9M6roDQeAEoCJ/8JsanG8ih2ymfPHIZuNyYIOrVekHN2zU6bnVn8/PCeZSjS6h5xYw+Jl5g" + System.lineSeparator() + - "zGI/n+F5CZ+THoH8pM4pGp6xRVzpiH12gvERGwgSIDXdn/+uZZj+4lE7n2ENRSOt5KcOGG99r60C" + System.lineSeparator() + - "AwEAAaOBvzCBvDAJBgNVHRMEAjAAMB0GA1UdDgQWBBSSFhBXNp7AaNrHdlgCV0mCEzt7ajCBjwYD" + System.lineSeparator() + - "VR0RBIGHMIGEgglsb2NhbGhvc3SCFWxvY2FsaG9zdC5sb2NhbGRvbWFpboIKbG9jYWxob3N0NIIX" + System.lineSeparator() + - "bG9jYWxob3N0NC5sb2NhbGRvbWFpbjSCCmxvY2FsaG9zdDaCF2xvY2FsaG9zdDYubG9jYWxkb21h" + System.lineSeparator() + - "aW42hwR/AAABhxAAAAAAAAAAAAAAAAAAAAABMA0GCSqGSIb3DQEBCwUAA4IBAQANvAkddfLxn4/B" + System.lineSeparator() + - "CY4LY/1ET3d7ZRldjFTyjjHRYJ3CYBXWVahMskLxIcFNca8YjKfXoX8mcK+NQK/dAbGHXqk76yMl" + System.lineSeparator() + - "krKjh1OQiZ1YAX5ryYerGrZ99N3E9wnbn72bW3iumoLlqmTWlHEpMI0Ql6J75BQLTgKHxCPupVA5" + System.lineSeparator() + - "sTbWkKwGjXXAi84rUlzhDJOR8jk3/7ct0iZO8Hk6AWMcNix5Wka3IDGUXuEVevYRlxgVyCxcnZWC" + System.lineSeparator() + - "7JWREpar5aIPQFkY6VCEglxwUyXbHZw5T/u6XaKKnS7gz8RiwRh68ddSQJeEHi5e4onUD7bOCJgf" + System.lineSeparator() + - "siUwdiCkDbfN9Yum8OIpmBRs" + - "" + - "" + - "" + - "urn:oasis:names:tc:SAML:2.0:nameid-format:persistent" + - "" + - "" + - "Hydra Kibana" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "" + - "Hydra" + - "Hydra" + - "https://hail.hydra/" + - "" + - "" + - "Wolfgang" + - "von Strucker" + - "baron.strucker@supreme.hydra" + - "" + - "" + - "Paul" + - "Ebersol" + - "pne@tech.hydra" + - "" + - "" - )); + + final String expectedCertificateOne = joinCertificateLines( + "MIIDWDCCAkCgAwIBAgIVANRTZaFrK+Pz19O8TZsb3HSJmAWpMA0GCSqGSIb3DQEBCwUAMB0xGzAZ", + "BgNVBAMTEkVsYXN0aWNzZWFyY2gtU0FNTDAeFw0xNzExMjkwMjQ3MjZaFw0yMDExMjgwMjQ3MjZa", + "MB0xGzAZBgNVBAMTEkVsYXN0aWNzZWFyY2gtU0FNTDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC", + "AQoCggEBALHTuPGOieCbD2mZUdYrdH4ofo7qFze6rQUROCLKqf69uBuwvraNWOcwxHUTKVlLMV3d", + "dKzYo+yfC44AMXrrV+79xVWsTCNHu9sxQzcDwiEx2OtOOX9MAk6tJQ3svNrMPNXWh8ftwmmY9XdF", + "ZwMYUdo6FPjSQj5uQTDmGWRgF08f7VRlk6N92d/fzn9DlDm+TFuaOr17OTSR4B6RTrNwKC29AmXQ", + "TwCijCObjLqyMEqP20dZCQeVf2qw8JKUHhW4r6mCLzqmeR+kRTqiHMSWxJddzxDGw6X7fOS7iuzB", + "0+TnsKwgu8nYrEXds9MkGf1Yco7WsM43g+Es+LhNHP+es70CAwEAAaOBjjCBizAdBgNVHQ4EFgQU", + "ILqVKGhIi8p5Xffsow/IKFLhRbIwWQYDVR0jBFIwUIAUILqVKGhIi8p5Xffsow/IKFLhRbKhIaQf", + "MB0xGzAZBgNVBAMTEkVsYXN0aWNzZWFyY2gtU0FNTIIVANRTZaFrK+Pz19O8TZsb3HSJmAWpMA8G", + "A1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAGhl4V9mp4SWSV2E3HAJ1PX+Vmp6k27K", + "d0tkOk1B9fyA13QB30teyiL7RR0vSHRyWFY8rQH1mHD366GKRWLITRG/QPULamGdYXX4h0pFj5ld", + "aubLxM/O9vEAxOgmo/lsdkeIq9tLBqY06r/5A/Mcgo63KGi00AFYBoyvqfOu6nRLPnQr+rKVfdNO", + "pWeIiFY1i2XTNZ3CZjNPSTwiQMUzrCxKXB9lL0vF6QL2Gj2iBhzNfXi88wf7xaR6XKY1wNuv3HLP", + "sL7n+PWby7LRX188dyS1dmKfQcrKL65OssBA5NC8CAYyBiygBmWN+5kVJM5fSb0SwPSoVWrNyz+8", + "IUldQE8=" + ); + + final String expectedCertificateTwo = joinCertificateLines( + "MIID0zCCArugAwIBAgIJALi5bDfjMszLMA0GCSqGSIb3DQEBCwUAMEgxDDAKBgNVBAoTA29yZzEW", + "MBQGA1UECxMNZWxhc3RpY3NlYXJjaDEgMB4GA1UEAxMXRWxhc3RpY3NlYXJjaCBUZXN0IE5vZGUw", + "HhcNMTUwOTIzMTg1MjU3WhcNMTkwOTIyMTg1MjU3WjBIMQwwCgYDVQQKEwNvcmcxFjAUBgNVBAsT", + "DWVsYXN0aWNzZWFyY2gxIDAeBgNVBAMTF0VsYXN0aWNzZWFyY2ggVGVzdCBOb2RlMIIBIjANBgkq", + "hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3rGZ1QbsW0+MuyrSLmMfDFKtLBkIFW8V0gRuurFg1PUK", + "KNR1Mq2tMVwjjYETAU/UY0iKZOzjgvYPKhDTYBTte/WHR1ZK4CYVv7TQX/gtFQG/ge/c7u0sLch9", + "p7fbd+/HZiLS/rBEZDIohvgUvzvnA8+OIYnw4kuxKo/5iboAIS41klMg/lATm8V71LMY68inht71", + "/ZkQoAHKgcR9z4yNYvQ1WqKG8DG8KROXltll3sTrKbl5zJhn660es/1ZnR6nvwt6xnSTl/mNHMjk", + "fv1bs4rJ/py3qPxicdoSIn/KyojUcgHVF38fuAy2CQTdjVG5fWj9iz+mQvLm3+qsIYQdFwIDAQAB", + "o4G/MIG8MAkGA1UdEwQCMAAwHQYDVR0OBBYEFEMMWLWQi/g83PzlHYqAVnty5L7HMIGPBgNVHREE", + "gYcwgYSCCWxvY2FsaG9zdIIVbG9jYWxob3N0LmxvY2FsZG9tYWluggpsb2NhbGhvc3Q0ghdsb2Nh", + "bGhvc3Q0LmxvY2FsZG9tYWluNIIKbG9jYWxob3N0NoIXbG9jYWxob3N0Ni5sb2NhbGRvbWFpbjaH", + "BH8AAAGHEAAAAAAAAAAAAAAAAAAAAAEwDQYJKoZIhvcNAQELBQADggEBAMjGGXT8Nt1tbl2GkiKt", + "miuGE2Ej66YuZ37WSJViaRNDVHLlg87TCcHek2rdO+6sFqQbbzEfwQ05T7xGmVu7tm54HwKMRugo", + "Q3wct0bQC5wEWYN+oMDvSyO6M28mZwWb4VtR2IRyWP+ve5DHwTM9mxWa6rBlGzsQqH6YkJpZojzq", + "k/mQTug+Y8aEmVoqRIPMHq9ob+S9qd5lp09+MtYpwPfTPx/NN+xMEooXWW/ARfpGhWPkg/FuCu4z", + "1tFmCqHgNcWirzMm3dQpF78muE9ng6OB2MXQwL4VgnVkxmlZNHbkR2v/t8MyZJxCy4g6cTMM3S/U", + "Mt5/+aIB2JAuMKyuD+A=" + ); + + final String expectedCertificateThree = joinCertificateLines( + "MIID1zCCAr+gAwIBAgIJALnUl/KSS74pMA0GCSqGSIb3DQEBCwUAMEoxDDAKBgNVBAoTA29yZzEW", + "MBQGA1UECxMNZWxhc3RpY3NlYXJjaDEiMCAGA1UEAxMZRWxhc3RpY3NlYXJjaCBUZXN0IENsaWVu", + "dDAeFw0xNTA5MjMxODUyNTVaFw0xOTA5MjIxODUyNTVaMEoxDDAKBgNVBAoTA29yZzEWMBQGA1UE", + "CxMNZWxhc3RpY3NlYXJjaDEiMCAGA1UEAxMZRWxhc3RpY3NlYXJjaCBUZXN0IENsaWVudDCCASIw", + "DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMKm+P6vDAff0c6BWKGdhnYoNl9HijLIgfU3d9CQ", + "cqKtwT+yUW3DPSVjIfaLmDIGj6Hl8jTHWPB7ZP4fzhrPi6m4qlRGclJMECBuNASZFiPDtEDv3mso", + "eqOKQet6n7PZvgpWM7hxYZO4P1aMKJtRsFAdvBAdZUnv0spR5G4UZTHzSKmMeanIKFkLaD0XVKiL", + "Qu9/z9M6roDQeAEoCJ/8JsanG8ih2ymfPHIZuNyYIOrVekHN2zU6bnVn8/PCeZSjS6h5xYw+Jl5g", + "zGI/n+F5CZ+THoH8pM4pGp6xRVzpiH12gvERGwgSIDXdn/+uZZj+4lE7n2ENRSOt5KcOGG99r60C", + "AwEAAaOBvzCBvDAJBgNVHRMEAjAAMB0GA1UdDgQWBBSSFhBXNp7AaNrHdlgCV0mCEzt7ajCBjwYD", + "VR0RBIGHMIGEgglsb2NhbGhvc3SCFWxvY2FsaG9zdC5sb2NhbGRvbWFpboIKbG9jYWxob3N0NIIX", + "bG9jYWxob3N0NC5sb2NhbGRvbWFpbjSCCmxvY2FsaG9zdDaCF2xvY2FsaG9zdDYubG9jYWxkb21h", + "aW42hwR/AAABhxAAAAAAAAAAAAAAAAAAAAABMA0GCSqGSIb3DQEBCwUAA4IBAQANvAkddfLxn4/B", + "CY4LY/1ET3d7ZRldjFTyjjHRYJ3CYBXWVahMskLxIcFNca8YjKfXoX8mcK+NQK/dAbGHXqk76yMl", + "krKjh1OQiZ1YAX5ryYerGrZ99N3E9wnbn72bW3iumoLlqmTWlHEpMI0Ql6J75BQLTgKHxCPupVA5", + "sTbWkKwGjXXAi84rUlzhDJOR8jk3/7ct0iZO8Hk6AWMcNix5Wka3IDGUXuEVevYRlxgVyCxcnZWC", + "7JWREpar5aIPQFkY6VCEglxwUyXbHZw5T/u6XaKKnS7gz8RiwRh68ddSQJeEHi5e4onUD7bOCJgf", + "siUwdiCkDbfN9Yum8OIpmBRs" + ); + + final String expectedXml = "" + + "" + + " " + + " " + + " " + + " " + + " %(expectedCertificateOne)" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(expectedCertificateTwo)" + + " " + + " " + + " " + + " " + + " " + + " " + + " %(expectedCertificateThree)" + + " " + + " " + + " " + + " " + + " urn:oasis:names:tc:SAML:2.0:nameid-format:persistent" + + " " + + " " + + " Hydra Kibana" + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " " + + " Hydra" + + " Hydra" + + " https://hail.hydra/" + + " " + + " " + + " Wolfgang" + + " von Strucker" + + " baron.strucker@supreme.hydra" + + " " + + " " + + " Paul" + + " Ebersol" + + " pne@tech.hydra" + + " " + + ""; + + final Map replacements = new HashMap<>(); + replacements.put("expectedCertificateOne", expectedCertificateOne); + replacements.put("expectedCertificateTwo", expectedCertificateTwo); + replacements.put("expectedCertificateThree", expectedCertificateThree); + + final String expectedXmlWithCertificate = NamedFormatter.format(expectedXml, replacements); + + assertThat(xml, equalTo(normaliseXml(expectedXmlWithCertificate))); + assertValidXml(xml); } @@ -306,4 +372,14 @@ public void testAttributeNameIsRequired() { private void assertValidXml(String xml) throws Exception { SamlUtils.validate(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)), SamlMetadataCommand.METADATA_SCHEMA); } + + private String joinCertificateLines(String... lines) { + return Arrays.stream(lines).collect(Collectors.joining(System.lineSeparator())); + } + + private String normaliseXml(String input) { + // Remove spaces between elements, and compress other spaces. These patterns don't use \s because + // that would match newlines. + return input.replaceAll("> +<", "><").replaceAll(" +", " "); + } } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerTests.java index 96222016c9702..dd46687263fdd 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerTests.java @@ -623,19 +623,21 @@ public void testConcatFoldingIsNotNull() { } public void testSimplifyCaseConditionsFoldWhenFalse() { - // CASE WHEN a = 1 THEN 'foo1' - // WHEN 1 = 2 THEN 'bar1' - // WHEN 2 = 1 THEN 'bar2' - // WHEN a > 1 THEN 'foo2' - // ELSE 'default' - // END - // - // ==> - // - // CASE WHEN a = 1 THEN 'foo1' - // WHEN a > 1 THEN 'foo2' - // ELSE 'default' - // END + /* + * CASE WHEN a = 1 THEN 'foo1' + * WHEN 1 = 2 THEN 'bar1' + * WHEN 2 = 1 THEN 'bar2' + * WHEN a > 1 THEN 'foo2' + * ELSE 'default' + * END + * + * ==> + * + * CASE WHEN a = 1 THEN 'foo1' + * WHEN a > 1 THEN 'foo2' + * ELSE 'default' + * END + */ Case c = new Case(EMPTY, Arrays.asList( new IfConditional(EMPTY, new Equals(EMPTY, getFieldAttribute(), ONE), Literal.of(EMPTY, "foo1")), @@ -654,19 +656,21 @@ public void testSimplifyCaseConditionsFoldWhenFalse() { } public void testSimplifyCaseConditionsFoldWhenTrue() { - // CASE WHEN a = 1 THEN 'foo1' - // WHEN 1 = 1 THEN 'bar1' - // WHEN 2 = 1 THEN 'bar2' - // WHEN a > 1 THEN 'foo2' - // ELSE 'default' - // END - // - // ==> - // - // CASE WHEN a = 1 THEN 'foo1' - // WHEN 1 = 1 THEN 'bar1' - // ELSE 'default' - // END + /* + * CASE WHEN a = 1 THEN 'foo1' + * WHEN 1 = 1 THEN 'bar1' + * WHEN 2 = 1 THEN 'bar2' + * WHEN a > 1 THEN 'foo2' + * ELSE 'default' + * END + * + * ==> + * + * CASE WHEN a = 1 THEN 'foo1' + * WHEN 1 = 1 THEN 'bar1' + * ELSE 'default' + * END + */ SimplifyCase rule = new SimplifyCase(); Case c = new Case(EMPTY, Arrays.asList( @@ -686,14 +690,16 @@ public void testSimplifyCaseConditionsFoldWhenTrue() { } public void testSimplifyCaseConditionsFoldCompletely() { - // CASE WHEN 1 = 2 THEN 'foo1' - // WHEN 1 = 1 THEN 'foo2' - // ELSE 'default' - // END - // - // ==> - // - // 'foo2' + /* + * CASE WHEN 1 = 2 THEN 'foo1' + * WHEN 1 = 1 THEN 'foo2' + * ELSE 'default' + * END + * + * ==> + * + * 'foo2' + */ SimplifyCase rule = new SimplifyCase(); Case c = new Case(EMPTY, Arrays.asList( diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformInfoTransportActionTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformInfoTransportActionTests.java index 6b99a60e10c1b..764506036c3d0 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformInfoTransportActionTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/TransformInfoTransportActionTests.java @@ -90,7 +90,8 @@ public void testParseSearchAggs() { 7, // indexTotal 8, // searchTotal 9, // indexFailures - 10); // searchFailures + 10 // searchFailures + ); int currentStat = 1; List aggs = new ArrayList<>(PROVIDED_STATS.length); From a4c1ffd511f297dd393a61b42eaf77987215e221 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Wed, 23 Oct 2019 12:22:15 +0100 Subject: [PATCH 08/15] Test fix --- .../common/xcontent/ConstructingObjectParserTests.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ConstructingObjectParserTests.java b/libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ConstructingObjectParserTests.java index 69b2f305c8862..9020e962e12da 100644 --- a/libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ConstructingObjectParserTests.java +++ b/libs/x-content/src/test/java/org/elasticsearch/common/xcontent/ConstructingObjectParserTests.java @@ -142,7 +142,9 @@ public void testMissingFirstConstructorArgButNotRequired() throws IOException { public void testBadParam() throws IOException { XContentParser parser = createParser( JsonXContent.jsonXContent, - "{ \"animal\": \"cat\", \"vegetable\": 2, \"a\": \"supercalifragilisticexpialidocious\" }" + // The following JSON needs to include newlines, in order to affect the line numbers + // included in the exception + "{\n" + " \"animal\": \"cat\",\n" + " \"vegetable\": 2,\n" + " \"a\": \"supercalifragilisticexpialidocious\"\n" + "}" ); XContentParseException e = expectThrows(XContentParseException.class, () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null)); @@ -155,7 +157,9 @@ public void testBadParam() throws IOException { public void testBadParamBeforeObjectBuilt() throws IOException { XContentParser parser = createParser( JsonXContent.jsonXContent, - "{ \"a\": \"supercalifragilisticexpialidocious\", \"animal\": \"cat\", \"vegetable\": 2 }" + // The following JSON needs to include newlines, in order to affect the line numbers + // included in the exception + "{\n" + " \"a\": \"supercalifragilisticexpialidocious\",\n" + " \"animal\": \"cat\"\n," + " \"vegetable\": 2\n" + "}" ); XContentParseException e = expectThrows(XContentParseException.class, () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null)); From 97a28d6453469c573f484e80c5d0348f01233517 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Wed, 23 Oct 2019 12:54:29 +0100 Subject: [PATCH 09/15] Put NamedFormatter in a better location --- .../common/util/NamedFormatter.java | 69 +++++++++++++++++++ .../common/util}/NamedFormatterTests.java | 21 +++++- .../security/authc/saml/NamedFormatter.java | 58 ---------------- .../authc/saml/SamlAuthenticatorTests.java | 1 + .../saml/SamlSpMetadataBuilderTests.java | 1 + 5 files changed, 91 insertions(+), 59 deletions(-) create mode 100644 test/framework/src/main/java/org/elasticsearch/common/util/NamedFormatter.java rename {x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml => test/framework/src/test/java/org/elasticsearch/common/util}/NamedFormatterTests.java (62%) delete mode 100644 x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatter.java diff --git a/test/framework/src/main/java/org/elasticsearch/common/util/NamedFormatter.java b/test/framework/src/main/java/org/elasticsearch/common/util/NamedFormatter.java new file mode 100644 index 0000000000000..819ac4c69c4c7 --- /dev/null +++ b/test/framework/src/main/java/org/elasticsearch/common/util/NamedFormatter.java @@ -0,0 +1,69 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.util; + +import java.util.Map; +import java.util.regex.Pattern; + +/** + * A formatter that allows named placeholders e.g. "%(param)" to be replaced. + */ +public class NamedFormatter { + private static final Pattern PARAM_REGEX = Pattern + .compile( + // Match either any backlash-escaped characters, or a "%(param)" pattern. + // COMMENTS is specified to allow whitespace in this pattern, for clarity + "\\\\(.) | (% \\( ([^)]+) \\) )", + Pattern.COMMENTS + ); + + private NamedFormatter() {} + + /** + * Replaces named parameters of the form %(param) in format strings. For example: + * + *
    + *
  • NamedFormatter.format("Hello, %(name)!", Map.of("name", "world"))"Hello, world!"
  • + *
  • NamedFormatter.format("Hello, \%(name)!", Map.of("name", "world"))"Hello, %(world)!"
  • + *
  • NamedFormatter.format("Hello, %(oops)!", Map.of("name", "world")) → {@link IllegalArgumentException}
  • + *
+ * + * @param fmt The format string. Any %(param) is replaced by its corresponding value in the values map. + * Parameter patterns can be escaped by prefixing with a backslash. + * @param values a map of parameter names to values. + * @return The formatted string. + * @throws IllegalArgumentException if a parameter is found in the format string with no corresponding value + */ + public static String format(String fmt, Map values) { + return PARAM_REGEX.matcher(fmt).replaceAll(match -> { + // Escaped characters are unchanged + if (match.group(1) != null) { + return match.group(1); + } + + final String paramName = match.group(3); + if (values.containsKey(paramName) == true) { + return values.get(paramName).toString(); + } + + throw new IllegalArgumentException("No parameter value for %(" + paramName + ")"); + }); + } +} diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatterTests.java b/test/framework/src/test/java/org/elasticsearch/common/util/NamedFormatterTests.java similarity index 62% rename from x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatterTests.java rename to test/framework/src/test/java/org/elasticsearch/common/util/NamedFormatterTests.java index 7af617bada515..7ee3ee6114cf5 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatterTests.java +++ b/test/framework/src/test/java/org/elasticsearch/common/util/NamedFormatterTests.java @@ -1,4 +1,23 @@ -package org.elasticsearch.xpack.security.authc.saml; +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.common.util; import org.junit.Rule; import org.junit.Test; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatter.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatter.java deleted file mode 100644 index 3053824284caf..0000000000000 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/NamedFormatter.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.elasticsearch.xpack.security.authc.saml; - -import java.util.Map; -import java.util.regex.Pattern; - -/** - * A formatter that allows named placeholders e.g. "%(var_name)" to be replaced. - * Unlike the original implementation, parameters without values will cause am - * {@link IllegalArgumentException} to be thrown. - * - * @see Original post on Stack Exchange. - */ -public class NamedFormatter { - private static final Pattern RE = Pattern.compile( - // Treat any character after a backslash literally, and look for %(keys) to replace - "\\\\(.) | (%\\(([^)]+)\\))", - Pattern.COMMENTS - ); - - private NamedFormatter() {} - - /** - * Expands format strings containing %(keys). - * - *

Examples:

- * - *
    - *
  • NamedFormatter.format("Hello, %(name)!", Map.of("name", "200_success"))"Hello, 200_success!"
  • - *
  • NamedFormatter.format("Hello, \%(name)!", Map.of("name", "200_success"))"Hello, %(name)!"
  • - *
  • NamedFormatter.format("Hello, %(name)!", Map.of("foo", "bar")) → {@link IllegalArgumentException}
  • - *
- * - * @param fmt The format string. Any character in the format string that - * follows a backslash is treated literally. Any - * %(key) is replaced by its corresponding value - * in the values map. If the key does not exist - * in the values map, then it is left unsubstituted. - * @param values Key-value pairs to be used in the substitutions. - * @return The formatted string. - */ - public static String format(String fmt, Map values) { - return RE.matcher(fmt) - .replaceAll(match -> { - // Escaped characters are unchanged - if (match.group(1) != null) { - return match.group(1); - } - - final String paramName = match.group(3); - if (values.containsKey(paramName)) { - return values.get(paramName).toString(); - } - - throw new IllegalArgumentException("No parameter value for %(" + paramName + ")"); - }); - } -} diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java index 03feb4c4782f4..824030beb49d9 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlAuthenticatorTests.java @@ -21,6 +21,7 @@ import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.util.NamedFormatter; import org.elasticsearch.test.MockLogAppender; import org.elasticsearch.xpack.core.watcher.watch.ClockMock; import org.hamcrest.Matchers; diff --git a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilderTests.java b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilderTests.java index 6eab867ec5db0..58d86996dd7c4 100644 --- a/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilderTests.java +++ b/x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/saml/SamlSpMetadataBuilderTests.java @@ -5,6 +5,7 @@ */ package org.elasticsearch.xpack.security.authc.saml; +import org.elasticsearch.common.util.NamedFormatter; import org.elasticsearch.xpack.core.ssl.CertParsingUtils; import org.hamcrest.Matchers; import org.junit.Before; From c05a87eadc2effd611a2d60531b8232a786f6834 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Wed, 23 Oct 2019 14:52:44 +0100 Subject: [PATCH 10/15] Fix forbidden APIs --- .../common/util/NamedFormatterTests.java | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/test/framework/src/test/java/org/elasticsearch/common/util/NamedFormatterTests.java b/test/framework/src/test/java/org/elasticsearch/common/util/NamedFormatterTests.java index 7ee3ee6114cf5..23956815fa358 100644 --- a/test/framework/src/test/java/org/elasticsearch/common/util/NamedFormatterTests.java +++ b/test/framework/src/test/java/org/elasticsearch/common/util/NamedFormatterTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.common.util; +import org.elasticsearch.test.ESTestCase; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -28,35 +29,30 @@ import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertThat; -public class NamedFormatterTests { +public class NamedFormatterTests extends ESTestCase { @Rule public ExpectedException thrown = ExpectedException.none(); - @Test - public void patternAreFormatted() { + public void testPatternAreFormatted() { assertThat(NamedFormatter.format("Hello, %(name)!", Map.of("name", "world")), equalTo("Hello, world!")); } - @Test - public void duplicatePatternsAreFormatted() { + public void testDuplicatePatternsAreFormatted() { assertThat(NamedFormatter.format("Hello, %(name) and %(name)!", Map.of("name", "world")), equalTo("Hello, world and world!")); } - @Test - public void multiplePatternsAreFormatted() { + public void testMultiplePatternsAreFormatted() { assertThat( NamedFormatter.format("Hello, %(name) and %(second_name)!", Map.of("name", "world", "second_name", "fred")), equalTo("Hello, world and fred!") ); } - @Test - public void escapedPatternsAreNotFormatted() { + public void testEscapedPatternsAreNotFormatted() { assertThat(NamedFormatter.format("Hello, \\%(name)!", Map.of("name", "world")), equalTo("Hello, %(name)!")); } - @Test - public void unknownPatternsThrowException() { + public void testUnknownPatternsThrowException() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("No parameter value for %(name)"); NamedFormatter.format("Hello, %(name)!", Map.of("foo", "world")); From 5c32bca24bbfcaabb806fa4c6471a4fe2e5c4959 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Wed, 23 Oct 2019 15:16:00 +0100 Subject: [PATCH 11/15] Checkstyle exception --- buildSrc/src/main/resources/checkstyle_suppressions.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/buildSrc/src/main/resources/checkstyle_suppressions.xml b/buildSrc/src/main/resources/checkstyle_suppressions.xml index 775c89c257a9c..ac2539527ac09 100644 --- a/buildSrc/src/main/resources/checkstyle_suppressions.xml +++ b/buildSrc/src/main/resources/checkstyle_suppressions.xml @@ -46,6 +46,10 @@ + + + From d37d4e8ea134809f81445e2567e3812f1697c28b Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Wed, 23 Oct 2019 15:29:36 +0100 Subject: [PATCH 12/15] Linting --- .../java/org/elasticsearch/common/util/NamedFormatterTests.java | 2 -- .../collector/cluster/ClusterStatsMonitoringDocTests.java | 2 ++ .../collector/indices/IndexRecoveryMonitoringDocTests.java | 2 ++ .../collector/indices/IndexStatsMonitoringDocTests.java | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/test/framework/src/test/java/org/elasticsearch/common/util/NamedFormatterTests.java b/test/framework/src/test/java/org/elasticsearch/common/util/NamedFormatterTests.java index 23956815fa358..06662adf0cd27 100644 --- a/test/framework/src/test/java/org/elasticsearch/common/util/NamedFormatterTests.java +++ b/test/framework/src/test/java/org/elasticsearch/common/util/NamedFormatterTests.java @@ -21,13 +21,11 @@ import org.elasticsearch.test.ESTestCase; import org.junit.Rule; -import org.junit.Test; import org.junit.rules.ExpectedException; import java.util.Map; import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; public class NamedFormatterTests extends ESTestCase { @Rule diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java index 017136ac82a24..bbb184bdcb66e 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java @@ -57,6 +57,7 @@ import java.io.IOException; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Map; import static java.util.Collections.emptyList; @@ -345,6 +346,7 @@ public void testToXContent() throws IOException { final BytesReference xContent = XContentHelper.toXContent(doc, XContentType.JSON, false); final String expected = XContentHelper.reformatJson( String.format( + Locale.ROOT, "{" + " \"cluster_uuid\": \"_cluster\"," + " \"timestamp\": \"2017-08-07T12:03:22.133Z\"," diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDocTests.java index bddb516db7e7a..633cb608a8a4e 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexRecoveryMonitoringDocTests.java @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import static java.util.Collections.singleton; @@ -117,6 +118,7 @@ public void testToXContent() throws IOException { final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); final String expected = XContentHelper.reformatJson( String.format( + Locale.ROOT, "{" + " \"cluster_uuid\": \"_cluster\"," + " \"timestamp\": \"2017-08-09T08:18:59.402Z\"," diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDocTests.java index 82a29ee17d0d3..2ce4d41f8ede6 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/indices/IndexStatsMonitoringDocTests.java @@ -126,7 +126,7 @@ public void testToXContent() throws IOException { new IndexStatsMonitoringDoc("_cluster", 1502266739402L, 1506593717631L, node, indexStats, metaData, routingTable); final BytesReference xContent = XContentHelper.toXContent(document, XContentType.JSON, false); - final String expected = XContentHelper.reformatJson(String.format("{" + final String expected = XContentHelper.reformatJson(String.format(Locale.ROOT, "{" + " \"cluster_uuid\": \"_cluster\"," + " \"timestamp\": \"2017-08-09T08:18:59.402Z\"," + " \"interval_ms\": 1506593717631," From 03ce841d4b803685cdd0a505edaab2c9f02e9c06 Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Wed, 23 Oct 2019 15:52:16 +0100 Subject: [PATCH 13/15] Test fixes --- .../cluster/ClusterStatsMonitoringDocTests.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java index bbb184bdcb66e..7d0b2fac0656e 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/cluster/ClusterStatsMonitoringDocTests.java @@ -385,22 +385,22 @@ public void testToXContent() throws IOException { + " \"shards\": {" + " \"total\": 1," + " \"primaries\": 1," - + " \"replication\": 0," + + " \"replication\": 0.0," + " \"index\": {" + " \"shards\": {" + " \"min\": 1," + " \"max\": 1," - + " \"avg\": 1" + + " \"avg\": 1.0" + " }," + " \"primaries\": {" + " \"min\": 1," + " \"max\": 1," - + " \"avg\": 1" + + " \"avg\": 1.0" + " }," + " \"replication\": {" - + " \"min\": 0," - + " \"max\": 0," - + " \"avg\": 0" + + " \"min\": 0.0," + + " \"max\": 0.0," + + " \"avg\": 0.0" + " }" + " }" + " }," @@ -439,7 +439,7 @@ public void testToXContent() throws IOException { + " \"index_writer_memory_in_bytes\": 0," + " \"version_map_memory_in_bytes\": 0," + " \"fixed_bit_set_memory_in_bytes\": 0," - + " \"max_unsafe_auto_id_timestamp\": -9223372036854776000," + + " \"max_unsafe_auto_id_timestamp\": -9223372036854775808," + " \"file_sizes\": {}" + " }" + " }," From faa25736538470ddf5400c6689dcdf2da6eb50cf Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Wed, 23 Oct 2019 16:41:07 +0100 Subject: [PATCH 14/15] More tests and checkstyle --- .../src/main/resources/checkstyle_suppressions.xml | 5 +++-- .../collector/ml/JobStatsMonitoringDocTests.java | 12 ++++++------ .../collector/node/NodeStatsMonitoringDocTests.java | 6 +++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/buildSrc/src/main/resources/checkstyle_suppressions.xml b/buildSrc/src/main/resources/checkstyle_suppressions.xml index ac2539527ac09..f85a49068e860 100644 --- a/buildSrc/src/main/resources/checkstyle_suppressions.xml +++ b/buildSrc/src/main/resources/checkstyle_suppressions.xml @@ -46,9 +46,10 @@ - + + diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java index e901de1937dc1..7814034bdf7d1 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/ml/JobStatsMonitoringDocTests.java @@ -179,12 +179,12 @@ public void testToXContent() throws IOException { + " \"timing_stats\": {" + " \"job_id\": \"_job_id\"," + " \"bucket_count\": 100," - + " \"total_bucket_processing_time_ms\": 2000," - + " \"minimum_bucket_processing_time_ms\": 10," - + " \"maximum_bucket_processing_time_ms\": 30," - + " \"average_bucket_processing_time_ms\": 20," - + " \"exponential_average_bucket_processing_time_ms\": 25," - + " \"exponential_average_bucket_processing_time_per_hour_ms\": 50" + + " \"total_bucket_processing_time_ms\": 2000.0," + + " \"minimum_bucket_processing_time_ms\": 10.0," + + " \"maximum_bucket_processing_time_ms\": 30.0," + + " \"average_bucket_processing_time_ms\": 20.0," + + " \"exponential_average_bucket_processing_time_ms\": 25.0," + + " \"exponential_average_bucket_processing_time_per_hour_ms\": 50.0" + " }" + " }" + "}" diff --git a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java index c2dfd3753ee21..59fbf1cb07f1d 100644 --- a/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java +++ b/x-pack/plugin/monitoring/src/test/java/org/elasticsearch/xpack/monitoring/collector/node/NodeStatsMonitoringDocTests.java @@ -172,9 +172,9 @@ public void testToXContent() throws IOException { + " \"os\": {" + " \"cpu\": {" + " \"load_average\": {" - + " \"1m\": 41," - + " \"5m\": 42," - + " \"15m\": 43" + + " \"1m\": 41.0," + + " \"5m\": 42.0," + + " \"15m\": 43.0" + " }" + " }," + " \"cgroup\": {" From 2e02711b32d47685edef436b23e3698040ce4b8e Mon Sep 17 00:00:00 2001 From: Rory Hunter Date: Wed, 23 Oct 2019 16:49:43 +0100 Subject: [PATCH 15/15] Lint --- .../collector/ccr/AutoFollowStatsMonitoringDocTests.java | 2 ++ .../monitoring/collector/ccr/FollowStatsMonitoringDocTests.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java index 436670fd0fae0..c9a50047b5915 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/AutoFollowStatsMonitoringDocTests.java @@ -26,6 +26,7 @@ import java.time.ZoneOffset; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.NavigableMap; import java.util.TreeMap; @@ -94,6 +95,7 @@ public void testToXContent() throws IOException { equalTo( XContentHelper.reformatJson( String.format( + Locale.ROOT, "{" + " \"cluster_uuid\": \"_cluster\"," + " \"timestamp\": \"%s\"," diff --git a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDocTests.java b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDocTests.java index 92646de476f40..c509e5598699f 100644 --- a/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDocTests.java +++ b/x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/monitoring/collector/ccr/FollowStatsMonitoringDocTests.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.time.ZoneOffset; import java.util.Collections; +import java.util.Locale; import java.util.Map; import java.util.NavigableMap; import java.util.TreeMap; @@ -148,6 +149,7 @@ public void testToXContent() throws IOException { equalTo( XContentHelper.reformatJson( String.format( + Locale.ROOT, "{" + " \"cluster_uuid\": \"_cluster\"," + " \"timestamp\": \"%s\","